74 |
ilm |
1 |
package org.openconcerto.modules.extensionbuilder;
|
|
|
2 |
|
|
|
3 |
import java.awt.event.ActionEvent;
|
|
|
4 |
import java.io.IOException;
|
|
|
5 |
import java.sql.SQLException;
|
|
|
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
|
|
|
10 |
import javax.swing.AbstractAction;
|
|
|
11 |
import javax.swing.JFrame;
|
|
|
12 |
import javax.swing.JOptionPane;
|
|
|
13 |
import javax.swing.SwingUtilities;
|
|
|
14 |
|
|
|
15 |
import org.openconcerto.erp.config.ComptaPropsConfiguration;
|
|
|
16 |
import org.openconcerto.erp.config.Log;
|
|
|
17 |
import org.openconcerto.erp.config.MainFrame;
|
|
|
18 |
import org.openconcerto.erp.modules.AbstractModule;
|
|
|
19 |
import org.openconcerto.erp.modules.ComponentsContext;
|
|
|
20 |
import org.openconcerto.erp.modules.DBContext;
|
|
|
21 |
import org.openconcerto.erp.modules.MenuContext;
|
|
|
22 |
import org.openconcerto.erp.modules.ModuleFactory;
|
|
|
23 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
24 |
import org.openconcerto.sql.model.DBSystemRoot;
|
|
|
25 |
import org.openconcerto.sql.model.SQLSelect;
|
|
|
26 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
27 |
import org.openconcerto.sql.utils.SQLCreateTable;
|
|
|
28 |
import org.openconcerto.ui.FrameUtil;
|
|
|
29 |
import org.openconcerto.utils.i18n.TranslationManager;
|
|
|
30 |
|
|
|
31 |
public final class ExtensionBuilderModule extends AbstractModule {
|
|
|
32 |
|
|
|
33 |
public static final String TABLE_NAME = "EXTENSION_XML";
|
|
|
34 |
private final ArrayList<Extension> extensions = new ArrayList<Extension>();
|
|
|
35 |
|
|
|
36 |
public ExtensionBuilderModule(ModuleFactory f) throws IOException {
|
|
|
37 |
super(f);
|
|
|
38 |
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
@Override
|
|
|
42 |
protected void install(DBContext ctxt) {
|
|
|
43 |
super.install(ctxt);
|
|
|
44 |
if (ctxt.getRoot().getTable(TABLE_NAME) == null) {
|
|
|
45 |
final SQLCreateTable createTable = ctxt.getCreateTable(TABLE_NAME);
|
|
|
46 |
createTable.addVarCharColumn("IDENTIFIER", 256);
|
|
|
47 |
createTable.addVarCharColumn("XML", 30000);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
@Override
|
|
|
52 |
protected void setupComponents(ComponentsContext ctxt) {
|
|
|
53 |
// ctxt.putAdditionalField("CLIENT", "ID_FIDELITY_CARD");
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
protected void setupMenu(MenuContext ctxt) {
|
|
|
59 |
final DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
|
|
|
60 |
final DBSystemRoot systemRoot = root.getDBSystemRoot();
|
|
|
61 |
|
|
|
62 |
// Load extensions
|
|
|
63 |
SQLSelect query = new SQLSelect(systemRoot, true);
|
|
|
64 |
final SQLTable table = systemRoot.findTable(ExtensionBuilderModule.TABLE_NAME);
|
|
|
65 |
query.addSelect(table.getField("IDENTIFIER"));
|
|
|
66 |
query.addSelect(table.getField("XML"));
|
|
|
67 |
|
|
|
68 |
@SuppressWarnings("unchecked")
|
|
|
69 |
final List<Map<String, String>> result = systemRoot.getDataSource().execute(query.asString());
|
|
|
70 |
if (result == null || result.isEmpty()) {
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
for (Map<String, String> row : result) {
|
|
|
74 |
final Extension e = new Extension(row.get("IDENTIFIER"));
|
|
|
75 |
e.importFromXML(row.get("XML"));
|
|
|
76 |
this.extensions.add(e);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
ctxt.addMenuItem(new AbstractAction("Gestionnaire d'extensions") {
|
|
|
80 |
|
|
|
81 |
@Override
|
|
|
82 |
public void actionPerformed(ActionEvent e) {
|
|
|
83 |
Log.get().info("Opening Extension Builder frame");
|
|
|
84 |
JFrame f = new JFrame("OpenConcerto Extension Builder - création simplifiée d'extensions");
|
|
|
85 |
f.setSize(800, 600);
|
|
|
86 |
f.setContentPane(new ExtensionListPanel(ExtensionBuilderModule.this));
|
|
|
87 |
f.setLocationRelativeTo(null);
|
|
|
88 |
FrameUtil.show(f);
|
|
|
89 |
}
|
|
|
90 |
}, "menu.extension");
|
|
|
91 |
|
|
|
92 |
// Start previously started extensions
|
|
|
93 |
for (Extension extension : extensions) {
|
|
|
94 |
if (extension.isAutoStart()) {
|
|
|
95 |
try {
|
|
|
96 |
extension.setupMenu(ctxt);
|
|
|
97 |
} catch (Throwable e) {
|
|
|
98 |
JOptionPane.showMessageDialog(new JFrame(), "Impossible de démarrer l'extension " + extension.getName() + "\n" + e.getMessage());
|
|
|
99 |
e.printStackTrace();
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
@Override
|
|
|
106 |
protected void start() {
|
|
|
107 |
Log.get().info("Starting Extension Builder");
|
|
|
108 |
final DBRoot root = ComptaPropsConfiguration.getInstanceCompta().getRootSociete();
|
|
|
109 |
|
|
|
110 |
// Start previously started extensions
|
|
|
111 |
for (Extension extension : extensions) {
|
|
|
112 |
if (extension.isAutoStart()) {
|
|
|
113 |
try {
|
|
|
114 |
extension.start(root);
|
|
|
115 |
} catch (Throwable e) {
|
|
|
116 |
JOptionPane.showMessageDialog(new JFrame(), "Impossible de démarrer l'extension " + extension.getName() + "\n" + e.getMessage());
|
|
|
117 |
e.printStackTrace();
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
@Override
|
|
|
125 |
protected void stop() {
|
|
|
126 |
for (Extension extension : extensions) {
|
|
|
127 |
extension.stop();
|
|
|
128 |
}
|
|
|
129 |
this.extensions.clear();
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public List<Extension> getExtensions() {
|
|
|
133 |
return this.extensions;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public void add(Extension e) {
|
|
|
137 |
this.extensions.add(e);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public void remove(Extension extension) {
|
|
|
141 |
this.extensions.remove(extension);
|
|
|
142 |
}
|
|
|
143 |
}
|