OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
102 ilm 1
package org.openconcerto.modules.common.batchprocessing;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.math.BigDecimal;
8
import java.sql.SQLException;
9
import java.util.List;
10
 
11
import javax.swing.ButtonGroup;
12
import javax.swing.JPanel;
13
import javax.swing.JRadioButton;
14
import javax.swing.JTextField;
15
 
181 ilm 16
import org.openconcerto.sql.model.SQLRow;
102 ilm 17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
20
 
21
public class NumberProcessor extends JPanel implements BatchProcessor {
22
 
181 ilm 23
    private final BatchField batchfield;
102 ilm 24
    // Editors
25
    final JTextField tReplace = new JTextField();
26
    private JRadioButton bReplace;
27
    final JTextField tAdd = new JTextField();
28
    private JRadioButton bAdd;
29
 
30
    final JTextField tRemove = new JTextField();
31
    private JRadioButton bRemove;
32
 
181 ilm 33
    public NumberProcessor(BatchField field) {
34
        this.batchfield = field;
102 ilm 35
 
36
        this.setLayout(new GridBagLayout());
37
        bReplace = new JRadioButton("remplacer par");
38
        bAdd = new JRadioButton("augmenter de");
39
        bRemove = new JRadioButton("diminuer de");
40
 
41
        final ButtonGroup group = new ButtonGroup();
42
        group.add(bReplace);
43
        group.add(bAdd);
44
        group.add(bRemove);
45
 
46
        GridBagConstraints c = new DefaultGridBagConstraints();
47
        // replace
48
        this.add(bReplace, c);
49
        c.gridx++;
50
        c.weightx = 1;
51
        this.add(tReplace, c);
52
        // add
53
        c.gridx = 0;
54
        c.gridy++;
55
        c.weightx = 0;
56
        this.add(bAdd, c);
57
        c.gridx++;
58
        c.weightx = 1;
59
        this.add(tAdd, c);
60
        // remove
61
        c.gridx = 0;
62
        c.gridy++;
63
        c.weightx = 0;
64
        this.add(bRemove, c);
65
        c.gridx++;
66
        c.weightx = 1;
67
        this.add(tRemove, c);
68
        //
69
        tAdd.setEnabled(false);
70
        tRemove.setEnabled(false);
71
 
72
        tAdd.setInputVerifier(new DecimalOrPercentVerifier());
73
        tRemove.setInputVerifier(new DecimalOrPercentVerifier());
74
 
75
        group.setSelected(bReplace.getModel(), true);
76
 
77
        bReplace.addActionListener(new ActionListener() {
78
 
79
            @Override
80
            public void actionPerformed(ActionEvent e) {
81
                tReplace.setEnabled(true);
82
                tAdd.setEnabled(false);
83
                tRemove.setEnabled(false);
84
            }
85
        });
86
        bAdd.addActionListener(new ActionListener() {
87
 
88
            @Override
89
            public void actionPerformed(ActionEvent e) {
90
                tReplace.setEnabled(false);
91
                tAdd.setEnabled(true);
92
                tRemove.setEnabled(false);
93
 
94
            }
95
        });
96
        bRemove.addActionListener(new ActionListener() {
97
 
98
            @Override
99
            public void actionPerformed(ActionEvent e) {
100
                tReplace.setEnabled(false);
101
                tAdd.setEnabled(false);
102
                tRemove.setEnabled(true);
103
 
104
            }
105
        });
106
    }
107
 
108
    @Override
109
    public void process(List<SQLRowValues> r) throws SQLException {
110
        if (bReplace.isSelected()) {
111
            BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
112
            for (SQLRowAccessor sqlRowAccessor : r) {
181 ilm 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 {
124
                    final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
125
                    rowValues.put(batchfield.getField().getName(), decimalToFieldType(v));
126
                    processBeforeUpdate(sqlRowAccessor, rowValues);
127
                    rowValues.update();
128
                }
102 ilm 129
            }
130
        } else if (bAdd.isSelected()) {
131
 
132
            String t = this.tAdd.getText().trim();
133
            boolean isPercent = false;
134
            if (t.endsWith("%")) {
135
                t = t.substring(0, t.length() - 1);
136
                isPercent = true;
137
            }
138
 
139
            BigDecimal v = new BigDecimal(t);
140
 
141
            for (SQLRowAccessor sqlRowAccessor : r) {
181 ilm 142
 
143
                if (batchfield.getForeignLinkRow() != null) {
144
 
145
                    final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
146
                    for (SQLRow sqlRowT : referentRow) {
147
 
148
                        SQLRowValues rowValues = sqlRowT.createEmptyUpdateRow();
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
                        }
102 ilm 160
                    }
181 ilm 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
 
169
                    if (value != null) {
170
                        if (isPercent) {
171
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(100)).add(BigDecimal.ONE))));
172
                        } else {
173
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
174
                        }
175
                        processBeforeUpdate(sqlRowAccessor, rowValues);
176
                        rowValues.update();
177
                    }
