OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 153 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 153 Rev 167
Line 15... Line 15...
15
import java.net.DatagramPacket;
15
import java.net.DatagramPacket;
16
import java.net.DatagramSocket;
16
import java.net.DatagramSocket;
17
import java.sql.SQLException;
17
import java.sql.SQLException;
18
import java.sql.Time;
18
import java.sql.Time;
19
import java.text.SimpleDateFormat;
19
import java.text.SimpleDateFormat;
-
 
20
import java.time.LocalDateTime;
-
 
21
import java.time.format.DateTimeFormatter;
-
 
22
import java.time.format.FormatStyle;
20
import java.util.Calendar;
23
import java.util.Calendar;
21
import java.util.Date;
24
import java.util.Date;
22
import java.util.List;
25
import java.util.List;
23
import java.util.Locale;
-
 
24
import java.util.Properties;
26
import java.util.Properties;
-
 
27
import java.util.prefs.BackingStoreException;
-
 
28
import java.util.prefs.Preferences;
25
 
29
 
26
import javax.swing.ImageIcon;
30
import javax.swing.ImageIcon;
27
import javax.swing.JFrame;
31
import javax.swing.JFrame;
28
import javax.swing.JOptionPane;
32
import javax.swing.JOptionPane;
29
import javax.swing.SwingUtilities;
33
import javax.swing.SwingUtilities;
30
 
34
 
31
import org.openconcerto.erp.config.ComptaPropsConfiguration;
35
import org.openconcerto.erp.config.ComptaPropsConfiguration;
32
import org.openconcerto.erp.config.ServerFinderPanel;
36
import org.openconcerto.erp.core.customerrelationship.customer.element.CustomerSQLElement;
33
import org.openconcerto.erp.modules.ModuleManager;
-
 
34
import org.openconcerto.erp.modules.ModuleVersion;
37
import org.openconcerto.erp.modules.ModuleVersion;
35
import org.openconcerto.sql.Configuration;
38
import org.openconcerto.erp.utils.HeadlessGestion;
36
import org.openconcerto.sql.element.SQLElement;
-
 
37
import org.openconcerto.sql.model.SQLBase;
-
 
38
import org.openconcerto.sql.model.SQLRow;
39
import org.openconcerto.sql.model.SQLRow;
39
import org.openconcerto.sql.model.SQLRowListRSH;
-
 
40
import org.openconcerto.sql.model.SQLRowValues;
40
import org.openconcerto.sql.model.SQLRowValues;
-
 
41
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
41
import org.openconcerto.sql.model.SQLSelect;
42
import org.openconcerto.sql.model.SQLSelect;
42
import org.openconcerto.sql.model.SQLTable;
43
import org.openconcerto.sql.model.SQLTable;
43
import org.openconcerto.sql.model.Where;
44
import org.openconcerto.sql.model.Where;
44
import org.openconcerto.sql.preferences.SQLPreferences;
-
 
45
import org.openconcerto.sql.preferences.UserProps;
-
 
46
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
-
 
47
import org.openconcerto.utils.ExceptionHandler;
45
import org.openconcerto.utils.cc.ITransformer;
48
import org.openconcerto.utils.i18n.TranslationManager;
-
 
49
 
46
 
