OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 Rev 156
Line 25... Line 25...
25
import org.openconcerto.sql.view.list.ListSQLLine;
25
import org.openconcerto.sql.view.list.ListSQLLine;
26
import org.openconcerto.sql.view.list.SQLTableModelLinesSourceOffline;
26
import org.openconcerto.sql.view.list.SQLTableModelLinesSourceOffline;
27
import org.openconcerto.ui.group.Group;
27
import org.openconcerto.ui.group.Group;
28
import org.openconcerto.ui.group.Item;
28
import org.openconcerto.ui.group.Item;
29
import org.openconcerto.ui.light.CustomEditorProvider;
29
import org.openconcerto.ui.light.CustomEditorProvider;
30
import org.openconcerto.ui.light.JSONToLightUIConvertor;
-
 
31
import org.openconcerto.ui.light.LightUICheckBox;
30
import org.openconcerto.ui.light.LightUICheckBox;
32
import org.openconcerto.ui.light.LightUIComboBox;
31
import org.openconcerto.ui.light.LightUIComboBox;
33
import org.openconcerto.ui.light.LightUIDate;
32
import org.openconcerto.ui.light.LightUIDate;
34
import org.openconcerto.ui.light.LightUIElement;
33
import org.openconcerto.ui.light.LightUIElement;
35
import org.openconcerto.ui.light.LightUIFrame;
34
import org.openconcerto.ui.light.LightUIFrame;
36
import org.openconcerto.utils.io.JSONConverter;
35
import org.openconcerto.utils.io.JSONConverter;
37
 
36
 
-
 
37
import java.io.IOException;
-
 
38
import java.io.ObjectInput;
-
 
39
import java.io.ObjectOutput;
38
import java.math.BigDecimal;
40
import java.math.BigDecimal;
39
import java.sql.SQLException;
41
import java.sql.SQLException;
40
import java.sql.Timestamp;
42
import java.sql.Timestamp;
41
import java.util.Date;
43
import java.util.Date;
42
import java.util.Map;
44
import java.util.Map;
Line 50... Line 52...
50
    private Group group;
52
    private Group group;
51
    private SQLRowValues sqlRow;
53
    private SQLRowValues sqlRow;
52
 
54
 
53
    private EditMode editMode = EditMode.READONLY;
55
    private EditMode editMode = EditMode.READONLY;
54
 
56
 
-
 
57
    public LightEditFrame() {
-
 
58
        // Serialization
-
 
59
    }
-
 
60
 
55
    // Init from json constructor
61
    // Init from json constructor
56
    public LightEditFrame(final JSONObject json) {
62
    public LightEditFrame(final JSONObject json) {
57
        super(json);
63
        super(json);
58
    }
64
    }
59
 
65
 
Line 174... Line 180...
174
                }
180
                }
175
            }
181
            }
176
        }
182
        }
177
    }
183
    }
178
 
184
 
