OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 177
Line 14... Line 14...
14
 package org.openconcerto.sql.view.list;
14
 package org.openconcerto.sql.view.list;
15
 
15
 
16
import org.openconcerto.sql.element.SQLElement;
16
import org.openconcerto.sql.element.SQLElement;
17
import org.openconcerto.sql.model.Constraint;
17
import org.openconcerto.sql.model.Constraint;
18
import org.openconcerto.sql.model.SQLField;
18
import org.openconcerto.sql.model.SQLField;
-
 
19
import org.openconcerto.sql.model.SQLInsert;
19
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRow;
20
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowAccessor;
21
import org.openconcerto.sql.model.SQLRowListRSH;
22
import org.openconcerto.sql.model.SQLRowListRSH;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreMode;
24
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreMode;
-
 
25
import org.openconcerto.sql.model.SQLRowValuesCluster.StoreResult;
24
import org.openconcerto.sql.model.SQLSelect;
26
import org.openconcerto.sql.model.SQLSelect;
25
import org.openconcerto.sql.model.SQLSyntax.ConstraintType;
27
import org.openconcerto.sql.model.SQLSyntax.ConstraintType;
26
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.SQLTable;
-
 
29
import org.openconcerto.sql.model.SQLUpdate;
27
import org.openconcerto.sql.model.UndefinedRowValuesCache;
30
import org.openconcerto.sql.model.UndefinedRowValuesCache;
28
import org.openconcerto.sql.model.Where;
31
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.sql.utils.AlterTable;
32
import org.openconcerto.sql.utils.AlterTable;
30
import org.openconcerto.utils.CompareUtils;
33
import org.openconcerto.utils.CompareUtils;
31
import org.openconcerto.utils.ExceptionHandler;
34
import org.openconcerto.utils.ExceptionHandler;
-
 
35
import org.openconcerto.utils.ListMap;
32
import org.openconcerto.utils.OrderedSet;
36
import org.openconcerto.utils.OrderedSet;
33
 
37
 
34
import java.math.BigDecimal;
38
import java.math.BigDecimal;
35
import java.sql.SQLException;
39
import java.sql.SQLException;
36
import java.util.ArrayList;
40
import java.util.ArrayList;
Line 281... Line 285...
281
 
285
 
282
    /**
286
    /**
283
     * Valider les modifications dans la base
287
     * Valider les modifications dans la base
284
     */
288
     */
