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.sql.SQLException;
8
import java.util.List;
9
 
10
import javax.swing.ButtonGroup;
11
import javax.swing.JPanel;
12
import javax.swing.JRadioButton;
13
import javax.swing.JTextField;
14
 
15
import org.openconcerto.sql.model.SQLField;
16
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.ui.DefaultGridBagConstraints;
19
 
20
public class StringProcessor extends JPanel implements BatchProcessor {
21
    private final SQLField field;
22
    // Editors
23
    final JTextField tReplace = new JTextField();
24
    private JRadioButton bReplace;
25
    final JTextField tPrefix = new JTextField();
26
    private JRadioButton bPrefix;
27
    final JTextField tSuffix = new JTextField();
28
    private JRadioButton bSuffix;
29
    private JRadioButton bLower;
30
    private JRadioButton bUpper;
31
 
181 ilm 32
    public StringProcessor(BatchField field) {
33
        this.field = field.getField();
102 ilm 34
 
35
        this.setLayout(new GridBagLayout());
36
        bReplace = new JRadioButton("remplacer par");
37
        bPrefix = new JRadioButton("préfixer par");
38
        bSuffix = new JRadioButton("suffixer par");
39
        bLower = new JRadioButton("mettre en minuscule");
40
        bUpper = new JRadioButton("mettre en majuscule");
41
 
42
        final ButtonGroup group = new ButtonGroup();
43
        group.add(bReplace);
44
        group.add(bPrefix);
45
        group.add(bSuffix);
46
        group.add(bLower);
47
        group.add(bUpper);
48
 
49
        GridBagConstraints c = new DefaultGridBagConstraints();
50
        // replace
51
        this.add(bReplace, c);
52
        c.gridx++;
53
        c.weightx = 1;
54
        this.add(tReplace, c);
55
        // prefix
56
        c.gridx = 0;
57
        c.gridy++;
58
        c.weightx = 0;
59
        this.add(bPrefix, c);
60
        c.gridx++;
61
        c.weightx = 1;
62
        this.add(tPrefix, c);
63
        // suffix
64
        c.gridx = 0;
65
        c.gridy++;
66
        c.weightx = 0;
67
        this.add(bSuffix, c);
68
        c.gridx++;
69
        c.weightx = 1;
70
        this.add(tSuffix, c);
71
        //
72
        c.gridx = 0;
73
        c.gridy++;
74
        c.weightx = 0;
75
        c.gridwidth = 2;
76
        this.add(bLower, c);
77
        c.gridy++;
78
        this.add(bUpper, c);
79
 
80
        tPrefix.setEnabled(false);
81
        tSuffix.setEnabled(false);
82
        group.setSelected(bReplace.getModel(), true);
83
 
84
        bLower.addActionListener(new ActionListener() {
85
 
86
            @Override
87
            public void actionPerformed(ActionEvent e) {
88
                tPrefix.setEnabled(false);
89
                tReplace.setEnabled(false);
90
                tSuffix.setEnabled(false);
91
 
92
            }
93
        });
94
        bUpper.addActionListener(new ActionListener() {
95
 
96
            @Override
97
            public void actionPerformed(ActionEvent e) {
98
                tPrefix.setEnabled(false);
99
                tReplace.setEnabled(false);
100
                tSuffix.setEnabled(false);
101
            }
102
        });
103
        bPrefix.addActionListener(new ActionListener() {
104
 
105
            @Override
106
            public void actionPerformed(ActionEvent e) {
107
                tPrefix.setEnabled(true);
108
                tReplace.setEnabled(false);
109
                tSuffix.setEnabled(false);
110
 
111
            }
112
        });
113
        bSuffix.addActionListener(new ActionListener() {
114
 
115
            @Override
116
            public void actionPerformed(ActionEvent e) {
117
                tPrefix.setEnabled(false);
118
                tReplace.setEnabled(false);
119
                tSuffix.setEnabled(true);
120
 
121
            }
122
        });
123
        bReplace.addActionListener(new ActionListener() {
124
 
125
            @Override
126
            public void actionPerformed(ActionEvent e) {
127
                tPrefix.setEnabled(false);
128
                tReplace.setEnabled(true);
129
                tSuffix.setEnabled(false);
130
 
131
            }
132
        });
133
 
134
    }
135
 
136
    @Override
137
    public void process(List<SQLRowValues> r) throws SQLException {
138
        if (bReplace.isSelected()) {
139
            final String t = ensureSize(tReplace.getText());
140
            for (SQLRowAccessor sqlRowAccessor : r) {
141
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
142
                rowValues.put(field.getName(), t);
143
                processBeforeUpdate(sqlRowAccessor, rowValues);
144
                rowValues.update();
145
            }
146
        } else if (bPrefix.isSelected()) {
147
            final String t = tPrefix.getText();
148
            for (SQLRowAccessor sqlRowAccessor : r) {
149
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
150
                String str = sqlRowAccessor.asRow().getString(field.getName());
151
                if (str == null)
152
                    str = "";
153
                rowValues.put(field.getName(), ensureSize(t + str));
154
                processBeforeUpdate(sqlRowAccessor, rowValues);
155
                rowValues.update();
156
            }
157
        } else if (bSuffix.isSelected()) {
158
            final String t = tSuffix.getText();
159
            for (SQLRowAccessor sqlRowAccessor : r) {
160
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
161
                String str = sqlRowAccessor.asRow().getString(field.getName());
162
                if (str == null)
163
                    str = "";
164
                rowValues.put(field.getName(), ensureSize(str + t));
165
                processBeforeUpdate(sqlRowAccessor, rowValues);
166
                rowValues.update();
167
            }
168
        } else if (bLower.isSelected()) {
169
            for (SQLRowAccessor sqlRowAccessor : r) {
170
                String str = sqlRowAccessor.asRow().getString(field.getName());
171
                if (str == null)
172
                    str = "";
173
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
174
                rowValues.put(field.getName(), str.toLowerCase());
175
                processBeforeUpdate(sqlRowAccessor, rowValues);
176
                rowValues.update();
177
            }
178
        } else if (bUpper.isSelected()) {
179
            for (SQLRowAccessor sqlRowAccessor : r) {
180
                String str = sqlRowAccessor.asRow().getString(field.getName());
181
                if (str == null)
182
                    str = "";
183
                final SQLRowValues rowValues = sqlRowAccessor.createEmptyUpdateRow();
184
                rowValues.put(field.getName(), str.toUpperCase());
185
                processBeforeUpdate(sqlRowAccessor, rowValues);
186
                rowValues.update();
187
            }
188
        }
189
    }
190
 
191
    private String ensureSize(String text) {
192
        if (text.length() < field.getType().getSize()) {
193
            return text;
194
        }
195
        return text.substring(0, field.getType().getSize());
196
    }
197
 
198
    @Override
199
    public boolean checkParameters() {
200
        if (bReplace.isSelected()) {
201
            return true;
202
        } else if (bPrefix.isSelected()) {
203
            return !tPrefix.getText().isEmpty();
204
        } else if (bSuffix.isSelected()) {
205
            return !bPrefix.getText().isEmpty();
206
        }
207
        return true;
208
    }
209
 
210
    @Override
211
    public void processBeforeUpdate(SQLRowAccessor from, SQLRowValues to) {
212
    }
213
}