OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.model;
14
 package org.openconcerto.sql.model;
15
 
15
 
16
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
16
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
17
import org.openconcerto.utils.CollectionUtils;
-
 
18
import org.openconcerto.utils.cc.IPredicate;
-
 
19
 
-
 
20
import java.util.Collection;
-
 
21
 
17
 
22
/**
18
/**
23
 * Allow to specify which rows we're interested in.
19
 * Allow to specify which rows we're interested in.
24
 * 
20
 * 
25
 * @author Sylvain CUAZ
21
 * @author Sylvain CUAZ
26
 */
22
 */
27
public class SQLRowMode {
23
public class SQLRowMode {
28
 
24
 
29
    public static final boolean check(final ArchiveMode archiveMode, final SQLRow r) {
25
    public static final boolean check(final ArchiveMode archiveMode, final SQLRow r) {
30
        if (archiveMode == SQLSelect.BOTH)
26
        if (archiveMode == SQLSelect.BOTH)
31
            return true;
27
            return true;
32
 
28
 
33
        return (archiveMode == SQLSelect.ARCHIVED && r.isArchived()) || (archiveMode == SQLSelect.UNARCHIVED && !r.isArchived());
29
        return (archiveMode == SQLSelect.ARCHIVED && r.isArchived()) || (archiveMode == SQLSelect.UNARCHIVED && !r.isArchived());
34
    }
30
    }
35
 
31
 
36
    public static final SQLRowMode DATA = new SQLRowMode(SQLSelect.UNARCHIVED, true, true);
32
    public static final SQLRowMode DATA = new SQLRowMode(SQLSelect.UNARCHIVED, true, true);
37
    public static final SQLRowMode DEFINED = new SQLRowMode(SQLSelect.BOTH, true, true);
33
    public static final SQLRowMode DEFINED = new SQLRowMode(SQLSelect.BOTH, true, true);
38
    public static final SQLRowMode VALID = new SQLRowMode(SQLSelect.UNARCHIVED, true, false);
34
    public static final SQLRowMode VALID = new SQLRowMode(SQLSelect.UNARCHIVED, true, false);
39
    public static final SQLRowMode EXIST = new SQLRowMode(SQLSelect.BOTH, true, false);
35
    public static final SQLRowMode EXIST = new SQLRowMode(SQLSelect.BOTH, true, false);
40
    public static final SQLRowMode INEXISTANT = new SQLRowMode(SQLSelect.BOTH, false, false);
36
    public static final SQLRowMode INEXISTANT = new SQLRowMode(SQLSelect.BOTH, false, false);
41
    /** any row matches : no access to the db */
37
    /** any row matches : no access to the db */
42
    public static final SQLRowMode NO_CHECK = INEXISTANT;
38
    public static final SQLRowMode NO_CHECK = INEXISTANT;
43
 
39
 
44
    private final ArchiveMode archiveMode;
40
    private final ArchiveMode archiveMode;
45
    private final boolean existing;
41
    private final boolean existing;
46
    private final boolean undefined;
42
    private final boolean undefined;
47
 
43
 
48
    public SQLRowMode(ArchiveMode archiveMode, boolean existing, boolean excludeUndefined) {
44
    public SQLRowMode(ArchiveMode archiveMode, boolean existing, boolean excludeUndefined) {
49
        this.archiveMode = archiveMode;
45
        this.archiveMode = archiveMode;
50
        this.existing = existing;
46
        this.existing = existing;
51
        this.undefined = excludeUndefined;
47
        this.undefined = excludeUndefined;
52
    }
48
    }
53
 
49
 
54
    public ArchiveMode getArchiveMode() {
50
    public ArchiveMode getArchiveMode() {
55
        return this.archiveMode;
51
        return this.archiveMode;
56
    }
52
    }
57
 
53
 
58
    public boolean wantExisting() {
54
    public boolean wantExisting() {
59
        return this.existing;
55
        return this.existing;
60
    }
56
    }
61
 
57
 
62
    public boolean excludeUndefined() {
58
    public boolean excludeUndefined() {
63
        return this.undefined;
59
        return this.undefined;
64
    }
60
    }
65
 
61
 
66
    /**
62
    /**
67
     * Checks whether <code>r</code> conforms to this mode.
63
     * Checks whether <code>r</code> conforms to this mode.
68
     * 
64
     * 
69
     * @param r the row to test, may be <code>null</code>.
65
     * @param r the row to test, may be <code>null</code>.
70
     * @return <code>true</code> if <code>r</code> conforms, always <code>false</code> for
66
     * @return <code>true</code> if <code>r</code> conforms, always <code>false</code> for
71
     *         <code>null</code>.
67
     *         <code>null</code>.
72
     */
68
     */
73
    public boolean check(SQLRow r) {
69
    public boolean check(SQLRow r) {
74
        if (this == NO_CHECK)
70
        if (this == NO_CHECK)
75
            return true;
71
            return true;
76
        if (r == null)
72
        if (r == null)
77
            return false;
73
            return false;
78
 
74
 
79
        // check undef first since it doesn't require values and thus a query
75
        // check undef first since it doesn't require values and thus a query
80
        if (this.excludeUndefined() && r.isUndefined())
76
        if (this.excludeUndefined() && r.isUndefined())
81
            return false;
77
            return false;
82
 
78
 
83
        if (!check(this.getArchiveMode(), r))
79
        if (!check(this.getArchiveMode(), r))
84
            return false;
80
            return false;
85
 
81
 
86
        if (this.wantExisting() != r.exists())
82
        if (this.wantExisting() != r.exists())
87
            return false;
83
            return false;
88
 
84
 
89
        return true;
85
        return true;
90
    }
86
    }
91
 
87
 
92
    public SQLRow filter(SQLRow r) {
88
    public SQLRow filter(SQLRow r) {
93
        return this.check(r) ? r : null;
89
        return this.check(r) ? r : null;
94
    }
90
    }
95
 
-
 
96
    public void filter(Collection<SQLRow> rows) {
-
 
97
        CollectionUtils.filter(rows, new IPredicate<SQLRow>() {
-
 
98
            @Override
-
 
99
            public boolean evaluateChecked(SQLRow r) {
-
 
100
                return check(r);
-
 
101
            }
-
 
102
        });
-
 
103
    }
-
 
104
 
-
 
105
}
91
}