OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Rev 182 | 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.model;
15
 
83 ilm 16
import org.openconcerto.sql.Configuration;
73 ilm 17
import org.openconcerto.sql.model.graph.SQLKey;
18
import org.openconcerto.sql.model.graph.TablesMap;
83 ilm 19
import org.openconcerto.sql.preferences.SQLPreferences;
73 ilm 20
import org.openconcerto.sql.utils.AlterTable;
21
import org.openconcerto.sql.utils.ChangeTable;
22
import org.openconcerto.sql.utils.SQLCreateTable;
23
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
83 ilm 24
import org.openconcerto.utils.StringUtils;
73 ilm 25
import org.openconcerto.utils.cc.ITransformer;
17 ilm 26
 
27
import java.sql.SQLException;
177 ilm 28
import java.text.DateFormat;
29
import java.text.SimpleDateFormat;
17 ilm 30
import java.util.ArrayList;
73 ilm 31
import java.util.Arrays;
17 ilm 32
import java.util.HashMap;
177 ilm 33
import java.util.HashSet;
73 ilm 34
import java.util.List;
17 ilm 35
import java.util.Map;
73 ilm 36
import java.util.Set;
17 ilm 37
 
38
public class SQLInjector {
39
 
73 ilm 40
    private final SQLTable tableSrc, tableDest;
41
    private final ArrayList<SQLField> from = new ArrayList<SQLField>();
42
    private final ArrayList<SQLField> to = new ArrayList<SQLField>();
43
    private final Map<SQLField, Object> values = new HashMap<SQLField, Object>();
44
    private final static Map<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>> allRegisteredInjectors = new HashMap<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>>();
17 ilm 45
 
73 ilm 46
    private boolean storeTransfer;
47
    // maps of injectors that store transfer
48
    private static Map<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>> injectors = new HashMap<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>>();
49
 
50
    public SQLInjector(final DBRoot r, final String src, final String dest, boolean storeTransfer) {
51
        this(r.findTable(src), r.findTable(dest), storeTransfer);
17 ilm 52
    }
53
 
73 ilm 54
    public SQLInjector(SQLTable src, SQLTable dest, boolean storeTransfer) {
17 ilm 55
        this.tableDest = dest;
56
        this.tableSrc = src;
73 ilm 57
        this.storeTransfer = storeTransfer;
58
        final DBRoot dbRoot = src.getDBRoot();
59
        Map<SQLTable, Map<SQLTable, SQLInjector>> inj = allRegisteredInjectors.get(dbRoot);
60
        if (inj == null) {
61
            inj = new HashMap<SQLTable, Map<SQLTable, SQLInjector>>();
62
            allRegisteredInjectors.put(dbRoot, inj);
63
        }
64
        Map<SQLTable, SQLInjector> srcs = inj.get(src);
17 ilm 65
        if (srcs == null) {
66
            srcs = new HashMap<SQLTable, SQLInjector>();
73 ilm 67
            inj.put(src, srcs);
17 ilm 68
        }
69
        srcs.put(dest, this);
73 ilm 70
 
71
        if (storeTransfer) {
72
            // Register only SQLInjector that store transfer
73
            inj = injectors.get(dbRoot);
74
            if (inj == null) {
75
                inj = new HashMap<SQLTable, Map<SQLTable, SQLInjector>>();
76
                injectors.put(dbRoot, inj);
77
            }
78
            srcs = inj.get(src);
79
            if (srcs == null) {
80
                srcs = new HashMap<SQLTable, SQLInjector>();
81
                inj.put(src, srcs);
82
            }
83
            srcs.put(dest, this);
84
        }
17 ilm 85
    }
86
 
73 ilm 87
    public synchronized SQLRowValues createRowValuesFrom(int idSrc) {
88
        final List<SQLRowAccessor> srcRows = new ArrayList<SQLRowAccessor>(1);
93 ilm 89
        srcRows.add(getSource().getRow(idSrc));
73 ilm 90
        return createRowValuesFrom(srcRows);
17 ilm 91
    }
92
 
73 ilm 93
    public synchronized SQLRowValues createRowValuesFrom(final SQLRow srcRow) {
94
        final SQLRowValues rowVals = new SQLRowValues(getDestination());
17 ilm 95
        if (!srcRow.getTable().equals(getSource()))
96
            throw new IllegalArgumentException("Row not from source table : " + srcRow);
73 ilm 97
        merge(srcRow, rowVals);
98
        return rowVals;
99
    }
17 ilm 100
 
73 ilm 101
    public synchronized SQLRowValues createRowValuesFrom(final List<? extends SQLRowAccessor> srcRows) {
102
        final SQLRowValues rowVals = new SQLRowValues(getDestination());
103
        for (SQLRowAccessor srcRow : srcRows) {
104
            if (!srcRow.getTable().equals(getSource()))
105
                throw new IllegalArgumentException("Row not from source table : " + srcRow);
106
            merge(srcRow, rowVals);
107
        }
108
        return rowVals;
109
    }
110
 
111
    public void commitTransfert(final List<? extends SQLRowAccessor> srcRows, int destId) throws SQLException {
112
 
113
        if (storeTransfer) {
114
            System.err.println("SQLInjector.commitTransfert() : transfert from " + this.getSource().getName() + " to " + this.getDestination().getName());
115
            // Transfert
116
            final SQLTable tableTransfert = getSource().getDBRoot().getTable(getTableTranferName());
117
            if (tableTransfert == null) {
118
                throw new IllegalStateException("No table transfer for " + getSource().getName());
119
            }
120
 
121
            for (SQLRowAccessor srcRow : srcRows) {
122
 
123
                final SQLRowValues rowTransfer = new SQLRowValues(tableTransfert);
124
 
125
                final Set<SQLField> foreignKeysSrc = tableTransfert.getForeignKeys(getSource());
126
                final Set<SQLField> foreignKeysDest = tableTransfert.getForeignKeys(getDestination());
127
                if (foreignKeysSrc.isEmpty()) {
128
                    throw new IllegalStateException("No foreign (src) to " + getSource().getName() + " in " + tableTransfert.getName());
129
                }
130
                if (foreignKeysDest.isEmpty()) {
131
                    throw new IllegalStateException("No foreign (dest) to " + getDestination().getName() + " in " + tableTransfert.getName());
132
                }
133
                rowTransfer.put(foreignKeysSrc.iterator().next().getName(), srcRow.getIDNumber());
134
                rowTransfer.put(foreignKeysDest.iterator().next().getName(), destId);
135
                // TODO: commit in one shot
136
                rowTransfer.commit();
137
 
138
            }
139
        }
140
 
141
    }
142
 
143
    private String getTableTranferName() {
144
        return "TR_" + getSource().getName();
145
    }
146
 
147
    protected void merge(SQLRowAccessor srcRow, SQLRowValues rowVals) {
17 ilm 148
        for (SQLField field : this.values.keySet()) {
149
            rowVals.put(field.getName(), this.values.get(field));
150
        }
73 ilm 151
        final SQLSystem dbSystem = srcRow.getTable().getDBSystemRoot().getServer().getSQLSystem();
152
        final int size = getFrom().size();
153
        for (int i = 0; i < size; i++) {
17 ilm 154
 
155
            final SQLField sqlFieldFrom = getFrom().get(i);
156
            final SQLField sqlFieldTo = getTo().get(i);
157
            final Object o = srcRow.getObject(sqlFieldFrom.getName());
158
 
159
            // Probleme avec H2 Primary Key en Long et foreignKey en Int
160
            if (dbSystem == SQLSystem.H2 && sqlFieldFrom.getType().getJavaType() == Long.class && sqlFieldTo.getType().getJavaType() == Integer.class) {
73 ilm 161
                merge(sqlFieldTo, ((Long) o).intValue(), rowVals);
17 ilm 162
            } else {
73 ilm 163
                merge(sqlFieldTo, o, rowVals);
17 ilm 164
            }
165
        }
73 ilm 166
    }
17 ilm 167
 
73 ilm 168
    protected void merge(SQLField field, Object value, SQLRowValues rowVals) {
169
        rowVals.put(field.getName(), value);
17 ilm 170
    }
171
 
180 ilm 172
    private String cleanRef(String value) {
173
        List<String> l = StringUtils.fastSplit(value, ',');
174
        Set<String> s = new HashSet<>(l);
175
        String nom = "";
176
        if (s.size() > 1) {
177
            Set<String> refAdded = new HashSet<>();
178
            for (String string : s) {
179
                if (string.trim().length() > 0 && !refAdded.contains(string.trim())) {
180
                    nom += string + ",";
181
                    refAdded.add(string.trim());
182
                }
183
            }
184
        } else if (s.size() == 1) {
185
            nom = s.iterator().next();
186
        }
187
        return nom;
188
    }
189
 
174 ilm 190
    protected void transfertReference(SQLRowAccessor srcRow, SQLRowValues rowVals, final SQLTable tableElementDestination, String refField, String from, String to) {
83 ilm 191
 
192
        String label = rowVals.getString(to);
174 ilm 193
        SQLPreferences prefs = SQLPreferences.getMemCached(srcRow.getTable().getDBRoot());
194
 
195
        if (prefs.getBoolean("TransfertRef", true) || !to.equals("NOM")) {
196
            if (label != null && label.trim().length() > 0) {
180 ilm 197
                rowVals.put(to, cleanRef(label + ", " + srcRow.getString(from)));
174 ilm 198
            } else {
199
                rowVals.put(to, srcRow.getString(from));
200
            }
201
        } else if (prefs.getBoolean("TransfertMultiRef", false)) {
202
            SQLRowValues rowValsHeader = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableElementDestination));
203
            // TODO taxe may be undefined set it to default
204
            rowValsHeader.put("NOM", srcRow.getString(from));
205
            rowValsHeader.put(refField, rowVals);
83 ilm 206
        }
