OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 156
Line 13... Line 13...
13
 
13
 
14
 package org.openconcerto.sql.model;
14
 package org.openconcerto.sql.model;
15
 
15
 
16
import org.openconcerto.utils.CompareUtils;
16
import org.openconcerto.utils.CompareUtils;
17
 
17
 
-
 
18
import java.math.BigDecimal;
18
import java.util.Comparator;
19
import java.util.Comparator;
19
 
20
 
20
/**
21
/**
21
 * Allow one to sort SQLRow of the same table by their ORDER field.
22
 * Allow one to sort SQLRow of the same table by their ORDER field.
22
 * 
23
 * 
23
 * @author Sylvain
24
 * @author Sylvain
24
 */
25
 */
25
public class OrderComparator implements Comparator<SQLRowAccessor> {
26
public class OrderComparator implements Comparator<SQLRowAccessor> {
26
 
27
 
-
 
28
    public static final Comparator<BigDecimal> BD_NULLS_FIRST = Comparator.nullsFirst(Comparator.naturalOrder());
-
 
29
    public static final Comparator<BigDecimal> BD_NULLS_LAST = Comparator.nullsLast(Comparator.naturalOrder());
-
 
30
 
27
    /**
31
    /**
28
     * Order rows by {@link SQLTable#getOrderField()}.
32
     * Order rows by {@link SQLTable#getOrderField()}.
29
     */
33
     */
30
    public static final OrderComparator INSTANCE = new OrderComparator(false);
34
    public static final OrderComparator INSTANCE = new OrderComparator(false, BD_NULLS_LAST);
31
    private static final OrderComparator INSTANCE_FALLBACK_TO_PK = new OrderComparator(true);
35
    private static final OrderComparator INSTANCE_FALLBACK_TO_PK = new OrderComparator(true, BD_NULLS_LAST);
32
 
36
 
33
    /**
37
    /**
34
     * Order rows by {@link SQLTable#getOrderField()}, or if the table is not
38
     * Order rows by {@link SQLTable#getOrderField()}, or if the table is not
35
     * {@link SQLTable#isOrdered() ordered} by {@link SQLTable#getKey()}.
39
     * {@link SQLTable#isOrdered() ordered} by {@link SQLTable#getKey()}.
36
     * 
40
     * 
Line 39... Line 43...
39
    public static final Comparator<SQLRowAccessor> getFallbackToPKInstance() {
43
    public static final Comparator<SQLRowAccessor> getFallbackToPKInstance() {
40
        return INSTANCE_FALLBACK_TO_PK;
44
        return INSTANCE_FALLBACK_TO_PK;
41
    }
45
    }
42
 
46
 
43
    private final boolean fallbackToPK;
47
    private final boolean fallbackToPK;
-
 
48
    private final Comparator<BigDecimal> bdComparator;
44
 
49
 
45
    // singleton
-
 
46
    private OrderComparator(boolean fallbackToPK) {
50
    public OrderComparator(final boolean fallbackToPK, final Comparator<BigDecimal> bdComparator) {
47
        super();
51
        super();
48
        this.fallbackToPK = fallbackToPK;
52
        this.fallbackToPK = fallbackToPK;
-
 
53
        this.bdComparator = bdComparator;
49
    }
54
    }
50
 
55
 
51
    @Override
56
    @Override
52
    public int compare(SQLRowAccessor r1, SQLRowAccessor r2) {
57
    public int compare(SQLRowAccessor r1, SQLRowAccessor r2) {
53
        // handle two nulls (but the next line throws an exception if only one is null)
58
        // handle two nulls (but the next line throws an exception if only one is null)
Line 55... Line 60...
55
        if (r1 == r2)
60
        if (r1 == r2)
56
            return 0;
61
            return 0;
57
        final SQLTable t = r1.getTable();
62
        final SQLTable t = r1.getTable();
58
        if (!t.equals(r2.getTable()))
63
        if (!t.equals(r2.getTable()))
59
            throw new IllegalArgumentException(r1 + " and " + r2 + " are not of the same table");
64
            throw new IllegalArgumentException(r1 + " and " + r2 + " are not of the same table");
-
 
65
        final SQLField orderField = t.getTable().getOrderField();
60
        if (t.isOrdered()) {
66
        if (orderField != null) {
-
 
67
            final BigDecimal order1 = r1.getObjectAs(orderField.getName(), true, BigDecimal.class);
-
 
68
            final BigDecimal order2 = r2.getObjectAs(orderField.getName(), true, BigDecimal.class);
61
            return r1.getOrder().compareTo(r2.getOrder());
69
            return this.bdComparator.compare(order1, order2);
62
        } else if (this.fallbackToPK) {
70
        } else if (this.fallbackToPK) {
63
            if (!t.isRowable())
71
            if (!t.isRowable())
64
                throw new IllegalArgumentException(t + " neither ordered nor rowable");
72
                throw new IllegalArgumentException(t + " neither ordered nor rowable");
65
            else if (r1.hasID() && r2.hasID())
73
            else if (r1.hasID() && r2.hasID())
66
                return CompareUtils.compareInt(r1.getID(), r2.getID());
74
                return CompareUtils.compareInt(r1.getID(), r2.getID());