OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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.request;
15
 
16
import org.openconcerto.sql.Configuration;
73 ilm 17
import org.openconcerto.sql.FieldExpander;
80 ilm 18
import org.openconcerto.sql.element.SQLComponent;
17 ilm 19
import org.openconcerto.sql.model.SQLField;
20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.Where;
23
 
24
import java.util.List;
25
 
182 ilm 26
import net.jcip.annotations.GuardedBy;
93 ilm 27
import net.jcip.annotations.ThreadSafe;
28
 
29
@ThreadSafe
17 ilm 30
public class ListSQLRequest extends FilteredFillSQLRequest {
31
 
182 ilm 32
    static private final boolean FETCH_METADATA = Boolean.getBoolean("listRequest.fetchMD");
33
 
142 ilm 34
    private static final FieldExpander getExpander(final FieldExpander showAs) {
35
        final FieldExpander res;
36
        if (showAs != null) {
37
            res = showAs;
38
        } else {
39
            final Configuration conf = Configuration.getInstance();
40
            if (conf == null) {
41
                res = FieldExpander.getEmpty();
42
            } else {
43
                res = conf.getShowAs();
44
            }
45
        }
46
        return res;
47
    }
17 ilm 48
 
182 ilm 49
    @GuardedBy("this")
50
    private boolean fetchMD = FETCH_METADATA;
51
 
142 ilm 52
    public ListSQLRequest(SQLTable table, List<String> fieldss) {
17 ilm 53
        this(table, fieldss, null);
54
    }
55
 
142 ilm 56
    public ListSQLRequest(SQLTable table, List<String> fieldss, Where where) {
17 ilm 57
        this(table, fieldss, where, null);
58
    }
59
 
142 ilm 60
    public ListSQLRequest(SQLTable table, List<String> fieldss, Where where, final FieldExpander showAs) {
61
        this(computeGraph(table, fieldss, getExpander(showAs)), where);
62
    }
63
 
64
    public ListSQLRequest(final SQLRowValues graph, final Where where) {
65
        super(graph, where);
17 ilm 66
    }
67
 
93 ilm 68
    protected ListSQLRequest(ListSQLRequest req, final boolean freeze) {
69
        super(req, freeze);
17 ilm 70
    }
71
 
93 ilm 72
    // wasFrozen() : our showAs might change but our fetcher won't, MAYBE remove final modifier and
73
    // clone showAs
74
 
61 ilm 75
    @Override
93 ilm 76
    public ListSQLRequest toUnmodifiable() {
77
        return this.toUnmodifiableP(ListSQLRequest.class);
78
    }
79
 
142 ilm 80
    // can't implement Cloneable since the contract is to return an object of the same class. But
81
    // this would either prevent the use of anonymous classes if we call clone(false), or require a
82
    // call to super.clone() and less final fields.
93 ilm 83
 
84
    @Override
85
    protected ListSQLRequest clone(boolean forFreeze) {
86
        return new ListSQLRequest(this, forFreeze);
87
    }
88
 
182 ilm 89
    public synchronized final void setMetadataFetched(boolean fetchMD) {
90
        this.fetchMD = fetchMD;
91
    }
92
 
93
    public synchronized final boolean isMetadataFetched() {
94
        return this.fetchMD;
95
    }
96
 
142 ilm 97
    // MAYBE use changeGraphToFetch()
93 ilm 98
    @Override
142 ilm 99
    protected final void customizeToFetch(SQLRowValues graphToFetch) {
17 ilm 100
        super.customizeToFetch(graphToFetch);
182 ilm 101
        if (this.isMetadataFetched()) {
102
            addField(graphToFetch, getPrimaryTable().getCreationDateField());
103
            addField(graphToFetch, getPrimaryTable().getCreationUserField());
104
            addField(graphToFetch, getPrimaryTable().getModifDateField());
105
            addField(graphToFetch, getPrimaryTable().getModifUserField());
106
        }
80 ilm 107
        addField(graphToFetch, getPrimaryTable().getFieldRaw(SQLComponent.READ_ONLY_FIELD));
108
        addField(graphToFetch, getPrimaryTable().getFieldRaw(SQLComponent.READ_ONLY_USER_FIELD));
17 ilm 109
    }
110
 
111
    private void addField(SQLRowValues graphToFetch, final SQLField f) {
80 ilm 112
        if (f != null && !graphToFetch.getFields().contains(f.getName())) {
17 ilm 113
            if (f.isKey())
80 ilm 114
                graphToFetch.putRowValues(f.getName()).putNulls("NOM", "PRENOM");
17 ilm 115
            else
116
                graphToFetch.put(f.getName(), null);
80 ilm 117
        }
17 ilm 118
    }
119
}