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

package org.openconcerto.modules.customersupport;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

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

import org.openconcerto.sql.element.GroupSQLComponent;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.sqlobject.SQLSearchableTextCombo;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.ui.JDateTime;
import org.openconcerto.ui.component.ComboLockedMode;
import org.openconcerto.ui.component.ITextArea;
import org.openconcerto.ui.group.Group;
import org.openconcerto.utils.ExceptionHandler;

public class CustomerSupportTicketSQLComponent extends GroupSQLComponent {
    public CustomerSupportTicketSQLComponent(SQLElement element, Group group) {
        super(element, group);
    }

    @Override
    protected Set<String> createRequiredNames() {
        final Set<String> s = new HashSet<String>(1);
        s.add("ID_CLIENT");
        s.add("ID_USER_COMMON");
        s.add("LABEL");
        s.add("DATE");
        s.add("STATUS");
        return s;
    }

    @Override
    public int insert(SQLRow order) {

        int id = super.insert(order);
        SQLRow row = getTable().getRow(id);
        try {
            final SQLRowValues createEmptyUpdateRow = row.createEmptyUpdateRow();
            createEmptyUpdateRow.put("NUMBER", id);
            if (row.getObject("DATE") == null) {
                createEmptyUpdateRow.put("DATE", new Date());
            }
            SQLRowValues rowValsHisto = new SQLRowValues(getTable().getTable(Module.TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY));
            rowValsHisto.put("INFORMATION", "Ouverture du ticket");
            rowValsHisto.put("DATE", row.getObject("DATE"));
            rowValsHisto.put("ID_USER_COMMON", row.getForeignID("ID_USER_COMMON"));
            rowValsHisto.put("ID_" + getTable().getName(), createEmptyUpdateRow);
            createEmptyUpdateRow.commit();
        } catch (SQLException e) {
            ExceptionHandler.handle("Impossible d'affecter un numéro sur le ticket", e);
        }
        return id;
    }

    @Override
    public JComponent createEditor(String id) {

        if (id.equals("INFOS")) {
            final ITextArea jTextArea = new ITextArea();
            jTextArea.setFont(new JLabel().getFont());
            jTextArea.setRows(3);
            return jTextArea;
        } else if (id.equals("TYPE") || id.equals("STATUS") || id.equals("RATING")) {
            return new SQLSearchableTextCombo(ComboLockedMode.UNLOCKED, 1, 20, false);
        } else if (id.equals("DATE")) {
            return new JDateTime(true);
        }
        return super.createEditor(id);
    }

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

        final int idUser = UserManager.getInstance().getCurrentUser().getId();
        rowVals.put("ID_USER_COMMON", idUser);

        return rowVals;
    }

}