179
    final protected void putValueFromUserControl(final Configuration configuration, final SQLElement sqlElement, final SQLField sqlField, final LightUIElement uiElement,
185
    protected final void putValueFromUserControl(final Configuration configuration, final SQLElement sqlElement, final SQLField sqlField, final LightUIElement uiElement,
180
            final Map<String, CustomEditorProvider> customEditors) {
186
            final Map<String, CustomEditorProvider> customEditors) {
181
        if (!uiElement.isNotSaved()) {
187
        if (!uiElement.isNotSaved()) {
-
 
188
            boolean useElementValue = true;
182
            final Class<?> fieldType = sqlField.getType().getJavaType();
189
            final Class<?> fieldType = sqlField.getType().getJavaType();
183
            if (customEditors.containsKey(uiElement.getId())) {
190
            if (customEditors.containsKey(uiElement.getId())) {
184
                final CustomEditorProvider customEditor = customEditors.get(uiElement.getId());
191
                final CustomEditorProvider customEditor = customEditors.get(uiElement.getId());
185
                if (customEditor instanceof SavableCustomEditorProvider) {
192
                if (customEditor instanceof SavableCustomEditorProvider) {
186
                    ((SavableCustomEditorProvider) customEditor).save(this.sqlRow, sqlField, uiElement);
193
                    ((SavableCustomEditorProvider) customEditor).save(this.sqlRow, sqlField, uiElement);
187
                } else {
194
                    useElementValue = false;
188
                    throw new IllegalStateException(customEditor + " must implement SavableCustomEditorProvider for field " + sqlField.getFieldName() + " of tye " + fieldType);
-
 
189
                }
195
                }
190
            } else {
196
            }
-
 
197
 
-
 
198
            if (useElementValue) {
191
                final String fieldName = sqlField.getFieldName();
199
                final String fieldName = sqlField.getFieldName();
192
                if (sqlField.isKey()) {
200
                if (sqlField.isKey()) {
193
                    if (!(uiElement instanceof LightUIComboBox)) {
201
                    if (!(uiElement instanceof LightUIComboBox)) {
194
                        throw new IllegalArgumentException("Invalid UI Element for field: " + fieldName + ". When field is foreign key, UI Element must be a LightUIDate");
202
                        throw new IllegalArgumentException("Invalid UI Element for field: " + fieldName + ". When field is foreign key, UI Element must be a LightUIDate");
195
                    }
203
                    }
Line 295... Line 303...
295
    // FIXME use SQLRowValues method
303
    // FIXME use SQLRowValues method
296
    protected final void setMetaData(final int userId) {
304
    protected final void setMetaData(final int userId) {
297
        final SQLTable sqlTable = this.sqlRow.getTable();
305
        final SQLTable sqlTable = this.sqlRow.getTable();
298
        final Date now = new Date();
306
        final Date now = new Date();
299
        // FIXME only set those fields at insertion time
307
        // FIXME only set those fields at insertion time
-
 
308
        if (sqlTable.getCreationUserField() != null) {
300
        if (this.sqlRow.getObject(sqlTable.getCreationUserField().getName()) == null || this.sqlRow.getObject(sqlTable.getCreationDateField().getName()) == null) {
309
            if (this.sqlRow.getObject(sqlTable.getCreationUserField().getName()) == null || this.sqlRow.getObject(sqlTable.getCreationDateField().getName()) == null) {
301
            setFieldValue(this.sqlRow, sqlTable.getCreationUserField(), false, userId);
310
                setFieldValue(this.sqlRow, sqlTable.getCreationUserField(), false, userId);
302
            setFieldValue(this.sqlRow, sqlTable.getCreationDateField(), false, now);
311
                setFieldValue(this.sqlRow, sqlTable.getCreationDateField(), false, now);
303
        }
312
            }
-
 
313
        }
-
 
314
        if (sqlTable.getModifUserField() != null) {
304
        setFieldValue(this.sqlRow, sqlTable.getModifUserField(), false, userId);
315
            setFieldValue(this.sqlRow, sqlTable.getModifUserField(), false, userId);
305
        setFieldValue(this.sqlRow, sqlTable.getModifDateField(), false, now);
316
            setFieldValue(this.sqlRow, sqlTable.getModifDateField(), false, now);
306
    }
317
        }
-
 
318
    }
307
 
319
 
308
    static private boolean setFieldValue(final SQLRowValues vals, final SQLField f, final boolean remove, final Object val) {
320
    static private boolean setFieldValue(final SQLRowValues vals, final SQLField f, final boolean remove, final Object val) {
309
        if (f == null)
321
        if (f == null)
310
            return false;
322
            return false;
311
        if (remove)
323
        if (remove)
Line 319... Line 331...
319
    public String getClassName() {
331
    public String getClassName() {
320
        return this.getClass().getName();
332
        return this.getClass().getName();
321
    }
333
    }
322
 
334
 
323
    @Override
335
    @Override
324
    public JSONToLightUIConvertor getConvertor() {
-
 
325
        return new JSONToLightUIConvertor() {
-
 
326
 
-
 
327
            @Override
-
 
328
            public LightUIElement convert(JSONObject json) {
-
 
329
                return new LightEditFrame(json);
-
 
330
            }
-
 
331
        };
-
 
332
    }
-
 
333
 
-
 
334
    @Override
-
 
335
    public LightUIElement clone() {
336
    public LightUIElement clone() {
336
        return new LightEditFrame(this);
337
        return new LightEditFrame(this);
337
    }
338
    }
338
 
339
 
339
    // TODO: implement JSONAble on SQLRowValues and Group
340
    // TODO: implement JSONAble on SQLRowValues and Group
Line 360... Line 361...
360
            this.editMode = EditMode.MODIFICATION;
361
            this.editMode = EditMode.MODIFICATION;
361
        } else if (jsonEditMode == 3) {
362
        } else if (jsonEditMode == 3) {
362
            this.editMode = EditMode.READONLY;
363
            this.editMode = EditMode.READONLY;
363
        }
364
        }
364
    }
365
    }
-
 
366
 
-
 
367
    @Override
-
 
368
    public void writeExternal(ObjectOutput out) throws IOException {
-
 
369
        super.writeExternal(out);
-
 
370
        if (this.editMode.equals(EditMode.CREATION)) {
-
 
371
            out.writeByte(0);
-
 
372
        } else if (this.editMode.equals(EditMode.MODIFICATION)) {
-
 
373
            out.writeByte(1);
-
 
374
        } else if (this.editMode.equals(EditMode.READONLY)) {
-
 
375
            out.writeByte(2);
-
 
376
        } else {
-
 
377
            throw new IllegalStateException("unknown mode " + this.editMode);
-
 
378
        }
-
 
379
    }
-
 
380
 
-
 
381
    @Override
-
 
382
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-
 
383
        super.readExternal(in);
-
 
384
        int mode = in.readByte();
-
 
385
        if (mode == 0) {
-
 
386
            this.editMode = EditMode.CREATION;
-
 
387
        } else if (mode == 1) {
-
 
388
            this.editMode = EditMode.MODIFICATION;
-
 
389
        } else if (mode == 2) {
-
 
390
            this.editMode = EditMode.READONLY;
-
 
391
        } else {
-
 
392
            throw new IllegalStateException("unknown mode " + mode);
-
 
393
        }
-
 
394
    }
365
}
395
}