OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Blame | Compare with Previous | 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 18 oct. 2011
 */
package org.openconcerto.erp.core.supplychain.order.element;

import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
import org.openconcerto.erp.core.supplychain.order.component.CommandeSQLComponent;
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLComponent;
import org.openconcerto.sql.model.SQLInjector;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel.EditMode;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.RowAction;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.sql.view.list.SQLTableModelSource;
import org.openconcerto.utils.ListMap;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;

public class DemandePrixSQLElement extends ComptaSQLConfElement {

    private SQLRowValues getLivraisonAdr(SQLRowAccessor rowAffaire) {
        SQLRowAccessor rowClient = rowAffaire.getForeign("ID_CLIENT");
        SQLRowAccessor rowAdrL = rowClient.getForeign("ID_ADRESSE_L");
        if (rowAdrL == null || rowAdrL.isUndefined()) {
            rowAdrL = rowClient.getForeign("ID_ADRESSE");
        }
        SQLRowValues rowVals = rowAdrL.asRowValues();
        rowVals.clearPrimaryKeys();
        return rowVals;
    }

    @Override
    protected void _initTableSource(SQLTableModelSource source) {
        super._initTableSource(source);
        // {
        // BaseSQLTableModelColumn cmd = new BaseSQLTableModelColumn("Commande associée",
        // String.class) {
        //
        // @Override
        // protected Object show_(SQLRowAccessor r) {
        // Collection<? extends SQLRowAccessor> l =
        // r.getReferentRows(r.getTable().getTable("COMMANDE"));
        // String s = "";
        // if (l != null && l.size() > 0) {
        // for (SQLRowAccessor sqlRowAccessor : l) {
        // s += sqlRowAccessor.getString("NUMERO") + " ";
        // }
        // }
        //
        // return s;
        // }
        //
        // @Override
        // public Set<FieldPath> getPaths() {
        // // TODO Raccord de méthode auto-généré
        // Path p = new Path(getTable());
        // p = p.add(getTable().getTable("COMMANDE").getField("ID_DEMANDE_PRIX"));
        //
        // return CollectionUtils.createSet(new FieldPath(p, "NUMERO"));
        // }
        // };
        // source.getColumns().add(cmd);
        // }

    }

    public DemandePrixSQLElement() {
        super("DEMANDE_PRIX");

        MouseSheetXmlListeListener l = new MouseSheetXmlListeListener(DemandePrixSheetXML.class) {

            @Override
            protected String getMailObject(SQLRow row) {
                // TODO Raccord de méthode auto-généré
                return "Veuillez trouver en pièce jointe notre demande de prix.";
            }

            @Override
            public List<RowAction> addToMenu() {
                List<RowAction> actions = new ArrayList<RowAction>();

                PredicateRowAction action = new PredicateRowAction(new AbstractAction("Marqué comme acceptée et transférer en commande") {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        SQLRowAccessor row = IListe.get(arg0).getSelectedRow().asRow();
                        SQLRowValues rowVals = row.asRowValues();
                        rowVals.put("ID_ETAT_DEMANDE_PRIX", EtatDemandePrixSQLElement.ACCEPTE);
                        try {
                            rowVals.update();
                        } catch (SQLException exn) {
                            exn.printStackTrace();
                        }
                        SQLInjector inj = SQLInjector.getInjector(row.getTable(), row.getTable().getTable("COMMANDE"));
                        EditFrame f = new EditFrame(Configuration.getInstance().getDirectory().getElement("COMMANDE"));
                        final CommandeSQLComponent sqlComponent = (CommandeSQLComponent) f.getPanel().getSQLComponent();
                        sqlComponent.select(inj.createRowValuesFrom(row.getID()));

                        // SQLRowAccessor foreignRow = row.getForeign("ID_ADRESSE");
                        // if (foreignRow != null && !foreignRow.isUndefined()) {
                        // ElementSQLObject obj = (ElementSQLObject)
                        // sqlComponent.getView("ID_ADRESSE").getComp();
                        // obj.setCreated(true);
                        //
                        // SQLRowValues asRowValues = getLivraisonAdr(row.getForeign("ID_AFFAIRE"));
                        // asRowValues.clearPrimaryKeys();
                        // obj.getSQLChild().select(asRowValues);
                        // }
                        sqlComponent.loadItem(sqlComponent.getRowValuesTablePanel(), Configuration.getInstance().getDirectory().getElement("DEMANDE_PRIX"), row.getID(),
                                Configuration.getInstance().getDirectory().getElement("DEMANDE_PRIX_ELEMENT"));
                        f.setVisible(true);
                    }
                }, false);
                action.setPredicate(IListeEvent.getSingleSelectionPredicate());
                actions.add(action);

                PredicateRowAction actionCreate = new PredicateRowAction(new AbstractAction("Créer à partir de ...") {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        SQLRowAccessor row = IListe.get(arg0).getSelectedRow().asRow();
                        DemandePrixSQLComponent createComponent = (DemandePrixSQLComponent) createComponent();
                        EditFrame frame = new EditFrame(createComponent, EditMode.CREATION);
                        createComponent.loadDemandeExistant(row.getID());
                        frame.setVisible(true);
                    }
                }, false);
                actionCreate.setPredicate(IListeEvent.getSingleSelectionPredicate());
                actions.add(actionCreate);

                // actions.addAll(new DemandePrixAction().getEtatAction());
                return actions;
            }
        };

        getRowActions().addAll(l.getRowActions());

    }

    @Override
    protected List<String> getListFields() {
        final List<String> l = new ArrayList<String>();
        l.add("NUMERO");
        l.add("ID_FOURNISSEUR");
        l.add("DATE");
        l.add("ID_ETAT_DEMANDE_PRIX");
        return l;
    }

    @Override
    protected List<String> getComboFields() {
        final List<String> l = new ArrayList<String>();
        l.add("NUMERO");
        l.add("ID_FOURNISSEUR");
        return l;
    }

    @Override
    public ListMap<String, String> getShowAs() {
        return ListMap.singleton(null, "NUMERO");
    }

    @Override
    public SQLComponent createComponent() {
        return new DemandePrixSQLComponent(this);
    }

    @Override
    protected String createCode() {
        return createCodeOfPackage() + ".demande";
    }

}