50
public class BadgeListener implements Runnable {
47
public class BadgeListener implements Runnable {
51
    private static final int UDP_PORT = 1470;
48
    private static final int UDP_PORT = 1470;
-
 
49
 
52
    private String doorIp;
50
    private String doorIp;
53
    private int relai;
51
    private int relai;
-
 
52
    private ComptaPropsConfiguration conf;
-
 
53
    private Preferences modulePrefs;
54
 
54
 
55
    protected TrayIcon trayIcon;
55
    protected TrayIcon trayIcon;
56
 
56
 
57
    public BadgeListener() {
57
    public BadgeListener() {
58
 
58
 
59
    }
59
    }
60
 
60
 
61
    public void init(String id) {
61
    public void init(String id) throws Exception {
62
        // TODO use HeadlessGestion
-
 
63
        TranslationManager.getInstance().addTranslationStreamFromClass(BadgeListener.class);
62
        final HeadlessGestion headlessGestion = new HeadlessGestion();
64
        TranslationManager.getInstance().setLocale(Locale.FRANCE);
63
        headlessGestion.setupGlobalState(2, Integer.valueOf(id));
65
        final ComptaPropsConfiguration conf = ComptaPropsConfiguration.create(true);
64
        this.conf = headlessGestion.getComptaPropsConfiguration();
66
        if (conf == null) {
-
 
67
            ServerFinderPanel.main(new String[0]);
-
 
68
            return;
-
 
69
        }
-
 
70
 
-
 
71
        Configuration.setInstance(conf);
-
 
72
        conf.getUserManager().setCurrentUserID(2);
-
 
73
 
65
 
74
        try {
-
 
75
            conf.getBase();
-
 
76
            // create table if necessary
-
 
77
            SQLPreferences.getPrefTable(conf.getRoot());
-
 
78
        } catch (Exception e) {
-
 
79
            ExceptionHandler.die("Erreur de connexion à la base de données", e);
-
 
80
            // since we're not in the EDT, the previous call doesn't block,
-
 
81
            // so return (it won't quit the VM since a dialog is displaying)
-
 
82
            return;
-
 
83
        }
-
 
84
        try {
-
 
85
            final File moduleDir = new File("Modules");
66
        final String moduleID = "org.openconcerto.modules.badge";
86
            moduleDir.mkdir();
-
 
87
            ModuleManager.getInstance().addFactories(moduleDir);
67
        final ModuleVersion vers = this.conf.getModuleManager().getDBInstalledModuleVersion(moduleID);
88
        } catch (Throwable e) {
-
 
89
            ExceptionHandler.handle("Erreur d'accès aux modules", e);
-
 
90
        }
-
 
91
 
-
 
92
        int selectedSociete = UserProps.getInstance().getLastSocieteID();
-
 
93
        if (id != null) {
-
 
94
            try {
-
 
95
                selectedSociete = Integer.valueOf(id);
-
 
96
            } catch (Exception e) {
-
 
97
                e.printStackTrace();
-
 
98
            }
-
 
99
        }
-
 
100
 
-
 
101
        if (selectedSociete < SQLRow.MIN_VALID_ID) {
-
 
102
            final SQLElement elem = conf.getDirectory().getElement(conf.getRoot().getTable("SOCIETE_COMMON"));
68
        this.modulePrefs = this.conf.getModuleManager().getFactories().get(moduleID).get(vers).getSQLPreferences(this.conf.getModuleManager().getRoot());
103
            final List<IComboSelectionItem> comboItems = elem.getComboRequest().getComboItems();
-
 
104
            if (comboItems.size() > 0)
-
 
105
                selectedSociete = comboItems.get(0).getId();
-
 
106
            else
-
 
107
                throw new IllegalStateException("No " + elem + " found");
-
 
108
        }
-
 
109
        System.err.println("BadgeListener.init() societe " + selectedSociete);
-
 
110
        conf.setUpSocieteDataBaseConnexion(selectedSociete);
-
 
111
    }
69
    }
112
 
70
 
113
    private PopupMenu createTrayMenu() {
71
    private PopupMenu createTrayMenu() {
114
        ActionListener exitListener = new ActionListener() {
72
        ActionListener exitListener = new ActionListener() {
115
            public void actionPerformed(ActionEvent e) {
73
            public void actionPerformed(ActionEvent e) {
Line 141... Line 99...
141
        TrayIcon ti = new TrayIcon(image, "Service de badge", popup);
99
        TrayIcon ti = new TrayIcon(image, "Service de badge", popup);
142
        ti.setImageAutoSize(true);
100
        ti.setImageAutoSize(true);
143
        return ti;
101
        return ti;
144
    }
102
    }
145
 
103
 
146
    /**
-
 
147
     * @param args
-
 
148
     */
-
 
149
    public static void main(String[] args) {
104
    public static void main(String[] args) throws Exception {
150
 
-
 
151
        BadgeListener bl = new BadgeListener();
105
        BadgeListener bl = new BadgeListener();
152
        bl.readConfiguration();
106
        bl.readConfiguration();
153
        bl.initUI();
107
        bl.initUI();
154
        bl.startDaemon();
108
        bl.startDaemon();
155
 
-
 
156
    }
109
    }
157
 
110
 
158
    public void startDaemon() {
111
    public void startDaemon() {
159
        Thread t = new Thread(this);
112
        Thread t = new Thread(this);
160
        t.setName("UDP Listener");
113
        t.setName("UDP Listener");
Line 186... Line 139...
186
            }
139
            }
187
        });
140
        });
188
 
141
 
189
    }
142
    }