174 ilm 207
 
83 ilm 208
    }
209
 
177 ilm 210
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
211
 
83 ilm 212
    protected void transfertNumberReference(SQLRowAccessor srcRow, SQLRowValues rowVals, final SQLTable tableElementDestination, String refField) {
174 ilm 213
        SQLPreferences prefs = SQLPreferences.getMemCached(srcRow.getTable().getDBRoot());
83 ilm 214
 
215
        if (prefs.getBoolean("TransfertRef", true)) {
216
            String label = rowVals.getString("NOM");
217
            if (label != null && label.trim().length() > 0) {
177 ilm 218
                final String value = label + ", " + srcRow.getString("NUMERO");
180 ilm 219
                rowVals.put("NOM", cleanRef(value));
83 ilm 220
            } else {
221
                rowVals.put("NOM", srcRow.getString("NUMERO"));
222
            }
223
        } else if (prefs.getBoolean("TransfertMultiRef", false)) {
224
            SQLRowValues rowValsHeader = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableElementDestination));
177 ilm 225
            // FIXME Style forcé en titre 1 via l'ID
226
            rowValsHeader.put("ID_STYLE", 3);
83 ilm 227
            String elementName = StringUtils.firstUp(Configuration.getInstance().getDirectory().getElement(getSource()).getName().getVariant(org.openconcerto.utils.i18n.Grammar.SINGULAR));
