OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 /*
 * Créé le 23 avr. 2012
 */
package org.openconcerto.erp.core.supplychain.stock.element;

import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
import org.openconcerto.sql.model.SQLDataSource;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.utils.SQLUtils;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.SwingThreadUtils;
import org.openconcerto.utils.ExceptionHandler;

import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImportInventairePanel extends JPanel {

    public ImportInventairePanel(final SQLElement depotElt) {
        super(new GridBagLayout());

        final SQLRequestComboBox boxDepot = new SQLRequestComboBox(false);
        boxDepot.uiInit(depotElt.createComboRequest());

        GridBagConstraints c = new DefaultGridBagConstraints();
        JLabel labelCom = new JLabel("Dépôt ");
        this.add(labelCom, c);
        c.gridx++;
        this.add(boxDepot, c);

        // final JDate dateDeb = new JDate();
        // this.add(dateDeb, c);
        // c.gridx++;
        // JLabel labelD = new JLabel("Début au");
        // final JDate dateDebut = new JDate();

        final JButton buttonValid = new JButton(new AbstractAction("Valider") {

            @Override
            public void actionPerformed(ActionEvent e) {
                SQLRowAccessor row = boxDepot.getSelectedRow();
                final SQLRowAccessor rowDepot;
                if (row != null && !row.isUndefined()) {
                    rowDepot = boxDepot.getSelectedRow();
                } else {
                    rowDepot = depotElt.getTable().getRow(DepotStockSQLElement.DEFAULT_ID);

                }

                ((Window) SwingUtilities.getRoot(ImportInventairePanel.this)).dispose();
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
                final FileDialog fd = new FileDialog(frame, "Import Inventaire", FileDialog.LOAD);
                fd.setVisible(true);
                if (fd.getFile() != null) {
                    final File f = new File(fd.getDirectory(), fd.getFile());
                    if (!f.exists()) {
                        JOptionPane.showMessageDialog(null, "Le ficher selectionné n'existe pas", "Erreur", JOptionPane.ERROR_MESSAGE);
                    } else if (f.isDirectory()) {
                        JOptionPane.showMessageDialog(null, "Le fichier selectionné n'est pas valide", "Erreur", JOptionPane.ERROR_MESSAGE);
                    } else {
                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                final InventaireFromEtatStockImporter impoter = new InventaireFromEtatStockImporter(rowDepot);
                                try {
                                    SQLUtils.executeAtomic(depotElt.getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
                                        @Override
                                        public Object handle(final SQLDataSource ds) throws SQLException, IOException {

                                            impoter.importArticles(f, depotElt.getTable().getDBRoot());
                                            return null;
                                        }
                                    });
                                } catch (Exception e1) {
                                    ExceptionHandler.handle("Erreur lors de l'importation", e1);
                                }

                            }
                        }).start();
                    }
                }
            }

        });
        c.gridx++;
        // buttonValid.setEnabled(false);
        this.add(buttonValid, c);

    }

}