Dépôt officiel du code source de l'ERP OpenConcerto
Rev 181 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.openconcerto.modules.common.batchprocessing;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.SQLFieldTranslator;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.ui.ReloadPanel;
import org.openconcerto.utils.ExceptionHandler;
public class BatchEditorPanel extends JPanel {
public BatchEditorPanel(final SQLElementDirectory dir, final List<SQLRowAccessor> rows, FieldFilter filter) {
SQLFieldTranslator translator = dir.getTranslator();
Set<SQLField> fields = rows.get(0).getTable().getFields();
List<BatchField> f = new ArrayList<BatchField>();
for (SQLField sqlField : fields) {
if (ForbiddenFieldName.isAllowed(sqlField.getName()) && translator.getLabelFor(sqlField) != null) {
if (filter == null || !filter.isFiltered(sqlField)) {
f.add(new BatchField(dir, sqlField, null));
}
}
}
// Tarif
SQLTable tableTarif = rows.get(0).getTable().getTable("TARIF");
SQLTable tableArticleTarif = rows.get(0).getTable().getTable("ARTICLE_TARIF");
SQLSelect sel = new SQLSelect();
sel.addSelectStar(tableTarif);
List<SQLRow> rowTarif = SQLRowListRSH.execute(sel);
for (SQLRow sqlRow : rowTarif) {
f.add(new BatchField(dir, tableArticleTarif.getField("PV_HT"), sqlRow));
if (tableArticleTarif.contains("POURCENT_REMISE")) {
f.add(new BatchField(dir, tableArticleTarif.getField("POURCENT_REMISE"), sqlRow));
}
}
// Tarif Promo
SQLTable tableTarifPromo = rows.get(0).getTable().getTable("TARIF_PROMOTION");
SQLTable tableArticleTarifPromo = rows.get(0).getTable().getTable("ARTICLE_TARIF_PROMOTION");
SQLSelect selT = new SQLSelect();
selT.addSelectStar(tableTarifPromo);
List<SQLRow> rowTarifP = SQLRowListRSH.execute(selT);
for (SQLRow sqlRow : rowTarifP) {
f.add(new BatchField(dir, tableArticleTarifPromo.getField("PV_HT"), sqlRow));
}
// Tarif quantite
SQLTable tableTarifQte = rows.get(0).getTable().getTable("TARIF_QUANTITE");
SQLSelect selQ = new SQLSelect();
selQ.addSelectStar(tableTarifQte);
Where w = Where.inValues(tableTarifQte.getField("ID_" + rows.get(0).getTable().getName()), SQLRow.getIDs(rows));
selQ.setWhere(w);
List<SQLRow> rowTarifQ = SQLRowListRSH.execute(selQ);
Set<BigDecimal> qtes = new HashSet<>();
for (SQLRow sqlRow : rowTarifQ) {
final BigDecimal qte = sqlRow.getBigDecimal("QUANTITE");
if (!qtes.contains(qte)) {
qtes.add(qte);
final BatchField batchFieldQte = new BatchField(dir, sqlRow.getTable().getField("PRIX_METRIQUE_VT_1"), null);
batchFieldQte.setDefaultMatchingValue(sqlRow.getTable().getField("QUANTITE"), qte);
f.add(batchFieldQte);
final BatchField batchFieldRemise = new BatchField(dir, sqlRow.getTable().getField("POURCENT_REMISE"), null);
batchFieldRemise.setDefaultMatchingValue(sqlRow.getTable().getField("QUANTITE"), qte);
f.add(batchFieldRemise);
}
}
// Stock
SQLTable tableDepotStock = rows.get(0).getTable().getTable("DEPOT_STOCK");
SQLTable tableStock = tableDepotStock.getTable("STOCK");
SQLSelect selDepot = new SQLSelect();
selDepot.addSelectStar(tableDepotStock);
List<SQLRow> rowDepot = SQLRowListRSH.execute(selDepot);
for (SQLRow sqlRow : rowDepot) {
f.add(new BatchField(dir, tableStock.getField("QTE_MIN"), sqlRow));
}
Collections.sort(f, new Comparator<BatchField>() {
@Override
public int compare(BatchField o1, BatchField o2) {
return o1.getComboName().compareToIgnoreCase(o2.getComboName());
}
});
this.setLayout(new GridBagLayout());
GridBagConstraints c = new DefaultGridBagConstraints();
this.add(new JLabel("Champ"), c);
final JComboBox<BatchField> combo = new JComboBox<BatchField>(f.toArray(new BatchField[1]));
combo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String label = ((BatchField) value).getComboName();
return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
}
});
combo.setSelectedIndex(0);
c.gridx++;
c.weightx = 1;
c.fill = GridBagConstraints.NONE;
this.add(combo, c);
c.gridx = 0;
c.gridy++;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
this.add(new JLabelBold("Action à appliquer"), c);
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy++;
c.anchor = GridBagConstraints.NORTHWEST;
final BatchDetailPanel comp = new BatchDetailPanel();
comp.setField((BatchField) combo.getSelectedItem());
this.add(comp, c);
JPanel actions = new JPanel();
actions.setLayout(new FlowLayout(FlowLayout.RIGHT));
final JButton buttonProcess = new JButton("Lancer le traitement");
final JButton buttonCancel = new JButton("Annuler");
final ReloadPanel reload = new ReloadPanel();
actions.add(reload);
actions.add(buttonProcess);
actions.add(buttonCancel);
c.gridy++;
c.weighty = 0;
this.add(actions, c);
combo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
comp.setField((BatchField) combo.getSelectedItem());
}
});
buttonProcess.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!comp.getProcessor().checkParameters()) {
JOptionPane.showMessageDialog(BatchEditorPanel.this, "Paramètres non valides");
return;
}
buttonProcess.setEnabled(false);
buttonCancel.setEnabled(false);
comp.setEnabled(false);
combo.setEnabled(false);
reload.setMode(ReloadPanel.MODE_ROTATE);
SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {
@Override
protected Object doInBackground() throws Exception {
try {
final BatchProcessor processor = comp.getProcessor();
processor.process(rows);
} catch (Exception e) {
ExceptionHandler.handle("Echec du traitement", e);
}
return null;
}
@Override
protected void done() {
reload.setMode(ReloadPanel.MODE_EMPTY);
JOptionPane.showMessageDialog(BatchEditorPanel.this, "Traitement terminé");
SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
}
};
w.execute();
}
});
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
}
});
}
}