OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | 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.Component;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.ItemEvent;
10
import java.awt.event.ItemListener;
183 ilm 11
import java.math.BigDecimal;
102 ilm 12
import java.util.ArrayList;
13
import java.util.Collections;
14
import java.util.Comparator;
183 ilm 15
import java.util.HashSet;
102 ilm 16
import java.util.List;
17
import java.util.Set;
18
 
19
import javax.swing.DefaultListCellRenderer;
20
import javax.swing.JButton;
21
import javax.swing.JComboBox;
22
import javax.swing.JLabel;
23
import javax.swing.JList;
24
import javax.swing.JOptionPane;
25
import javax.swing.JPanel;
26
import javax.swing.SwingUtilities;
147 ilm 27
import javax.swing.SwingWorker;
102 ilm 28
 
181 ilm 29
import org.openconcerto.sql.element.SQLElementDirectory;
102 ilm 30
import org.openconcerto.sql.model.SQLField;
181 ilm 31
import org.openconcerto.sql.model.SQLRow;
183 ilm 32
import org.openconcerto.sql.model.SQLRowAccessor;
181 ilm 33
import org.openconcerto.sql.model.SQLRowListRSH;
34
import org.openconcerto.sql.model.SQLSelect;
35
import org.openconcerto.sql.model.SQLTable;
183 ilm 36
import org.openconcerto.sql.model.Where;
102 ilm 37
import org.openconcerto.sql.request.SQLFieldTranslator;
38
import org.openconcerto.ui.DefaultGridBagConstraints;
39
import org.openconcerto.ui.JLabelBold;
40
import org.openconcerto.ui.ReloadPanel;
41
import org.openconcerto.utils.ExceptionHandler;
42
 
