OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 147 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.customersupport;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.openconcerto.erp.config.Gestion;
import org.openconcerto.erp.config.MainFrame;
import org.openconcerto.erp.modules.AbstractModule;
import org.openconcerto.erp.modules.ComponentsContext;
import org.openconcerto.erp.modules.DBContext;
import org.openconcerto.erp.modules.MenuContext;
import org.openconcerto.erp.modules.ModuleFactory;
import org.openconcerto.erp.modules.ModuleManager;
import org.openconcerto.erp.modules.ModulePackager;
import org.openconcerto.erp.modules.RuntimeModuleFactory;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.GlobalMapper;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.FieldPath;
import org.openconcerto.sql.model.SQLRequestLog;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.model.graph.Path;
import org.openconcerto.sql.ui.ConnexionPanel;
import org.openconcerto.sql.utils.SQLCreateTable;
import org.openconcerto.sql.view.ListeAddPanel;
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
import org.openconcerto.utils.CollectionUtils;

public final class Module extends AbstractModule {

    public static final String TABLE_CUSTOMER_SUPPORT_TICKET = "CUSTOMER_SUPPORT_TICKET";
    public static final String TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY = "CUSTOMER_SUPPORT_TICKET_HISTORY";

    public Module(ModuleFactory f) throws IOException {
        super(f);
    }

    @Override
    protected void install(DBContext ctxt) throws SQLException, IOException {
        super.install(ctxt);
        // TODO use version to upgrade
        if (ctxt.getRoot().getTable(TABLE_CUSTOMER_SUPPORT_TICKET) == null) {
            final SQLCreateTable createTable = ctxt.getCreateTable(TABLE_CUSTOMER_SUPPORT_TICKET);

            createTable.addIntegerColumn("NUMBER", 0);
            createTable.addVarCharColumn("LABEL", 256);
            //
            createTable.addForeignColumn("CLIENT");
            createTable.addForeignColumn("ID_USER_COMMON", ctxt.getRoot().findTable("USER_COMMON"));
            //
            createTable.addVarCharColumn("INFOS", 4096);
            //
            createTable.addVarCharColumn("RATING", 200);
            createTable.addVarCharColumn("TYPE", 128);
            createTable.addVarCharColumn("STATUS", 128);
            createTable.addDateAndTimeColumn("REMIND_DATE");
            createTable.addDateAndTimeColumn("DATE");
            createTable.addBooleanColumn("CLOSED_AND_ARCHIVED", Boolean.FALSE, false);

            // Suivi
            final SQLCreateTable createHistory = ctxt.getCreateTable(TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY);
            createHistory.addDateAndTimeColumn("DATE");
            createHistory.addForeignColumn(createTable);
            createHistory.addVarCharColumn("INFORMATION", 10240);
            createHistory.addIntegerColumn("ATTACHMENTS", 0);
            createHistory.addForeignColumn("ID_USER_COMMON", ctxt.getRoot().findTable("USER_COMMON"));

            // STATUT
            // NOUVEAU, OUVERT, FERME, REJETE, EN COURS
            try {
                List<String> status = Arrays.asList("En cours", "Fermé", "Nouveau", "Ouvert", "Rejeté");
                for (String s : status) {

                    SQLRowValues rowVals = new SQLRowValues(ctxt.getRoot().getTable("COMPLETION"));
                    rowVals.put("CHAMP", TABLE_CUSTOMER_SUPPORT_TICKET + ".STATUS");
                    rowVals.put("LABEL", s);
                    rowVals.insert(true, false);
                }

                List<String> prio = Arrays.asList("Basse", "Normale", "Haute");
                for (String p : prio) {

                    SQLRowValues rowVals = new SQLRowValues(ctxt.getRoot().getTable("COMPLETION"));
                    rowVals.put("CHAMP", TABLE_CUSTOMER_SUPPORT_TICKET + ".RATING");
                    rowVals.put("LABEL", p);
                    rowVals.insert(true, false);
                }

            } catch (SQLException e) {
                throw new IllegalStateException("erreur lors de l'ajout des status", e);
            }
        }
    }

    @Override
    protected void setupElements(final SQLElementDirectory dir) {
        super.setupElements(dir);
        final CustomerSupportTicketSQLElement element = new CustomerSupportTicketSQLElement(this);
        GlobalMapper.getInstance().map(element.getCode() + ".default", new CustomerSupportTicketGroup());
        dir.addSQLElement(element);
        dir.addSQLElement(new CustomerTicketHistorySQLElement(this));
    }

    @Override
    protected void setupComponents(ComponentsContext ctxt) {
        // nothing
    }

    @Override
    protected void setupMenu(final MenuContext ctxt) {
        ctxt.addMenuItem(new CustomerSupportTicketListAction(), MainFrame.LIST_MENU);
        ctxt.addMenuItem(new CustomerSupportTicketHistoryListAction(), MainFrame.LIST_MENU);
    }

    @Override
    protected void start() {

        final SQLElement elt = Configuration.getInstance().getDirectory().getElement(Module.TABLE_CUSTOMER_SUPPORT_TICKET);
        // Filter
        final SQLTableModelSourceOnline source = elt.getTableSource(true);
        source.getColumns().remove(source.getColumn(elt.getTable().getField("CLOSED_AND_ARCHIVED")));
        Where wAttente = new Where(elt.getTable().getField("CLOSED_AND_ARCHIVED"), "=", Boolean.FALSE);
        source.getReq().setWhere(wAttente);

        // Filter
        BaseSQLTableModelColumn dateRemind = new BaseSQLTableModelColumn("Date de rappel", Date.class) {

            @Override
            protected Object show_(SQLRowAccessor r) {

                Calendar c = r.getDate("REMIND_DATE");
                if (c == null) {
                    return null;
                } else {
                    return new Date(c.getTime().getTime());
                }

            }

            @Override
            public Set<FieldPath> getPaths() {
                Path p = new Path(elt.getTable());
                return CollectionUtils.createSet(new FieldPath(p, "REMIND_DATE"));
            }
        };

        dateRemind.setRenderer(new RemindDateRenderer());
        source.getColumns().add(dateRemind);

        final ListeAddPanel pane = new ListeAddPanel(elt, new IListe(source), "Open");
        pane.getListe().setOpaque(false);
        pane.setOpaque(false);
        MainFrame.getInstance().getTabbedPane().addTab("Ticket support ouvert", pane);

    }

    @Override
    protected void stop() {
        // nothing to stop
    }

    public static void main(String[] args) throws IOException {
        System.setProperty(ConnexionPanel.QUICK_LOGIN, "true");
        final File propsFile = new File("module.properties");
        System.out.println(propsFile.getAbsolutePath());
        final ModuleFactory factory = new RuntimeModuleFactory(propsFile);
        SQLRequestLog.setEnabled(true);
        SQLRequestLog.showFrame();
        // uncomment to create and use the jar
        final ModulePackager modulePackager = new ModulePackager(propsFile, new File("bin/"));
        modulePackager.writeToDir(new File("../OpenConcerto/Modules"));
        ModuleManager.getInstance().addFactories(new File("../OpenConcerto/Modules"));
        ModuleManager.getInstance().addFactoryAndStart(factory, false);
        Gestion.main(args);
    }

}