OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 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.ui.light;
15
 
16
import org.openconcerto.sql.Log;
17
import org.openconcerto.sql.PropsConfiguration;
174 ilm 18
import org.openconcerto.sql.element.SQLElement;
142 ilm 19
import org.openconcerto.sql.model.Constraint;
94 ilm 20
import org.openconcerto.sql.model.SQLField;
132 ilm 21
import org.openconcerto.sql.model.SQLRowAccessor;
142 ilm 22
import org.openconcerto.sql.model.SQLSyntax.ConstraintType;
94 ilm 23
import org.openconcerto.sql.model.SQLTable;
142 ilm 24
import org.openconcerto.sql.request.ComboSQLRequest;
25
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
132 ilm 26
import org.openconcerto.ui.light.LightUIComboBox;
27
import org.openconcerto.ui.light.LightUIComboBoxElement;
94 ilm 28
import org.openconcerto.ui.light.LightUIElement;
29
import org.openconcerto.ui.light.LightUILine;
30
import org.openconcerto.ui.light.LightUIPanel;
132 ilm 31
import org.openconcerto.utils.io.JSONConverter;
94 ilm 32
 
132 ilm 33
import java.util.Calendar;
34
import java.util.Date;
174 ilm 35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
142 ilm 38
import java.util.Set;
132 ilm 39
 
94 ilm 40
/**
41
 * Fill value from default or database
42
 */