43
public class BatchEditorPanel extends JPanel {
44
 
183 ilm 45
    public BatchEditorPanel(final SQLElementDirectory dir, final List<SQLRowAccessor> rows, FieldFilter filter) {
181 ilm 46
        SQLFieldTranslator translator = dir.getTranslator();
102 ilm 47
        Set<SQLField> fields = rows.get(0).getTable().getFields();
181 ilm 48
        List<BatchField> f = new ArrayList<BatchField>();
102 ilm 49
        for (SQLField sqlField : fields) {
50
            if (ForbiddenFieldName.isAllowed(sqlField.getName()) && translator.getLabelFor(sqlField) != null) {
147 ilm 51
                if (filter == null || !filter.isFiltered(sqlField)) {
181 ilm 52
                    f.add(new BatchField(dir, sqlField, null));
102 ilm 53
                }
54
            }
55
        }
183 ilm 56
 
57
        // Tarif
181 ilm 58
        SQLTable tableTarif = rows.get(0).getTable().getTable("TARIF");
59
        SQLTable tableArticleTarif = rows.get(0).getTable().getTable("ARTICLE_TARIF");
60
        SQLSelect sel = new SQLSelect();
61
        sel.addSelectStar(tableTarif);
62
        List<SQLRow> rowTarif = SQLRowListRSH.execute(sel);
63
        for (SQLRow sqlRow : rowTarif) {
64
            f.add(new BatchField(dir, tableArticleTarif.getField("PV_HT"), sqlRow));
65
            if (tableArticleTarif.contains("POURCENT_REMISE")) {
66
                f.add(new BatchField(dir, tableArticleTarif.getField("POURCENT_REMISE"), sqlRow));
67
            }
68
        }
102 ilm 69
 
183 ilm 70
        // Tarif Promo
71
        SQLTable tableTarifPromo = rows.get(0).getTable().getTable("TARIF_PROMOTION");
72
        SQLTable tableArticleTarifPromo = rows.get(0).getTable().getTable("ARTICLE_TARIF_PROMOTION");
73
        SQLSelect selT = new SQLSelect();
74
        selT.addSelectStar(tableTarifPromo);
75
        List<SQLRow> rowTarifP = SQLRowListRSH.execute(selT);
76
        for (SQLRow sqlRow : rowTarifP) {
77
            f.add(new BatchField(dir, tableArticleTarifPromo.getField("PV_HT"), sqlRow));
78
        }
79
 
80
        // Tarif quantite
81
        SQLTable tableTarifQte = rows.get(0).getTable().getTable("TARIF_QUANTITE");
82
        SQLSelect selQ = new SQLSelect();
83
        selQ.addSelectStar(tableTarifQte);
84
        Where w = Where.inValues(tableTarifQte.getField("ID_" + rows.get(0).getTable().getName()), SQLRow.getIDs(rows));
85
        selQ.setWhere(w);
86
        List<SQLRow> rowTarifQ = SQLRowListRSH.execute(selQ);
87
        Set<BigDecimal> qtes = new HashSet<>();
88
        for (SQLRow sqlRow : rowTarifQ) {
89
            final BigDecimal qte = sqlRow.getBigDecimal("QUANTITE");
90
            if (!qtes.contains(qte)) {
91
                qtes.add(qte);
92
                final BatchField batchFieldQte = new BatchField(dir, sqlRow.getTable().getField("PRIX_METRIQUE_VT_1"), null);
93
                batchFieldQte.setDefaultMatchingValue(sqlRow.getTable().getField("QUANTITE"), qte);
94
                f.add(batchFieldQte);
95
                final BatchField batchFieldRemise = new BatchField(dir, sqlRow.getTable().getField("POURCENT_REMISE"), null);
96
                batchFieldRemise.setDefaultMatchingValue(sqlRow.getTable().getField("QUANTITE"), qte);
97
                f.add(batchFieldRemise);
98
            }
99
        }
100
 
101
        // Stock
102
        SQLTable tableDepotStock = rows.get(0).getTable().getTable("DEPOT_STOCK");
103
        SQLTable tableStock = tableDepotStock.getTable("STOCK");
104
        SQLSelect selDepot = new SQLSelect();
105
        selDepot.addSelectStar(tableDepotStock);
106
        List<SQLRow> rowDepot = SQLRowListRSH.execute(selDepot);
107
        for (SQLRow sqlRow : rowDepot) {
108
            f.add(new BatchField(dir, tableStock.getField("QTE_MIN"), sqlRow));
109
        }
110
 
181 ilm 111
        Collections.sort(f, new Comparator<BatchField>() {
102 ilm 112
 
113
            @Override
181 ilm 114
            public int compare(BatchField o1, BatchField o2) {
115
                return o1.getComboName().compareToIgnoreCase(o2.getComboName());
102 ilm 116
            }
117
        });
118
        this.setLayout(new GridBagLayout());
119
        GridBagConstraints c = new DefaultGridBagConstraints();
120
        this.add(new JLabel("Champ"), c);
121
 
181 ilm 122
        final JComboBox<BatchField> combo = new JComboBox<BatchField>(f.toArray(new BatchField[1]));
102 ilm 123
        combo.setRenderer(new DefaultListCellRenderer() {
124
            @Override
125
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
181 ilm 126
                String label = ((BatchField) value).getComboName();
147 ilm 127
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
102 ilm 128
            }
129
        });
130
 
131
        combo.setSelectedIndex(0);
132
 
133
        c.gridx++;
134
        c.weightx = 1;
135
        c.fill = GridBagConstraints.NONE;
136
        this.add(combo, c);
137
        c.gridx = 0;
138
        c.gridy++;
139
        c.gridwidth = 2;
140
        c.fill = GridBagConstraints.HORIZONTAL;
141
        this.add(new JLabelBold("Action à appliquer"), c);
142
        c.weightx = 1;
143
        c.weighty = 1;
144
        c.fill = GridBagConstraints.HORIZONTAL;
145
        c.gridy++;
146
        c.anchor = GridBagConstraints.NORTHWEST;
147
        final BatchDetailPanel comp = new BatchDetailPanel();
181 ilm 148
        comp.setField((BatchField) combo.getSelectedItem());
102 ilm 149
        this.add(comp, c);
150
 
151
        JPanel actions = new JPanel();
152
        actions.setLayout(new FlowLayout(FlowLayout.RIGHT));
153
        final JButton buttonProcess = new JButton("Lancer le traitement");
154
 
155
        final JButton buttonCancel = new JButton("Annuler");
156
        final ReloadPanel reload = new ReloadPanel();
157
        actions.add(reload);
158
        actions.add(buttonProcess);
159
 
160
        actions.add(buttonCancel);
161
 
162
        c.gridy++;
163
        c.weighty = 0;
164
        this.add(actions, c);
165
 
166
        combo.addItemListener(new ItemListener() {
167
 
168
            @Override
169
            public void itemStateChanged(ItemEvent e) {
181 ilm 170
                comp.setField((BatchField) combo.getSelectedItem());
102 ilm 171
 
172
            }
173
        });
174
        buttonProcess.addActionListener(new ActionListener() {
175
 
176
            @Override
177
            public void actionPerformed(ActionEvent e) {
178
                if (!comp.getProcessor().checkParameters()) {
179
                    JOptionPane.showMessageDialog(BatchEditorPanel.this, "Paramètres non valides");
180
                    return;
181
                }
182
 
183
                buttonProcess.setEnabled(false);
184
                buttonCancel.setEnabled(false);
185
                comp.setEnabled(false);
186
 
187
                combo.setEnabled(false);
188
                reload.setMode(ReloadPanel.MODE_ROTATE);
147 ilm 189
                SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {
102 ilm 190
 
191
                    @Override
192
                    protected Object doInBackground() throws Exception {
193
                        try {
194
                            final BatchProcessor processor = comp.getProcessor();
195
                            processor.process(rows);
196
                        } catch (Exception e) {
197
                            ExceptionHandler.handle("Echec du traitement", e);
198
                        }
199
                        return null;
200
                    }
201
 
202
                    @Override
203
                    protected void done() {
204
                        reload.setMode(ReloadPanel.MODE_EMPTY);
205
                        JOptionPane.showMessageDialog(BatchEditorPanel.this, "Traitement terminé");
206
                        SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
207
                    }
208
 
209
                };
210
                w.execute();
211
            }
212
        });
213
        buttonCancel.addActionListener(new ActionListener() {
214
 
215
            @Override
216
            public void actionPerformed(ActionEvent e) {
217
                SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
218
            }
219
        });
220
    }
221
}