OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 182 Rev 185
Line 29... Line 29...
29
import org.openconcerto.sql.model.SQLUpdate;
29
import org.openconcerto.sql.model.SQLUpdate;
30
import org.openconcerto.sql.model.UndefinedRowValuesCache;
30
import org.openconcerto.sql.model.UndefinedRowValuesCache;
31
import org.openconcerto.sql.model.Where;
31
import org.openconcerto.sql.model.Where;
32
import org.openconcerto.sql.utils.AlterTable;
32
import org.openconcerto.sql.utils.AlterTable;
33
import org.openconcerto.utils.CompareUtils;
33
import org.openconcerto.utils.CompareUtils;
34
import org.openconcerto.utils.ExceptionHandler;
-
 
35
import org.openconcerto.utils.ListMap;
34
import org.openconcerto.utils.ListMap;
36
import org.openconcerto.utils.OrderedSet;
35
import org.openconcerto.utils.OrderedSet;
37
 
36
 
38
import java.math.BigDecimal;
37
import java.math.BigDecimal;
39
import java.sql.SQLException;
38
import java.sql.SQLException;
Line 56... Line 55...
56
    // modification of rowValues MUST be done in AWT EDT
55
    // modification of rowValues MUST be done in AWT EDT
57
    // methods that perform requests, MUST use the runnableQueue
56
    // methods that perform requests, MUST use the runnableQueue
58
    // synchronized is used to protect list access
57
    // synchronized is used to protect list access
59
    private List<SQLRowValues> rowValues = new ArrayList<SQLRowValues>();
58
    private List<SQLRowValues> rowValues = new ArrayList<SQLRowValues>();
60
 
59
 
-
 
60
    private final boolean debug = false;
-
 
61
 
61
    private OrderedSet<TableModelListener> tableModelListeners = new OrderedSet<TableModelListener>();
62
    private OrderedSet<TableModelListener> tableModelListeners = new OrderedSet<TableModelListener>();
62
 
63
 
63
    protected SQLElement element;
64
    protected SQLElement element;
64
 
65
 
65
    private int nbColumn;
66
    private int nbColumn;
Line 76... Line 77...
76
 
77
 
77
    private boolean editable = true;
78
    private boolean editable = true;
78
 
79
 
79
    private Where fillWhere;
80
    private Where fillWhere;
80
 
81
 
-
 
82
    private boolean gestionReferentActive = false;
-
 
83
 
81
    public RowValuesTableModel() {
84
    public RowValuesTableModel() {
82
 
85
 
83
    }
86
    }
84
 
87
 
