OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
43 ilm 1
package org.openconcerto.modules.badge;
2
 
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.IOException;
6
import java.text.DateFormat;
7
import java.text.SimpleDateFormat;
8
import java.util.ArrayList;
9
import java.util.Calendar;
10
import java.util.Date;
11
import java.util.HashSet;
12
import java.util.List;
13
import java.util.Set;
14
 
15
import javax.swing.Action;
16
import javax.swing.JFrame;
17
import javax.swing.JTable;
18
import javax.swing.table.DefaultTableCellRenderer;
19
 
20
import org.openconcerto.erp.action.CreateFrameAbstractAction;
21
import org.openconcerto.erp.config.Gestion;
22
import org.openconcerto.erp.config.MainFrame;
23
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
24
import org.openconcerto.erp.core.common.ui.ListeViewPanel;
25
import org.openconcerto.erp.modules.AbstractModule;
26
import org.openconcerto.erp.modules.ComponentsContext;
27
import org.openconcerto.erp.modules.DBContext;
28
import org.openconcerto.erp.modules.ModuleFactory;
29
import org.openconcerto.erp.modules.ModuleManager;
30
import org.openconcerto.erp.modules.ModulePackager;
31
import org.openconcerto.erp.modules.RuntimeModuleFactory;
32
import org.openconcerto.sql.Configuration;
33
import org.openconcerto.sql.element.SQLComponent;
34
import org.openconcerto.sql.element.SQLElement;
35
import org.openconcerto.sql.element.SQLElementDirectory;
36
import org.openconcerto.sql.element.UISQLComponent;
37
import org.openconcerto.sql.model.SQLName;
38
import org.openconcerto.sql.model.SQLRowAccessor;
39
import org.openconcerto.sql.model.SQLSyntax;
40
import org.openconcerto.sql.model.Where;
41
import org.openconcerto.sql.utils.SQLCreateTable;
42
import org.openconcerto.sql.view.FileDropHandler;
43
import org.openconcerto.sql.view.IListFrame;
44
import org.openconcerto.sql.view.IListPanel;
45
import org.openconcerto.sql.view.ListeAddPanel;
46
import org.openconcerto.sql.view.list.IListe;
47
import org.openconcerto.sql.view.list.RowAction;
48
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
49
import org.openconcerto.ui.PanelFrame;
50
import org.openconcerto.utils.CollectionMap;
51
import org.openconcerto.utils.FileUtils;
52
import org.openconcerto.utils.cc.IClosure;
53
 