102 ilm 178
                }
179
            }
180
        } else if (bRemove.isSelected()) {
181
            String t = this.tRemove.getText().trim();
182
            boolean isPercent = false;
183
            if (t.endsWith("%")) {
184
                t = t.substring(0, t.length() - 1);
185
                isPercent = true;
186
            }
187
 
188
            BigDecimal v = new BigDecimal(t);
189
            for (SQLRowAccessor sqlRowAccessor : r) {
181 ilm 190
                if (batchfield.getForeignLinkRow() != null) {
191
                    final List<SQLRow> referentRow = batchfield.getReferentRows(sqlRowAccessor);
192
                    if (referentRow != null && !referentRow.isEmpty()) {
193
                        for (SQLRow sqlRowT : referentRow) {
102 ilm 194
 
181 ilm 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
                        }
102 ilm 207
                    }
181 ilm 208
                } else {
209
 
210
                    SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
211
                    final BigDecimal value = sqlRowAccessor.asRow().getBigDecimal(batchfield.getField().getName());
212
 
213
                    if (value != null) {
214
                        if (isPercent) {
215
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.multiply(v.divide(new BigDecimal(-100)).add(BigDecimal.ONE))));
216
                        } else {
217
                            rowValues.put(batchfield.getField().getName(), decimalToFieldType(value.add(v)));
218
                        }
219
                        processBeforeUpdate(sqlRowAccessor, rowValues);
220
                        rowValues.update();
221
                    }
102 ilm 222
                }
223
            }
224
        }
225
    }
226
 
227
    private Object decimalToFieldType(BigDecimal v) {
181 ilm 228
        final Class<?> javaType = batchfield.getField().getType().getJavaType();
102 ilm 229
        if (javaType.equals(BigDecimal.class)) {
230
            return v;
231
        } else if (javaType.equals(Float.class)) {
232
            return v.floatValue();
233
        } else if (javaType.equals(Double.class)) {
234
            return v.doubleValue();
235
        } else if (javaType.equals(Integer.class)) {
236
            return v.intValue();
237
        } else if (javaType.equals(Long.class)) {
238
            return v.longValue();
239
        }
240
        return v;
241
    }
242
 
243
    @Override
244
    public boolean checkParameters() {
245
        if (bReplace.isSelected()) {
246
            try {
247
                BigDecimal v = new BigDecimal(this.tReplace.getText().trim());
248
                return v != null;
249
            } catch (Exception e) {
250
                return false;
251
            }
252
        } else if (bAdd.isSelected()) {
253
            return tAdd.getInputVerifier().verify(tAdd);
254
 
255
        } else if (bRemove.isSelected()) {
256
            return tRemove.getInputVerifier().verify(tRemove);
257
        }
258
        return false;
259
    }
260
 
261
    @Override
262
    public void processBeforeUpdate(SQLRowAccessor from, SQLRowValues to) {
263
        // TODO Auto-generated method stub
264
 
265
    }
266
}