OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.model;
15
 
16
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
17
 
18
/**
19
 * Allow to specify which rows we're interested in.
20
 *
21
 * @author Sylvain CUAZ
22
 */
23
public class SQLRowMode {
24
 
132 ilm 25
    public static final boolean check(final ArchiveMode archiveMode, final SQLRow r) {
26
        if (archiveMode == SQLSelect.BOTH)
27
            return true;
28
 
29
        return (archiveMode == SQLSelect.ARCHIVED && r.isArchived()) || (archiveMode == SQLSelect.UNARCHIVED && !r.isArchived());
30
    }
31
 
17 ilm 32
    public static final SQLRowMode DATA = new SQLRowMode(SQLSelect.UNARCHIVED, true, true);
33
    public static final SQLRowMode DEFINED = new SQLRowMode(SQLSelect.BOTH, true, true);
34
    public static final SQLRowMode VALID = new SQLRowMode(SQLSelect.UNARCHIVED, true, false);
35
    public static final SQLRowMode EXIST = new SQLRowMode(SQLSelect.BOTH, true, false);
36
    public static final SQLRowMode INEXISTANT = new SQLRowMode(SQLSelect.BOTH, false, false);
37
    /** any row matches : no access to the db */
38
    public static final SQLRowMode NO_CHECK = INEXISTANT;
39
 
40
    private final ArchiveMode archiveMode;
41
    private final boolean existing;
42
    private final boolean undefined;
43
 
132 ilm 44
    public SQLRowMode(ArchiveMode archiveMode, boolean existing, boolean excludeUndefined) {
17 ilm 45
        this.archiveMode = archiveMode;
46
        this.existing = existing;
132 ilm 47
        this.undefined = excludeUndefined;
17 ilm 48
    }
49
 
50
    public ArchiveMode getArchiveMode() {
51
        return this.archiveMode;
52
    }
53
 
54
    public boolean wantExisting() {
55
        return this.existing;
56
    }
57
 
58
    public boolean excludeUndefined() {
59
        return this.undefined;
60
    }
61
 
62
    /**
63
     * Checks whether <code>r</code> conforms to this mode.
64
     *
65
     * @param r the row to test, may be <code>null</code>.
66
     * @return <code>true</code> if <code>r</code> conforms, always <code>false</code> for
67
     *         <code>null</code>.
68
     */
69
    public boolean check(SQLRow r) {
70
        if (this == NO_CHECK)
71
            return true;
72
        if (r == null)
73
            return false;
74
 
75
        // check undef first since it doesn't require values and thus a query
76
        if (this.excludeUndefined() && r.isUndefined())
77
            return false;
78
 
132 ilm 79
        if (!check(this.getArchiveMode(), r))
17 ilm 80
            return false;
81
 
82
        if (this.wantExisting() != r.exists())
83
            return false;
84
 
85
        return true;
86
    }
87
 
88
    public SQLRow filter(SQLRow r) {
89
        return this.check(r) ? r : null;
90
    }
91
}