OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 128 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
75 ilm 1
package org.openconcerto.modules.project.element;
2
 
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8
 
9
import javax.swing.SwingUtilities;
10
 
11
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
12
import org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement;
13
import org.openconcerto.sql.Configuration;
14
import org.openconcerto.sql.element.SQLComponent;
15
import org.openconcerto.sql.element.UISQLComponent;
16
import org.openconcerto.sql.model.SQLRow;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.sql.sqlobject.ElementComboBox;
21
import org.openconcerto.sql.sqlobject.JUniqueTextField;
22
import org.openconcerto.sql.view.EditFrame;
23
import org.openconcerto.ui.JDate;
24
import org.openconcerto.ui.component.ITextArea;
25
import org.openconcerto.utils.CollectionMap;
26
import org.openconcerto.utils.ExceptionHandler;
27
 
28
public class ProjectSQLElement extends ComptaSQLConfElement {
29
 
30
    public ProjectSQLElement() {
31
        super("AFFAIRE", "une affaire", "affaires");
32
    }
33
 
34
    @Override
35
    protected List<String> getListFields() {
36
        final List<String> l = new ArrayList<String>();
37
        l.add("ID_CLIENT");
38
        l.add("ID_TYPE_AFFAIRE");
39
        l.add("NUMERO");
40
        l.add("ID_ETAT_AFFAIRE");
41
        l.add("ID_COMMERCIAL");
42
        l.add("INFOS");
43
        return l;
44
    }
45
 
46
    @Override
47
    protected List<String> getComboFields() {
48
        final List<String> l = new ArrayList<String>();
49
        l.add("NUMERO");
50
        l.add("ID_CLIENT");
51
        return l;
52
    }
53
 
54
    @Override
55
    public CollectionMap<String, String> getShowAs() {
56
        return CollectionMap.singleton(null, getComboFields());
57
    }
58
 
59
    @Override
60
    public Set<String> getInsertOnlyFields() {
61
        Set<String> s = new HashSet<String>(1);
62
        s.add("ID_DEVIS");
63
        return s;
64
    }
65
 
66
    @Override
67
    public SQLComponent createComponent() {
68
        return new UISQLComponent(this, 2) {
69
 
70
            final JUniqueTextField field = new JUniqueTextField();
71
 
72
            @Override
73
            protected void addViews() {
74
 
75
                this.addView(this.field, "NUMERO", "1");
76
                this.addView(new JDate(true), "DATE", "1");
77
                this.addView("ID_CLIENT", "1;" + REQ);
78
                this.addView("ID_COMMERCIAL", "1");
79
                this.addView("ID_DEVIS", "1");
80
 
81
                this.addView("ID_TYPE_AFFAIRE", "1;left");
82
                final ElementComboBox boxEtatAffaire = new ElementComboBox();
83
                this.addView(boxEtatAffaire, "ID_ETAT_AFFAIRE", "1;left;" + REQ);
84
                this.addView(new ITextArea(), "INFOS", "2");
85
 
86
            }
87
 
88
            @Override
89
            public void select(SQLRowAccessor r) {
90
                super.select(r);
91
                if (r != null) {
92
                    this.field.setIdSelected(r.getID());
93
                }
94
            }
95
 
96
            @Override
97
            public void update() {
98
                if (!this.field.checkValidation()) {
99
                    ExceptionHandler.handle("Impossible d'ajouter, numéro d'affaire existant.");
100
                    Object root = SwingUtilities.getRoot(this);
101
                    if (root instanceof EditFrame) {
102
                        EditFrame frame = (EditFrame) root;
103
                        frame.getPanel().setAlwaysVisible(true);
104
                    }
105
                    return;
106
                }
107
                super.update();
108
            }
109
 
110
            @Override
111
            public int insert(SQLRow order) {
112
 
113
                int idCommande = getSelectedID();
114
 
115
                // on verifie qu'un devis du meme numero n'a pas été inséré entre temps
116
                if (this.field.checkValidation()) {
117
 
118
                    idCommande = super.insert(order);
119
                    // incrémentation du numéro auto
120
                    if (NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class).equalsIgnoreCase(this.field.getText().trim())) {
121
                        SQLTable tableNum = Configuration.getInstance().getRoot().findTable("NUMEROTATION_AUTO");
122
                        SQLRowValues rowVals = new SQLRowValues(tableNum);
123
                        int val = tableNum.getRow(2).getInt("AFFAIRE_START");
124
                        val++;
125
                        rowVals.put("AFFAIRE_START", new Integer(val));
126
 
127
                        try {
128
                            rowVals.update(2);
129
                        } catch (SQLException e) {
130
                            e.printStackTrace();
131
                        }
132
                    }
133
 
134
                } else {
135
                    ExceptionHandler.handle("Impossible d'ajouter, numéro d'affaire existant.");
136
                    Object root = SwingUtilities.getRoot(this);
137
                    if (root instanceof EditFrame) {
138
                        EditFrame frame = (EditFrame) root;
139
                        frame.getPanel().setAlwaysVisible(true);
140
                    }
141
                }
142
                SQLRow row = getTable().getRow(idCommande);
143
                SQLRow rowDevis = row.getForeign("ID_DEVIS");
144
                if (rowDevis != null && !rowDevis.isUndefined()) {
145
                    SQLRowValues rowVals = rowDevis.asRowValues();
146
                    rowVals.put("ID_AFFAIRE", idCommande);
147
                    try {
148
                        rowVals.update();
149
                    } catch (SQLException exn) {
150
                        // TODO Bloc catch auto-généré
151
                        exn.printStackTrace();
152
                    }
153
                }
154
                return idCommande;
155
            }
156
 
157
            @Override
158
            protected SQLRowValues createDefaults() {
159
                SQLRowValues rowVals = new SQLRowValues(getTable());
160
                rowVals.put("NUMERO", NumerotationAutoSQLElement.getNextNumero(ProjectSQLElement.class));
161
                rowVals.put("ID_ETAT_AFFAIRE", ProjectStateSQLElement.EN_COURS);
162
                return rowVals;
163
            }
164
        };
165
 
166
    }
167
}