OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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