17,11 → 17,15 |
import java.sql.SQLException; |
import java.sql.Time; |
import java.text.SimpleDateFormat; |
import java.time.LocalDateTime; |
import java.time.format.DateTimeFormatter; |
import java.time.format.FormatStyle; |
import java.util.Calendar; |
import java.util.Date; |
import java.util.List; |
import java.util.Locale; |
import java.util.Properties; |
import java.util.prefs.BackingStoreException; |
import java.util.prefs.Preferences; |
|
import javax.swing.ImageIcon; |
import javax.swing.JFrame; |
29,28 → 33,24 |
import javax.swing.SwingUtilities; |
|
import org.openconcerto.erp.config.ComptaPropsConfiguration; |
import org.openconcerto.erp.config.ServerFinderPanel; |
import org.openconcerto.erp.modules.ModuleManager; |
import org.openconcerto.erp.core.customerrelationship.customer.element.CustomerSQLElement; |
import org.openconcerto.erp.modules.ModuleVersion; |
import org.openconcerto.sql.Configuration; |
import org.openconcerto.sql.element.SQLElement; |
import org.openconcerto.sql.model.SQLBase; |
import org.openconcerto.erp.utils.HeadlessGestion; |
import org.openconcerto.sql.model.SQLRow; |
import org.openconcerto.sql.model.SQLRowListRSH; |
import org.openconcerto.sql.model.SQLRowValues; |
import org.openconcerto.sql.model.SQLRowValuesListFetcher; |
import org.openconcerto.sql.model.SQLSelect; |
import org.openconcerto.sql.model.SQLTable; |
import org.openconcerto.sql.model.Where; |
import org.openconcerto.sql.preferences.SQLPreferences; |
import org.openconcerto.sql.preferences.UserProps; |
import org.openconcerto.sql.sqlobject.IComboSelectionItem; |
import org.openconcerto.utils.ExceptionHandler; |
import org.openconcerto.utils.i18n.TranslationManager; |
import org.openconcerto.utils.cc.ITransformer; |
|
public class BadgeListener implements Runnable { |
private static final int UDP_PORT = 1470; |
|
private String doorIp; |
private int relai; |
private ComptaPropsConfiguration conf; |
private Preferences modulePrefs; |
|
protected TrayIcon trayIcon; |
|
58,56 → 58,14 |
|
} |
|
public void init(String id) { |
// TODO use HeadlessGestion |
TranslationManager.getInstance().addTranslationStreamFromClass(BadgeListener.class); |
TranslationManager.getInstance().setLocale(Locale.FRANCE); |
final ComptaPropsConfiguration conf = ComptaPropsConfiguration.create(true); |
if (conf == null) { |
ServerFinderPanel.main(new String[0]); |
return; |
} |
public void init(String id) throws Exception { |
final HeadlessGestion headlessGestion = new HeadlessGestion(); |
headlessGestion.setupGlobalState(2, Integer.valueOf(id)); |
this.conf = headlessGestion.getComptaPropsConfiguration(); |
|
Configuration.setInstance(conf); |
conf.getUserManager().setCurrentUserID(2); |
|
try { |
conf.getBase(); |
// create table if necessary |
SQLPreferences.getPrefTable(conf.getRoot()); |
} catch (Exception e) { |
ExceptionHandler.die("Erreur de connexion à la base de données", e); |
// since we're not in the EDT, the previous call doesn't block, |
// so return (it won't quit the VM since a dialog is displaying) |
return; |
} |
try { |
final File moduleDir = new File("Modules"); |
moduleDir.mkdir(); |
ModuleManager.getInstance().addFactories(moduleDir); |
} catch (Throwable e) { |
ExceptionHandler.handle("Erreur d'accès aux modules", e); |
} |
|
int selectedSociete = UserProps.getInstance().getLastSocieteID(); |
if (id != null) { |
try { |
selectedSociete = Integer.valueOf(id); |
} catch (Exception e) { |
e.printStackTrace(); |
} |
} |
|
if (selectedSociete < SQLRow.MIN_VALID_ID) { |
final SQLElement elem = conf.getDirectory().getElement(conf.getRoot().getTable("SOCIETE_COMMON")); |
final List<IComboSelectionItem> comboItems = elem.getComboRequest().getComboItems(); |
if (comboItems.size() > 0) |
selectedSociete = comboItems.get(0).getId(); |
else |
throw new IllegalStateException("No " + elem + " found"); |
} |
System.err.println("BadgeListener.init() societe " + selectedSociete); |
conf.setUpSocieteDataBaseConnexion(selectedSociete); |
final String moduleID = "org.openconcerto.modules.badge"; |
final ModuleVersion vers = this.conf.getModuleManager().getDBInstalledModuleVersion(moduleID); |
this.modulePrefs = this.conf.getModuleManager().getFactories().get(moduleID).get(vers).getSQLPreferences(this.conf.getModuleManager().getRoot()); |
} |
|
private PopupMenu createTrayMenu() { |
143,16 → 101,11 |
return ti; |
} |
|
/** |
* @param args |
*/ |
public static void main(String[] args) { |
|
public static void main(String[] args) throws Exception { |
BadgeListener bl = new BadgeListener(); |
bl.readConfiguration(); |
bl.initUI(); |
bl.startDaemon(); |
|
} |
|
public void startDaemon() { |
188,7 → 141,7 |
|
} |
|
public void readConfiguration() { |
public void readConfiguration() throws Exception { |
final Properties props = new Properties(); |
final File file = new File("badge.properties"); |
System.out.println("Reading from: " + file.getAbsolutePath()); |
241,7 → 194,6 |
} |
|
public void cardIdReceived(final String sentence) { |
System.err.println("BadgeListener.cardIdReceived() " + sentence); |
if (isBadgeAllowed(sentence)) { |
boolean b = openDoor(4); |
if (b) { |
255,66 → 207,79 |
} |
} |
|
private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); |
|
public void displayMessage(String title, String txt) { |
if (trayIcon != null) { |
trayIcon.displayMessage(title, txt, TrayIcon.MessageType.INFO); |
displayMessage(title, txt, TrayIcon.MessageType.INFO); |
} |
|
private void displayMessage(String title, String txt, final TrayIcon.MessageType msgType) { |
if (this.trayIcon != null) { |
this.trayIcon.displayMessage(title, txt, msgType); |
} else { |
System.out.println("[INFO] " + title + " : " + txt); |
System.out.println("[" + msgType.name() + "] " + DATE_FMT.format(LocalDateTime.now()) + " " + title + " : " + txt); |
} |
} |
|
public void displayError(String title, String txt) { |
if (trayIcon != null) { |
trayIcon.displayMessage(title, txt, TrayIcon.MessageType.ERROR); |
} else { |
System.out.println("[ERROR] " + title + " : " + txt); |
} |
|
displayMessage(title, txt, TrayIcon.MessageType.ERROR); |
} |
|
public boolean isBadgeAllowed(String cardNumber) { |
SQLBase base = Configuration.getInstance().getBase(); |
SQLSelect sel = new SQLSelect(base); |
// FIXME use SQLElement |
SQLTable tableAdh = Configuration.getInstance().getRoot().findTable("ADHERENT"); |
sel.addSelectStar(tableAdh); |
sel.setWhere(new Where(tableAdh.getField("NUMERO_CARTE"), "=", cardNumber)); |
List<SQLRow> list = (List<SQLRow>) base.getDataSource().execute(sel.asString(), SQLRowListRSH.createFromSelect(sel)); |
public boolean isBadgeAllowed(final String cardNumber) { |
final CustomerSQLElement clientElem = this.conf.getDirectory().getElement(CustomerSQLElement.class); |
final String clientAdhFieldName = "ID_ADHERENT"; |
final SQLTable tableAdh = clientElem.getForeignElement(clientAdhFieldName).getTable(); |
// TODO use createFetcher() |
final SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(clientElem.createGraph()); |
fetcher.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() { |
@Override |
public SQLSelect transformChecked(SQLSelect sel) { |
sel.andWhere(new Where(sel.getAlias(tableAdh).getField("NUMERO_CARTE"), "=", cardNumber)); |
return sel; |
} |
}); |
final List<SQLRowValues> list = fetcher.fetch(); |
|
String motif = ""; |
// FIXME use actual module installed in the DB (MAYBE init() the ModuleManager) |
Boolean onlyAdmin = ModuleManager.getInstance().getFactories().get("org.openconcerto.modules.badge").get(new ModuleVersion(1, 0)).getSQLPreferences(tableAdh.getDBRoot()) |
.getBoolean(Module.ENTREE_PREF, false); |
|
boolean allow = false; |
SQLRow adh = null; |
SQLRowValues adh = null; |
String name = null; |
// Aucun adhérent assigné à cette carte |
if (list == null || list.isEmpty()) { |
motif = "Aucun adhérent associé à la carte " + cardNumber; |
System.err.println(motif); |
displayError("Erreur", motif); |
|
} else if (list.size() > 1) { |
motif = list.size() + " adhérents sont liés à la même carte " + cardNumber; |
System.err.println(motif); |
displayError("Erreur", motif); |
Thread.dumpStack(); |
} else { |
|
for (SQLRow sqlRow : list) { |
for (SQLRowValues clientR : list) { |
name = clientR.getString("NOM"); |
adh = (SQLRowValues) clientR.getForeign(clientAdhFieldName); |
|
adh = sqlRow; |
|
// Admin toujours autorisé |
if (sqlRow.getBoolean("ADMIN")) { |
if (adh.getBoolean("ADMIN")) { |
allow = true; |
motif = "Administrateur toujours autorisé"; |
break; |
} |
|
// get up to date values |
try { |
this.modulePrefs.sync(); |
} catch (BackingStoreException e) { |
e.printStackTrace(); |
} |
final boolean onlyAdmin = this.modulePrefs.getBoolean(Module.ENTREE_PREF, false); |
if (onlyAdmin) { |
motif = "Seul les membres administrateurs sont autorisés!"; |
break; |
} |
|
if (!sqlRow.getBoolean("ACTIF")) { |
if (!adh.getBoolean("ACTIF")) { |
motif = "La carte de l'adhérent n'est pas active dans sa fiche"; |
break; |
} |
321,7 → 286,7 |
|
Calendar cal = Calendar.getInstance(); |
final Date d = cal.getTime(); |
final Calendar dateValidite = sqlRow.getDate("DATE_VALIDITE_INSCRIPTION"); |
final Calendar dateValidite = adh.getDate("DATE_VALIDITE_INSCRIPTION"); |
|
if (dateValidite != null && dateValidite.before(cal)) { |
motif = "La date d'autorisation est expirée"; |
328,7 → 293,7 |
break; |
} |
|
SQLRow rowPlage = sqlRow.getForeignRow("ID_PLAGE_HORAIRE"); |
SQLRow rowPlage = adh.asRow().getForeignRow("ID_PLAGE_HORAIRE"); |
|
if (rowPlage != null) { |
Time time = new Time(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)); |
378,7 → 343,7 |
} |
|
// Création de l'entrée dans la table |
SQLTable tableEntree = Configuration.getInstance().getRoot().findTable("ENTREE"); |
SQLTable tableEntree = this.conf.getDirectory().getElement("ENTREE").getTable(); |
SQLRowValues rowVals = new SQLRowValues(tableEntree); |
rowVals.put("DATE", new Date()); |
rowVals.put("NUMERO_CARTE", cardNumber); |
385,8 → 350,8 |
rowVals.put("ACCEPTE", allow); |
rowVals.put("MOTIF", motif); |
|
if (adh != null) { |
rowVals.put("ADHERENT", adh.getString("NOM") + " " + adh.getString("PRENOM")); |
if (name != null) { |
rowVals.put("ADHERENT", name); |
} |
try { |
rowVals.commit(); |