OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 102 Rev 181
Line 11... Line 11...
11
import javax.swing.ButtonGroup;
11
import javax.swing.ButtonGroup;
12
import javax.swing.JPanel;
12
import javax.swing.JPanel;
13
import javax.swing.JRadioButton;
13
import javax.swing.JRadioButton;
14
import javax.swing.JTextField;
14
import javax.swing.JTextField;
15
 
15
 
16
import org.openconcerto.sql.model.SQLField;
16
import org.openconcerto.sql.model.SQLRow;
17
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
20
 
20
 
21
public class NumberProcessor extends JPanel implements BatchProcessor {
21
public class NumberProcessor extends JPanel implements BatchProcessor {
22
 
22
 
23
    private final SQLField field;
23
    private final BatchField batchfield;
24
    // Editors
24
    // Editors
25
    final JTextField tReplace = new JTextField();
25
    final JTextField tReplace = new JTextField();
26
    private JRadioButton bReplace;
26
    private JRadioButton bReplace;
27
    final JTextField tAdd = new JTextField();
27
    final JTextField tAdd = new JTextField();
28
    private JRadioButton bAdd;
28
    private JRadioButton bAdd;
29
 
29
 
30
    final JTextField tRemove = new JTextField();
30
    final JTextField tRemove = new JTextField();
31
    private JRadioButton bRemove;
31
    private JRadioButton bRemove;
32
 
32
 
33
    public NumberProcessor(SQLField field) {
33
    public NumberProcessor(BatchField field) {
34
        this.field = field;
34
        this.batchfield = field;
35
 
35
 
36
        this.setLayout(new GridBagLayout());
36
        this.setLayout(new GridBagLayout());
37
        bReplace = new JRadioButton("remplacer par");
37
        bReplace = new JRadioButton("remplacer par");
38
        bAdd = new JRadioButton("augmenter de");
38
        bAdd = new JRadioButton("augmenter de");
39
        bRemove = new JRadioButton("diminuer de");
39
        bRemove = new JRadioButton("diminuer de");
Line 108... Line 108...
108
    @Override
108
    @Override
109
    public void process(List<SQLRowValues> r) throws SQLException {
109
    public void process(List<SQLRowValues> r) throws SQLException {
110
        if (bReplace.isSelected()) {
110
        if (bReplace.isSelected()) {
111
            BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
111
            BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
112
            for (SQLRowAccessor sqlRowAccessor : r) {
112
            for (SQLRowAccessor sqlRowAccessor : r) {
-
 
113
 
-
 
114
                if (batchfield.getForeignLinkRow() != null) {
-
 
115
 
-
 
116
                    final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
-
 
117
                    for (SQLRow sqlRowT : referentRow) {
-
 
118
                        SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
-
 
119
                        rowValues.put(batchfield.getField().getName(), decimalToFieldType(v));
-
 
120
                        processBeforeUpdate(sqlRowT, rowValues);
-
 
121
                        rowValues.update();
-
 
122
                    }
-
 
123
                } else {
113
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
124
                    final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
114
                rowValues.put(field.getName(), decimalToFieldType(v));
125
                    rowValues.put(batchfield.getField().getName(), decimalToFieldType(v));
115
                processBeforeUpdate(sqlRowAccessor, rowValues);
126
                    processBeforeUpdate(sqlRowAccessor, rowValues);
116
                rowValues.update();
127
                    rowValues.update();
117
            }
128
                }
-
 
129
            }
118
        } else if (bAdd.isSelected()) {
130
        } else if (bAdd.isSelected()) {
119
 
131
 
120
            String t = this.tAdd.getText().trim();
132
            String t = this.tAdd.getText().trim();
121
            boolean isPercent = false;
133
            boolean isPercent = false;
122
            if (t.endsWith("%")) {
134
            if (t.endsWith("%")) {
Line 125... Line 137...
125
            }
137
            }
126
 
138
 
127
            BigDecimal v = new BigDecimal(t);
139
            BigDecimal v = new BigDecimal(t);
128
 
140
 
129
            for (SQLRowAccessor sqlRowAccessor : r) {
141
            for (SQLRowAccessor sqlRowAccessor : r) {
-
 
142
 
-
 
143
                if (batchfield.getForeignLinkRow() != null) {
-
 
144
 
-
 
145
                    final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
-
 
146
                    for (SQLRow sqlRowT : referentRow) {
-
 
147
 
130
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
148
                        SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
131
                final BigDecimal value = sqlRowAccessor.asRow().getBigDecimal(field.getName());
149
                        BigDecimal value = sqlRowT.asRow().getBigDecimal(batchfield.getField().getName());
-
 
150
 
-
 
151
                        if (value != null) {
-
 
152
                            if (isPercent) {
-
 
153
                                rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
-
 
154
                            } else {
-
 
155
                                rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
-
 
156
                            }
-
 
157
                            processBeforeUpdate(sqlRowT, rowValues);
-
 
158
                            rowValues.update();
-
 
159
                        }
-
 
160
                    }
-
 
161
                } else {
-
 
162
 
-
 
163
                    final SQLRowValues rowValues;
-
 
164
                    final BigDecimal value;
-
 
165
 
-
 
166
                    rowValues = sqlRowAccessor.createEmptyUpdateRow();
-
 
167
                    value = sqlRowAccessor.asRow().getBigDecimal(batchfield.getField().getName());
-
 
168
 
132
                if (value != null) {
169
                    if (value != null) {
133
                    if (isPercent) {
170
                        if (isPercent) {
134
                        rowValues.put(field.getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
171
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
135
                    } else {
172
                        } else {
136
                        rowValues.put(field.getName(), decimalToFieldType(value.add(v)));
173
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
137
                    }
174
                        }
138
                    processBeforeUpdate(sqlRowAccessor, rowValues);
175
                        processBeforeUpdate(sqlRowAccessor, rowValues);
139
                    rowValues.update();
176
                        rowValues.update();
140
                }
177
                    }
141
            }
178
                }
-
 
179
            }
142
        } else if (bRemove.isSelected()) {
180
        } else if (bRemove.isSelected()) {
143
            String t = this.tRemove.getText().trim();
181
            String t = this.tRemove.getText().trim();
144
            boolean isPercent = false;
182
            boolean isPercent = false;
145
            if (t.endsWith("%")) {
183
            if (t.endsWith("%")) {
146
                t = t.substring(0, t.length() - 1);
184
                t = t.substring(0, t.length() - 1);
147
                isPercent = true;
185
                isPercent = true;
148
            }
186
            }
149
 
187
 
150
            BigDecimal v = new BigDecimal(t);
188
            BigDecimal v = new BigDecimal(t);
151
            for (SQLRowAccessor sqlRowAccessor : r) {
189
            for (SQLRowAccessor sqlRowAccessor : r) {
-
 
190
                if (batchfield.getForeignLinkRow() != null) {
-
 
191
                    final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
-
 
192
                    if (referentRow != null && !referentRow.isEmpty()) {
-
 
193
                        for (SQLRow sqlRowT : referentRow) {
-
 
194
 
-
 
195
                            SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
-
 
196
                            final BigDecimal value = sqlRowT.getBigDecimal(batchfield.getField().getName());
-
 
197
                            if (value != null) {
-
 
198
                                if (isPercent) {
-
 
199
                                    rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
-
 
200
                                } else {
-
 
201
                                    rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
-
 
202
                                }
-
 
203
                                processBeforeUpdate(sqlRowT, rowValues);
-
 
204
                                rowValues.update();
-
 
205
                            }
-
 
206
                        }
-
 
207
                    }
-
 
208
                } else {
-
 
209
 
152
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
210
                    SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
-
 
211
                    final BigDecimal value = sqlRowAccessor.asRow().getBigDecimal(batchfield.getField().getName());
153
 
212
 
154
                final BigDecimal value = sqlRowAccessor.asRow().getBigDecimal(field.getName());
-
 
155
                if (value != null) {
213
                    if (value != null) {
156
                    if (isPercent) {
214
                        if (isPercent) {
157
                        rowValues.put(field.getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
215
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
158
                    } else {
216
                        } else {
159
                        rowValues.put(field.getName(), decimalToFieldType(value.add(v)));
217
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
160
                    }
218
                        }
161
                    processBeforeUpdate(sqlRowAccessor, rowValues);
219
                        processBeforeUpdate(sqlRowAccessor, rowValues);
162
                    rowValues.update();
220
                        rowValues.update();
163
                }
221
                    }
164
            }
222
                }
165
        }
223
            }
166
    }
224
        }
-
 
225
    }
167
 
226
 
168
    private Object decimalToFieldType(BigDecimal v) {
227
    private Object decimalToFieldType(BigDecimal v) {
169
        final Class<?> javaType = field.getType().getJavaType();
228
        final Class<?> javaType = batchfield.getField().getType().getJavaType();
170
        if (javaType.equals(BigDecimal.class)) {
229
        if (javaType.equals(BigDecimal.class)) {
171
            return v;
230
            return v;
172
        } else if (javaType.equals(Float.class)) {
231
        } else if (javaType.equals(Float.class)) {
173
            return v.floatValue();
232
            return v.floatValue();
174
        } else if (javaType.equals(Double.class)) {
233
        } else if (javaType.equals(Double.class)) {