177 ilm 228
            rowValsHeader.put("NOM", elementName + "\n N° " + srcRow.getString("NUMERO") + " du " + dateFormat.format(srcRow.getDate("DATE").getTime()));
83 ilm 229
            rowValsHeader.put(refField, rowVals);
230
        }
231
    }
232
 
73 ilm 233
    public synchronized SQLRow insertFrom(final SQLRowAccessor srcRow) throws SQLException {
234
        return createRowValuesFrom(Arrays.asList(srcRow)).insert();
17 ilm 235
    }
236
 
237
    // TODO gettable()..getName()..equalsIgnoreCase( by .getTable().equals(
238
    /**
239
     * mettre une valeur par défaut pour un champ donné
240
     *
241
     * @param fieldDest
242
     * @param defaultValue
243
     */
73 ilm 244
    protected synchronized final void mapDefaultValues(SQLField fieldDest, Object defaultValue) {
17 ilm 245
        if (fieldDest.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
246
            this.values.put(fieldDest, defaultValue);
247
        } else {
248
            throw new IllegalArgumentException("SQLField " + fieldDest + " is not a field of table " + this.tableDest);
249
        }
250
    }
251
 
73 ilm 252
    protected synchronized final void map(SQLField from, SQLField to) throws IllegalArgumentException {
17 ilm 253
        // Verification de la validité des SQLField
254
        if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
255
            throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
256
        } else {
257
            if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
258
                throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
259
            }
