OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
76 ilm 1
package org.openconcerto.modules.timetracking;
2
 
3
import java.awt.Dimension;
4
import java.awt.event.ActionEvent;
5
import java.io.IOException;
152 ilm 6
import java.sql.SQLException;
76 ilm 7
import java.util.Arrays;
8
import java.util.List;
9
import java.util.Set;
10
 
11
import javax.swing.AbstractAction;
12
import javax.swing.JOptionPane;
13
 
14
import org.openconcerto.erp.core.common.ui.PanelFrame;
15
import org.openconcerto.erp.modules.AbstractModule;
16
import org.openconcerto.erp.modules.AlterTableRestricted;
17
import org.openconcerto.erp.modules.ComponentsContext;
18
import org.openconcerto.erp.modules.DBContext;
19
import org.openconcerto.erp.modules.ModuleFactory;
20
import org.openconcerto.erp.modules.ModulePreferencePanel;
21
import org.openconcerto.erp.modules.ModulePreferencePanelDesc;
22
import org.openconcerto.erp.preferences.GenerationDocumentGestCommPreferencePanel;
23
import org.openconcerto.erp.preferences.TemplateNXProps;
24
import org.openconcerto.modules.timetracking.element.ProjectTimeTrackingSQLElement;
25
import org.openconcerto.modules.timetracking.mail.MailingTimeTracking;
26
import org.openconcerto.sql.Configuration;
27
import org.openconcerto.sql.element.GlobalMapper;
28
import org.openconcerto.sql.element.SQLElement;
29
import org.openconcerto.sql.element.SQLElementDirectory;
157 ilm 30
import org.openconcerto.sql.model.DBRoot;
76 ilm 31
import org.openconcerto.sql.model.FieldPath;
32
import org.openconcerto.sql.model.SQLRow;
33
import org.openconcerto.sql.model.SQLRowAccessor;
34
import org.openconcerto.sql.model.SQLRowValues;
35
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
36
import org.openconcerto.sql.model.SQLSelect;
37
import org.openconcerto.sql.model.SQLSelectJoin;
38
import org.openconcerto.sql.model.SQLTable;
39
import org.openconcerto.sql.model.TableRef;
40
import org.openconcerto.sql.model.Where;
41
import org.openconcerto.sql.model.graph.Path;
42
import org.openconcerto.sql.users.UserManager;
43
import org.openconcerto.sql.utils.SQLCreateTable;
44
import org.openconcerto.sql.view.EditFrame;
45
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
46
import org.openconcerto.sql.view.list.IListe;
47
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
48
import org.openconcerto.sql.view.list.RowAction;
49
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
50
import org.openconcerto.utils.CollectionUtils;
95 ilm 51
import org.openconcerto.utils.ListMap;
76 ilm 52
import org.openconcerto.utils.PrefType;
53
import org.openconcerto.utils.Tuple2;
54
import org.openconcerto.utils.cc.ITransformer;
55
import org.openconcerto.utils.i18n.TranslationManager;
56
 
