OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 128 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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