54
public final class Module extends AbstractModule {
55
 
56
    public Module(ModuleFactory f) throws IOException {
57
        super(f);
58
 
59
    }
60
 
61
    @Override
62
    protected void install(DBContext ctxt) {
63
        super.install(ctxt);
64
 
65
        if (!ctxt.getTablesPreviouslyCreated().contains("ENTREE")) {
66
            // ENTREE
67
            final SQLCreateTable createTableEntree = ctxt.getCreateTable("ENTREE");
68
            createTableEntree.addDateAndTimeColumn("DATE");
69
            createTableEntree.addVarCharColumn("NUMERO_CARTE", 256);
70
            createTableEntree.addVarCharColumn("ADHERENT", 512);
71
            createTableEntree.addVarCharColumn("MOTIF", 2048);
72
            createTableEntree.addColumn("ACCEPTE", "boolean");
73
        }
74
        if (!ctxt.getTablesPreviouslyCreated().contains("PLAGE_HORAIRE")) {
75
            // PLAGE
76
            final SQLCreateTable createTablePlage = ctxt.getCreateTable("PLAGE_HORAIRE");
77
            createTablePlage.addVarCharColumn("NOM", 256);
78
            createTablePlage.addColumn("DEBUT_1_LUNDI", "time");
79
            createTablePlage.addColumn("DEBUT_1_MARDI", "time");
80
            createTablePlage.addColumn("DEBUT_1_MERCREDI", "time");
81
            createTablePlage.addColumn("DEBUT_1_JEUDI", "time");
82
            createTablePlage.addColumn("DEBUT_1_VENDREDI", "time");
83
            createTablePlage.addColumn("DEBUT_1_SAMEDI", "time");
84
            createTablePlage.addColumn("DEBUT_1_DIMANCHE", "time");
85
 
86
            createTablePlage.addColumn("DEBUT_2_LUNDI", "time");
87
            createTablePlage.addColumn("DEBUT_2_MARDI", "time");
88
            createTablePlage.addColumn("DEBUT_2_MERCREDI", "time");
89
            createTablePlage.addColumn("DEBUT_2_JEUDI", "time");
90
            createTablePlage.addColumn("DEBUT_2_VENDREDI", "time");
91
            createTablePlage.addColumn("DEBUT_2_SAMEDI", "time");
92
            createTablePlage.addColumn("DEBUT_2_DIMANCHE", "time");
93
 
94
            createTablePlage.addColumn("DEBUT_3_LUNDI", "time");
95
            createTablePlage.addColumn("DEBUT_3_MARDI", "time");
96
            createTablePlage.addColumn("DEBUT_3_MERCREDI", "time");
97
            createTablePlage.addColumn("DEBUT_3_JEUDI", "time");
98
            createTablePlage.addColumn("DEBUT_3_VENDREDI", "time");
99
            createTablePlage.addColumn("DEBUT_3_SAMEDI", "time");
100
            createTablePlage.addColumn("DEBUT_3_DIMANCHE", "time");
101
 
102
            createTablePlage.addColumn("FIN_1_LUNDI", "time");
103
            createTablePlage.addColumn("FIN_1_MARDI", "time");
104
            createTablePlage.addColumn("FIN_1_MERCREDI", "time");
105
            createTablePlage.addColumn("FIN_1_JEUDI", "time");
106
            createTablePlage.addColumn("FIN_1_VENDREDI", "time");
107
            createTablePlage.addColumn("FIN_1_SAMEDI", "time");
108
            createTablePlage.addColumn("FIN_1_DIMANCHE", "time");
109
 
110
            createTablePlage.addColumn("FIN_2_LUNDI", "time");
111
            createTablePlage.addColumn("FIN_2_MARDI", "time");
112
            createTablePlage.addColumn("FIN_2_MERCREDI", "time");
113
            createTablePlage.addColumn("FIN_2_JEUDI", "time");
114
            createTablePlage.addColumn("FIN_2_VENDREDI", "time");
115
            createTablePlage.addColumn("FIN_2_SAMEDI", "time");
116
            createTablePlage.addColumn("FIN_2_DIMANCHE", "time");
117
 
118
            createTablePlage.addColumn("FIN_3_LUNDI", "time");
119
            createTablePlage.addColumn("FIN_3_MARDI", "time");
120
            createTablePlage.addColumn("FIN_3_MERCREDI", "time");
121
            createTablePlage.addColumn("FIN_3_JEUDI", "time");
122
            createTablePlage.addColumn("FIN_3_VENDREDI", "time");
123
            createTablePlage.addColumn("FIN_3_SAMEDI", "time");
124
            createTablePlage.addColumn("FIN_3_DIMANCHE", "time");
125
        }
126
 
127
        if (!ctxt.getTablesPreviouslyCreated().contains("ADHERENT")) {
128
            // ADHERENT
129
            final SQLCreateTable createTable = ctxt.getCreateTable("ADHERENT");
130
 
131
            createTable.addVarCharColumn("NUMERO_CARTE", 256);
132
            createTable.addVarCharColumn("NOM", 256);
133
 
134
            createTable.addVarCharColumn("TEL", 256);
135
            createTable.addColumn("ACTIF", "boolean default false");
136
            createTable.addColumn("ADMIN", "boolean default false");
137
            createTable.addVarCharColumn("MAIL", 256);
138
            createTable.addVarCharColumn("PRENOM", 256);
139
            createTable.addVarCharColumn("INFOS", 2048);
140
            createTable.addColumn("DATE_VALIDITE_INSCRIPTION", "date");
141
            createTable.addColumn("DATE_NAISSANCE", "date");
142
 
143
            createTable.addForeignColumn("ID_ADRESSE", Configuration.getInstance().getRoot().findTable("ADRESSE"));
144
            createTable.addForeignColumn("ID_PLAGE_HORAIRE", new SQLName("PLAGE_HORAIRE"), SQLSyntax.ID_NAME, null);
145
        }
146
    }
147
 
148
    @Override
149
    protected void setupElements(SQLElementDirectory dir) {
150
        super.setupElements(dir);
151
 
152
        final ComptaSQLConfElement entreeElement = new ComptaSQLConfElement("ENTREE", "une entrée", "entrées") {
153
 
154
            @Override
155
            protected List<String> getListFields() {
156
                final List<String> l = new ArrayList<String>();
157
                l.add("DATE");
158
                l.add("NUMERO_CARTE");
159
                l.add("ADHERENT");
160
                l.add("MOTIF");
161
                l.add("ACCEPTE");
162
                return l;
163
            }
164
 
165
            @Override
166
            public Set<String> getReadOnlyFields() {
167
                Set<String> s = new HashSet<String>(super.getReadOnlyFields());
168
                s.add("NUMERO_CARTE");
169
                return s;
170
            }
171
 
172
            @Override
173
            protected List<String> getComboFields() {
174
                final List<String> l = new ArrayList<String>();
175
                l.add("NUMERO_CARTE");
176
                l.add("DATE");
177
                return l;
178
            }
179
 
180
            @Override
181
            public CollectionMap<String, String> getShowAs() {
182
                return CollectionMap.singleton(null, getComboFields());
183
            }
184
 
185
            @Override
186
            public SQLComponent createComponent() {
187
                return new UISQLComponent(this) {
188
                    @Override
189
                    protected void addViews() {
190
                        this.addView("NUMERO_CARTE");
191
                        this.addView("DATE");
192
                    }
193
                };
194
            }
195
        };
196
        dir.addSQLElement(entreeElement);
197
        dir.addSQLElement(new PlageHoraireSQLElement());
198
        dir.addSQLElement(new AdherentSQLElement());
199
    }
200
 
201
    private IListPanel getPanelEntree(boolean filtered) {
202
        final SQLElement element = Configuration.getInstance().getDirectory().getElement("ENTREE");
203
 
204
        final SQLTableModelSourceOnline tableModelEntree = element.getTableSource(true);
205
 
206
        if (filtered) {
207
            Calendar cal = Calendar.getInstance();
208
            cal.add(Calendar.DAY_OF_MONTH, -15);
209
            Where wAttente = new Where(element.getTable().getField("DATE"), ">=", cal.getTime());
210
            tableModelEntree.getReq().setWhere(wAttente);
211
        }
212
        tableModelEntree.getColumn(element.getTable().getField("DATE")).setRenderer(new DefaultTableCellRenderer() {
213
 
214
            DateFormat format = new SimpleDateFormat("EEEE dd MMMM yyyy HH:mm:ss");
215
 
216
            @Override
217
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
218
                final String format2 = format.format((Date) value);
219
                return super.getTableCellRendererComponent(table, format2, isSelected, hasFocus, row, column);
220
 
221
            }
222
        });
223
 
224
        final IListPanel panel = new ListeViewPanel(element, new IListe(tableModelEntree));
225
 
226
        final Action createAction = RowAction.createAction("Assigner à", null, new IClosure<List<SQLRowAccessor>>() {
227
 
228
            @Override
229
            public void executeChecked(List<SQLRowAccessor> input) {
230
                SQLRowAccessor row = input.get(0);
231
                PanelFrame frame = new PanelFrame(new AssignationPanel(row.getString("NUMERO_CARTE")), "Assignation d'une carte");
232
                frame.setLocationRelativeTo(null);
233
                frame.setVisible(true);
234
 
235
            }
236
        });
237
 
238
        panel.getListe().addRowAction(createAction);
239
 
240
        return panel;
241
    }