57
public final class Module extends AbstractModule {
58
 
59
    public Module(ModuleFactory f) throws IOException {
60
        super(f);
61
    }
62
 
63
    @Override
152 ilm 64
    protected void install(DBContext ctxt) throws SQLException, IOException {
76 ilm 65
        super.install(ctxt);
66
        if (!ctxt.getRoot().findTable("AFFAIRE").contains("ENVOI_MAIL_AUTO")) {
67
            final AlterTableRestricted alter = ctxt.getAlterTable("AFFAIRE");
68
            alter.addColumn("ENVOI_MAIL_AUTO", "boolean DEFAULT false");
69
        }
70
        if (!ctxt.getRoot().contains("AFFAIRE_TEMPS")) {
71
            // Gestion des temps
72
            final SQLCreateTable createTable = ctxt.getCreateTable("AFFAIRE_TEMPS");
73
            createTable.addVarCharColumn("DESCRIPTIF", 1024);
74
            createTable.addVarCharColumn("INFOS", 1024);
75
            createTable.addForeignColumn("ID_AFFAIRE", ctxt.getRoot().getTable("AFFAIRE"));
76
            createTable.addForeignColumn("ID_COMMANDE_CLIENT_ELEMENT", ctxt.getRoot().getTable("COMMANDE_CLIENT_ELEMENT"));
77
            createTable.addColumn("DATE", "date");
78
            createTable.addDateAndTimeColumn("DATE_ENVOI");
79
            createTable.addColumn("TEMPS", "real DEFAULT 0");
80
            createTable.addForeignColumn("ID_USER_COMMON", ctxt.getRoot().findTable("USER_COMMON"));
81
            createTable.addColumn("ENVOYE_PAR_MAIL", "boolean DEFAULT false");
82
        }
83
 
84
    }
85
 
86
    @Override
87
    protected void setupElements(SQLElementDirectory dir) {
88
        super.setupElements(dir);
89
        TranslationManager.getInstance().addTranslationStreamFromClass(this.getClass());
90
 
91
        GlobalMapper.getInstance().map(ProjectTimeTrackingSQLElement.ELEMENT_CODE + ".default", new ProjectTimeTrackingGroup());
92
        final ProjectTimeTrackingSQLElement element = new ProjectTimeTrackingSQLElement();
93
        dir.addSQLElement(element);
94
 
95
        final SQLElement elementAffaire = dir.getElement("AFFAIRE");
96
        elementAffaire.putAdditionalField("ENVOI_MAIL_AUTO");
97
        elementAffaire.getRowActions().add(getCreateTempsAffaireAction());
98
        elementAffaire.getRowActions().add(getReportingAction());
99
        elementAffaire.getRowActions().add(getSendMailTempsAffaireAction());
100
 
101
        final SQLElement orderItemElement = dir.getElement("COMMANDE_CLIENT_ELEMENT");
102
        orderItemElement.addListColumn(new BaseSQLTableModelColumn("Commande", String.class) {
103
 
104
            @Override
105
            protected Object show_(SQLRowAccessor r) {
106
                final SQLRowAccessor row = r.getForeign("ID_COMMANDE_CLIENT");
107
                return row.getString("NUMERO");
108
            }
109
 
110
            @Override
111
            public Set<FieldPath> getPaths() {
112
                final SQLTable tableOrderItemTable = orderItemElement.getTable();
88 ilm 113
                final Path p = new Path(tableOrderItemTable).addForeignField("ID_COMMANDE_CLIENT");
76 ilm 114
                return CollectionUtils.createSet(new FieldPath(p, "NUMERO"));
115
            }
116
        });
117
 
118
    }
119
 
120
    private RowAction getSendMailTempsAffaireAction() {
121
 
122
        final PredicateRowAction action = new PredicateRowAction(new AbstractAction() {
123
 
124
            @Override
125
            public void actionPerformed(ActionEvent e) {
126
 
127
                final SQLTable tableTemps = Configuration.getInstance().getRoot().findTable("AFFAIRE_TEMPS");
128
                final SQLTable tableAff = Configuration.getInstance().getRoot().findTable("AFFAIRE");
129
                final SQLTable tableClient = Configuration.getInstance().getRoot().findTable("CLIENT");
130
                final SQLTable tableUser = Configuration.getInstance().getRoot().findTable("USER_COMMON");
131
 
132
                final SQLRowValues rowValsUser = new SQLRowValues(tableUser);
133
                rowValsUser.put("NOM", null);
134
                rowValsUser.put("PRENOM", null);
135
 
136
                final SQLRowValues rowValsClient = new SQLRowValues(tableClient);
137
                rowValsClient.put("NOM", null);
138
                rowValsClient.put("MAIL", null);
139
 
140
                final SQLRowValues rowValsAff = new SQLRowValues(tableAff);
141
                rowValsAff.put("NUMERO", null);
142
                rowValsAff.put("ID_CLIENT", rowValsClient);
143
 
144
                final SQLRowValues rowVals = new SQLRowValues(tableTemps);
145
                rowVals.put("ID_AFFAIRE", rowValsAff);
146
                rowVals.put("ID_USER_COMMON", rowValsUser);
147
                rowVals.put("DESCRIPTIF", null);
148
                rowVals.put("DATE", null);
149
                rowVals.put("TEMPS", null);
150
 
151
                final SQLRowValuesListFetcher fetcher = new SQLRowValuesListFetcher(rowVals);
152
                fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
153
                    @Override
154
                    public SQLSelect transformChecked(SQLSelect sel) {
155
                        final TableRef tableFAlias = sel.getAlias(tableTemps);
156
                        final SQLSelectJoin join = sel.addJoin("LEFT", tableFAlias.getField("ID_AFFAIRE"));
157
                        final Where w = new Where(join.getJoinedTable().getField("ENVOI_MAIL_AUTO"), "=", Boolean.TRUE);
158
                        join.setWhere(w);
159
                        sel.setWhere(new Where(tableFAlias.getField("ENVOYE_PAR_MAIL"), "=", Boolean.FALSE));
160
                        return sel;
161
                    }
162
                });
163
 
164
                final List<SQLRowValues> list = fetcher.fetch();
95 ilm 165
                final ListMap<Number, SQLRowValues> mailingMap = new ListMap<Number, SQLRowValues>();
76 ilm 166
                for (SQLRowValues sqlRowValues : list) {
167
                    final SQLRowAccessor foreign = sqlRowValues.getForeign("ID_AFFAIRE");
168
                    if (foreign != null) {
169
                        SQLRowAccessor foreign2 = foreign.getForeign("ID_CLIENT");
170
                        if (foreign2 != null) {
95 ilm 171
                            mailingMap.add(foreign2.getID(), sqlRowValues);
76 ilm 172
                        }
173
                    }
174
                }
175
 
176
                new Thread(new Runnable() {
177
                    @Override
178
                    public void run() {
179
                        new MailingTimeTracking(mailingMap, getFactory());
180
                    }
181
                }).start();
182
            }
183
        }, true, "timetracking.customer.report.email.send");
