OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 140 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.customerrelationship.lead;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.openconcerto.modules.customerrelationship.lead.visit.LeadActionItemTable;
import org.openconcerto.sql.element.GroupSQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLBackgroundTableCache;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.sqlobject.ElementComboBox;
import org.openconcerto.sql.sqlobject.SQLSearchableTextCombo;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.ui.JDate;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.ui.component.ComboLockedMode;
import org.openconcerto.ui.component.ITextArea;
import org.openconcerto.ui.group.Group;

public class LeadSQLComponent extends GroupSQLComponent {

    private LeadActionItemTable tableCall, tableVisit;

    public LeadSQLComponent(SQLElement element, Group group) {
        super(element, group);
        startTabGroupAfter("customerrelationship.lead.state");
    }

    @Override
    protected Set<String> createRequiredNames() {
        final Set<String> s = new HashSet<String>(1);
        s.add("ID_ADRESSE");
        s.add("NOM");
        return s;
    }

    @Override
    public JComponent getLabel(String id) {
        if (id.equals("customerrelationship.lead.person")) {
            return new JLabelBold("Contact");
        }
        if (id.equals("customerrelationship.lead.items.visit.tab")) {
            return new JLabelBold("Visites");
        }
        if (id.equals("customerrelationship.lead.items.visit")) {
            return new JLabelBold("");
        }
        if (id.equals("customerrelationship.lead.items.call.tab")) {
            return new JLabelBold("Appels");
        }
        if (id.equals("customerrelationship.lead.items.call")) {
            return new JLabelBold("");
        } else if (id.equals("customerrelationship.lead.contact")) {
            return new JLabel();
        } else if (id.equals("customerrelationship.lead.address")) {
            return new JLabelBold("Adresse");
        } else if (id.equals("customerrelationship.lead.info")) {
            return new JLabelBold("Infos");
        } else if (id.equals("customerrelationship.lead.state")) {
            return new JLabelBold("Statut");
        } else {
            return super.getLabel(id);
        }
    }

    @Override
    public JComponent createEditor(String id) {
        if (id.equals("INFORMATION") || id.equals("INFOS")) {
            final ITextArea jTextArea = new ITextArea(3,3);
            jTextArea.setFont(new JLabel().getFont());
            return jTextArea;
        } else if (id.equals("ID_COMMERCIAL") || id.equals("ID_TITRE_PERSONNEL")) {
            ElementComboBox comp = new ElementComboBox(false, 1);
            ((ElementComboBox) comp).init(getElement().getForeignElement(id));
            return comp;
        } else if (id.equals("customerrelationship.lead.items.call")) {
            tableCall = new LeadActionItemTable(getElement().getDirectory().getElement(Module.TABLE_LEAD_CALL));
            return tableCall;
        } else if (id.equals("customerrelationship.lead.items.visit")) {
            tableVisit = new LeadActionItemTable(getElement().getDirectory().getElement(Module.TABLE_LEAD_VISIT));
            return tableVisit;
        } else if (id.equals("INDUSTRY") || id.equals("STATUS") || id.equals("RATING") || id.equals("SOURCE") || id.equals("DISPO")) {
            return new SQLSearchableTextCombo(ComboLockedMode.UNLOCKED, 1, 1, false);
        } else if (id.equals("DATE")) {
            return new JDate(true);
        }
        
        JComponent comp = super.createEditor(id);
        
        if(comp.getClass() == JTextField.class) {
                JTextField jtxt = new JTextField(10);
                comp = jtxt;
        }
        
        return comp;
    }

    @Override
    public int insert(SQLRow order) {
        int id = super.insert(order);
        this.tableCall.updateField("ID_LEAD", id);
        this.tableVisit.updateField("ID_LEAD", id);
        return id;
    }

    @Override
    public void select(SQLRowAccessor r) {
        super.select(r);
        if (r != null) {
            this.tableCall.insertFrom("ID_LEAD", r.getID());
            this.tableVisit.insertFrom("ID_LEAD", r.getID());
        }
    }

    @Override
    public void update() {
        super.update();
        this.tableCall.updateField("ID_LEAD", getSelectedID());
        this.tableVisit.updateField("ID_LEAD", getSelectedID());
    }

    @Override
    protected SQLRowValues createDefaults() {
        SQLRowValues rowVals = new SQLRowValues(getTable());
        rowVals.put("STATUS", "Nouveau");

        final int idUser = UserManager.getInstance().getCurrentUser().getId();
        final SQLTable foreignTableComm = getTable().getForeignTable("ID_COMMERCIAL");
        SQLRow rowsComm = SQLBackgroundTableCache.getInstance().getCacheForTable(foreignTableComm).getFirstRowContains(idUser, foreignTableComm.getField("ID_USER_COMMON"));

        if (rowsComm != null) {
            rowVals.put("ID_COMMERCIAL", rowsComm.getID());
        }
        return rowVals;
    }
}