285
    public void commitData() {
289
    public void commitData() {
-
 
290
        commitData(false);
-
 
291
    }
-
 
292
 
-
 
293
    /**
-
 
294
     * Valider les modifications dans la base
-
 
295
     */
-
 
296
    public void commitData(boolean useMultipleInsertUpdate) {
286
        checkEDT();
297
        checkEDT();
287
        final List<SQLRowValues> rowsToCommmit = new ArrayList<SQLRowValues>();
298
        final List<SQLRowValues> rowsToCommmit = new ArrayList<SQLRowValues>();
288
        rowsToCommmit.addAll(this.rowValues);
299
        rowsToCommmit.addAll(this.rowValues);
289
        try {
300
        try {
290
            final SQLTable table = getDefaultRowValues().getTable();
301
            final SQLTable table = getDefaultRowValues().getTable();
Line 294... Line 305...
294
                final String req = new AlterTable(table).dropConstraint(constraint.getName()).asString();
305
                final String req = new AlterTable(table).dropConstraint(constraint.getName()).asString();
295
                table.getDBSystemRoot().getDataSource().execute(req);
306
                table.getDBSystemRoot().getDataSource().execute(req);
296
                table.fetchFields();
307
                table.fetchFields();
297
                table.getSchema().updateVersion();
308
                table.getSchema().updateVersion();
298
            }
309
            }
-
 
310
 
299
            BigDecimal o = table.getMaxOrder(Boolean.FALSE);
311
            BigDecimal o = table.getMaxOrder(Boolean.FALSE);
300
            final int size = rowsToCommmit.size();
312
            final int size = rowsToCommmit.size();
-
 
313
 
-
 
314
            if (useMultipleInsertUpdate) {
-
 
315
                List<SQLUpdate> updates = new ArrayList<>();
-
 
316
                ListMap<List<SQLField>, SQLInsert> mapInsertByFields = new ListMap<>();
-
 
317
                ListMap<List<SQLField>, SQLRowValues> mapInsertRowValues = new ListMap<>();
-
 
318
 
-
 
319
                for (int i = 0; i < size; i++) {
-
 
320
                    final SQLRowValues r = rowsToCommmit.get(i);
-
 
321
                    r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
-
 
322
                    if (r.hasID()) {
-
 
323
                        SQLUpdate up = new SQLUpdate(new Where(table.getKey(), "=", r.getID()));
-
 
324
                        up.importValuesFrom(r);
-
 
325
                        updates.add(up);
-
 
326
                    } else {
-
 
327
                        SQLInsert insert = new SQLInsert();
-
 
328
                        insert.importValuesFrom(r);
-
 
329
                        List<SQLField> fields = insert.getFields();
-
 
330
                        mapInsertByFields.add(fields, insert);
-
 
331
                        mapInsertRowValues.add(fields, r);
-
 
332
                    }
-
 
333
                }
-
 
334
                if (!mapInsertByFields.isEmpty()) {
-
 
335
 
-
 
336
                    for (List<SQLField> fieldSize : mapInsertByFields.keySet()) {
-
 
337
                        List<Number> ids = SQLInsert.executeSimilarInserts(table.getDBSystemRoot(), mapInsertByFields.get(fieldSize), true);
-
 
338
                        List<SQLRowValues> insertsRowValues = mapInsertRowValues.get(fieldSize);
-
 
339
                        if (ids.size() == insertsRowValues.size()) {
-
 
340
                            for (int i = 0; i < ids.size(); i++) {
-
 
341
                                final SQLRowValues r = insertsRowValues.get(i);
-
 
342
                                r.setID(ids.get(i));
-
 
343
                            }
-
 
344
                        }
-
 
345
 
-
 
346
                    }
-
 
347
 
-
 
348
                }
-
 
349
                if (!updates.isEmpty()) {
-
 
350
                    SQLUpdate.executeMultipleWithBatch(table.getDBSystemRoot(), updates);
-
 
351
                }
-
 
352
                table.fireTableModified(-1);
-
 
353
            } else {
301
            for (int i = 0; i < size; i++) {
354
                for (int i = 0; i < size; i++) {
302
                final SQLRowValues r = rowsToCommmit.get(i);
355
                    final SQLRowValues r = rowsToCommmit.get(i);
303
                r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
356
                    r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
304
                final SQLRow row = r.getGraph().store(StoreMode.COMMIT).getStoredRow(r);
357
                    final StoreResult store = r.getGraph().store(StoreMode.COMMIT, false);
-
 
358
                    final SQLRow row = store.getStoredRow(r);
305
                r.setID(row.getIDNumber());
359
                    r.setID(row.getIDNumber());
306
            }
360
                }
-
 
361
 
-
 
362
            }
307
        } catch (SQLException e) {
363
        } catch (SQLException e) {
308
            ExceptionHandler.handle("Unable to commit rows", e);
364
            ExceptionHandler.handle("Unable to commit rows", e);
309
        }
365
        }
-
 
366
 
310
    }
367
    }
311
 
368
 
312
    public void addTableModelListener(TableModelListener l) {
369
    public void addTableModelListener(TableModelListener l) {
313
        this.tableModelListeners.add(l);
370
        this.tableModelListeners.add(l);
314
    }
371
    }
Line 598... Line 655...
598
        }
655
        }
599
        if (rowVals != null) {
656
        if (rowVals != null) {
600
 
657
 
601
            final List<SQLRowValues> newRows = new ArrayList<SQLRowValues>();
658
            final List<SQLRowValues> newRows = new ArrayList<SQLRowValues>();
602
 
659
 
603
            if (rowVals.getID() > 1) {
660
            if (rowVals.hasID() && !rowVals.isUndefined() && rowVals.getID() != SQLRow.NONEXISTANT_ID) {
604
                SQLRow row = rowVals.getTable().getRow(rowVals.getID());
661
                SQLRow row = rowVals.getTable().getRow(rowVals.getID());
605
                List<SQLRow> rowSet;
662
                List<SQLRow> rowSet;
606
                if (referentField == null) {
663
                if (referentField == null) {
607
                    rowSet = row.getReferentRows(RowValuesTableModel.this.element.getTable());
664
                    rowSet = row.getReferentRows(RowValuesTableModel.this.element.getTable());
608
                } else {
665
                } else {