85
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField) {
88
    public RowValuesTableModel(final SQLElement e, final List<SQLTableElement> list, SQLField validField) {
Line 150... Line 153...
150
 
153
 
151
    public SQLRowValues getDefaultRowValues() {
154
    public SQLRowValues getDefaultRowValues() {
152
        return this.defautRow;
155
        return this.defautRow;
153
    }
156
    }
154
 
157
 
-
 
158
    public void setGestionReferentActive(boolean gestionReferent) {
-
 
159
        this.gestionReferentActive = gestionReferent;
-
 
160
    }
-
 
161
 
155
    public synchronized void addColumn(SQLTableElement e) {
162
    public synchronized void addColumn(SQLTableElement e) {
156
        this.nbColumn++;
163
        this.nbColumn++;
157
        this.list.add(e);
164
        this.list.add(e);
158
    }
165
    }
159
 
166
 
Line 175... Line 182...
175
            return false;
182
            return false;
176
 
183
 
177
        SQLTableElement elt = this.list.get(columnIndex);
184
        SQLTableElement elt = this.list.get(columnIndex);
178
        boolean validate = false;
185
        boolean validate = false;
179
        boolean fieldValidate = false;
186
        boolean fieldValidate = false;
180
        if (this.validationField != null) {
187
        if (this.validationField != null && this.validationField.getTable().getName().equals(getSQLElement().getTable().getName())) {
181
            fieldValidate = elt.getField().getName().equalsIgnoreCase(this.validationField.getName());
188
            fieldValidate = elt.getField().getName().equalsIgnoreCase(this.validationField.getName());
182
            validate = this.getRowValuesAt(rowIndex).getBoolean(this.validationField.getName());
189
            validate = this.getRowValuesAt(rowIndex).getBoolean(this.validationField.getName());
183
        }
190
        }
184
        if (validate && fieldValidate) {
191
        if (validate && fieldValidate) {
185
            return this.list.get(columnIndex).isCellEditable(this.getRowValuesAt(rowIndex), rowIndex, columnIndex);
192
            return this.list.get(columnIndex).isCellEditable(this.getRowValuesAt(rowIndex), rowIndex, columnIndex);
Line 203... Line 210...
203
        } else {
210
        } else {
204
            SQLRowValues val = this.rowValues.get(rowIndex);
211
            SQLRowValues val = this.rowValues.get(rowIndex);
205
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
212
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
206
            Object storedObject = val.getObject(sqlTableElem.getRowField());
213
            Object storedObject = val.getObject(sqlTableElem.getRowField());
207
            result = sqlTableElem.getValueFrom(val);
214
            result = sqlTableElem.getValueFrom(val);
-
 
215
            final Object resultToCheck;
-
 
216
            if (sqlTableElem.getElementClass() != null && Number.class.isAssignableFrom(sqlTableElem.getElementClass()) && result != null && SQLRowAccessor.class.isAssignableFrom(result.getClass())
-
 
217
                    && storedObject != null && Number.class.isAssignableFrom(storedObject.getClass())) {
-
 
218
                resultToCheck = ((SQLRowAccessor) result).getIDNumber();
-
 
219
            } else {
-
 
220
                resultToCheck = result;
-
 
221
            }
208
            if (sqlTableElem.getElementClass() != null && Number.class.isAssignableFrom(sqlTableElem.getElementClass()) && !CompareUtils.equals(result, storedObject)) {
222
            if (sqlTableElem.getElementClass() != null && Number.class.isAssignableFrom(sqlTableElem.getElementClass()) && !CompareUtils.equals(resultToCheck, storedObject)) {
209
                fireTableDataChanged();
223
                fireTableDataChanged();
210
            }
224
            }
211
 
225
 
212
        }
226
        }
213
 
227
 
Line 222... Line 236...
222
    public void putValue(Object value, int rowIndex, String fieldName) {
236
    public void putValue(Object value, int rowIndex, String fieldName) {
223
        putValue(value, rowIndex, fieldName, false);
237
        putValue(value, rowIndex, fieldName, false);
224
    }
238
    }
225
 
239
 
226
    public void putValue(Object value, int rowIndex, String fieldName, boolean forcedFire) {
240
    public void putValue(Object value, int rowIndex, String fieldName, boolean forcedFire) {
-
 
241
        if (debug) {
-
 
242
            System.err.println("putValue(row:" + rowIndex + "]) :" + value + "(" + value.getClass() + "   fieldName : " + fieldName + ")");
-
 
243
            Thread.dumpStack();
-
 
244
        }
-
 
245
 
227
        checkEDT();
246
        checkEDT();
228
        final SQLRowValues rowVal = this.rowValues.get(rowIndex);
247
        final SQLRowValues rowVal = this.rowValues.get(rowIndex);
229
        Object oldValue = rowVal.getObject(fieldName);
248
        Object oldValue = rowVal.getObject(fieldName);
230
        if (!forcedFire) {
249
        if (!forcedFire) {
231
            if (oldValue == value) {
250
            if (oldValue == value) {
Line 257... Line 276...
257
            return;
276
            return;
258
        }
277
        }
259
        if (oldValue != null && oldValue.equals(aValue)) {
278
        if (oldValue != null && oldValue.equals(aValue)) {
260
            return;
279
            return;
261
        }
280
        }
-
 
281
 
262
        try {
282
        try {
263
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
283
            SQLTableElement sqlTableElem = this.list.get(columnIndex);
264
 
284
 
265
            SQLRowValues rowVal = this.rowValues.get(rowIndex);
285
            SQLRowValues rowVal = this.rowValues.get(rowIndex);
266
            Object realVal = sqlTableElem.convertEditorValueToModel(aValue, rowVal);
286
            Object realVal = sqlTableElem.convertEditorValueToModel(aValue, rowVal);
267
            if (realVal == null || realVal.getClass() == this.getColumnClass(columnIndex)) {
287
            if (realVal == null || realVal.getClass() == this.getColumnClass(columnIndex)) {
268
                sqlTableElem.setValueFrom(rowVal, realVal);
288
                sqlTableElem.setValueFrom(rowVal, realVal);
269
                fireTableChanged(new TableModelEvent(this, rowIndex, rowIndex, columnIndex));
289
                fireTableChanged(new TableModelEvent(this, rowIndex, rowIndex, columnIndex));
270
            } else {
-
 
271
                System.err.println("RowValuesTableModel:setValueAt:" + realVal + "(" + realVal.getClass() + ") at (row:" + rowIndex + "/col:" + columnIndex + ") column class : "
-
 
272
                        + this.getColumnClass(columnIndex));
-
 
273
                Thread.dumpStack();
-
 
274
            }
290
            }
275
        } catch (Exception e) {
291
        } catch (Exception e) {
276
            // can append when stop editing occur while removing a line
292
            // can append when stop editing occur while removing a line
277
            e.printStackTrace();
293
            e.printStackTrace();
278
        }
294
        }
Line 293... Line 309...
293
        return sqlTableElem.getColumnName();
309
        return sqlTableElem.getColumnName();
294
    }
310
    }
295
 
311
 
296
    /**
312
    /**
297
     * Valider les modifications dans la base
313
     * Valider les modifications dans la base
-
 
314
     * 
-
 
315
     * @throws SQLException
298
     */
316
     */
299
    public void commitData() {
317
    public void commitData() throws SQLException {
300
        commitData(false);
318
        commitData(false);
301
    }
319
    }
302
 
320
 
303
    /**
321
    /**
304
     * Valider les modifications dans la base
322
     * Valider les modifications dans la base
305
     */
323
     */
306
    public void commitData(boolean useMultipleInsertUpdate) {
324
    public void commitData(boolean useMultipleInsertUpdate) throws SQLException {
307
        checkEDT();
325
        checkEDT();
308
        final List<SQLRowValues> rowsToCommmit = new ArrayList<SQLRowValues>();
326
        final List<SQLRowValues> rowsToCommmit = new ArrayList<SQLRowValues>();
309
        rowsToCommmit.addAll(this.rowValues);
327
        rowsToCommmit.addAll(this.rowValues);
310
        try {
328
 
311
            final SQLTable table = getDefaultRowValues().getTable();
329
        final SQLTable table = getDefaultRowValues().getTable();
312
            // Remove constraint on ORDRE for private
330
        // Remove constraint on ORDRE for private
313
            final Constraint constraint = table.getConstraint(ConstraintType.UNIQUE, Arrays.asList(table.getOrderField().getName()));
331
        final Constraint constraint = table.getConstraint(ConstraintType.UNIQUE, Arrays.asList(table.getOrderField().getName()));
314
            if (constraint != null) {
332
        if (constraint != null) {
315
                final String req = new AlterTable(table).dropConstraint(constraint.getName()).asString();
333
            final String req = new AlterTable(table).dropConstraint(constraint.getName()).asString();
Line 357... Line 375...
357
 
375
 
358
                }
376
            }
359
                if (!updates.isEmpty()) {
377
            if (!updates.isEmpty()) {
360
                    SQLUpdate.executeMultipleWithBatch(table.getDBSystemRoot(), updates);
378
                SQLUpdate.executeMultipleWithBatch(table.getDBSystemRoot(), updates);
361
                }
379
            }
-
 
380
            if (this.gestionReferentActive) {
-
 
381
 
-
 
382
                // Traitement des référents
-
 
383
                List<SQLInsert> insertsRefs = new ArrayList<>();
-
 
384
                List<SQLUpdate> updatesRefs = new ArrayList<>();
-
 
385
 
-
 
386
                for (SQLRowValues rowVals : rowsToCommmit) {
-
 
387
                    final Collection<SQLRowValues> referentRows = rowVals.getReferentRows();
-
 
388
                    System.err.println(referentRows.size() + " ref Rows pour RowValues " + rowVals);
-
 
389
                    for (SQLRowValues refRow : referentRows) {
-
 
390
 
-
 
391
                        System.err.println("\t" + refRow);
-
 
392
                        if (refRow.hasID()) {
-
 
393
                            SQLUpdate update = new SQLUpdate(new Where(refRow.getTable().getKey(), "=", refRow.getID()));
-
 
394
                            update.importValuesFrom(refRow);
-
 
395
                            updatesRefs.add(update);
-
 
396
                        } else {
-
 
397
                            SQLInsert insert = new SQLInsert();
-
 
398
                            insert.importValuesFrom(refRow);
-
 
399
                            System.err.println("\t\t" + insert.asString());
-
 
400
                            insertsRefs.add(insert);
-
 
401
                        }
-
 
402
                    }
-
 
403
                }
-
 
404
 
-
 
405
                if (!insertsRefs.isEmpty()) {
-
 
406
                    SQLInsert.executeMultiple(table.getDBSystemRoot(), insertsRefs);
-
 
407
                }
-
 
408
                if (!updatesRefs.isEmpty()) {
-
 
409
                    SQLUpdate.executeMultipleWithBatch(table.getDBSystemRoot(), updatesRefs);
-
 
410
                }
-
 
411
            }
362
                table.fireTableModified(-1);
412
            table.fireTableModified(-1);
363
            } else {
413
        } else {
364
                for (int i = 0; i < size; i++) {
414
            for (int i = 0; i < size; i++) {
365
                    final SQLRowValues r = rowsToCommmit.get(i);
415
                final SQLRowValues r = rowsToCommmit.get(i);
366
                    r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
416
                r.put(r.getTable().getOrderField().getFieldName(), o.add(new BigDecimal(i + 1)));
367
                    final StoreResult store = r.getGraph().store(StoreMode.COMMIT, false);
417
                final StoreResult store = r.getGraph().store(StoreMode.COMMIT, false);
368
                    final SQLRow row = store.getStoredRow(r);
418
                final SQLRow row = store.getStoredRow(r);
369
                    r.setID(row.getIDNumber());
419
                r.setID(row.getIDNumber());
370
                }
420
            }
371
 
-
 
372
            }
-
 
373
        } catch (SQLException e) {
-
 
374
            ExceptionHandler.handle("Unable to commit rows", e);
-
 
375
        }
421
        }
376
 
422
 
377
    }
423
    }
378
 
424
 
379
    public void addTableModelListener(TableModelListener l) {
425
    public void addTableModelListener(TableModelListener l) {
Line 540... Line 586...
540
 
586
 
541
    public final List<SQLTableElement> getList() {
587
    public final List<SQLTableElement> getList() {
542
        return this.list;
588
        return this.list;
543
    }
589
    }
544
 
590
 
545
    public void updateField(String field, SQLRowValues rowVals, String fieldCondition) {
591
    public void updateField(String field, SQLRowValues rowVals, String fieldCondition) throws SQLException {
546
        checkEDT();
592
        checkEDT();
547
        if (rowVals != null) {
593
        if (rowVals != null) {
548
            int stop = this.rowValues.size();
594
            int stop = this.rowValues.size();
549
 
595
 
550
            // FIXME check à faire sur l'ensemble des rows avec la methode isValid(). Quand
596
            // FIXME check à faire sur l'ensemble des rows avec la methode isValid(). Quand
Line 588... Line 634...
588
                } else {
634
                } else {
589
                    rowVals2.putEmptyLink(field);
635
                    rowVals2.putEmptyLink(field);
590
                }
636
                }
591
            }
637
            }
592
            this.rowValuesDeleted.clear();
638
            this.rowValuesDeleted.clear();
593
            try {
-
 
-
 
639
 
594
                this.element.archive(toArchive);
640
            this.element.archive(toArchive);
595
            } catch (SQLException e) {
-
 
596
                throw new IllegalStateException("Unable to archive rows : " + toArchive, e);
-
 
597
            }
-
 
598
 
641
 
599
            if (id != SQLRow.NONEXISTANT_ID) {
642
            if (id != SQLRow.NONEXISTANT_ID) {
600
                this.commitData();
643
                this.commitData();
601
            }
644
            }
602
        }
645
        }
603
    }
646
    }