260
        }
261
 
262
        int index = this.from.indexOf(from);
263
        if (index > 0) {
264
            this.to.set(index, to);
265
        } else {
266
            this.from.add(from);
267
            this.to.add(to);
268
        }
269
    }
270
 
73 ilm 271
    protected synchronized final void remove(SQLField from, SQLField to) throws IllegalArgumentException {
17 ilm 272
        // Verification de la validité des SQLField
273
        if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
274
            throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
275
        } else {
276
            if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
277
                throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
278
            }
279
        }
280
 
281
        int index = this.from.indexOf(from);
282
        if (this.to.get(index).getName().equalsIgnoreCase(to.getName())) {
283
            this.to.remove(to);
284
            this.from.remove(from);
285
        }
286
    }
287
 
288
    /**
289
     * Créer l'association entre les champs portant le nom dans les deux tables
290
     *
291
     */
73 ilm 292
    public synchronized void createDefaultMap() {
17 ilm 293
        for (SQLField field : this.tableSrc.getContentFields()) {
294
 
295
            if (this.tableDest.contains(field.getName())) {
296
                map(field, this.tableDest.getField(field.getName()));
297
            }
298
        }
299
    }
300
 
73 ilm 301
    public synchronized ArrayList<SQLField> getFrom() {
17 ilm 302
        return this.from;
303
    }
304
 
73 ilm 305
    public synchronized ArrayList<SQLField> getTo() {
17 ilm 306
        return this.to;
307
    }
308
 
309
    /**
310
     * Creer un SQLInjector par défaut si aucun n'est déja défini
311
     *
312
     * @param src
313
     * @param dest
314
     * @return un SQLInjector par défaut si aucun n'est déja défini
315
     */
73 ilm 316
    public static synchronized SQLInjector getInjector(SQLTable src, SQLTable dest) {
317
        SQLInjector injector = getRegistrereddInjector(src, dest);
318
        if (injector == null) {
319
            injector = createDefaultInjector(src, dest);
17 ilm 320
        }
73 ilm 321
        return injector;
17 ilm 322
    }
323
 
73 ilm 324
    public static synchronized SQLInjector getRegistrereddInjector(SQLTable src, SQLTable dest) {
325
        final Map<SQLTable, Map<SQLTable, SQLInjector>> map = allRegisteredInjectors.get(src.getDBRoot());
326
        if (map == null) {
327
            return null;
328
        }
329
        Map<SQLTable, SQLInjector> m = map.get(src);
330
        if (m != null) {
331
            return m.get(dest);
332
        }
333
        return null;
334
    }
335
 
336
    private static synchronized SQLInjector createDefaultInjector(SQLTable src, SQLTable dest) {
337
        System.err.println("No SQLInjector defined for " + src + " , " + dest + ". SQLInjector created automatically.");
338
        SQLInjector injector = new SQLInjector(src, dest, false);
339
        injector.createDefaultMap();
340
        return injector;
341
    }
342
 
343
    public synchronized SQLTable getDestination() {
17 ilm 344
        return this.tableDest;
345
    }
346
 
73 ilm 347
    public synchronized SQLTable getSource() {
17 ilm 348
        return this.tableSrc;
349
    }
73 ilm 350
 