242
 
243
    @Override
244
    protected void setupComponents(ComponentsContext ctxt) {
245
        ctxt.addFileDropHandler("ADHERENT", new FileDropHandler() {
246
 
247
            @Override
248
            public boolean handle(File f, Component source) {
249
                AdherentImporter importer;
250
                try {
251
                    importer = new AdherentImporter(f);
252
                    importer.importAdherent();
253
                } catch (Exception e) {
254
                    e.printStackTrace();
255
                }
256
                return true;
257
            }
258
 
259
            @Override
260
            public boolean canHandle(File f) {
261
                String n = f.getName().toLowerCase();
262
                return n.endsWith(".xls") || n.endsWith(".ods");
263
            }
264
        });
265
        ctxt.addMenuItem(new CreateFrameAbstractAction("Liste des adhérents") {
266
 
267
            @Override
268
            public JFrame createFrame() {
269
                IListFrame frameAdh = new IListFrame(new ListeAddPanel(Configuration.getInstance().getDirectory().getElement("ADHERENT")));
270
                return frameAdh;
271
            }
272
        }, MainFrame.LIST_MENU);
273
 
274
        ctxt.addMenuItem(new CreateFrameAbstractAction("Liste des entrées") {
275
 
276
            @Override
277
            public JFrame createFrame() {
278
 
279
                return new IListFrame(getPanelEntree(false));
280
            }
281
        }, MainFrame.LIST_MENU);
282
 
283
    }
284
 
285
    @Override
286
    protected void start() {
287
        MainFrame.getInstance().getTabbedPane().addTab("Liste des entrées", getPanelEntree(true));
288
    }
289
 
290
    @Override
291
    protected void stop() {
292
    }
293
 
294
    public static void main(String[] args) throws IOException {
295
        final File propsFile = new File("gestionModule.properties");
296
 
297
        final ModuleFactory factory = new RuntimeModuleFactory(propsFile);
298
 
299
        final File distDir = new File("dist");
300
        FileUtils.mkdir_p(distDir);
301
        new ModulePackager(propsFile, new File("bin/")).writeToDir(distDir);
302
        new ModulePackager(propsFile, new File("bin/")).writeToDir(new File("../OpenConcerto/Modules"));
303
 
304
        ModuleManager.getInstance().addFactoryAndStart(factory, false);
305
 
306
        Gestion.main(args);
307
 
308
    }
309
}