190
 
143
 
191
    public void readConfiguration() {
144
    public void readConfiguration() throws Exception {
192
        final Properties props = new Properties();
145
        final Properties props = new Properties();
193
        final File file = new File("badge.properties");
146
        final File file = new File("badge.properties");
194
        System.out.println("Reading from: " + file.getAbsolutePath());
147
        System.out.println("Reading from: " + file.getAbsolutePath());
195
        try {
148
        try {
196
            final FileInputStream inStream = new FileInputStream(file);
149
            final FileInputStream inStream = new FileInputStream(file);
Line 239... Line 192...
239
            }
192
            }
240
        }
193
        }
241
    }
194
    }
242
 
195
 
243
    public void cardIdReceived(final String sentence) {
196
    public void cardIdReceived(final String sentence) {
244
        System.err.println("BadgeListener.cardIdReceived() " + sentence);
-
 
245
        if (isBadgeAllowed(sentence)) {
197
        if (isBadgeAllowed(sentence)) {
246
            boolean b = openDoor(4);
198
            boolean b = openDoor(4);
247
            if (b) {
199
            if (b) {
248
                displayMessage("Ouverture", "Ouverture de la porte OK");
200
                displayMessage("Ouverture", "Ouverture de la porte OK");
249
            } else {
201
            } else {
Line 253... Line 205...
253
        } else {
205
        } else {
254
            displayMessage("Carte refusée", "Carte " + sentence + " non acceptée");
206
            displayMessage("Carte refusée", "Carte " + sentence + " non acceptée");
255
        }
207
        }
256
    }
208
    }
257
 
209
 
-
 
210
    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
-
 
211
 
258
    public void displayMessage(String title, String txt) {
212
    public void displayMessage(String title, String txt) {
-
 
213
        displayMessage(title, txt, TrayIcon.MessageType.INFO);
-
 
214
    }
-
 
215
 
-
 
216
    private void displayMessage(String title, String txt, final TrayIcon.MessageType msgType) {
259
        if (trayIcon != null) {
217
        if (this.trayIcon != null) {
260
            trayIcon.displayMessage(title, txt, TrayIcon.MessageType.INFO);
218
            this.trayIcon.displayMessage(title, txt, msgType);
261
        } else {
219
        } else {
262
            System.out.println("[INFO]  " + title + " : " + txt);
220
            System.out.println("[" + msgType.name() + "] " + DATE_FMT.format(LocalDateTime.now()) + " " + title + " : " + txt);
263
        }
221
        }
264
    }
222
    }
265
 
223
 
266
    public void displayError(String title, String txt) {
224
    public void displayError(String title, String txt) {
267
        if (trayIcon != null) {
-
 
268
            trayIcon.displayMessage(title, txt, TrayIcon.MessageType.ERROR);
225
        displayMessage(title, txt, TrayIcon.MessageType.ERROR);
269
        } else {
-
 
270
            System.out.println("[ERROR] " + title + " : " + txt);
-
 
271
        }
226
    }
272
 
227
 
-
 
228
    public boolean isBadgeAllowed(final String cardNumber) {
-
 
229
        final CustomerSQLElement clientElem = this.conf.getDirectory().getElement(CustomerSQLElement.class);
-
 
230
        final String clientAdhFieldName = "ID_ADHERENT";
-
 
231
        final SQLTable tableAdh = clientElem.getForeignElement(clientAdhFieldName).getTable();
-
 
232
        // TODO use createFetcher()
-
 
233
        final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(clientElem.createGraph());
-
 
234
        fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
-
 
235
            @Override
-
 
236
            public SQLSelect transformChecked(SQLSelect sel) {
-
 
237
                sel.andWhere(new Where(sel.getAlias(tableAdh).getField("NUMERO_CARTE"), "=", cardNumber));
-
 
238
                return sel;
273
    }
239
            }
274
 
-
 
275
    public boolean isBadgeAllowed(String cardNumber) {
-
 
276
        SQLBase base = Configuration.getInstance().getBase();
-
 
277
        SQLSelect sel = new SQLSelect(base);
-
 
278
        // FIXME use SQLElement
240
        });
279
        SQLTable tableAdh = Configuration.getInstance().getRoot().findTable("ADHERENT");
-
 
280
        sel.addSelectStar(tableAdh);
241
        final List<SQLRowValues> list = fetcher.fetch();
281
        sel.setWhere(new Where(tableAdh.getField("NUMERO_CARTE"), "=", cardNumber));
-
 
282
        List<SQLRow> list = (List<SQLRow>) base.getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel));
