OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Details | Compare with Previous | Last modification | View Log | RSS feed

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