184
 
185
        action.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
186
        return action;
187
    }
188
 
189
    private RowAction getCreateTempsAffaireAction() {
190
        PredicateRowAction action = new PredicateRowAction(new AbstractAction() {
191
 
192
            @Override
193
            public void actionPerformed(ActionEvent e) {
194
 
195
                final SQLElement element = Configuration.getInstance().getDirectory().getElement("AFFAIRE_TEMPS");
196
                final EditFrame frame = new EditFrame(element);
197
                final SQLRowValues rowVals = new SQLRowValues(element.getTable());
141 ilm 198
                final SQLRowValues selectedRow = IListe.get(e).getSelectedRow();
199
                final List<SQLRow> rows = selectedRow.getTable().getRow(selectedRow.getID()).getReferentRows(element.getTable().getTable("COMMANDE_CLIENT"));
76 ilm 200
                if (rows.size() < 1) {
201
                    JOptionPane.showMessageDialog(null, "Aucune commande en cours associée à cette affaire.");
202
                    return;
203
                }
204
                rowVals.put("ID_AFFAIRE", IListe.get(e).getSelectedId());
205
                rowVals.put("ID_USER_COMMON", UserManager.getUserID());
206
 
207
                frame.getPanel().getSQLComponent().select(rowVals);
208
                frame.setVisible(true);
209
            }
210
        }, true, "timetracking.task.create");
211
        action.setPredicate(IListeEvent.getNonEmptySelectionPredicate());
212
        return action;
213
    }
214
 