-
 
283
 
242
 
284
        String motif = "";
243
        String motif = "";
285
        // FIXME use actual module installed in the DB (MAYBE init() the ModuleManager)
-
 
286
        Boolean onlyAdmin = ModuleManager.getInstance().getFactories().get("org.openconcerto.modules.badge").get(new ModuleVersion(1, 0)).getSQLPreferences(tableAdh.getDBRoot())
-
 
287
                .getBoolean(Module.ENTREE_PREF, false);
-
 
-
 
244
 
288
        boolean allow = false;
245
        boolean allow = false;
289
        SQLRow adh = null;
246
        SQLRowValues adh = null;
-
 
247
        String name = null;
290
        // Aucun adhérent assigné à cette carte
248
        // Aucun adhérent assigné à cette carte
291
        if (list == null || list.isEmpty()) {
249
        if (list == null || list.isEmpty()) {
292
            motif = "Aucun adhérent associé à la carte " + cardNumber;
250
            motif = "Aucun adhérent associé à la carte " + cardNumber;
293
            System.err.println(motif);
251
            displayError("Erreur", motif);
294
 
252
 
295
        } else if (list.size() > 1) {
253
        } else if (list.size() > 1) {
296
            motif = list.size() + " adhérents sont liés à la même carte " + cardNumber;
254
            motif = list.size() + " adhérents sont liés à la même carte " + cardNumber;
297
            System.err.println(motif);
255
            displayError("Erreur", motif);
298
            Thread.dumpStack();
256
            Thread.dumpStack();
299
        } else {
257
        } else {
300
 
258
 
301
            for (SQLRow sqlRow : list) {
259
            for (SQLRowValues clientR : list) {
302
 
-
 
303
                adh = sqlRow;
260
                name = clientR.getString("NOM");
-
 
261
                adh = (SQLRowValues) clientR.getForeign(clientAdhFieldName);
304
 
262
 
305
                // Admin toujours autorisé
263
                // Admin toujours autorisé
306
                if (sqlRow.getBoolean("ADMIN")) {
264
                if (adh.getBoolean("ADMIN")) {
307
                    allow = true;
265
                    allow = true;
308
                    motif = "Administrateur toujours autorisé";
266
                    motif = "Administrateur toujours autorisé";
309
                    break;
267
                    break;
310
                }
268
                }
311
 
269
 
-
 
270
                // get up to date values
-
 
271
                try {
-
 
272
                    this.modulePrefs.sync();
-
 
273
                } catch (BackingStoreException e) {
-
 
274
                    e.printStackTrace();
-
 
275
                }
-
 
276
                final boolean onlyAdmin = this.modulePrefs.getBoolean(Module.ENTREE_PREF, false);
312
                if (onlyAdmin) {
277
                if (onlyAdmin) {
313
                    motif = "Seul les membres administrateurs sont autorisés!";
278
                    motif = "Seul les membres administrateurs sont autorisés!";
314
                    break;
279
                    break;
315
                }
280
                }
316
 
281
 
317
                if (!sqlRow.getBoolean("ACTIF")) {
282
                if (!adh.getBoolean("ACTIF")) {
318
                    motif = "La carte de l'adhérent n'est pas active dans sa fiche";
283
                    motif = "La carte de l'adhérent n'est pas active dans sa fiche";
319
                    break;
284
                    break;
320
                }
285
                }
321
 
286
 
322
                Calendar cal = Calendar.getInstance();
287
                Calendar cal = Calendar.getInstance();
323
                final Date d = cal.getTime();
288
                final Date d = cal.getTime();
324
                final Calendar dateValidite = sqlRow.getDate("DATE_VALIDITE_INSCRIPTION");
289
                final Calendar dateValidite = adh.getDate("DATE_VALIDITE_INSCRIPTION");
325
 
290
 
326
                if (dateValidite != null && dateValidite.before(cal)) {
291
                if (dateValidite != null && dateValidite.before(cal)) {
327
                    motif = "La date d'autorisation est expirée";
292
                    motif = "La date d'autorisation est expirée";
328
                    break;
293
                    break;
329
                }
294
                }
330
 
295
 
331
                SQLRow rowPlage = sqlRow.getForeignRow("ID_PLAGE_HORAIRE");
296
                SQLRow rowPlage = adh.asRow().getForeignRow("ID_PLAGE_HORAIRE");
332
 
297
 
333
                if (rowPlage != null) {
298
                if (rowPlage != null) {
334
                    Time time = new Time(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
299
                    Time time = new Time(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
335
                    SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE");
300
                    SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE");
336
                    String day = dateFormat.format(d).toUpperCase();
301
                    String day = dateFormat.format(d).toUpperCase();
Line 376... Line 341...
376
                }
341
                }
377
            }
342
            }
378
        }
343
        }
379
 
344
 
380
        // Création de l'entrée dans la table
345
        // Création de l'entrée dans la table
381
        SQLTable tableEntree = Configuration.getInstance().getRoot().findTable("ENTREE");
346
        SQLTable tableEntree = this.conf.getDirectory().getElement("ENTREE").getTable();
382
        SQLRowValues rowVals = new SQLRowValues(tableEntree);
347
        SQLRowValues rowVals = new SQLRowValues(tableEntree);
383
        rowVals.put("DATE", new Date());
348
        rowVals.put("DATE", new Date());
384
        rowVals.put("NUMERO_CARTE", cardNumber);
349
        rowVals.put("NUMERO_CARTE", cardNumber);
385
        rowVals.put("ACCEPTE", allow);
350
        rowVals.put("ACCEPTE", allow);
386
        rowVals.put("MOTIF", motif);
351
        rowVals.put("MOTIF", motif);
387
 
352
 
388
        if (adh != null) {
353
        if (name != null) {
389
            rowVals.put("ADHERENT", adh.getString("NOM") + " " + adh.getString("PRENOM"));
354
            rowVals.put("ADHERENT", name);
390
        }
355
        }
391
        try {
356
        try {
392
            rowVals.commit();
357
            rowVals.commit();
393
        } catch (SQLException exn) {
358
        } catch (SQLException exn) {
394
            exn.printStackTrace();
359
            exn.printStackTrace();