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 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 80 Rev 83
Line 53... Line 53...
53
 
53
 
54
    static public final String getValueFieldName() {
54
    static public final String getValueFieldName() {
55
        return "LABEL";
55
        return "LABEL";
56
    }
56
    }
57
 
57
 
58
    static public final SQLCreateMoveableTable getCreateTable(SQLSyntax syntax) {
58
    static public final SQLCreateMoveableTable getCreateTable(final SQLSyntax syntax) {
59
        final SQLCreateMoveableTable createTable = new SQLCreateMoveableTable(syntax, getTableName());
59
        final SQLCreateMoveableTable createTable = new SQLCreateMoveableTable(syntax, getTableName());
60
        createTable.addVarCharColumn(getRefFieldName(), 100);
60
        createTable.addVarCharColumn(getRefFieldName(), 100);
61
        createTable.addVarCharColumn(getValueFieldName(), 200);
61
        createTable.addVarCharColumn(getValueFieldName(), 200);
62
        createTable.setPrimaryKey(getRefFieldName(), getValueFieldName());
62
        createTable.setPrimaryKey(getRefFieldName(), getValueFieldName());
63
        return createTable;
63
        return createTable;
Line 65... Line 65...
65
 
65
 
66
    public SQLTextCombo() {
66
    public SQLTextCombo() {
67
        super();
67
        super();
68
    }
68
    }
69
 
69
 
70
    public SQLTextCombo(boolean locked) {
70
    public SQLTextCombo(final boolean locked) {
71
        super(locked);
71
        super(locked);
72
    }
72
    }
73
 
73
 
74
    public SQLTextCombo(ComboLockedMode mode) {
74
    public SQLTextCombo(final ComboLockedMode mode) {
75
        super(mode);
75
        super(mode);
76
    }
76
    }
77
 
77
 
78
    @Override
78
    @Override
79
    public void init(SQLRowItemView v) {
79
    public void init(final SQLRowItemView v) {
80
        if (!this.hasCache()) {
80
        if (!this.hasCache()) {
81
            final ITextComboCacheSQL cache = new ITextComboCacheSQL(v.getField());
81
            final ITextComboCacheSQL cache = new ITextComboCacheSQL(v.getField());
82
            if (cache.isValid())
82
            if (cache.isValid())
83
                this.initCache(cache);
83
                this.initCache(cache);
84
        }
84
        }
85
    }
85
    }
86
 
86
 
87
    static public class ITextComboCacheSQL implements ITextComboCache {
87
    static public class ITextComboCacheSQL extends AbstractComboCacheSQL {
88
 
88
 
89
        private final String field;
89
        private final String id;
90
        private final SQLTable t;
-
 
91
        private final List<String> cache;
-
 
92
        private boolean loadedOnce;
-
 
93
 
90
 
94
        public ITextComboCacheSQL(final SQLField f) {
91
        public ITextComboCacheSQL(final SQLField f) {
95
            this(f.getDBRoot(), f.getFullName());
92
            this(f.getDBRoot(), f.getFullName());
96
        }
93
        }
97
 
94
 
98
        public ITextComboCacheSQL(final DBRoot r, final String id) {
95
        public ITextComboCacheSQL(final DBRoot r, final String id) {
99
            this.field = id;
-
 
100
            this.t = r.findTable(getTableName());
96
            super(r.findTable(getTableName()), getValueFieldName());
101
            if (!this.isValid())
97
            this.id = id;
102
                Log.get().warning("no completion found for " + this.field);
-
 
103
            this.cache = new ArrayList<String>();
-
 
104
            this.loadedOnce = false;
-
 
105
        }
98
        }
106
 
99
 
-
 
100
        @Override
107
        public final boolean isValid() {
101
        public final boolean isValid() {
108
            return this.t != null;
102
            return this.getTable() != null;
-
 
103
        }
-
 
104
 
-
 
105
        @Override
-
 
106
        protected Where createWhere() {
-
 
107
            return new Where(this.getTable().getField(getRefFieldName()), "=", this.id);
-
 
108
        }
-
 
109
 
-
 
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;
109
        }
155
        }
110
 
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)
111
        private final SQLDataSource getDS() {
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) {
112
            return this.t.getDBSystemRoot().getDataSource();
177
            this(t == null ? null : t.getField(fieldName));
113
        }
178
        }
114
 
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
115
        public List<String> loadCache(final boolean dsCache) {
193
        public List<String> loadCache(final boolean dsCache) {
116
            final SQLSelect sel = new SQLSelect();
194
            final SQLSelect sel = new SQLSelect();
117
            sel.addSelect(this.t.getField(getValueFieldName()));
195
            sel.addSelect(getField());
118
            sel.setWhere(new Where(this.t.getField(getRefFieldName()), "=", this.field));
196
            sel.setWhere(createWhere());
119
            // predictable order
197
            // predictable order
120
            sel.addFieldOrder(this.t.getField(getValueFieldName()));
198
            sel.addFieldOrder(getField());
121
            // ignore DS cache to allow the fetching of rows modified by another VM
199
            // ignore DS cache to allow the fetching of rows modified by another VM
122
            @SuppressWarnings("unchecked")
200
            @SuppressWarnings("unchecked")
123
            final List<String> items = (List<String>) this.getDS().execute(sel.asString(), new IResultSetHandler(SQLDataSource.COLUMN_LIST_HANDLER) {
201
            final List<String> items = (List<String>) this.getDS().execute(sel.asString(), new IResultSetHandler(SQLDataSource.COLUMN_LIST_HANDLER) {
124
                @Override
202
                @Override
125
                public boolean readCache() {
203
                public boolean readCache() {
Line 135... Line 213...
135
            this.cache.addAll(items);
213
            this.cache.addAll(items);
136
 
214
 
137
            return this.cache;
215
            return this.cache;
138
        }
216
        }
139
 
217
 
-
 
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
140
        public List<String> getCache() {
229
        public List<String> getCache() {
141
            if (!this.loadedOnce) {
230
            if (!this.loadedOnce) {
142
                this.loadCache(true);
231
                this.loadCache(true);
143
                this.loadedOnce = true;
232
                this.loadedOnce = true;
144
            }
233
            }
145
            return this.cache;
234
            return this.cache;
146
        }
235
        }
147
 
236
 
148
        public void addToCache(String string) {
-
 
149
            if (!this.cache.contains(string)) {
-
 
150
                final Map<String, Object> m = new HashMap<String, Object>();
-
 
151
                m.put(getRefFieldName(), this.field);
-
 
152
                m.put(getValueFieldName(), string);
-
 
153
                try {
-
 
154
                    // the primary key is not generated so don't let SQLRowValues remove it.
-
 
155
                    new SQLRowValues(this.t, m).insert(true, false);
-
 
156
                } catch (SQLException e) {
-
 
157
                    // e.g. some other VM hasn't already added it
-
 
158
                    e.printStackTrace();
-
 
159
                }
-
 
160
                // add anyway since we didn't contain it
-
 
161
                this.cache.add(string);
-
 
162
            }
-
 
163
        }
-
 
164
 
-
 
165
        public void deleteFromCache(String string) {
-
 
166
            final Where w = new Where(this.t.getField(getRefFieldName()), "=", this.field).and(new Where(this.t.getField(getValueFieldName()), "=", string));
-
 
167
            this.getDS().executeScalar("DELETE FROM " + this.t.getSQLName().quote() + " WHERE " + w.getClause());
-
 
168
            this.cache.removeAll(Collections.singleton(string));
-
 
169
        }
-
 
170
 
-
 
171
        @Override
237
        @Override
172
        public String toString() {
238
        public String toString() {
173
            return this.getClass().getName() + " on " + this.field;
239
            return this.getClass().getName() + " on " + this.getField();
174
        }
240
        }
175
 
241
 
176
    }
242
    }
177
}
243
}