43
public class LightUIPanelFiller {
44
    private final LightUIPanel panel;
45
 
46
    public LightUIPanelFiller(LightUIPanel panel) {
47
        this.panel = panel;
48
    }
49
 
50
    public void fillWithDefaultValues() {
132 ilm 51
        final int panelChildCount = this.panel.getChildrenCount();
52
        for (int i = 0; i < panelChildCount; i++) {
53
            final LightUILine panelChild = this.panel.getChild(i, LightUILine.class);
54
            final int lineChildCount = panelChild.getChildrenCount();
55
            for (int j = 0; j < lineChildCount; j++) {
56
                final LightUIElement element = panelChild.getChild(j);
94 ilm 57
                if (element.getType() == LightUIElement.TYPE_DATE) {
58
                    // Set date to current server date
132 ilm 59
                    element.setValue(JSONConverter.getJSON(new Date(System.currentTimeMillis())).toString());
94 ilm 60
                }
61
            }
62
        }
63
    }
64
 
174 ilm 65
    public void fillFromRow(final PropsConfiguration configuration, SQLElement element, final SQLRowAccessor row, String sessionSecurityToken) {
66
        this.fillFromRow(this.panel, configuration, element, row, sessionSecurityToken);
94 ilm 67
    }
132 ilm 68
 
174 ilm 69
    private void fillFromRow(final LightUIPanel panel, final PropsConfiguration configuration, SQLElement sqlElement, SQLRowAccessor sqlRow, String sessionSecurityToken) {
70
        List<CustomRowEditor> editors = sqlElement.getCustomRowEditors(configuration, sessionSecurityToken);
71
        Map<String, CustomRowEditor> mapEditor = new HashMap<>(editors.size());
72
        for (CustomRowEditor e : editors) {
73
            mapEditor.put(e.getItemId(), e);
74
        }
75
 
132 ilm 76
        final int panelChildCount = panel.getChildrenCount();
77
        // Convert as sqlrow if possible to get all values from db
142 ilm 78
        if (sqlRow.hasID()) {
79
            sqlRow = sqlRow.asRow();
132 ilm 80
        }
81
        for (int i = 0; i < panelChildCount; i++) {
82
            final LightUILine panelChild = panel.getChild(i, LightUILine.class);
83
            final int lineChildCount = panelChild.getChildrenCount();
84
            for (int j = 0; j < lineChildCount; j++) {
85
                final LightUIElement element = panelChild.getChild(j);
174 ilm 86
                if (mapEditor.containsKey(element.getId())) {
87
                    mapEditor.get(element.getId()).fillFrom(element, sqlRow);
88
                } else {
94 ilm 89
 
174 ilm 90
                    final SQLField sqlField = configuration.getFieldMapper().getSQLFieldForItem(element.getId());
142 ilm 91
 
174 ilm 92
                    SQLRowAccessor sqlRowTmp = this.getSQLRowForField(sqlRow, sqlField);
93
                    if (sqlRowTmp == null) {
94
                        throw new IllegalArgumentException("Impossible to reach the field: " + sqlField.getName() + " from table " + sqlRow.getTable().getName());
95
                    }
94 ilm 96
 
174 ilm 97
                    int type = element.getType();
98
                    if (type == LightUIElement.TYPE_TEXT_FIELD || type == LightUIElement.TYPE_TEXT_AREA) {
99
 
100
                        if (sqlField == null) {
101
                            Log.get().severe("No field found for text field : " + element.getId());
102
                            continue;
94 ilm 103
                        }
174 ilm 104
                        element.setValue(sqlRowTmp.getString(sqlField.getName()));
105
                    } else if (sqlField != null && sqlField.isKey() && (type == LightUIElement.TYPE_COMBOBOX || type == LightUIElement.TYPE_AUTOCOMPLETE_COMBOBOX)) {
106
                        // send: id,value
107
                        final LightUIComboBox combo = (LightUIComboBox) element;
108
                        LightUIComboBoxElement value = null;
109
                        final Number foreignID = sqlRowTmp.getForeignIDNumber(sqlField.getName());
110
                        if (foreignID != null) {
111
                            final SQLTable foreignTable = sqlField.getForeignTable();
112
                            final ComboSQLRequest req = configuration.getDirectory().getElement(foreignTable).getComboRequest();
113
                            final IComboSelectionItem comboItem = req.getComboItem(foreignID.intValue());
114
                            if (comboItem != null) {
115
                                value = new LightUIComboBoxElement(comboItem.getId());
116
                                value.setValue1(comboItem.getLabel());
117
                            }
118
                        }
119
                        combo.setSelectedValue(value);
120
                    } else if (type == LightUIElement.TYPE_CHECKBOX) {
121
                        if (sqlRowTmp.getObject(sqlField.getName()) != null && sqlRowTmp.getBoolean(sqlField.getName())) {
122
                            element.setValue("true");
123
                        } else {
124
                            element.setValue("false");
125
                        }
126
                    } else if (type == LightUIElement.TYPE_DATE) {
127
                        Calendar date = sqlRowTmp.getDate(sqlField.getName());
128
                        if (date != null) {
129
                            element.setValue(JSONConverter.getJSON(date).toString());
130
                        }
131
                    } else if (type == LightUIElement.TYPE_PANEL) {
132
                        this.fillFromRow((LightUIPanel) element, configuration, sqlElement, sqlRowTmp, sessionSecurityToken);
133
                    } else if (type == LightUIElement.TYPE_SLIDER) {
134
                        final Integer value = sqlRowTmp.getInt(sqlField.getName());
135
                        if (value != null) {
136
                            element.setValue(value.toString());
137
                        }
94 ilm 138
                    }
139
                }
140
            }
141
        }
142
    }
142 ilm 143
 
144
    public SQLRowAccessor getSQLRowForField(final SQLRowAccessor sqlRow, final SQLField sqlField) {
145
        SQLRowAccessor sqlRowResult = sqlRow;
146
        if (sqlField != null && !sqlField.getTable().getName().equals(sqlRow.getTable().getName())) {
147
            sqlRowResult = this.findSQLRow(sqlRow, sqlField);
148
        }
149
        return sqlRowResult;
150
    }
151
 
152
    public SQLRowAccessor findSQLRow(final SQLRowAccessor sqlRow, final SQLField sqlField) {
153
        final Set<Constraint> constraints = sqlRow.getTable().getAllConstraints();
154
        for (final Constraint constraint : constraints) {
155
            if (constraint.getType().equals(ConstraintType.FOREIGN_KEY)) {
156
                // FIXME: this doesn't work when foreign key is composed of more than one field
157
                final String firstFkCols = constraint.getCols().get(0);
158
                final SQLRowAccessor fkRow = sqlRow.getForeign(firstFkCols);
159
                if (fkRow != null) {
160
                    if (fkRow.getTable().getName().equals(sqlField.getTable().getName())) {
161
                        return fkRow;
162
                    } else {
163
                        final SQLRowAccessor sqlRowResult = this.findSQLRow(fkRow, sqlField);
164
                        if (sqlRowResult != null) {
165
                            return sqlRowResult;
166
                        }
167
                    }
168
                }
169
            }
170
        }
171
        return null;
172
    }
94 ilm 173
}