604
 
647
 
605
    public void updateField(String field, SQLRowValues rowVals) {
648
    public void updateField(String field, SQLRowValues rowVals) throws SQLException {
606
        updateField(field, rowVals, null);
649
        updateField(field, rowVals, null);
607
    }
650
    }
608
 
651
 
609
    public void updateField(String field, int id) {
652
    public void updateField(String field, int id) throws SQLException {
610
        updateField(field, id, null);
653
        updateField(field, id, null);
611
    }
654
    }
612
 
655
 
613
    public void updateField(String field, int id, String fieldCondition) {
656
    public void updateField(String field, int id, String fieldCondition) throws SQLException {
614
        if (id > 0) {
657
        if (id > 0) {
615
            updateField(field, this.element.getTable().getForeignTable(field).getRow(id).createUpdateRow(), fieldCondition);
658
            updateField(field, this.element.getTable().getForeignTable(field).getRow(id).createUpdateRow(), fieldCondition);
616
        }
659
        }
617
    }
660
    }
618
 
661
 
Line 665... Line 708...
665
        }
708
        }
666
        if (rowVals != null) {
709
        if (rowVals != null) {
667
 
710
 
668
            final List<SQLRowValues> newRows = new ArrayList<SQLRowValues>();
711
            final List<SQLRowValues> newRows = new ArrayList<SQLRowValues>();
669
 
712
 
-
 
713
            final SQLTable table = RowValuesTableModel.this.element.getTable();
670
            if (rowVals.hasID() && !rowVals.isUndefined() && rowVals.getID() != SQLRow.NONEXISTANT_ID) {
714
            if (rowVals.hasID() && !rowVals.isUndefined() && rowVals.getID() != SQLRow.NONEXISTANT_ID) {
671
                SQLRow row = rowVals.getTable().getRow(rowVals.getID());
-
 
672
                List<SQLRow> rowSet;
-
 
673
                if (referentField == null) {
-
 
674
                    rowSet = row.getReferentRows(RowValuesTableModel.this.element.getTable());
715
                newRows.addAll(fetchDataFromDB(rowVals, referentField, fieldWhere, value));
675
                } else {
-
 
676
                    rowSet = row.getReferentRows(referentField);
-
 
677
                }
-
 
678
                for (SQLRow row2 : rowSet) {
-
 
679
                    if (fieldWhere == null || CompareUtils.equals(row2.getObject(fieldWhere.getName()), value)) {
-
 
680
                        SQLRowValues rowVals2 = new SQLRowValues(RowValuesTableModel.this.element.getTable());
-
 
681
                        rowVals2.loadAbsolutelyAll(row2);
-
 
682
                        newRows.add(rowVals2);
-
 
683
                    }
-
 
684
                }
-
 
685
 
-
 
686
            } else {
716
            } else {
687
                final Collection<? extends SQLRowAccessor> colRows;
717
                final Collection<? extends SQLRowAccessor> colRows;
688
                if (referentField == null) {
718
                if (referentField == null) {
689
                    colRows = rowVals.getReferentRows(RowValuesTableModel.this.element.getTable());
719
                    colRows = rowVals.getReferentRows(table);
690
                } else {
720
                } else {
691
                    colRows = rowVals.getReferentRows(referentField);
721
                    colRows = rowVals.getReferentRows(referentField);
692
                }
722
                }
693
                for (SQLRowAccessor rowValues : colRows) {
723
                for (SQLRowAccessor rowValues : colRows) {
694
                    if (fieldWhere == null || CompareUtils.equals(rowValues.getObject(fieldWhere.getName()), value)) {
724
                    if (fieldWhere == null || CompareUtils.equals(rowValues.getObject(fieldWhere.getName()), value)) {
Line 702... Line 732...
702
            fireTableModelModified(RowValuesTableModel.this.rowValues.size());
732
            fireTableModelModified(RowValuesTableModel.this.rowValues.size());
703
 
733
 
704
        }
734
        }
705
    }
735
    }
706
 
736
 
-
 
737
    public List<SQLRowValues> fetchDataFromDB(final SQLRowAccessor rowVals, final SQLField referentField, final SQLField fieldWhere, final Object value) {
-
 
738
        final List<SQLRowValues> newRows = new ArrayList<>();
-
 
739
        final SQLTable table = getSQLElement().getTable();
-
 
740
 
-
 
741
        SQLRow row = rowVals.getTable().getRow(rowVals.getID());
-
 
742
        List<SQLRow> rowSet;
-
 
743
        if (referentField == null) {
-
 
744
            rowSet = row.getReferentRows(table);
-
 
745
        } else {
-
 
746
            rowSet = row.getReferentRows(referentField);
-
 
747
        }
-
 
748
        for (SQLRow row2 : rowSet) {
-
 
749
            if (fieldWhere == null || CompareUtils.equals(row2.getObject(fieldWhere.getName()), value)) {
-
 
750
                SQLRowValues rowVals2 = new SQLRowValues(table);
-
 
751
                rowVals2.loadAbsolutelyAll(row2);
-
 
752
                newRows.add(rowVals2);
-
 
753
            }
-
 
754
        }
-
 
755
        return newRows;
-
 
756
    }
-
 
757
 
707
    public void addRow(SQLRowValues row) {
758
    public void addRow(SQLRowValues row) {
708
        checkEDT();
759
        checkEDT();
709
        addRow(row, true);
760
        addRow(row, true);
710
    }
761
    }
711
 
762