215
    private RowAction getReportingAction() {
216
        final PredicateRowAction action = new PredicateRowAction(new AbstractAction() {
217
 
218
            @Override
219
            public void actionPerformed(ActionEvent e) {
220
                final PanelFrame frame = new PanelFrame(new ReportingPanel(), "Reporting");
221
                frame.pack();
222
                frame.setLocationRelativeTo(null);
223
                frame.setMinimumSize((Dimension) frame.getSize().clone());
224
                frame.setVisible(true);
225
            }
226
        }, true, "timetracking.task.report.create");
227
 
95 ilm 228
        action.setPredicate(IListeEvent.getSingleSelectionPredicate());
76 ilm 229
        return action;
230
    }
231
 
232
    @Override
233
    protected void setupComponents(final ComponentsContext ctxt) {
234
 
235
    }
236
 
237
    public static final String SMTP_PREFS = "smtp";
238
    public static final String ID_MAIL_PREFS = "id_mail";
239
    public static final String ADR_MAIL_PREFS = "adr_mail";
240
    public static final String PWD_MAIL_PREFS = "pwd_mail";
241
    public static final String EXPEDITEUR_MAIL_PREFS = "expediteur_mail";
242
    public static final String SUBJECT_MAIL_PREFS = "subject_mail";
243
    public static final String ENTETE_MAIL_PREFS = "entete_mail";
244
    public static final String PIED_MAIL_PREFS = "pied_mail";
245
    public static final String SSL_MAIL_PREFS = "ssl_mail";
246
    public static final String PORT_MAIL_PREFS = "port_mail";
247
 
248
    @Override
157 ilm 249
    public List<ModulePreferencePanelDesc> getPrefDescriptors(final DBRoot root) {
76 ilm 250
 
251
        return Arrays.<ModulePreferencePanelDesc> asList(new ModulePreferencePanelDesc("Envoi par mail des temps") {
252
            @Override
253
            protected ModulePreferencePanel createPanel() {
157 ilm 254
                return new ModulePreferencePanel(root, "Envoi par mail des temps") {
76 ilm 255
                    @Override
256
                    protected void addViews() {
257
 
258
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Nom de l'expéditeur ", EXPEDITEUR_MAIL_PREFS));
259
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Adresse ", ADR_MAIL_PREFS));
260
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Serveur Sortant (SMTP) ", SMTP_PREFS));
261
                        this.addView(new SQLPrefView<Integer>(PrefType.INT_TYPE, "Port ", PORT_MAIL_PREFS));
262
                        this.addView(new SQLPrefView<Boolean>(PrefType.BOOLEAN_TYPE, "SSL ", SSL_MAIL_PREFS));
263
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Identifiant ", ID_MAIL_PREFS));
264
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Mot de passe ", PWD_MAIL_PREFS));
265
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, "Sujet ", SUBJECT_MAIL_PREFS));
266
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, 1024, "Entête ", ENTETE_MAIL_PREFS));
267
                        this.addView(new SQLPrefView<String>(PrefType.STRING_TYPE, 1024, "Pied ", PIED_MAIL_PREFS));
268
                    }
269
                };
270
            }
271
            // pas forcement monté sur la même lettre
272
        }.setLocal(false).setKeywords("mail"));
273
    }
274
 
275
    @Override
276
    protected void start() {
277
        final Tuple2<String, String> create = Tuple2.create(ReportingSheetXml.TEMPLATE_ID, ReportingSheetXml.TEMPLATE_PROPERTY_NAME);
278
        GenerationDocumentGestCommPreferencePanel.addPref(create, "Reporting");
279
        ((TemplateNXProps) TemplateNXProps.getInstance()).register(ReportingSheetXml.TEMPLATE_ID, ReportingSheetXml.TEMPLATE_PROPERTY_NAME, "Reporting");
280
    }
281
 
282
    @Override
283
    protected void stop() {
284
    }
285
}