351
    public synchronized static void createTransferTables(DBRoot root) throws SQLException {
352
        Map<SQLTable, Map<SQLTable, SQLInjector>> map = injectors.get(root);
353
        if (root == null) {
354
            System.err.println("No SQLInjector for root " + root);
355
            return;
356
        }
357
 
358
        final Set<SQLTable> srcTables = map.keySet();
359
        if (srcTables.isEmpty()) {
360
            System.err.println("No SQLInjector for root " + root);
361
            return;
362
        }
363
 
364
        final List<SQLCreateTable> createTablesQueries = new ArrayList<SQLCreateTable>();
365
        // Create table if needed
366
        for (SQLTable sqlTable : srcTables) {
367
            final String trTableName = "TR_" + sqlTable.getName();
368
            if (root.getTable(trTableName) == null) {
369
                final SQLCreateTable createTable = new SQLCreateTable(root, trTableName);
370
                createTable.setPlain(false);
371
                // createTable.addColumn(SQLSyntax.ID_NAME,
372
                // createTable.getSyntax().getPrimaryIDDefinition());
373
                createTable.addForeignColumn(SQLKey.PREFIX + sqlTable.getName(), sqlTable);
374
                createTablesQueries.add(createTable);
375
            }
376
        }
377
        if (createTablesQueries.size() > 0) {
378
            root.createTables(createTablesQueries);
379
        }
380
 
381
        // Create transfer fields if needed
382
        final List<AlterTable> alterTablesQueries = new ArrayList<AlterTable>();
383
        final TablesMap toRefresh = new TablesMap();
384
        for (SQLTable srcTable : srcTables) {
385
            final String trTableName = "TR_" + srcTable.getName();
386
            final SQLTable transfertTable = root.getTable(trTableName);
387
            final AlterTable alter = new AlterTable(transfertTable);
388
            final Set<SQLTable> destTables = map.get(srcTable).keySet();
389
            for (SQLTable destTable : destTables) {
390
                final String fk = SQLKey.PREFIX + destTable.getName();
391
                if (!transfertTable.contains(fk)) {
392
                    alter.addForeignColumn(fk, destTable);
393
                }
394
            }
395
            if (!alter.isEmpty()) {
396
                alterTablesQueries.add(alter);
397
                toRefresh.add(alter.getRootName(), alter.getName());
398
            }
399
        }
400
        for (final String q : ChangeTable.cat(alterTablesQueries)) {
401
            root.getDBSystemRoot().getDataSource().execute(q);
402
        }
403
        root.getSchema().updateVersion();
404
        root.getDBSystemRoot().refresh(toRefresh, false);
405
 
406
    }
407
 
408
    public void setOnlyTransfered(SQLTableModelSourceOnline tableSource) {
409
        // needed for distinct
410
        tableSource.getReq().setLockSelect(false);
411
 
412
        tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
413
 
414
            @Override
415
            public SQLSelect transformChecked(SQLSelect input) {
416
 
417
                final SQLTable tableTR = getSource().getTable(getTableTranferName());
418
                // FIXME: preprocess TR_ .. content before join : group by id_src
419
                final SQLSelectJoin j = input.addBackwardJoin("INNER", null, tableTR.getForeignKeys(getSource()).iterator().next(), null);
420
                j.setWhere(new Where(tableTR.getForeignKeys(getDestination()).iterator().next(), "!=", getDestination().getUndefinedID()));
421
                input.setDistinct(true);
422
 
423
                System.err.println(input.asString());
424
                return input;
425
            }
426
        });
427
    }
428
 
429
    public void setOnlyNotTransfered(SQLTableModelSourceOnline tableSource) {
430
        tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
431
 
432
            @Override
433
            public SQLSelect transformChecked(SQLSelect input) {
434
                final SQLTable tableTR = getSource().getTable(getTableTranferName());
435
 
436
                final Where w = new Where(tableTR.getForeignKeys(getSource()).iterator().next(), "=", input.getAlias(getSource().getKey()));
437
                input.addJoin("LEFT", tableTR, w);
438
                final Where w2 = new Where(tableTR.getForeignKeys(getDestination()).iterator().next(), "IS", (Object) null);
439
                input.setWhere(w2);
440
 
441
                System.err.println(input.asString());
442
                return input;
443
            }
444
        });
445
    }
446
 
447
    /**
448
     * register manually a transfer, use with caution
449
     *
450
     * @throws SQLException
174 ilm 451
     */
73 ilm 452
    public void addTransfert(int idFrom, int idTo) throws SQLException {
453
        final SQLTable tableTransfert = getSource().getTable(getTableTranferName());
454
        final SQLRowValues rowTransfer = new SQLRowValues(tableTransfert);
455
        final Set<SQLField> foreignKeysSrc = tableTransfert.getForeignKeys(getSource());
456
        final Set<SQLField> foreignKeysDest = tableTransfert.getForeignKeys(getDestination());
457
        rowTransfer.put(foreignKeysSrc.iterator().next().getName(), idFrom);
458
        rowTransfer.put(foreignKeysDest.iterator().next().getName(), idTo);
459
        rowTransfer.commit();
460
 
461
    }
462
 
17 ilm 463
}