OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Go to most recent revision | 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.sqlobject;
15
 
16
import org.openconcerto.sql.Log;
19 ilm 17
import org.openconcerto.sql.model.DBRoot;
25 ilm 18
import org.openconcerto.sql.model.IResultSetHandler;
17 ilm 19
import org.openconcerto.sql.model.SQLDataSource;
20
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLRowValues;
22
import org.openconcerto.sql.model.SQLSelect;
80 ilm 23
import org.openconcerto.sql.model.SQLSyntax;
17 ilm 24
import org.openconcerto.sql.model.SQLTable;
25
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.sql.request.SQLRowItemView;
27
import org.openconcerto.sql.sqlobject.itemview.RowItemViewComponent;
80 ilm 28
import org.openconcerto.sql.utils.SQLCreateMoveableTable;
17 ilm 29
import org.openconcerto.ui.component.ComboLockedMode;
30
import org.openconcerto.ui.component.ITextComboCache;
31
 
32
import java.sql.SQLException;
33
import java.util.ArrayList;
80 ilm 34
import java.util.Collections;
17 ilm 35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38
 
39
/**
40
 * An ITextCombo with the cache from COMPLETION.
41
 *
42
 * @author Sylvain CUAZ
43
 */
44
public class SQLTextCombo extends org.openconcerto.ui.component.ITextCombo implements RowItemViewComponent {
45
 
80 ilm 46
    static public final String getTableName() {
47
        return "COMPLETION";
48
    }
49
 
50
    static public final String getRefFieldName() {
51
        return "CHAMP";
52
    }
53
 
54
    static public final String getValueFieldName() {
55
        return "LABEL";
56
    }
57
 
83 ilm 58
    static public final SQLCreateMoveableTable getCreateTable(final SQLSyntax syntax) {
80 ilm 59
        final SQLCreateMoveableTable createTable = new SQLCreateMoveableTable(syntax, getTableName());
60
        createTable.addVarCharColumn(getRefFieldName(), 100);
61
        createTable.addVarCharColumn(getValueFieldName(), 200);
62
        createTable.setPrimaryKey(getRefFieldName(), getValueFieldName());
63
        return createTable;
64
    }
65
 
17 ilm 66
    public SQLTextCombo() {
67
        super();
68
    }
69
 
83 ilm 70
    public SQLTextCombo(final boolean locked) {
17 ilm 71
        super(locked);
72
    }
73
 
83 ilm 74
    public SQLTextCombo(final ComboLockedMode mode) {
17 ilm 75
        super(mode);
76
    }
77
 
65 ilm 78
    @Override
83 ilm 79
    public void init(final SQLRowItemView v) {
65 ilm 80
        if (!this.hasCache()) {
81
            final ITextComboCacheSQL cache = new ITextComboCacheSQL(v.getField());
82
            if (cache.isValid())
83
                this.initCache(cache);
84
        }
17 ilm 85
    }
86
 
83 ilm 87
    static public class ITextComboCacheSQL extends AbstractComboCacheSQL {
17 ilm 88
 
83 ilm 89
        private final String id;
17 ilm 90
 
91
        public ITextComboCacheSQL(final SQLField f) {
19 ilm 92
            this(f.getDBRoot(), f.getFullName());
93
        }
94
 
95
        public ITextComboCacheSQL(final DBRoot r, final String id) {
83 ilm 96
            super(r.findTable(getTableName()), getValueFieldName());
97
            this.id = id;
17 ilm 98
        }
99
 
83 ilm 100
        @Override
17 ilm 101
        public final boolean isValid() {
83 ilm 102
            return this.getTable() != null;
17 ilm 103
        }
104
 
83 ilm 105
        @Override
106
        protected Where createWhere() {
107
            return new Where(this.getTable().getField(getRefFieldName()), "=", this.id);
17 ilm 108
        }
109
 
83 ilm 110
        @Override
111
        public void addToCache(final String string) {
112
            if (!this.cache.contains(string)) {
113
                final Map<String, Object> m = new HashMap<String, Object>();
114
                m.put(getRefFieldName(), this.id);
115
                m.put(getValueFieldName(), string);
116
                try {
117
                    // the primary key is not generated so don't let SQLRowValues remove it.
118
                    new SQLRowValues(this.getTable(), m).insert(true, false);
119
                } catch (final SQLException e) {
120
                    // e.g. some other VM hasn't already added it
121
                    e.printStackTrace();
122
                }
123
                // add anyway since we didn't contain it
124
                this.cache.add(string);
125
            }
126
        }
127
 
128
        @Override
129
        public void deleteFromCache(final String string) {
130
            final Where w = new Where(this.getTable().getField(getRefFieldName()), "=", this.id).and(new Where(this.getField(), "=", string));
131
            this.getDS().executeScalar("DELETE FROM " + this.getTable().getSQLName().quote() + " WHERE " + w.getClause());
132
            this.cache.removeAll(Collections.singleton(string));
133
        }
134
 
135
        @Override
136
        public String toString() {
137
            return super.toString() + "/" + this.id;
138
        }
139
    }
140
 
141
    static public class ITextComboCacheExistingValues extends AbstractComboCacheSQL {
142
 
143
        public ITextComboCacheExistingValues(final SQLField f) {
144
            super(f);
145
        }
146
 
147
        @Override
148
        public boolean isValid() {
149
            return String.class.isAssignableFrom(this.getField().getType().getJavaType());
150
        }
151
 
152
        @Override
153
        protected Where createWhere() {
154
            return null;
155
        }
156
 
157
        @Override
158
        public void addToCache(final String string) {
159
            throw new UnsupportedOperationException();
160
        }
161
 
162
        @Override
163
        public void deleteFromCache(final String string) {
164
            throw new UnsupportedOperationException();
165
        }
166
    }
167
 
168
    // a cache that takes values from an SQL field
169
    static public abstract class AbstractComboCacheSQL implements ITextComboCache {
170
 
171
        // values are in this field (e.g. COMPLETION.LABEL)
172
        private final SQLField f;
173
        protected final List<String> cache;
174
        private boolean loadedOnce;
175
 
176
        protected AbstractComboCacheSQL(final SQLTable t, final String fieldName) {
177
            this(t == null ? null : t.getField(fieldName));
178
        }
179
 
180
        protected AbstractComboCacheSQL(final SQLField f) {
181
            this.f = f;
182
            this.cache = new ArrayList<String>();
183
            this.loadedOnce = false;
184
            if (!this.isValid())
185
                Log.get().warning("no completion found for " + this);
186
        }
187
 
188
        protected final SQLDataSource getDS() {
189
            return this.f.getDBSystemRoot().getDataSource();
190
        }
191
 
192
        @Override
25 ilm 193
        public List<String> loadCache(final boolean dsCache) {
80 ilm 194
            final SQLSelect sel = new SQLSelect();
83 ilm 195
            sel.addSelect(getField());
196
            sel.setWhere(createWhere());
80 ilm 197
            // predictable order
83 ilm 198
            sel.addFieldOrder(getField());
25 ilm 199
            // ignore DS cache to allow the fetching of rows modified by another VM
200
            @SuppressWarnings("unchecked")
201
            final List<String> items = (List<String>) this.getDS().execute(sel.asString(), new IResultSetHandler(SQLDataSource.COLUMN_LIST_HANDLER) {
202
                @Override
203
                public boolean readCache() {
204
                    return dsCache;
205
                }
206
 
207
                @Override
208
                public boolean writeCache() {
209
                    return true;
210
                }
211
            });
17 ilm 212
            this.cache.clear();
25 ilm 213
            this.cache.addAll(items);
17 ilm 214
 
215
            return this.cache;
216
        }
217
 
83 ilm 218
        protected final SQLTable getTable() {
219
            return this.getField().getTable();
220
        }
221
 
222
        protected final SQLField getField() {
223
            return this.f;
224
        }
225
 
226
        protected abstract Where createWhere();
227
 
228
        @Override
17 ilm 229
        public List<String> getCache() {
230
            if (!this.loadedOnce) {
25 ilm 231
                this.loadCache(true);
17 ilm 232
                this.loadedOnce = true;
233
            }
234
            return this.cache;
235
        }
236
 
237
        @Override
238
        public String toString() {
83 ilm 239
            return this.getClass().getName() + " on " + this.getField();
17 ilm 240
        }
241
 
242
    }
243
}