OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 139 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 139 Rev 153
1
package org.openconcerto.modules.extensionbuilder;
1
package org.openconcerto.modules.extensionbuilder;
2
 
2
 
3
import java.io.BufferedOutputStream;
3
import java.io.BufferedOutputStream;
4
import java.io.ByteArrayOutputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
5
import java.io.IOException;
6
import java.io.StringReader;
6
import java.io.StringReader;
7
import java.io.UnsupportedEncodingException;
7
import java.io.UnsupportedEncodingException;
8
import java.math.BigDecimal;
8
import java.math.BigDecimal;
9
import java.sql.SQLException;
9
import java.sql.SQLException;
10
import java.util.ArrayList;
10
import java.util.ArrayList;
11
import java.util.Collection;
11
import java.util.Collection;
12
import java.util.Collections;
12
import java.util.Collections;
13
import java.util.Comparator;
13
import java.util.Comparator;
14
import java.util.HashSet;
14
import java.util.HashSet;
15
import java.util.LinkedHashSet;
15
import java.util.LinkedHashSet;
16
import java.util.List;
16
import java.util.List;
17
import java.util.Locale;
17
import java.util.Locale;
18
import java.util.Set;
18
import java.util.Set;
19
import java.util.logging.Level;
19
import java.util.logging.Level;
20
 
20
 
21
import javax.swing.JFrame;
21
import javax.swing.JFrame;
22
import javax.swing.JOptionPane;
22
import javax.swing.JOptionPane;
23
import javax.swing.event.ChangeEvent;
23
import javax.swing.event.ChangeEvent;
24
import javax.swing.event.ChangeListener;
24
import javax.swing.event.ChangeListener;
25
 
25
 
26
import org.jdom.Document;
26
import org.jdom.Document;
27
import org.jdom.Element;
27
import org.jdom.Element;
28
import org.jdom.input.SAXBuilder;
28
import org.jdom.input.SAXBuilder;
29
import org.jdom.output.Format;
29
import org.jdom.output.Format;
30
import org.jdom.output.XMLOutputter;
30
import org.jdom.output.XMLOutputter;
31
import org.openconcerto.erp.action.CreateFrameAbstractAction;
31
import org.openconcerto.erp.action.CreateFrameAbstractAction;
32
import org.openconcerto.erp.config.ComptaPropsConfiguration;
32
import org.openconcerto.erp.config.ComptaPropsConfiguration;
33
import org.openconcerto.erp.config.Log;
33
import org.openconcerto.erp.config.Log;
34
import org.openconcerto.erp.config.MenuAndActions;
34
import org.openconcerto.erp.config.MenuAndActions;
35
import org.openconcerto.erp.config.MenuManager;
35
import org.openconcerto.erp.config.MenuManager;
36
import org.openconcerto.erp.modules.MenuContext;
36
import org.openconcerto.erp.modules.MenuContext;
37
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
37
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
38
import org.openconcerto.modules.extensionbuilder.list.ColumnDescriptor;
38
import org.openconcerto.modules.extensionbuilder.list.ColumnDescriptor;
39
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
39
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
40
import org.openconcerto.modules.extensionbuilder.menu.mainmenu.MenuDescriptor;
40
import org.openconcerto.modules.extensionbuilder.menu.mainmenu.MenuDescriptor;
41
import org.openconcerto.modules.extensionbuilder.meu.actions.ActionDescriptor;
41
import org.openconcerto.modules.extensionbuilder.meu.actions.ActionDescriptor;
42
import org.openconcerto.modules.extensionbuilder.table.AllTableListModel;
42
import org.openconcerto.modules.extensionbuilder.table.AllTableListModel;
43
import org.openconcerto.modules.extensionbuilder.table.ElementDescriptor;
43
import org.openconcerto.modules.extensionbuilder.table.ElementDescriptor;
44
import org.openconcerto.modules.extensionbuilder.table.FieldDescriptor;
44
import org.openconcerto.modules.extensionbuilder.table.FieldDescriptor;
45
import org.openconcerto.modules.extensionbuilder.table.TableDescritor;
45
import org.openconcerto.modules.extensionbuilder.table.TableDescritor;
46
import org.openconcerto.modules.extensionbuilder.translation.Translation;
46
import org.openconcerto.modules.extensionbuilder.translation.Translation;
47
import org.openconcerto.modules.extensionbuilder.translation.action.ActionTranslation;
47
import org.openconcerto.modules.extensionbuilder.translation.action.ActionTranslation;
48
import org.openconcerto.modules.extensionbuilder.translation.field.FieldTranslation;
48
import org.openconcerto.modules.extensionbuilder.translation.field.FieldTranslation;
49
import org.openconcerto.modules.extensionbuilder.translation.field.TableTranslation;
49
import org.openconcerto.modules.extensionbuilder.translation.field.TableTranslation;
50
import org.openconcerto.modules.extensionbuilder.translation.menu.MenuTranslation;
50
import org.openconcerto.modules.extensionbuilder.translation.menu.MenuTranslation;
51
import org.openconcerto.sql.element.GroupSQLComponent;
51
import org.openconcerto.sql.element.GroupSQLComponent;
52
import org.openconcerto.sql.element.SQLElement;
52
import org.openconcerto.sql.element.SQLElement;
53
import org.openconcerto.sql.model.DBRoot;
53
import org.openconcerto.sql.model.DBRoot;
54
import org.openconcerto.sql.model.FieldPath;
54
import org.openconcerto.sql.model.FieldPath;
55
import org.openconcerto.sql.model.SQLBase;
55
import org.openconcerto.sql.model.SQLBase;
56
import org.openconcerto.sql.model.SQLField;
56
import org.openconcerto.sql.model.SQLField;
57
import org.openconcerto.sql.model.SQLName;
57
import org.openconcerto.sql.model.SQLName;
58
import org.openconcerto.sql.model.SQLRowAccessor;
58
import org.openconcerto.sql.model.SQLRowAccessor;
59
import org.openconcerto.sql.model.SQLRowValues;
59
import org.openconcerto.sql.model.SQLRowValues;
60
import org.openconcerto.sql.model.SQLTable;
60
import org.openconcerto.sql.model.SQLTable;
61
import org.openconcerto.sql.model.graph.PathBuilder;
61
import org.openconcerto.sql.model.graph.PathBuilder;
62
import org.openconcerto.sql.request.ListSQLRequest;
62
import org.openconcerto.sql.request.ListSQLRequest;
63
import org.openconcerto.sql.request.RowItemDesc;
63
import org.openconcerto.sql.request.RowItemDesc;
-
 
64
import org.openconcerto.sql.request.SQLFieldTranslator;
64
import org.openconcerto.sql.utils.AlterTable;
65
import org.openconcerto.sql.utils.AlterTable;
65
import org.openconcerto.sql.utils.ChangeTable;
66
import org.openconcerto.sql.utils.ChangeTable;
66
import org.openconcerto.sql.utils.SQLCreateTable;
67
import org.openconcerto.sql.utils.SQLCreateTable;
67
import org.openconcerto.sql.view.EditFrame;
68
import org.openconcerto.sql.view.EditFrame;
68
import org.openconcerto.sql.view.EditPanel;
69
import org.openconcerto.sql.view.EditPanel;
69
import org.openconcerto.sql.view.EditPanel.EditMode;
70
import org.openconcerto.sql.view.EditPanel.EditMode;
70
import org.openconcerto.sql.view.IListFrame;
71
import org.openconcerto.sql.view.IListFrame;
71
import org.openconcerto.sql.view.ListeAddPanel;
72
import org.openconcerto.sql.view.ListeAddPanel;
72
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
73
import org.openconcerto.sql.view.list.BaseSQLTableModelColumn;
73
import org.openconcerto.sql.view.list.IListe;
74
import org.openconcerto.sql.view.list.IListe;
74
import org.openconcerto.sql.view.list.IListeAction;
75
import org.openconcerto.sql.view.list.IListeAction;
75
import org.openconcerto.sql.view.list.RowAction;
76
import org.openconcerto.sql.view.list.RowAction;
76
import org.openconcerto.sql.view.list.SQLTableModelColumn;
77
import org.openconcerto.sql.view.list.SQLTableModelColumn;
77
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
78
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
78
import org.openconcerto.ui.group.Group;
79
import org.openconcerto.ui.group.Group;
79
import org.openconcerto.ui.group.Item;
80
import org.openconcerto.ui.group.Item;
80
import org.openconcerto.ui.group.LayoutHints;
81
import org.openconcerto.ui.group.LayoutHints;
81
import org.openconcerto.ui.group.modifier.AddGroupModifier;
82
import org.openconcerto.ui.group.modifier.AddGroupModifier;
82
import org.openconcerto.ui.group.modifier.AddItemModifier;
83
import org.openconcerto.ui.group.modifier.AddItemModifier;
83
import org.openconcerto.ui.group.modifier.MoveToGroupModifier;
84
import org.openconcerto.ui.group.modifier.MoveToGroupModifier;
84
import org.openconcerto.utils.CollectionUtils;
85
import org.openconcerto.utils.CollectionUtils;
-
 
86
import org.openconcerto.utils.i18n.Grammar;
-
 
87
import org.openconcerto.utils.i18n.Grammar_fr;
-
 
88
import org.openconcerto.utils.i18n.NounClass;
-
 
89
import org.openconcerto.utils.i18n.Phrase;
85
import org.openconcerto.utils.i18n.TranslationManager;
90
import org.openconcerto.utils.i18n.TranslationManager;
86
 
91
 
87
public class Extension {
92
public class Extension {
88
 
93
 
89
    // Descriptors of the extension
94
    // Descriptors of the extension
90
    private List<ElementDescriptor> elementList = new ArrayList<ElementDescriptor>();
95
    private List<ElementDescriptor> elementList = new ArrayList<ElementDescriptor>();
91
    private List<TableDescritor> createTableList = new ArrayList<TableDescritor>();
96
    private List<TableDescritor> createTableList = new ArrayList<TableDescritor>();
92
    private List<TableDescritor> modifyTableList = new ArrayList<TableDescritor>();
97
    private List<TableDescritor> modifyTableList = new ArrayList<TableDescritor>();
93
    private List<ListDescriptor> createListList = new ArrayList<ListDescriptor>();
98
    private List<ListDescriptor> createListList = new ArrayList<ListDescriptor>();
94
    private List<TableTranslation> tableTranslations = new ArrayList<TableTranslation>();
99
    private List<TableTranslation> tableTranslations = new ArrayList<TableTranslation>();
95
    private List<FieldTranslation> fieldTranslations = new ArrayList<FieldTranslation>();
100
    private List<FieldTranslation> fieldTranslations = new ArrayList<FieldTranslation>();
96
    private List<MenuTranslation> menuTranslations = new ArrayList<MenuTranslation>();
101
    private List<MenuTranslation> menuTranslations = new ArrayList<MenuTranslation>();
97
    private List<ActionTranslation> actionTranslations = new ArrayList<ActionTranslation>();
102
    private List<ActionTranslation> actionTranslations = new ArrayList<ActionTranslation>();
98
    private List<ComponentDescritor> createComponentList = new ArrayList<ComponentDescritor>();
103
    private List<ComponentDescritor> createComponentList = new ArrayList<ComponentDescritor>();
99
    private List<ComponentDescritor> modifyComponentList = new ArrayList<ComponentDescritor>();
104
    private List<ComponentDescritor> modifyComponentList = new ArrayList<ComponentDescritor>();
100
    private List<MenuDescriptor> createMenuList = new ArrayList<MenuDescriptor>();
105
    private List<MenuDescriptor> createMenuList = new ArrayList<MenuDescriptor>();
101
    private List<MenuDescriptor> removeMenuList = new ArrayList<MenuDescriptor>();
106
    private List<MenuDescriptor> removeMenuList = new ArrayList<MenuDescriptor>();
102
    private List<ActionDescriptor> createActionList = new ArrayList<ActionDescriptor>();
107
    private List<ActionDescriptor> createActionList = new ArrayList<ActionDescriptor>();
103
 
108
 
104
    // Listeners
109
    // Listeners
105
    private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
110
    private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
106
 
111
 
107
    private String name;
112
    private String name;
108
    private boolean notSaved;
113
    private boolean notSaved;
109
    private boolean autoStart;
114
    private boolean autoStart;
110
    private boolean isStarted;
115
    private boolean isStarted;
111
 
116
 
112
    public Extension(String name) {
117
    public Extension(String name) {
113
        this.name = name;
118
        this.name = name;
114
        this.notSaved = true;
119
        this.notSaved = true;
115
    }
120
    }
116
 
121
 
117
    public void clearAll() {
122
    public void clearAll() {
118
        elementList.clear();
123
        elementList.clear();
119
        createTableList.clear();
124
        createTableList.clear();
120
        modifyTableList.clear();
125
        modifyTableList.clear();
121
        createListList.clear();
126
        createListList.clear();
122
        tableTranslations.clear();
127
        tableTranslations.clear();
123
        fieldTranslations.clear();
128
        fieldTranslations.clear();
124
        menuTranslations.clear();
129
        menuTranslations.clear();
125
        actionTranslations.clear();
130
        actionTranslations.clear();
126
        createComponentList.clear();
131
        createComponentList.clear();
127
        modifyComponentList.clear();
132
        modifyComponentList.clear();
128
        createMenuList.clear();
133
        createMenuList.clear();
129
        removeMenuList.clear();
134
        removeMenuList.clear();
130
        createActionList.clear();
135
        createActionList.clear();
131
        listeners.clear();
136
        listeners.clear();
132
 
137
 
133
        notSaved = true;
138
        notSaved = true;
134
        autoStart = false;
139
        autoStart = false;
135
        isStarted = false;
140
        isStarted = false;
136
 
141
 
137
    }
142
    }
138
 
143
 
139
    boolean isStarted() {
144
    boolean isStarted() {
140
        return this.isStarted;
145
        return this.isStarted;
141
    }
146
    }
142
 
147
 
143
    public boolean isAutoStart() {
148
    public boolean isAutoStart() {
144
        return autoStart;
149
        return autoStart;
145
    }
150
    }
146
 
151
 
147
    public void start(DBRoot root, boolean inModuleStart) throws SQLException {
152
    public void start(DBRoot root, boolean inModuleStart) throws SQLException {
148
 
153
 
149
        // Ensure that database is configured
154
        // Ensure that database is configured
150
        boolean databaseOk = setupDatabase(root);
155
        boolean databaseOk = setupDatabase(root);
151
        if (!databaseOk) {
156
        if (!databaseOk) {
152
            Log.get().severe("Extension " + this.getName() + " not started due to database error");
157
            Log.get().severe("Extension " + this.getName() + " not started due to database error");
153
            return;
158
            return;
154
        }
159
        }
155
        // Register translations
160
        // Register translations
156
        registerTranslations();
161
        registerTranslations(root);
157
        // Create menus
162
        // Create menus
158
        if (!inModuleStart) {
163
        if (!inModuleStart) {
159
            final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
164
            final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
160
            registerMenuActions(copy);
165
            registerMenuActions(copy);
161
            MenuManager.getInstance().setMenuAndActions(copy);
166
            MenuManager.getInstance().setMenuAndActions(copy);
162
        }
167
        }
163
        Log.get().info("Extension " + this.getName() + " started");
168
        Log.get().info("Extension " + this.getName() + " started");
164
        this.isStarted = true;
169
        this.isStarted = true;
165
        this.autoStart = true;
170
        this.autoStart = true;
166
        fireChanged();
171
        fireChanged();
167
    }
172
    }
168
 
173
 
169
    private void registerTranslations() {
174
    private void registerTranslations(DBRoot root) {
170
        final String locale = Locale.getDefault().toString();
175
        final String locale = Locale.getDefault().toString();
171
        for (MenuTranslation mTranslation : this.menuTranslations) {
176
        for (MenuTranslation mTranslation : this.menuTranslations) {
172
            if (locale.equals(mTranslation.getLocale())) {
177
            if (locale.equals(mTranslation.getLocale())) {
173
                TranslationManager.getInstance().setTranslationForMenu(mTranslation.getId(), mTranslation.getLabel());
178
                TranslationManager.getInstance().setTranslationForMenu(mTranslation.getId(), mTranslation.getLabel());
174
            }
179
            }
175
        }
180
        }
176
        for (ActionTranslation mTranslation : this.actionTranslations) {
181
        for (ActionTranslation mTranslation : this.actionTranslations) {
177
            if (locale.equals(mTranslation.getLocale())) {
182
            if (locale.equals(mTranslation.getLocale())) {
178
                TranslationManager.getInstance().setTranslationForAction(mTranslation.getId(), mTranslation.getLabel());
183
                TranslationManager.getInstance().setTranslationForAction(mTranslation.getId(), mTranslation.getLabel());
179
            }
184
            }
180
        }
185
        }
181
        for (FieldTranslation mTranslation : this.fieldTranslations) {
186
        for (FieldTranslation mTranslation : this.fieldTranslations) {
182
            if (locale.equals(mTranslation.getLocale())) {
187
            if (locale.equals(mTranslation.getLocale())) {
183
                TranslationManager.getInstance().setTranslationForItem(mTranslation.getTableName() + "." + mTranslation.getFieldName(), mTranslation.getLabel());
188
                TranslationManager.getInstance().setTranslationForItem(mTranslation.getTableName() + "." + mTranslation.getFieldName(), mTranslation.getLabel());
-
 
189
                // TODO: getDescFor SQLFieldTranslator
184
            }
190
            }
185
        }
191
        }
186
        for (TableTranslation mTranslation : this.tableTranslations) {
192
        for (TableTranslation mTranslation : this.tableTranslations) {
187
            if (locale.equals(mTranslation.getLocale())) {
193
            if (locale.equals(mTranslation.getLocale())) {
188
                // FIXME voir avec Sylvain
194
                // FIXME voir avec Sylvain, impossible pour l'instant
-
 
195
                SQLElement e = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(mTranslation.getTableName());
-
 
196
                e.setDefaultName(createPhrase(mTranslation.getSingular(), mTranslation.getPlural()));
189
            }
197
            }
190
        }
198
        }
191
    }
199
    }
192
 
200
 
-
 
201
    private static Phrase createPhrase(String singular, String plural) {
-
 
202
        final NounClass nounClass;
-
 
203
        final String base;
-
 
204
        if (singular.startsWith("une ")) {
-
 
205
            nounClass = NounClass.FEMININE;
-
 
206
            base = singular.substring(4);
-
 
207
        } else if (singular.startsWith("un ")) {
-
 
208
            nounClass = NounClass.MASCULINE;
-
 
209
            base = singular.substring(3);
-
 
210
        } else {
-
 
211
            nounClass = null;
-
 
212
            base = singular;
-
 
213
        }
-
 
214
        final Phrase res = new Phrase(Grammar_fr.getInstance(), base, nounClass);
-
 
215
        if (nounClass != null)
-
 
216
            res.putVariant(Grammar.INDEFINITE_ARTICLE_SINGULAR, singular);
-
 
217
        res.putVariant(Grammar.PLURAL, plural);
-
 
218
        return res;
-
 
219
    }
-
 
220
 
193
    private void registerMenuActions(MenuAndActions menuAndActions) {
221
    private void registerMenuActions(MenuAndActions menuAndActions) {
194
        // register actions
222
        // register actions
195
        for (final MenuDescriptor element : getCreateMenuList()) {
223
        for (final MenuDescriptor menuDescriptor : getCreateMenuList()) {
196
            if (element.getType().equals(MenuDescriptor.CREATE)) {
224
            if (menuDescriptor.getType().equals(MenuDescriptor.CREATE)) {
197
                Log.get().info("Registering action for menu creation id:'" + element.getId() + "'");
225
                Log.get().info("Registering action for menu creation id:'" + menuDescriptor.getId() + "'");
198
 
226
 
199
                menuAndActions.putAction(new CreateFrameAbstractAction() {
227
                menuAndActions.putAction(new CreateFrameAbstractAction() {
200
 
228
 
201
                    @Override
229
                    @Override
202
                    public JFrame createFrame() {
230
                    public JFrame createFrame() {
203
 
231
 
204
                        JFrame editFrame = new JFrame();
232
                        JFrame editFrame = new JFrame();
205
                        String componentId = element.getComponentId();
233
                        String componentId = menuDescriptor.getComponentId();
206
                        if (componentId == null) {
234
                        if (componentId == null) {
207
                            throw new IllegalStateException("No ComponentId for menu " + element.getId());
235
                            throw new IllegalStateException("No ComponentId for menu " + menuDescriptor.getId());
208
                        }
236
                        }
209
                        ComponentDescritor n = getCreateComponentFromId(componentId);
237
                        ComponentDescritor n = getCreateComponentFromId(componentId);
210
                        if (n == null) {
238
                        if (n == null) {
211
                            throw new IllegalStateException("No ComponentDescritor for " + componentId);
239
                            throw new IllegalStateException("No ComponentDescritor for " + componentId);
212
                        }
240
                        }
213
                        final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
241
                        final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(n.getTable());
214
                        if (t == null) {
242
                        if (t == null) {
215
                            throw new IllegalStateException("No table  " + n.getTable());
243
                            throw new IllegalStateException("No table  " + n.getTable());
216
                        }
244
                        }
217
                        final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
245
                        final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
218
 
246
 
219
                        final GroupSQLComponent gComponent = new GroupSQLComponent(element, n.getGroup()) {
247
                        final GroupSQLComponent gComponent = new ExtensionGroupSQLComponent(element, n.getGroup());
220
                            public RowItemDesc getRIVDescForId(final String id) {
-
 
221
                                return super.getRIVDescForId(t.getName() + "." + id);
-
 
222
                            }
-
 
223
 
-
 
224
                        };
-
 
225
                        editFrame.setTitle(EditFrame.getCreateMessage(element));
248
                        editFrame.setTitle(EditFrame.getCreateMessage(element));
226
                        editFrame.setContentPane(new EditPanel(gComponent, EditMode.CREATION));
249
                        editFrame.setContentPane(new EditPanel(gComponent, EditMode.CREATION));
227
                        editFrame.pack();
250
                        editFrame.pack();
228
 
-
 
229
                        return editFrame;
251
                        return editFrame;
230
 
252
 
231
                    }
253
                    }
232
                }, element.getId(), true);
254
                }, menuDescriptor.getId(), true);
233
 
255
 
234
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
256
            } else if (menuDescriptor.getType().equals(MenuDescriptor.LIST)) {
235
                if (element.getListId() != null) {
257
                if (menuDescriptor.getListId() != null) {
236
                    menuAndActions.putAction(new CreateFrameAbstractAction() {
258
                    menuAndActions.putAction(new CreateFrameAbstractAction() {
237
 
259
 
238
                        @Override
260
                        @Override
239
                        public JFrame createFrame() {
261
                        public JFrame createFrame() {
240
                            final String componentId = element.getListId();
262
                            final String componentId = menuDescriptor.getListId();
241
                            if (componentId == null) {
263
                            if (componentId == null) {
242
                                throw new IllegalStateException("null ListId for MenuDescriptor " + element.getId());
264
                                throw new IllegalStateException("null ListId for MenuDescriptor " + menuDescriptor.getId());
243
                            }
265
                            }
244
                            final ListDescriptor listDesc = getCreateListFromId(componentId);
266
                            final ListDescriptor listDesc = getCreateListFromId(componentId);
245
                            if (listDesc == null) {
267
                            if (listDesc == null) {
246
                                throw new IllegalStateException("No ListDescriptor  " + componentId);
268
                                throw new IllegalStateException("No ListDescriptor  " + componentId);
247
                            }
269
                            }
248
                            final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(listDesc.getMainTable());
270
                            final SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(listDesc.getMainTable());
249
                            if (t == null) {
271
                            if (t == null) {
250
                                throw new IllegalStateException("No table  " + listDesc.getMainTable());
272
                                throw new IllegalStateException("No table  " + listDesc.getMainTable());
251
                            }
273
                            }
252
                            final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
274
                            final SQLElement element = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElement(t);
253
                            final ListSQLRequest req = new ListSQLRequest(element.getTable(), new ArrayList<String>(0));
275
                            final ListSQLRequest req = new ListSQLRequest(element.getTable(), new ArrayList<String>(0));
-
 
276
                            SQLTableModelSourceOnline source = createSource(element, req, listDesc);
-
 
277
 
-
 
278
                            final IListe list = new IListe(source);
-
 
279
                            final IListFrame editFrame = new IListFrame(new ListeAddPanel(element, list));
-
 
280
                            editFrame.pack();
-
 
281
                            return editFrame;
-
 
282
 
-
 
283
                        }
-
 
284
                    }, menuDescriptor.getId(), true);
-
 
285
                }
-
 
286
            } else if (menuDescriptor.getType().equals(MenuDescriptor.LIST)) {
-
 
287
                // No action to register
-
 
288
            } else {
-
 
289
                Log.get().warning("unknown type " + menuDescriptor.getType());
-
 
290
            }
-
 
291
        }
-
 
292
 
-
 
293
        // System.err.println("****" + MenuManager.getInstance().getActionForId("test1"));
-
 
294
        //
-
 
295
        // final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
-
 
296
        // // create group
-
 
297
        // final Group group = copy.getGroup();
-
 
298
        initMenuGroup(menuAndActions.getGroup());
-
 
299
        // MenuManager.getInstance().setMenuAndActions(copy);
-
 
300
        // System.err.println("*******" + MenuManager.getInstance().getActionForId("test1"));
-
 
301
    }
-
 
302
 
-
 
303
    public SQLTableModelSourceOnline createSource(SQLElement element, ListSQLRequest req, ListDescriptor listDesc) {
254
                            final SQLTableModelSourceOnline source = new SQLTableModelSourceOnline(req, element);
304
        final SQLTableModelSourceOnline source = new SQLTableModelSourceOnline(req, element);
255
                            final List<SQLTableModelColumn> cols = new ArrayList<SQLTableModelColumn>();
305
        final List<SQLTableModelColumn> cols = new ArrayList<>();
256
                            for (ColumnDescriptor cDesc : listDesc.getColumns()) {
306
        for (ColumnDescriptor cDesc : listDesc.getColumns()) {
257
                                final String fieldspath = cDesc.getFieldsPaths();
307
            final String fieldspath = cDesc.getFieldsPaths();
258
                                final String[] paths = fieldspath.split(",");
308
            final String[] paths = fieldspath.split(",");
259
                                final Set<FieldPath> fps = new LinkedHashSet<FieldPath>();
309
            final Set<FieldPath> fps = new LinkedHashSet<FieldPath>();
260
                                for (int i = 0; i < paths.length; i++) {
310
            for (int i = 0; i < paths.length; i++) {
261
                                    // LOCAL, id_batiment.id_site.nom
311
                // LOCAL, id_batiment.id_site.nom
262
                                    final SQLName name = SQLName.parse(paths[i].trim());
312
                final SQLName name = SQLName.parse(paths[i].trim());
263
 
313
 
264
                                    final PathBuilder p = new PathBuilder(element.getTable());
314
                final PathBuilder p = new PathBuilder(element.getTable());
265
                                    final int stop = name.getItemCount() - 1;
315
                final int stop = name.getItemCount() - 1;
266
                                    for (int j = 0; j < stop; j++) {
316
                for (int j = 0; j < stop; j++) {
267
                                        String it = name.getItem(j);
317
                    String it = name.getItem(j);
268
                                        p.addForeignField(it);
318
                    p.addForeignField(it);
269
                                    }
319
                }
270
                                    final FieldPath fp = new FieldPath(p.build(), name.getName());
320
                final FieldPath fp = new FieldPath(p.build(), name.getName());
271
                                    fps.add(fp);
321
                fps.add(fp);
272
 
322
 
273
                                }
323
            }
-
 
324
            String columnName = cDesc.getId();
-
 
325
            if (!columnName.contains(".")) {
-
 
326
                columnName = element.getTable().getTable().getName() + "." + columnName;
-
 
327
            }
-
 
328
            String tranlatedName = TranslationManager.getInstance().getTranslationForItem(columnName);
-
 
329
            if (tranlatedName == null) {
-
 
330
                tranlatedName = SQLFieldTranslator.getDefaultDesc(fps.iterator().next().getField()).getTitleLabel();
-
 
331
            }
-
 
332
            if (tranlatedName == null) {
-
 
333
                tranlatedName = cDesc.getId();
-
 
334
            }
-
 
335
 
274
                                cols.add(new BaseSQLTableModelColumn(cDesc.getId(), String.class) {
336
            cols.add(new BaseSQLTableModelColumn(tranlatedName, String.class) {
275
 
337
 
276
                                    @Override
338
                @Override
277
                                    protected Object show_(SQLRowAccessor r) {
339
                protected Object show_(SQLRowAccessor r) {
278
                                        final List<String> l = new ArrayList<String>();
340
                    final List<String> l = new ArrayList<>();
279
                                        for (final FieldPath fp : fps) {
341
                    for (final FieldPath fp : fps) {
280
                                            final String string = fp.getString((SQLRowValues) r);
342
                        final String string = fp.getString((SQLRowValues) r);
281
                                            if (string != null)
343
                        if (string != null)
282
                                                l.add(string);
344
                            l.add(string);
283
                                        }
345
                    }
284
                                        return CollectionUtils.join(l, " ");
346
                    return CollectionUtils.join(l, " ");
285
                                    }
347
                }
286
 
348
 
287
                                    @Override
349
                @Override
288
                                    public Set<FieldPath> getPaths() {
350
                public Set<FieldPath> getPaths() {
289
                                        return fps;
351
                    return fps;
290
                                    }
352
                }
291
                                });
353
            });
292
 
354
 
293
                            }
355
        }
294
 
356
 
295
                            source.getColumns().addAll(cols);
357
        source.getColumns().addAll(cols);
296
 
-
 
297
                            final IListe list = new IListe(source);
-
 
298
                            final IListFrame editFrame = new IListFrame(new ListeAddPanel(element, list));
-
 
299
                            editFrame.pack();
-
 
300
                            return editFrame;
-
 
301
 
-
 
302
                        }
-
 
303
                    }, element.getId(), true);
-
 
304
                }
-
 
305
            } else if (element.getType().equals(MenuDescriptor.LIST)) {
-
 
306
                // No action to register
-
 
307
            } else {
358
        return source;
308
                Log.get().warning("unknown type " + element.getType());
-
 
309
            }
-
 
310
        }
-
 
311
 
-
 
312
        // System.err.println("****" + MenuManager.getInstance().getActionForId("test1"));
-
 
313
        //
-
 
314
        // final MenuAndActions copy = MenuManager.getInstance().copyMenuAndActions();
-
 
315
        // // create group
-
 
316
        // final Group group = copy.getGroup();
-
 
317
        initMenuGroup(menuAndActions.getGroup());
-
 
318
        // MenuManager.getInstance().setMenuAndActions(copy);
-
 
319
        // System.err.println("*******" + MenuManager.getInstance().getActionForId("test1"));
-
 
320
    }
359
    }
321
 
360
 
322
    public void initMenuGroup(final Group group) {
361
    public void initMenuGroup(final Group group) {
323
        for (MenuDescriptor element : getCreateMenuList()) {
362
        for (MenuDescriptor element : getCreateMenuList()) {
324
            if (element.getType().equals(MenuDescriptor.GROUP)) {
363
            if (element.getType().equals(MenuDescriptor.GROUP)) {
325
                AddGroupModifier add = new AddGroupModifier(element.getId());
364
                AddGroupModifier add = new AddGroupModifier(element.getId());
326
                if (group.getDescFromID(element.getId()) == null) {
365
                if (group.getDescFromID(element.getId()) == null) {
327
                    // only add if not exists
366
                    // only add if not exists
328
                    add.applyOn(group);
367
                    add.applyOn(group);
329
                }
368
                }
330
                String menuId = element.getInsertInMenu();
369
                String menuId = element.getInsertInMenu();
331
                Group dest = (Group) group.getDescFromID(menuId);
370
                Group dest = (Group) group.getDescFromID(menuId);
332
                if (dest != null) {
371
                if (dest != null) {
333
                    MoveToGroupModifier mode = new MoveToGroupModifier(element.getId(), dest);
372
                    MoveToGroupModifier mode = new MoveToGroupModifier(element.getId(), dest);
334
                    mode.applyOn(group);
373
                    mode.applyOn(group);
335
                } else {
374
                } else {
336
                    Log.get().severe("No group " + menuId + " found to move item " + element.getId());
375
                    Log.get().severe("No group " + menuId + " found to move item " + element.getId());
337
                }
376
                }
338
            }
377
            }
339
        }
378
        }
340
        // create items
379
        // create items
341
        for (MenuDescriptor element : getCreateMenuList()) {
380
        for (MenuDescriptor element : getCreateMenuList()) {
342
            if (!element.getType().equals(MenuDescriptor.GROUP)) {
381
            if (!element.getType().equals(MenuDescriptor.GROUP)) {
343
                AddItemModifier add = new AddItemModifier(element.getId());
382
                AddItemModifier add = new AddItemModifier(element.getId());
344
                if (group.getDescFromID(element.getId()) == null) {
383
                if (group.getDescFromID(element.getId()) == null) {
345
                    // only add if not exists
384
                    // only add if not exists
346
                    add.applyOn(group);
385
                    add.applyOn(group);
347
                }
386
                }
348
 
387
 
349
                String menuId = element.getInsertInMenu();
388
                String menuId = element.getInsertInMenu();
350
                Group dest = (Group) group.getDescFromID(menuId);
389
                Group dest = (Group) group.getDescFromID(menuId);
351
                if (dest != null) {
390
                if (dest != null) {
352
                    MoveToGroupModifier mode = new MoveToGroupModifier(element.getId(), dest);
391
                    MoveToGroupModifier mode = new MoveToGroupModifier(element.getId(), dest);
353
                    mode.applyOn(group);
392
                    mode.applyOn(group);
354
                } else {
393
                } else {
355
                    Log.get().severe("No group " + menuId + " found to move group " + element.getId());
394
                    Log.get().severe("No group " + menuId + " found to move group " + element.getId());
356
                }
395
                }
357
            }
396
            }
358
        }
397
        }
359
        for (MenuDescriptor element : getRemoveMenuList()) {
398
        for (MenuDescriptor element : getRemoveMenuList()) {
360
            String eId = element.getId();
399
            String eId = element.getId();
361
            Item item = group.getDescFromID(eId);
400
            Item item = group.getDescFromID(eId);
362
            if (item != null) {
401
            if (item != null) {
363
                item.setLocalHint(item.getLocalHint().getBuilder().setVisible(false).build());
402
                item.setLocalHint(item.getLocalHint().getBuilder().setVisible(false).build());
364
            } else {
403
            } else {
365
                Log.get().severe("No Item " + eId + " found in group " + group.getId());
404
                Log.get().severe("No Item " + eId + " found in group " + group.getId());
366
            }
405
            }
367
        }
406
        }
368
        System.err.println("Extension.initMenuGroup()" + group.printTree());
407
        System.err.println("Extension.initMenuGroup()" + group.printTree());
369
 
408
 
370
    }
409
    }
371
 
410
 
372
    public ComponentDescritor getCreateComponentFromId(String id) {
411
    public ComponentDescritor getCreateComponentFromId(String id) {
373
        for (ComponentDescritor menuDescriptor : this.createComponentList) {
412
        for (ComponentDescritor menuDescriptor : this.createComponentList) {
374
            if (menuDescriptor.getId().equals(id)) {
413
            if (menuDescriptor.getId().equals(id)) {
375
                return menuDescriptor;
414
                return menuDescriptor;
376
            }
415
            }
377
        }
416
        }
378
        return null;
417
        return null;
379
    }
418
    }
380
 
419
 
381
    private boolean setupDatabase(DBRoot root) throws SQLException {
420
    private boolean setupDatabase(DBRoot root) throws SQLException {
382
        List<ChangeTable<?>> changesToApply = new ArrayList<ChangeTable<?>>();
421
        List<ChangeTable<?>> changesToApply = new ArrayList<ChangeTable<?>>();
383
        List<SQLCreateTable> createToApply = new ArrayList<SQLCreateTable>();
422
        List<SQLCreateTable> createToApply = new ArrayList<SQLCreateTable>();
384
        // Create fields and tables if needed
423
        // Create fields and tables if needed
385
        final List<TableDescritor> t = new ArrayList<TableDescritor>();
424
        final List<TableDescritor> t = new ArrayList<TableDescritor>();
386
        t.addAll(this.createTableList);
425
        t.addAll(this.createTableList);
387
        t.addAll(this.modifyTableList);
426
        t.addAll(this.modifyTableList);
388
        Set<String> tableNames = new HashSet<String>();
427
        Set<String> tableNames = new HashSet<String>();
389
        for (TableDescritor tDesc : t) {
428
        for (TableDescritor tDesc : t) {
390
            String tableName = tDesc.getName();
429
            String tableName = tDesc.getName();
391
            tableNames.add(tableName);
430
            tableNames.add(tableName);
392
            final SQLTable table = root.getTable(tableName);
431
            final SQLTable table = root.getTable(tableName);
393
            final ChangeTable<?> createTable;
432
            final ChangeTable<?> createTable;
394
            if (table == null) {
433
            if (table == null) {
395
                createTable = new SQLCreateTable(root, tableName);
434
                createTable = new SQLCreateTable(root, tableName);
396
                createToApply.add((SQLCreateTable) createTable);
435
                createToApply.add((SQLCreateTable) createTable);
397
            } else {
436
            } else {
398
                createTable = new AlterTable(table);
437
                createTable = new AlterTable(table);
399
 
438
 
400
            }
439
            }
401
            // fields creation
440
            // fields creation
402
            boolean mustAdd = false;
441
            boolean mustAdd = false;
403
            for (FieldDescriptor fDesc : tDesc.getFields()) {
442
            for (FieldDescriptor fDesc : tDesc.getFields()) {
404
                final SQLField f = (table == null) ? null : table.getFieldRaw(fDesc.getName());
443
                final SQLField f = (table == null) ? null : table.getFieldRaw(fDesc.getName());
405
                if (f == null) {
444
                if (f == null) {
406
                    final String type = fDesc.getType();
445
                    final String type = fDesc.getType();
407
                    if (type.equals(FieldDescriptor.TYPE_STRING)) {
446
                    if (type.equals(FieldDescriptor.TYPE_STRING)) {
408
                        int l = 256;
447
                        int l = 256;
409
                        try {
448
                        try {
410
                            l = Integer.parseInt(fDesc.getLength());
449
                            l = Integer.parseInt(fDesc.getLength());
411
                        } catch (Exception e) {
450
                        } catch (Exception e) {
412
                            Log.get().log(Level.WARNING, "Extension: unable to parse length: " + fDesc.getLength(), e);
451
                            Log.get().log(Level.WARNING, "Extension: unable to parse length: " + fDesc.getLength(), e);
413
                        }
452
                        }
414
                        createTable.addVarCharColumn(fDesc.getName(), l);
453
                        createTable.addVarCharColumn(fDesc.getName(), l);
415
                    } else if (type.equals(FieldDescriptor.TYPE_INTEGER)) {
454
                    } else if (type.equals(FieldDescriptor.TYPE_INTEGER)) {
416
                        int defaultVal = 0;
455
                        int defaultVal = 0;
417
                        try {
456
                        try {
418
                            defaultVal = Integer.parseInt(fDesc.getDefaultValue());
457
                            defaultVal = Integer.parseInt(fDesc.getDefaultValue());
419
                        } catch (Exception e) {
458
                        } catch (Exception e) {
420
                            Log.get().log(Level.WARNING, "Extension: unable to parse default integer value : " + fDesc.getDefaultValue(), e);
459
                            Log.get().log(Level.WARNING, "Extension: unable to parse default integer value : " + fDesc.getDefaultValue(), e);
421
                        }
460
                        }
422
                        createTable.addIntegerColumn(fDesc.getName(), defaultVal);
461
                        createTable.addIntegerColumn(fDesc.getName(), defaultVal);
423
                    } else if (type.equals(FieldDescriptor.TYPE_DECIMAL)) {
462
                    } else if (type.equals(FieldDescriptor.TYPE_DECIMAL)) {
424
                        BigDecimal defaultVal = BigDecimal.ZERO;
463
                        BigDecimal defaultVal = BigDecimal.ZERO;
425
                        try {
464
                        try {
426
                            defaultVal = new BigDecimal(fDesc.getDefaultValue());
465
                            defaultVal = new BigDecimal(fDesc.getDefaultValue());
427
                        } catch (Exception e) {
466
                        } catch (Exception e) {
428
                            Log.get().log(Level.WARNING, "Extension: unable to parse default bigdecimal value : " + fDesc.getDefaultValue(), e);
467
                            Log.get().log(Level.WARNING, "Extension: unable to parse default bigdecimal value : " + fDesc.getDefaultValue(), e);
429
                        }
468
                        }
430
                        createTable.addNumberColumn(fDesc.getName(), BigDecimal.class, defaultVal, false);
469
                        createTable.addNumberColumn(fDesc.getName(), BigDecimal.class, defaultVal, false);
431
                    } else if (type.equals(FieldDescriptor.TYPE_BOOLEAN)) {
470
                    } else if (type.equals(FieldDescriptor.TYPE_BOOLEAN)) {
432
                        String defaultValue = "false";
471
                        String defaultValue = "false";
433
                        if (fDesc.getDefaultValue() != null && fDesc.getDefaultValue().equals("true")) {
472
                        if (fDesc.getDefaultValue() != null && fDesc.getDefaultValue().equals("true")) {
434
                            defaultValue = "true";
473
                            defaultValue = "true";
435
                        }
474
                        }
436
                        createTable.addColumn(fDesc.getName(), "boolean", defaultValue, false);
475
                        createTable.addColumn(fDesc.getName(), "boolean", defaultValue, false);
437
                    } else if (type.equals(FieldDescriptor.TYPE_DATE)) {
476
                    } else if (type.equals(FieldDescriptor.TYPE_DATE)) {
438
                        createTable.addColumn(fDesc.getName(), "date");
477
                        createTable.addColumn(fDesc.getName(), "date");
439
                    } else if (type.equals(FieldDescriptor.TYPE_TIME)) {
478
                    } else if (type.equals(FieldDescriptor.TYPE_TIME)) {
440
                        createTable.addColumn(fDesc.getName(), "time");
479
                        createTable.addColumn(fDesc.getName(), "time");
441
                    } else if (type.equals(FieldDescriptor.TYPE_DATETIME)) {
480
                    } else if (type.equals(FieldDescriptor.TYPE_DATETIME)) {
442
                        createTable.addDateAndTimeColumn(fDesc.getName());
481
                        createTable.addDateAndTimeColumn(fDesc.getName());
443
                    } else if (type.equals(FieldDescriptor.TYPE_REF)) {
482
                    } else if (type.equals(FieldDescriptor.TYPE_REF)) {
444
                        // created later
483
                        // created later
445
                        mustAdd = false;
484
                        mustAdd = false;
446
                    }
485
                    }
447
                    mustAdd = true;
486
                    mustAdd = true;
448
                } else {
487
                } else {
449
                    // Le champs existe, on ne fait rien
488
                    // Le champs existe, on ne fait rien
450
                    // checker les types
489
                    // checker les types
451
                }
490
                }
452
            }
491
            }
453
            if (mustAdd && !(createTable instanceof SQLCreateTable)) {
492
            if (mustAdd && !(createTable instanceof SQLCreateTable)) {
454
                changesToApply.add(createTable);
493
                changesToApply.add(createTable);
455
            }
494
            }
456
        }
495
        }
457
        // Let's do it
496
        // Let's do it
458
        // FIXME : if changesToApply create field that createToApply : bing
497
        // FIXME : if changesToApply create field that createToApply : bing
459
        if (!createToApply.isEmpty()) {
498
        if (!createToApply.isEmpty()) {
460
            root.createTables(createToApply);
499
            root.createTables(createToApply);
461
        }
500
        }
462
        if (!changesToApply.isEmpty()) {
501
        if (!changesToApply.isEmpty()) {
463
            for (String change : ChangeTable.cat(changesToApply)) {
502
            for (String change : ChangeTable.cat(changesToApply)) {
464
                root.getDBSystemRoot().getDataSource().execute(change);
503
                root.getDBSystemRoot().getDataSource().execute(change);
465
            }
504
            }
466
 
505
 
467
        }
506
        }
468
        // Refetch if needed
507
        // Refetch if needed
469
        if (!changesToApply.isEmpty() || !createToApply.isEmpty()) {
508
        if (!changesToApply.isEmpty() || !createToApply.isEmpty()) {
470
            root.getSchema().updateVersion();
509
            root.getSchema().updateVersion();
471
            root.refetch(tableNames);
510
            root.refetch(tableNames);
472
            Log.get().info("Fetching table changes (" + changesToApply.size() + " fields and " + createToApply.size() + " tables)");
511
            Log.get().info("Fetching table changes (" + changesToApply.size() + " fields and " + createToApply.size() + " tables)");
473
        }
512
        }
474
        // Compute foreign keys to create
513
        // Compute foreign keys to create
475
        changesToApply.clear();
514
        changesToApply.clear();
476
        for (TableDescritor tDesc : t) {
515
        for (TableDescritor tDesc : t) {
477
            final SQLTable table = root.getTable(tDesc.getName());
516
            final SQLTable table = root.getTable(tDesc.getName());
478
            for (FieldDescriptor fDesc : tDesc.getFields()) {
517
            for (FieldDescriptor fDesc : tDesc.getFields()) {
479
                final SQLField f = (table == null) ? null : table.getFieldRaw(fDesc.getName());
518
                final SQLField f = (table == null) ? null : table.getFieldRaw(fDesc.getName());
480
                if (f == null && fDesc.getType().equals(FieldDescriptor.TYPE_REF)) {
519
                if (f == null && fDesc.getType().equals(FieldDescriptor.TYPE_REF)) {
481
                    final String fTableName = fDesc.getForeignTable();
520
                    final String fTableName = fDesc.getForeignTable();
482
                    final SQLTable fTable = root.getTable(fTableName);
521
                    final SQLTable fTable = root.getTable(fTableName);
483
                    if (fTable != null) {
522
                    if (fTable != null) {
484
                        final AlterTable mTable = new AlterTable(table);
523
                        final AlterTable mTable = new AlterTable(table);
485
                        mTable.addForeignColumn(fDesc.getName(), fTable);
524
                        mTable.addForeignColumn(fDesc.getName(), fTable);
486
                        changesToApply.add(mTable);
525
                        changesToApply.add(mTable);
487
                    } else {
526
                    } else {
488
                        JOptionPane.showMessageDialog(new JFrame(), "L'extension ne peut pas s'installer car la table " + fTableName + " n'existe pas.");
527
                        JOptionPane.showMessageDialog(new JFrame(), "L'extension ne peut pas s'installer car la table " + fTableName + " n'existe pas.");
489
                        return false;
528
                        return false;
490
                    }
529
                    }
491
                }
530
                }
492
            }
531
            }
493
        }
532
        }
494
        // Create foreign keys
533
        // Create foreign keys
495
        if (!changesToApply.isEmpty()) {
534
        if (!changesToApply.isEmpty()) {
496
            for (String change : ChangeTable.cat(changesToApply)) {
535
            for (String change : ChangeTable.cat(changesToApply)) {
497
                root.getDBSystemRoot().getDataSource().execute(change);
536
                root.getDBSystemRoot().getDataSource().execute(change);
498
            }
537
            }
499
            root.getSchema().updateVersion();
538
            root.getSchema().updateVersion();
500
            root.refetch(tableNames);
539
            root.refetch(tableNames);
501
            Log.get().info("Fetching " + changesToApply.size() + " foreign fields creation");
540
            Log.get().info("Fetching " + changesToApply.size() + " foreign fields creation");
502
        }
541
        }
503
        // Create elements for created tables
542
        // Create elements for created tables
504
        for (TableDescritor tDesc : t) {
543
        for (TableDescritor tDesc : t) {
505
 
544
 
506
            tDesc.createElement(this);
545
            tDesc.createElement(this);
507
        }
546
        }
508
        return true;
547
        return true;
509
    }
548
    }
510
 
549
 
511
    public void stop() {
550
    public void stop() {
512
        this.isStarted = false;
551
        this.isStarted = false;
513
        this.autoStart = false;
552
        this.autoStart = false;
514
        Log.get().info("Extension " + this.getName() + " stopped");
553
        Log.get().info("Extension " + this.getName() + " stopped");
515
        // TODO : remove menu changes
554
        // TODO : remove menu changes
516
        fireChanged();
555
        fireChanged();
517
    }
556
    }
518
 
557
 
519
    public String getName() {
558
    public String getName() {
520
        return name;
559
        return name;
521
    }
560
    }
522
 
561
 
523
    @SuppressWarnings("unchecked")
562
    @SuppressWarnings("unchecked")
524
    public void importFromXML(String xml) {
563
    public void importFromXML(String xml) {
525
 
564
 
526
        System.out.println("Extension.importFromXML():" + xml);
565
        System.out.println("Extension.importFromXML():" + xml);
527
        if (xml.length() > 0) {
566
        if (xml.length() > 0) {
528
            final SAXBuilder sxb = new SAXBuilder();
567
            final SAXBuilder sxb = new SAXBuilder();
529
            try {
568
            try {
530
                final Document doc = sxb.build(new StringReader(xml));
569
                final Document doc = sxb.build(new StringReader(xml));
531
                final Element root = doc.getRootElement();
570
                final Element root = doc.getRootElement();
532
                if (root.getAttributeValue("autostart", "false").equals("true")) {
571
                if (root.getAttributeValue("autostart", "false").equals("true")) {
533
                    this.autoStart = true;
572
                    this.autoStart = true;
534
                }
573
                }
535
                // elements parsing
574
                // elements parsing
536
                final List<Element> elements = root.getChildren("element");
575
                final List<Element> elements = root.getChildren("element");
537
                for (Element eElement : elements) {
576
                for (Element eElement : elements) {
538
                    final String id = eElement.getAttributeValue("id");
577
                    final String id = eElement.getAttributeValue("id");
539
                    final String tableName = eElement.getAttributeValue("tableName");
578
                    final String tableName = eElement.getAttributeValue("tableName");
540
                    ElementDescriptor eDesc = new ElementDescriptor(id, tableName);
579
                    ElementDescriptor eDesc = new ElementDescriptor(id, tableName);
541
                    // TODO : fkeyaction
580
                    // TODO : fkeyaction
542
                    this.elementList.add(eDesc);
581
                    this.elementList.add(eDesc);
543
                }
582
                }
544
                // tables parsing
583
                // tables parsing
545
                final List<Element> tables = root.getChildren("table");
584
                final List<Element> tables = root.getChildren("table");
546
                for (Element eTable : tables) {
585
                for (Element eTable : tables) {
547
                    final String type = eTable.getAttributeValue("type");
586
                    final String type = eTable.getAttributeValue("type");
548
                    final String name = eTable.getAttributeValue("name");
587
                    final String name = eTable.getAttributeValue("name");
549
                    final TableDescritor tDesc = new TableDescritor(name);
588
                    final TableDescritor tDesc = new TableDescritor(name);
550
                    final List<Element> fields = eTable.getChildren("field");
589
                    final List<Element> fields = eTable.getChildren("field");
551
                    for (Element field : fields) {
590
                    for (Element field : fields) {
552
                        FieldDescriptor f = createFieldDescriptorFrom(name, field);
591
                        FieldDescriptor f = createFieldDescriptorFrom(name, field);
553
                        tDesc.add(f);
592
                        tDesc.add(f);
554
                    }
593
                    }
555
                    if (tDesc.getFields().size() > 0) {
594
                    if (tDesc.getFields().size() > 0) {
556
                        if (type.equals("create")) {
595
                        if (type.equals("create")) {
557
                            this.createTableList.add(tDesc);
596
                            this.createTableList.add(tDesc);
558
                        } else if (type.equals("modify")) {
597
                        } else if (type.equals("modify")) {
559
                            this.modifyTableList.add(tDesc);
598
                            this.modifyTableList.add(tDesc);
560
                        } else {
599
                        } else {
561
                            throw new IllegalStateException("Unknown table type: " + type);
600
                            throw new IllegalStateException("Unknown table type: " + type);
562
                        }
601
                        }
563
                    }
602
                    }
564
 
603
 
565
                }
604
                }
566
                // translations
605
                // translations
567
                final List<Element> translations = root.getChildren("translation");
606
                final List<Element> translations = root.getChildren("translation");
568
                for (Element eTranslation : translations) {
607
                for (Element eTranslation : translations) {
569
                    final String lang = eTranslation.getAttributeValue("locale");
608
                    final String lang = eTranslation.getAttributeValue("locale");
570
                    if (lang == null) {
609
                    if (lang == null) {
571
                        throw new IllegalArgumentException("no locale found in translation element");
610
                        throw new IllegalArgumentException("no locale found in translation element");
572
                    }
611
                    }
573
                    final List<Element> tTables = eTranslation.getChildren("element");
612
                    final List<Element> tTables = eTranslation.getChildren("element");
574
                    for (Element element : tTables) {
613
                    for (Element element : tTables) {
575
                        final String tableName = element.getAttributeValue("refid");
614
                        final String tableName = element.getAttributeValue("refid");
576
                        final TableTranslation t = new TableTranslation(lang, tableName);
615
                        final TableTranslation t = new TableTranslation(lang, tableName);
577
                        t.setSingular(element.getAttributeValue("singular"));
616
                        t.setSingular(element.getAttributeValue("singular"));
578
                        t.setPlural(element.getAttributeValue("plural"));
617
                        t.setPlural(element.getAttributeValue("plural"));
579
                        this.tableTranslations.add(t);
618
                        this.tableTranslations.add(t);
580
                        final List<Element> tFields = element.getChildren("item");
619
                        final List<Element> tFields = element.getChildren("item");
581
                        for (Element elementF : tFields) {
620
                        for (Element elementF : tFields) {
582
                            final FieldTranslation tF = new FieldTranslation(lang, tableName, elementF.getAttributeValue("id"));
621
                            final FieldTranslation tF = new FieldTranslation(lang, tableName, elementF.getAttributeValue("id"));
583
                            tF.setLabel(elementF.getAttributeValue("label"));
622
                            tF.setLabel(elementF.getAttributeValue("label"));
584
                            tF.setDocumentation(elementF.getAttributeValue("doc"));
623
                            tF.setDocumentation(elementF.getAttributeValue("doc"));
585
                            this.fieldTranslations.add(tF);
624
                            this.fieldTranslations.add(tF);
586
                        }
625
                        }
587
                    }
626
                    }
588
 
627
 
589
                    final List<Element> tMenu = eTranslation.getChildren("menu");
628
                    final List<Element> tMenu = eTranslation.getChildren("menu");
590
                    for (Element element : tMenu) {
629
                    for (Element element : tMenu) {
591
                        final MenuTranslation t = new MenuTranslation(lang, element.getAttributeValue("refid"));
630
                        final MenuTranslation t = new MenuTranslation(lang, element.getAttributeValue("refid"));
592
                        t.setLabel(element.getAttributeValue("label"));
631
                        t.setLabel(element.getAttributeValue("label"));
593
                        this.menuTranslations.add(t);
632
                        this.menuTranslations.add(t);
594
                    }
633
                    }
595
                    final List<Element> tActions = eTranslation.getChildren("action");
634
                    final List<Element> tActions = eTranslation.getChildren("action");
596
                    for (Element element : tActions) {
635
                    for (Element element : tActions) {
597
                        final ActionTranslation t = new ActionTranslation(lang, element.getAttributeValue("refid"));
636
                        final ActionTranslation t = new ActionTranslation(lang, element.getAttributeValue("refid"));
598
                        t.setLabel(element.getAttributeValue("label"));
637
                        t.setLabel(element.getAttributeValue("label"));
599
                        this.actionTranslations.add(t);
638
                        this.actionTranslations.add(t);
600
                    }
639
                    }
601
                }
640
                }
602
 
641
 
603
                // list parsing
642
                // list parsing
604
                final List<Element> lists = root.getChildren("list");
643
                final List<Element> lists = root.getChildren("list");
605
                for (Element eList : lists) {
644
                for (Element eList : lists) {
606
                    final String type = eList.getAttributeValue("type");
645
                    final String type = eList.getAttributeValue("type");
607
                    final String id = eList.getAttributeValue("id");
646
                    final String id = eList.getAttributeValue("id");
608
                    final String refid = eList.getAttributeValue("refid");
647
                    final String refid = eList.getAttributeValue("refid");
609
 
648
 
610
                    final ListDescriptor listDesc = new ListDescriptor(id);
649
                    final ListDescriptor listDesc = new ListDescriptor(id);
611
                    String mainTable = this.getTableNameForElementId(refid);
650
                    String mainTable = this.getTableNameForElementId(refid);
612
                    if (mainTable == null) {
651
                    if (mainTable == null) {
613
                        // fallback to table name
652
                        // fallback to table name
614
                        mainTable = refid;
653
                        mainTable = refid;
615
                    }
654
                    }
616
                    listDesc.setMainTable(mainTable);
655
                    listDesc.setMainTable(mainTable);
617
                    final List<Element> columns = eList.getChildren("column");
656
                    final List<Element> columns = eList.getChildren("column");
618
                    for (Element field : columns) {
657
                    for (Element field : columns) {
619
                        ColumnDescriptor f = createColumnDescriptorFrom(mainTable, field);
658
                        ColumnDescriptor f = createColumnDescriptorFrom(mainTable, field);
620
                        listDesc.add(f);
659
                        listDesc.add(f);
621
                    }
660
                    }
622
                    if (listDesc.getColumnCount() > 0) {
661
                    if (listDesc.getColumnCount() > 0) {
623
                        if (type.equals("create")) {
662
                        if (type.equals("create")) {
624
                            this.createListList.add(listDesc);
663
                            this.createListList.add(listDesc);
625
                        } else {
664
                        } else {
626
                            throw new IllegalStateException("Unknown table type: " + type);
665
                            throw new IllegalStateException("Unknown table type: " + type);
627
                        }
666
                        }
628
                    }
667
                    }
629
 
668
 
630
                } // component parsing
669
                } // component parsing
631
                final List<Element> components = root.getChildren("component");
670
                final List<Element> components = root.getChildren("component");
632
                for (Element eList : components) {
671
                for (Element eList : components) {
633
                    final String type = eList.getAttributeValue("type");
672
                    final String type = eList.getAttributeValue("type");
634
                    if (type.equals("create")) {
673
                    if (type.equals("create")) {
635
                        final String id = eList.getAttributeValue("id");
674
                        final String id = eList.getAttributeValue("id");
636
                        final String table = eList.getAttributeValue("table");
675
                        final String table = eList.getAttributeValue("table");
637
                        final ComponentDescritor tDesc = new ComponentDescritor(id);
676
                        final ComponentDescritor tDesc = new ComponentDescritor(id);
638
                        tDesc.setTable(table);
677
                        tDesc.setTable(table);
639
                        walkGroup(eList, tDesc.getGroup());
678
                        walkGroup(eList, tDesc.getGroup());
640
                        System.out.println("SimpleXMLAddon.importFromXML() " + tDesc);
679
                        System.out.println("SimpleXMLAddon.importFromXML() " + tDesc);
641
                        this.createComponentList.add(tDesc);
680
                        this.createComponentList.add(tDesc);
642
                    }
681
                    }
643
                }
682
                }
644
                // Menu
683
                // Menu
645
                final List<Element> menus = root.getChildren("menu");
684
                final List<Element> menus = root.getChildren("menu");
646
                for (Element eList : menus) {
685
                for (Element eList : menus) {
647
                    final String type = eList.getAttributeValue("type");
686
                    final String type = eList.getAttributeValue("type");
648
                    if (type.equals("create")) {
687
                    if (type.equals("create")) {
649
                        final List<Element> actions = eList.getChildren("action");
688
                        final List<Element> actions = eList.getChildren("action");
650
                        for (Element action : actions) {
689
                        for (Element action : actions) {
651
                            final String id = action.getAttributeValue("id");
690
                            final String id = action.getAttributeValue("id");
652
                            final String insertInMenu = action.getAttributeValue("insertInMenu");
691
                            final String insertInMenu = action.getAttributeValue("insertInMenu");
653
                            final String actionType = action.getAttributeValue("type");
692
                            final String actionType = action.getAttributeValue("type");
654
                            if (actionType.equals(MenuDescriptor.LIST)) {
693
                            if (actionType.equals(MenuDescriptor.LIST)) {
655
                                // a list frame
694
                                // a list frame
656
                                final String listId = action.getAttributeValue("listId");
695
                                final String listId = action.getAttributeValue("listId");
657
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
696
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
658
                                mDesc.setInsertInMenu(insertInMenu);
697
                                mDesc.setInsertInMenu(insertInMenu);
659
                                mDesc.setType(actionType);
698
                                mDesc.setType(actionType);
660
                                mDesc.setListId(listId);
699
                                mDesc.setListId(listId);
661
                                this.createMenuList.add(mDesc);
700
                                this.createMenuList.add(mDesc);
662
                            } else if (actionType.equals(MenuDescriptor.CREATE)) {
701
                            } else if (actionType.equals(MenuDescriptor.CREATE)) {
663
                                // a create frame
702
                                // a create frame
664
                                final String componentId = action.getAttributeValue("componentId");
703
                                final String componentId = action.getAttributeValue("componentId");
665
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
704
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
666
                                mDesc.setInsertInMenu(insertInMenu);
705
                                mDesc.setInsertInMenu(insertInMenu);
667
                                mDesc.setType(actionType);
706
                                mDesc.setType(actionType);
668
                                mDesc.setComponentId(componentId);
707
                                mDesc.setComponentId(componentId);
669
                                this.createMenuList.add(mDesc);
708
                                this.createMenuList.add(mDesc);
670
                            } else if (actionType.equals(MenuDescriptor.GROUP)) {
709
                            } else if (actionType.equals(MenuDescriptor.GROUP)) {
671
                                // a create frame
710
                                // a create frame
672
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
711
                                final MenuDescriptor mDesc = new MenuDescriptor(id);
673
                                mDesc.setInsertInMenu(insertInMenu);
712
                                mDesc.setInsertInMenu(insertInMenu);
674
                                mDesc.setType(actionType);
713
                                mDesc.setType(actionType);
675
                                this.createMenuList.add(mDesc);
714
                                this.createMenuList.add(mDesc);
676
                            } else {
715
                            } else {
677
                                throw new IllegalStateException("Unknown action type " + actionType + " for action " + id);
716
                                throw new IllegalStateException("Unknown action type " + actionType + " for action " + id);
678
                            }
717
                            }
679
 
718
 
680
                        }
719
                        }
681
                    } else if (type.equals("remove")) {
720
                    } else if (type.equals("remove")) {
682
                        final List<Element> actions = eList.getChildren("action");
721
                        final List<Element> actions = eList.getChildren("action");
683
                        for (Element action : actions) {
722
                        for (Element action : actions) {
684
                            final String id = action.getAttributeValue("id");
723
                            final String id = action.getAttributeValue("id");
685
                            this.removeMenuList.add(new MenuDescriptor(id));
724
                            this.removeMenuList.add(new MenuDescriptor(id));
686
                        }
725
                        }
687
                    }
726
                    }
688
                }
727
                }
689
 
728
 
690
                // Actions
729
                // Actions
691
                final List<Element> actions = root.getChildren("action");
730
                final List<Element> actions = root.getChildren("action");
692
                for (Element actionElement : actions) {
731
                for (Element actionElement : actions) {
693
                    final String type = actionElement.getAttributeValue("type");
732
                    final String type = actionElement.getAttributeValue("type");
694
                    if (type.equals("create")) {
733
                    if (type.equals("create")) {
695
 
734
 
696
                        final String id = actionElement.getAttributeValue("id");
735
                        final String id = actionElement.getAttributeValue("id");
697
                        final String location = actionElement.getAttributeValue("location");
736
                        final String location = actionElement.getAttributeValue("location");
698
                        final String table = actionElement.getAttributeValue("table");
737
                        final String table = actionElement.getAttributeValue("table");
699
                        final String componentId = actionElement.getAttributeValue("componentId");
738
                        final String componentId = actionElement.getAttributeValue("componentId");
700
                        ActionDescriptor action = new ActionDescriptor(id);
739
                        ActionDescriptor action = new ActionDescriptor(id);
701
                        action.setLocation(location);
740
                        action.setLocation(location);
702
                        action.setTable(table);
741
                        action.setTable(table);
703
                        action.setComponentId(componentId);
742
                        action.setComponentId(componentId);
704
                        this.createActionList.add(action);
743
                        this.createActionList.add(action);
705
 
744
 
706
                    } else {
745
                    } else {
707
                        // TODO: remove
746
                        // TODO: remove
708
                        throw new IllegalStateException("Unknown action type " + type);
747
                        throw new IllegalStateException("Unknown action type " + type);
709
                    }
748
                    }
710
 
749
 
711
                }
750
                }
712
 
751
 
713
            } catch (Exception e) {
752
            } catch (Exception e) {
714
                System.err.println("SimpleXMLAddon.importFromXML(): parsing error :" + e.getMessage());
753
                System.err.println("SimpleXMLAddon.importFromXML(): parsing error :" + e.getMessage());
715
                e.printStackTrace();
754
                e.printStackTrace();
716
            }
755
            }
717
        }
756
        }
718
        notSaved = false;
757
        notSaved = false;
719
 
758
 
720
        fireChanged();
759
        fireChanged();
721
    }
760
    }
722
 
761
 
723
    String toXML() {
762
    String toXML() {
724
        final Element rootElement = new Element("extension");
763
        final Element rootElement = new Element("extension");
725
        rootElement.setAttribute("id", this.name);
764
        rootElement.setAttribute("id", this.name);
726
        rootElement.setAttribute("autostart", String.valueOf(this.isStarted));
765
        rootElement.setAttribute("autostart", String.valueOf(this.isStarted));
727
        rootElement.setAttribute("format", "1.0");
766
        rootElement.setAttribute("format", "1.0");
728
        final Document document = new Document(rootElement);
767
        final Document document = new Document(rootElement);
729
 
768
 
730
        // Element
769
        // Element
731
        for (ElementDescriptor eDescriptor : this.elementList) {
770
        for (ElementDescriptor eDescriptor : this.elementList) {
732
            final Element eElement = new Element("element");
771
            final Element eElement = new Element("element");
733
            eElement.setAttribute("tableName", eDescriptor.getTableName());
772
            eElement.setAttribute("tableName", eDescriptor.getTableName());
734
            eElement.setAttribute("id", eDescriptor.getId());
773
            eElement.setAttribute("id", eDescriptor.getId());
735
            // TODO: fkey action : <fkeyaction action="set_empty|cascade|restrict"
774
            // TODO: fkey action : <fkeyaction action="set_empty|cascade|restrict"
736
            // fields="id_language"/>
775
            // fields="id_language"/>
737
            rootElement.addContent(eElement);
776
            rootElement.addContent(eElement);
738
        }
777
        }
739
 
778
 
740
        // Table create
779
        // Table create
741
        for (TableDescritor tDescriptor : this.createTableList) {
780
        for (TableDescritor tDescriptor : this.createTableList) {
742
            final Element eTable = new Element("table");
781
            final Element eTable = new Element("table");
743
            eTable.setAttribute("type", "create");
782
            eTable.setAttribute("type", "create");
744
            eTable.setAttribute("name", tDescriptor.getName());
783
            eTable.setAttribute("name", tDescriptor.getName());
745
            for (FieldDescriptor fDescriptor : tDescriptor.getFields()) {
784
            for (FieldDescriptor fDescriptor : tDescriptor.getFields()) {
746
                final Element eField = new Element("field");
785
                final Element eField = new Element("field");
747
                eField.setAttribute("name", fDescriptor.getName());
786
                eField.setAttribute("name", fDescriptor.getName());
748
                eField.setAttribute("type", fDescriptor.getType());
787
                eField.setAttribute("type", fDescriptor.getType());
749
                if (fDescriptor.getLength() != null) {
788
                if (fDescriptor.getLength() != null) {
750
                    eField.setAttribute("length", fDescriptor.getLength());
789
                    eField.setAttribute("length", fDescriptor.getLength());
751
                }
790
                }
752
                if (fDescriptor.getDefaultValue() != null) {
791
                if (fDescriptor.getDefaultValue() != null) {
753
                    eField.setAttribute("default", fDescriptor.getDefaultValue());
792
                    eField.setAttribute("default", fDescriptor.getDefaultValue());
754
                }
793
                }
755
                if (fDescriptor.getForeignTable() != null) {
794
                if (fDescriptor.getForeignTable() != null) {
756
                    eField.setAttribute("ftable", fDescriptor.getForeignTable());
795
                    eField.setAttribute("ftable", fDescriptor.getForeignTable());
757
                }
796
                }
758
                eTable.addContent(eField);
797
                eTable.addContent(eField);
759
            }
798
            }
760
            rootElement.addContent(eTable);
799
            rootElement.addContent(eTable);
761
        }
800
        }
762
 
801
 
763
        // Table modify
802
        // Table modify
764
        for (TableDescritor tDescriptor : this.modifyTableList) {
803
        for (TableDescritor tDescriptor : this.modifyTableList) {
765
            final Element eTable = new Element("table");
804
            final Element eTable = new Element("table");
766
            eTable.setAttribute("type", "modify");
805
            eTable.setAttribute("type", "modify");
767
            eTable.setAttribute("name", tDescriptor.getName());
806
            eTable.setAttribute("name", tDescriptor.getName());
768
            for (FieldDescriptor fDescriptor : tDescriptor.getFields()) {
807
            for (FieldDescriptor fDescriptor : tDescriptor.getFields()) {
769
                final Element eField = new Element("field");
808
                final Element eField = new Element("field");
770
                eField.setAttribute("name", fDescriptor.getName());
809
                eField.setAttribute("name", fDescriptor.getName());
771
                eField.setAttribute("type", fDescriptor.getType());
810
                eField.setAttribute("type", fDescriptor.getType());
772
                if (fDescriptor.getLength() != null) {
811
                if (fDescriptor.getLength() != null) {
773
                    eField.setAttribute("length", fDescriptor.getLength());
812
                    eField.setAttribute("length", fDescriptor.getLength());
774
                }
813
                }
775
                if (fDescriptor.getDefaultValue() != null) {
814
                if (fDescriptor.getDefaultValue() != null) {
776
                    eField.setAttribute("default", fDescriptor.getDefaultValue());
815
                    eField.setAttribute("default", fDescriptor.getDefaultValue());
777
                }
816
                }
778
                if (fDescriptor.getForeignTable() != null) {
817
                if (fDescriptor.getForeignTable() != null) {
779
                    eField.setAttribute("ftable", fDescriptor.getForeignTable());
818
                    eField.setAttribute("ftable", fDescriptor.getForeignTable());
780
                }
819
                }
781
                eTable.addContent(eField);
820
                eTable.addContent(eField);
782
            }
821
            }
783
            rootElement.addContent(eTable);
822
            rootElement.addContent(eTable);
784
        }
823
        }
785
        // Translations
824
        // Translations
786
        final HashSet<String> locales = new HashSet<String>();
825
        final HashSet<String> locales = new HashSet<String>();
787
        for (Translation tr : tableTranslations) {
826
        for (Translation tr : tableTranslations) {
788
            locales.add(tr.getLocale());
827
            locales.add(tr.getLocale());
789
        }
828
        }
790
        for (Translation tr : fieldTranslations) {
829
        for (Translation tr : fieldTranslations) {
791
            locales.add(tr.getLocale());
830
            locales.add(tr.getLocale());
792
        }
831
        }
793
        for (Translation tr : menuTranslations) {
832
        for (Translation tr : menuTranslations) {
794
            locales.add(tr.getLocale());
833
            locales.add(tr.getLocale());
795
        }
834
        }
796
        for (Translation tr : actionTranslations) {
835
        for (Translation tr : actionTranslations) {
797
            locales.add(tr.getLocale());
836
            locales.add(tr.getLocale());
798
        }
837
        }
799
        final List<String> lLocales = new ArrayList<String>(locales);
838
        final List<String> lLocales = new ArrayList<String>(locales);
800
        Collections.sort(lLocales);
839
        Collections.sort(lLocales);
801
        for (String locale : lLocales) {
840
        for (String locale : lLocales) {
802
            final Element eTranslation = new Element("translation");
841
            final Element eTranslation = new Element("translation");
803
            eTranslation.setAttribute("locale", locale);
842
            eTranslation.setAttribute("locale", locale);
804
            rootElement.addContent(eTranslation);
843
            rootElement.addContent(eTranslation);
805
            // Tables
844
            // Tables
806
            for (TableTranslation tTranslation : tableTranslations) {
845
            for (TableTranslation tTranslation : tableTranslations) {
807
                if (tTranslation.getLocale().equals(locale)) {
846
                if (tTranslation.getLocale().equals(locale)) {
808
                    final Element eTable = new Element("element");
847
                    final Element eTable = new Element("element");
809
                    eTable.setAttribute("refid", tTranslation.getTableName());
848
                    eTable.setAttribute("refid", tTranslation.getTableName());
810
                    final String singular = tTranslation.getSingular();
849
                    final String singular = tTranslation.getSingular();
811
                    if (singular != null && !singular.isEmpty()) {
850
                    if (singular != null && !singular.isEmpty()) {
812
                        eTable.setAttribute("singular", singular);
851
                        eTable.setAttribute("singular", singular);
813
                    }
852
                    }
814
                    final String plural = tTranslation.getPlural();
853
                    final String plural = tTranslation.getPlural();
815
                    if (plural != null && !plural.isEmpty()) {
854
                    if (plural != null && !plural.isEmpty()) {
816
                        eTable.setAttribute("plural", plural);
855
                        eTable.setAttribute("plural", plural);
817
                    }
856
                    }
818
                    for (FieldTranslation fTranslation : fieldTranslations) {
857
                    for (FieldTranslation fTranslation : fieldTranslations) {
819
                        // Fields
858
                        // Fields
820
                        if (fTranslation.getLocale().equals(locale) && fTranslation.getTableName().equals(tTranslation.getTableName())) {
859
                        if (fTranslation.getLocale().equals(locale) && fTranslation.getTableName().equals(tTranslation.getTableName())) {
821
                            final Element eField = new Element("item");
860
                            final Element eField = new Element("item");
822
                            eField.setAttribute("id", fTranslation.getFieldName());
861
                            eField.setAttribute("id", fTranslation.getFieldName());
823
                            eField.setAttribute("label", fTranslation.getLabel());
862
                            eField.setAttribute("label", fTranslation.getLabel());
824
                            if (fTranslation.getDocumentation() != null) {
863
                            if (fTranslation.getDocumentation() != null) {
825
                                eField.setAttribute("doc", fTranslation.getDocumentation());
864
                                eField.setAttribute("doc", fTranslation.getDocumentation());
826
                            }
865
                            }
827
                            eTable.addContent(eField);
866
                            eTable.addContent(eField);
828
                        }
867
                        }
829
                    }
868
                    }
830
                    eTranslation.addContent(eTable);
869
                    eTranslation.addContent(eTable);
831
 
870
 
832
                }
871
                }
833
            }
872
            }
834
            // Menus
873
            // Menus
835
            for (MenuTranslation tMenu : menuTranslations) {
874
            for (MenuTranslation tMenu : menuTranslations) {
836
                if (tMenu.getLocale().equals(locale)) {
875
                if (tMenu.getLocale().equals(locale)) {
837
                    final Element eMenu = new Element("menu");
876
                    final Element eMenu = new Element("menu");
838
                    eMenu.setAttribute("refid", tMenu.getId());
877
                    eMenu.setAttribute("refid", tMenu.getId());
839
                    eMenu.setAttribute("label", tMenu.getLabel());
878
                    eMenu.setAttribute("label", tMenu.getLabel());
840
                    eTranslation.addContent(eMenu);
879
                    eTranslation.addContent(eMenu);
841
                }
880
                }
842
            }
881
            }
843
 
882
 
844
            // Actions
883
            // Actions
845
            for (ActionTranslation tAction : actionTranslations) {
884
            for (ActionTranslation tAction : actionTranslations) {
846
                if (tAction.getLocale().equals(locale)) {
885
                if (tAction.getLocale().equals(locale)) {
847
                    final Element eMenu = new Element("action");
886
                    final Element eMenu = new Element("action");
848
                    eMenu.setAttribute("refid", tAction.getId());
887
                    eMenu.setAttribute("refid", tAction.getId());
849
                    eMenu.setAttribute("label", tAction.getLabel());
888
                    eMenu.setAttribute("label", tAction.getLabel());
850
                    eTranslation.addContent(eMenu);
889
                    eTranslation.addContent(eMenu);
851
                }
890
                }
852
            }
891
            }
853
        }
892
        }
854
 
893
 
855
        // Actions
894
        // Actions
856
        for (ActionDescriptor action : this.createActionList) {
895
        for (ActionDescriptor action : this.createActionList) {
857
            final Element eAction = new Element("action");
896
            final Element eAction = new Element("action");
858
            eAction.setAttribute("type", "create");
897
            eAction.setAttribute("type", "create");
859
            eAction.setAttribute("id", action.getId());
898
            eAction.setAttribute("id", action.getId());
860
            eAction.setAttribute("location", action.getLocation());
899
            eAction.setAttribute("location", action.getLocation());
861
            eAction.setAttribute("table", action.getTable());
900
            eAction.setAttribute("table", action.getTable());
862
            eAction.setAttribute("componentId", action.getComponentId());
901
            eAction.setAttribute("componentId", action.getComponentId());
863
            rootElement.addContent(eAction);
902
            rootElement.addContent(eAction);
864
        }
903
        }
865
        // Menu create
904
        // Menu create
866
        if (!this.createMenuList.isEmpty()) {
905
        if (!this.createMenuList.isEmpty()) {
867
            final Element eMenu = new Element("menu");
906
            final Element eMenu = new Element("menu");
868
            eMenu.setAttribute("type", "create");
907
            eMenu.setAttribute("type", "create");
869
            for (MenuDescriptor menu : this.createMenuList) {
908
            for (MenuDescriptor menu : this.createMenuList) {
870
                final Element eActionMenu = new Element("action");
909
                final Element eActionMenu = new Element("action");
871
                eActionMenu.setAttribute("id", menu.getId());
910
                eActionMenu.setAttribute("id", menu.getId());
872
                eActionMenu.setAttribute("insertInMenu", menu.getInsertInMenu());
911
                eActionMenu.setAttribute("insertInMenu", menu.getInsertInMenu());
873
                final String type = menu.getType();
912
                final String type = menu.getType();
874
                eActionMenu.setAttribute("type", type);
913
                eActionMenu.setAttribute("type", type);
875
                if (!type.equals(MenuDescriptor.CREATE) && !type.equals(MenuDescriptor.LIST) && !type.equals(MenuDescriptor.GROUP)) {
914
                if (!type.equals(MenuDescriptor.CREATE) && !type.equals(MenuDescriptor.LIST) && !type.equals(MenuDescriptor.GROUP)) {
876
                    throw new IllegalStateException("Menu type " + type + " not supported");
915
                    throw new IllegalStateException("Menu type " + type + " not supported");
877
                }
916
                }
878
 
917
 
879
                if (type.endsWith("list") && menu.getListId() != null) {
918
                if (type.endsWith("list") && menu.getListId() != null) {
880
                    eActionMenu.setAttribute("listId", menu.getListId());
919
                    eActionMenu.setAttribute("listId", menu.getListId());
881
                } else if (type.endsWith("create") && menu.getComponentId() != null) {
920
                } else if (type.endsWith("create") && menu.getComponentId() != null) {
882
                    eActionMenu.setAttribute("componentId", menu.getComponentId());
921
                    eActionMenu.setAttribute("componentId", menu.getComponentId());
883
                }
922
                }
884
                eMenu.addContent(eActionMenu);
923
                eMenu.addContent(eActionMenu);
885
            }
924
            }
886
            rootElement.addContent(eMenu);
925
            rootElement.addContent(eMenu);
887
 
926
 
888
        }
927
        }
889
        // Menu remove
928
        // Menu remove
890
        if (!this.removeMenuList.isEmpty()) {
929
        if (!this.removeMenuList.isEmpty()) {
891
            final Element eMenu = new Element("menu");
930
            final Element eMenu = new Element("menu");
892
            eMenu.setAttribute("type", "remove");
931
            eMenu.setAttribute("type", "remove");
893
            for (MenuDescriptor menu : this.removeMenuList) {
932
            for (MenuDescriptor menu : this.removeMenuList) {
894
                final Element eActionMenu = new Element("action");
933
                final Element eActionMenu = new Element("action");
895
                eActionMenu.setAttribute("id", menu.getId());
934
                eActionMenu.setAttribute("id", menu.getId());
896
                eMenu.addContent(eActionMenu);
935
                eMenu.addContent(eActionMenu);
897
            }
936
            }
898
            rootElement.addContent(eMenu);
937
            rootElement.addContent(eMenu);
899
        }
938
        }
900
        // List create
939
        // List create
901
        for (ListDescriptor listDescriptor : this.createListList) {
940
        for (ListDescriptor listDescriptor : this.createListList) {
902
            final Element eList = new Element("list");
941
            final Element eList = new Element("list");
903
            eList.setAttribute("type", "create");
942
            eList.setAttribute("type", "create");
904
            eList.setAttribute("id", listDescriptor.getId());
943
            eList.setAttribute("id", listDescriptor.getId());
905
            String refFromTable = getRefFromTable(listDescriptor.getMainTable());
944
            String refFromTable = getRefFromTable(listDescriptor.getMainTable());
906
            if (refFromTable == null) {
945
            if (refFromTable == null) {
907
                refFromTable = listDescriptor.getMainTable();
946
                refFromTable = listDescriptor.getMainTable();
908
            }
947
            }
909
            if (refFromTable != null) {
948
            if (refFromTable != null) {
910
                eList.setAttribute("refid", refFromTable);
949
                eList.setAttribute("refid", refFromTable);
911
                for (ColumnDescriptor fieldDescriptor : listDescriptor.getColumns()) {
950
                for (ColumnDescriptor fieldDescriptor : listDescriptor.getColumns()) {
912
                    final Element eField = new Element("column");
951
                    final Element eField = new Element("column");
913
                    eField.setAttribute("id", fieldDescriptor.getId());
952
                    eField.setAttribute("id", fieldDescriptor.getId());
914
                    eField.setAttribute("fields", fieldDescriptor.getFieldsPaths());
953
                    eField.setAttribute("fields", fieldDescriptor.getFieldsPaths());
915
                    eField.setAttribute("style", fieldDescriptor.getStyle());
954
                    eField.setAttribute("style", fieldDescriptor.getStyle());
916
                    eList.addContent(eField);
955
                    eList.addContent(eField);
917
                }
956
                }
918
 
957
 
919
                rootElement.addContent(eList);
958
                rootElement.addContent(eList);
920
            }
959
            }
921
 
960
 
922
        }
961
        }
923
        // Component create
962
        // Component create
924
        for (ComponentDescritor componentDescriptor : this.createComponentList) {
963
        for (ComponentDescritor componentDescriptor : this.createComponentList) {
925
            final Element eComponent = new Element("component");
964
            final Element eComponent = new Element("component");
926
            eComponent.setAttribute("type", "create");
965
            eComponent.setAttribute("type", "create");
927
            eComponent.setAttribute("id", componentDescriptor.getId());
966
            eComponent.setAttribute("id", componentDescriptor.getId());
928
            eComponent.setAttribute("table", componentDescriptor.getTable());
967
            eComponent.setAttribute("table", componentDescriptor.getTable());
929
            appendGroup(eComponent, componentDescriptor.getGroup());
968
            appendGroup(eComponent, componentDescriptor.getGroup());
930
 
969
 
931
            rootElement.addContent(eComponent);
970
            rootElement.addContent(eComponent);
932
        }
971
        }
933
        // Component modify
972
        // Component modify
934
        for (ComponentDescritor componentDescriptor : this.modifyComponentList) {
973
        for (ComponentDescritor componentDescriptor : this.modifyComponentList) {
935
            System.out.println(componentDescriptor);
974
            System.out.println(componentDescriptor);
936
            throw new IllegalAccessError("Not yet implemented");
975
            throw new IllegalAccessError("Not yet implemented");
937
        }
976
        }
938
        // Output
977
        // Output
939
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
978
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
940
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
979
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
941
        final BufferedOutputStream oStream = new BufferedOutputStream(bOut);
980
        final BufferedOutputStream oStream = new BufferedOutputStream(bOut);
942
        try {
981
        try {
943
            out.output(document, oStream);
982
            out.output(document, oStream);
944
        } catch (IOException e) {
983
        } catch (IOException e) {
945
            e.printStackTrace();
984
            e.printStackTrace();
946
        } finally {
985
        } finally {
947
            try {
986
            try {
948
                oStream.close();
987
                oStream.close();
949
            } catch (IOException e) {
988
            } catch (IOException e) {
950
                e.printStackTrace();
989
                e.printStackTrace();
951
            }
990
            }
952
        }
991
        }
953
        try {
992
        try {
954
            return bOut.toString("utf8");
993
            return bOut.toString("utf8");
955
        } catch (UnsupportedEncodingException e) {
994
        } catch (UnsupportedEncodingException e) {
956
            e.printStackTrace();
995
            e.printStackTrace();
957
            return "";
996
            return "";
958
        }
997
        }
959
    }
998
    }
960
 
999
 
961
    private void appendGroup(Element eComponent, Group group) {
1000
    private void appendGroup(Element eComponent, Group group) {
962
        int size = group.getSize();
1001
        int size = group.getSize();
963
        for (int i = 0; i < size; i++) {
1002
        for (int i = 0; i < size; i++) {
964
            Item it = group.getItem(i);
1003
            Item it = group.getItem(i);
965
            final Element gr;
1004
            final Element gr;
966
            if (it instanceof Group) {
1005
            if (it instanceof Group) {
967
                gr = new Element("group");
1006
                gr = new Element("group");
968
                appendGroup(gr, (Group) it);
1007
                appendGroup(gr, (Group) it);
969
            } else {
1008
            } else {
970
                gr = new Element("item");
1009
                gr = new Element("item");
971
            }
1010
            }
972
            gr.setAttribute("id", it.getId());
1011
            gr.setAttribute("id", it.getId());
973
            if (it.getLocalHint() != null) {
1012
            if (it.getLocalHint() != null) {
974
                final LayoutHints hints = it.getLocalHint();
1013
                final LayoutHints hints = it.getLocalHint();
975
                final String type;
1014
                final String type;
976
                if (hints.largeWidth()) {
1015
                if (hints.largeWidth()) {
977
                    type = "verylarge";
1016
                    type = "verylarge";
978
                } else if (hints.fillWidth()) {
1017
                } else if (hints.fillWidth()) {
979
                    type = "large";
1018
                    type = "large";
980
                } else {
1019
                } else {
981
                    type = "normal";
1020
                    type = "normal";
982
                }
1021
                }
983
                gr.setAttribute("type", type);
1022
                gr.setAttribute("type", type);
984
                if (hints.isSeparated()) {
1023
                if (hints.isSeparated()) {
985
                    gr.setAttribute("isSeparated", "true");
1024
                    gr.setAttribute("isSeparated", "true");
986
                }
1025
                }
987
                if (hints.showLabel()) {
1026
                if (hints.showLabel()) {
988
                    gr.setAttribute("showLabel", "true");
1027
                    gr.setAttribute("showLabel", "true");
989
                }
1028
                }
990
            }
1029
            }
991
 
1030
 
992
            eComponent.addContent(gr);
1031
            eComponent.addContent(gr);
993
 
1032
 
994
        }
1033
        }
995
 
1034
 
996
    }
1035
    }
997
 
1036
 
998
    private void walkGroup(Element e, Group group) {
1037
    private void walkGroup(Element e, Group group) {
999
        @SuppressWarnings("unchecked")
1038
        @SuppressWarnings("unchecked")
1000
        final List<Element> elements = e.getChildren();
1039
        final List<Element> elements = e.getChildren();
1001
        for (Element element : elements) {
1040
        for (Element element : elements) {
1002
            String id = element.getAttributeValue("id", "unknown");
1041
            String id = element.getAttributeValue("id", "unknown");
1003
            String type = element.getAttributeValue("type", "default");
1042
            String type = element.getAttributeValue("type", "default");
1004
            String showLabel = element.getAttributeValue("showLabel", "true");
1043
            String showLabel = element.getAttributeValue("showLabel", "true");
1005
            String isSeparated = element.getAttributeValue("isSeparated", "false");
1044
            String isSeparated = element.getAttributeValue("isSeparated", "false");
1006
            if (element.getName().equals("item")) {
1045
            if (element.getName().equals("item")) {
1007
                final Item it = new Item(id);
1046
                final Item it = new Item(id);
1008
                if (type.equals("large")) {
1047
                if (type.equals("large")) {
1009
                    it.setLocalHint(new LayoutHints(false, false, showLabel.equals("true"), isSeparated.equals("true"), true, false));
1048
                    it.setLocalHint(new LayoutHints(false, false, showLabel.equals("true"), isSeparated.equals("true"), true, false));
1010
                } else if (type.equals("verylarge")) {
1049
                } else if (type.equals("verylarge")) {
1011
                    it.setLocalHint(new LayoutHints(true, false, showLabel.equals("true"), isSeparated.equals("true"), true, false));
1050
                    it.setLocalHint(new LayoutHints(true, false, showLabel.equals("true"), isSeparated.equals("true"), true, false));
1012
                } else {
1051
                } else {
1013
                    it.setLocalHint(new LayoutHints(false, false, showLabel.equals("true"), isSeparated.equals("true"), false, false));
1052
                    it.setLocalHint(new LayoutHints(false, false, showLabel.equals("true"), isSeparated.equals("true"), false, false));
1014
                }
1053
                }
1015
                System.out.println("Extension.walkGroup()" + it + " " + it.getLocalHint() + " from " + type);
1054
                System.out.println("Extension.walkGroup()" + it + " " + it.getLocalHint() + " from " + type);
1016
                group.add(it);
1055
                group.add(it);
1017
 
1056
 
1018
            } else if (element.getName().equals("group")) {
1057
            } else if (element.getName().equals("group")) {
1019
                final Group g = new Group(id);
1058
                final Group g = new Group(id);
1020
                group.add(g);
1059
                group.add(g);
1021
                walkGroup(element, g);
1060
                walkGroup(element, g);
1022
            } else {
1061
            } else {
1023
                throw new IllegalStateException("Unknown element: " + element.getName());
1062
                throw new IllegalStateException("Unknown element: " + element.getName());
1024
            }
1063
            }
1025
        }
1064
        }
1026
    }
1065
    }
1027
 
1066
 
1028
    private FieldDescriptor createFieldDescriptorFrom(String table, Element field) {
1067
    private FieldDescriptor createFieldDescriptorFrom(String table, Element field) {
1029
        FieldDescriptor f = new FieldDescriptor(table, field.getAttributeValue("name"), field.getAttributeValue("type"), field.getAttributeValue("default"), field.getAttributeValue("length"),
1068
        FieldDescriptor f = new FieldDescriptor(table, field.getAttributeValue("name"), field.getAttributeValue("type"), field.getAttributeValue("default"), field.getAttributeValue("length"),
1030
                field.getAttributeValue("ftable"));
1069
                field.getAttributeValue("ftable"));
1031
        Element child = field.getChild("field");
1070
        Element child = field.getChild("field");
1032
        if (child != null) {
1071
        if (child != null) {
1033
            f.setLink(createFieldDescriptorFrom(field.getAttributeValue("ftable"), child));
1072
            f.setLink(createFieldDescriptorFrom(field.getAttributeValue("ftable"), child));
1034
        }
1073
        }
1035
        return f;
1074
        return f;
1036
    }
1075
    }
1037
 
1076
 
1038
    private ColumnDescriptor createColumnDescriptorFrom(String table, Element field) {
1077
    private ColumnDescriptor createColumnDescriptorFrom(String table, Element field) {
1039
        final ColumnDescriptor f = new ColumnDescriptor(field.getAttributeValue("id"));
1078
        final ColumnDescriptor f = new ColumnDescriptor(field.getAttributeValue("id"));
1040
        f.setFieldsPaths(field.getAttributeValue("fields"));
1079
        f.setFieldsPaths(field.getAttributeValue("fields"));
1041
        f.setStyle(field.getAttributeValue("style"));
1080
        f.setStyle(field.getAttributeValue("style"));
1042
        return f;
1081
        return f;
1043
    }
1082
    }
1044
 
1083
 
1045
    private void fireChanged() {
1084
    private void fireChanged() {
1046
        for (ChangeListener listener : listeners) {
1085
        for (ChangeListener listener : listeners) {
1047
            listener.stateChanged(new ChangeEvent(this));
1086
            listener.stateChanged(new ChangeEvent(this));
1048
        }
1087
        }
1049
 
1088
 
1050
    }
1089
    }
1051
 
1090
 
1052
    public List<TableDescritor> getCreateTableList() {
1091
    public List<TableDescritor> getCreateTableList() {
1053
        return createTableList;
1092
        return createTableList;
1054
    }
1093
    }
1055
 
1094
 
1056
    public void addCreateTable(TableDescritor value) {
1095
    public void addCreateTable(TableDescritor value) {
1057
        this.createTableList.add(value);
1096
        this.createTableList.add(value);
1058
        setChanged();
1097
        setChanged();
1059
    }
1098
    }
1060
 
1099
 
1061
    public void removeCreateTable(TableDescritor value) {
1100
    public void removeCreateTable(TableDescritor value) {
1062
        this.createTableList.remove(value);
1101
        this.createTableList.remove(value);
1063
        setChanged();
1102
        setChanged();
1064
    }
1103
    }
1065
 
1104
 
1066
    public List<TableDescritor> getModifyTableList() {
1105
    public List<TableDescritor> getModifyTableList() {
1067
        return modifyTableList;
1106
        return modifyTableList;
1068
    }
1107
    }
1069
 
1108
 
1070
    public List<ListDescriptor> getCreateListList() {
1109
    public List<ListDescriptor> getCreateListList() {
1071
        return createListList;
1110
        return createListList;
1072
    }
1111
    }
1073
 
1112
 
1074
    public ListDescriptor getCreateListFromId(String id) {
1113
    public ListDescriptor getCreateListFromId(String id) {
1075
        for (ListDescriptor listDescriptor : this.createListList) {
1114
        for (ListDescriptor listDescriptor : this.createListList) {
1076
            if (listDescriptor.getId().equals(id)) {
1115
            if (listDescriptor.getId().equals(id)) {
1077
                return listDescriptor;
1116
                return listDescriptor;
1078
            }
1117
            }
1079
        }
1118
        }
1080
        return null;
1119
        return null;
1081
    }
1120
    }
1082
 
1121
 
1083
    public void addCreateList(ListDescriptor item) {
1122
    public void addCreateList(ListDescriptor item) {
1084
        this.createListList.add(item);
1123
        this.createListList.add(item);
1085
        setChanged();
1124
        setChanged();
1086
 
1125
 
1087
    }
1126
    }
1088
 
1127
 
1089
    public void removeCreateList(ListDescriptor item) {
1128
    public void removeCreateList(ListDescriptor item) {
1090
        this.createListList.remove(item);
1129
        this.createListList.remove(item);
1091
        setChanged();
1130
        setChanged();
1092
    }
1131
    }
1093
 
1132
 
1094
    public void addChangeListener(ChangeListener listener) {
1133
    public void addChangeListener(ChangeListener listener) {
1095
        if (!this.listeners.contains(listener))
1134
        if (!this.listeners.contains(listener))
1096
            this.listeners.add(listener);
1135
            this.listeners.add(listener);
1097
 
1136
 
1098
    }
1137
    }
1099
 
1138
 
1100
    public TableDescritor getOrCreateTableDescritor(String tableName) {
1139
    public TableDescritor getOrCreateTableDescritor(String tableName) {
1101
 
1140
 
1102
        for (TableDescritor td : this.modifyTableList) {
1141
        for (TableDescritor td : this.modifyTableList) {
1103
            if (td.getName().equalsIgnoreCase(tableName)) {
1142
            if (td.getName().equalsIgnoreCase(tableName)) {
1104
                return td;
1143
                return td;
1105
            }
1144
            }
1106
        }
1145
        }
1107
        // create table descritor for the table
1146
        // create table descritor for the table
1108
        final TableDescritor td = new TableDescritor(tableName);
1147
        final TableDescritor td = new TableDescritor(tableName);
1109
        this.modifyTableList.add(td);
1148
        this.modifyTableList.add(td);
1110
        setChanged();
1149
        setChanged();
1111
        return td;
1150
        return td;
1112
    }
1151
    }
1113
 
1152
 
1114
    public SQLTable getSQLTable(TableDescritor tableDesc) {
1153
    public SQLTable getSQLTable(TableDescritor tableDesc) {
1115
        try {
1154
        try {
1116
            SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(tableDesc.getName());
1155
            SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(tableDesc.getName());
1117
            return t;
1156
            return t;
1118
        } catch (Exception e) {
1157
        } catch (Exception e) {
1119
            return null;
1158
            return null;
1120
        }
1159
        }
1121
    }
1160
    }
1122
 
1161
 
1123
    public boolean isNotSaved() {
1162
    public boolean isNotSaved() {
1124
        return this.notSaved;
1163
        return this.notSaved;
1125
    }
1164
    }
1126
 
1165
 
1127
    public void setChanged() {
1166
    public void setChanged() {
1128
        this.notSaved = true;
1167
        this.notSaved = true;
1129
        this.fireChanged();
1168
        this.fireChanged();
1130
        String xml = this.toXML();
1169
        String xml = this.toXML();
1131
        System.out.println(xml);
1170
        System.out.println(xml);
1132
    }
1171
    }
1133
 
1172
 
1134
    // FIXME: filter textfield pour eviter les pb d'insert/update
1173
    // FIXME: filter textfield pour eviter les pb d'insert/update
1135
    // TODO: eviter les doublons lors du renommage
1174
    // TODO: eviter les doublons lors du renommage
1136
 
1175
 
1137
    public void save() {
1176
    public void save() {
1138
        String xml = this.toXML();
1177
        String xml = this.toXML();
1139
        System.out.println(xml);
1178
        System.out.println(xml);
1140
        // delete old version
-
 
1141
        deleteFromDB();
-
 
1142
        // insert new version
1179
        // insert new version
1143
        final SQLTable extensionTable = getExtensionTable();
1180
        final SQLTable extensionTable = getExtensionTable();
1144
        SQLRowValues v = new SQLRowValues(extensionTable);
1181
        SQLRowValues v = new SQLRowValues(extensionTable);
1145
        v.put("IDENTIFIER", this.getName());
1182
        v.put("IDENTIFIER", this.getName());
1146
        v.put("XML", this.toXML());
1183
        v.put("XML", xml);
1147
        try {
1184
        try {
-
 
1185
            // delete old version
-
 
1186
            deleteFromDB();
1148
            v.insert();
1187
            v.insert();
1149
            this.notSaved = false;
1188
            this.notSaved = false;
1150
        } catch (SQLException e) {
1189
        } catch (SQLException e) {
1151
            e.printStackTrace();
1190
            e.printStackTrace();
1152
            JOptionPane.showMessageDialog(new JFrame(), "Error while saving extension");
1191
            JOptionPane.showMessageDialog(new JFrame(), "Error while saving extension");
1153
        }
1192
        }
1154
 
1193
 
1155
        this.fireChanged();
1194
        this.fireChanged();
1156
    }
1195
    }
1157
 
1196
 
1158
    private SQLTable getExtensionTable() {
1197
    private SQLTable getExtensionTable() {
1159
        return ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(ExtensionBuilderModule.TABLE_NAME);
1198
        return ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(ExtensionBuilderModule.TABLE_NAME);
1160
    }
1199
    }
1161
 
1200
 
1162
    private void deleteFromDB() {
1201
    private void deleteFromDB() {
1163
        final SQLTable extensionTable = getExtensionTable();
1202
        final SQLTable extensionTable = getExtensionTable();
1164
        String query = "DELETE FROM " + extensionTable.getSQL() + " WHERE \"IDENTIFIER\" = " + SQLBase.quoteStringStd(this.getName());
1203
        String query = "DELETE FROM " + extensionTable.getSQL() + " WHERE \"IDENTIFIER\" = " + SQLBase.quoteStringStd(this.getName());
1165
        extensionTable.getDBSystemRoot().getDataSource().execute(query);
1204
        extensionTable.getDBSystemRoot().getDataSource().execute(query);
1166
    }
1205
    }
1167
 
1206
 
1168
    public List<String> getAllKnownTableNames() {
1207
    public List<String> getAllKnownTableNames() {
1169
        final List<String> l = new ArrayList<String>();
1208
        final List<String> l = new ArrayList<String>();
1170
        final Set<String> s = new HashSet<String>();
1209
        final Set<String> s = new HashSet<String>();
1171
        for (SQLTable t : AllTableListModel.getAllDatabaseTables()) {
1210
        for (SQLTable t : AllTableListModel.getAllDatabaseTables()) {
1172
            s.add(t.getName());
1211
            s.add(t.getName());
1173
        }
1212
        }
1174
        for (TableDescritor td : this.getCreateTableList()) {
1213
        for (TableDescritor td : this.getCreateTableList()) {
1175
            s.add(td.getName());
1214
            s.add(td.getName());
1176
        }
1215
        }
1177
        s.remove("FWK_MODULE_METADATA");
1216
        s.remove("FWK_MODULE_METADATA");
1178
        s.remove("FWK_SCHEMA_METADATA");
1217
        s.remove("FWK_SCHEMA_METADATA");
1179
        s.remove("FWK_UNDEFINED_IDS");
1218
        s.remove("FWK_UNDEFINED_IDS");
1180
        s.remove(ExtensionBuilderModule.TABLE_NAME);
1219
        s.remove(ExtensionBuilderModule.TABLE_NAME);
1181
        l.addAll(s);
1220
        l.addAll(s);
1182
        Collections.sort(l, new Comparator<String>() {
1221
        Collections.sort(l, new Comparator<String>() {
1183
            @Override
1222
            @Override
1184
            public int compare(String o1, String o2) {
1223
            public int compare(String o1, String o2) {
1185
                return o1.compareToIgnoreCase(o2);
1224
                return o1.compareToIgnoreCase(o2);
1186
            }
1225
            }
1187
        });
1226
        });
1188
 
1227
 
1189
        return l;
1228
        return l;
1190
    }
1229
    }
1191
 
1230
 
1192
    public TableDescritor getTableListDescriptor(String tableName) {
1231
    public TableDescritor getTableListDescriptor(String tableName) {
1193
        for (TableDescritor td : this.createTableList) {
1232
        for (TableDescritor td : this.createTableList) {
1194
            if (td.getName().equalsIgnoreCase(tableName)) {
1233
            if (td.getName().equalsIgnoreCase(tableName)) {
1195
                return td;
1234
                return td;
1196
            }
1235
            }
1197
        }
1236
        }
1198
        return null;
1237
        return null;
1199
    }
1238
    }
1200
 
1239
 
1201
    public List<String> getTranslatedFieldOfTable(String tableName) {
1240
    public List<String> getTranslatedFieldOfTable(String tableName) {
1202
        final List<String> l = new ArrayList<String>();
1241
        final List<String> l = new ArrayList<String>();
1203
        for (FieldTranslation tr : this.fieldTranslations) {
1242
        for (FieldTranslation tr : this.fieldTranslations) {
1204
            if (tr.getTableName().equals(tableName)) {
1243
            if (tr.getTableName().equals(tableName)) {
1205
                l.add(tr.getFieldName());
1244
                l.add(tr.getFieldName());
1206
            }
1245
            }
1207
        }
1246
        }
1208
        return l;
1247
        return l;
1209
    }
1248
    }
1210
 
1249
 
1211
    public TableTranslation getTableTranslation(String lang, String tableName) {
1250
    public TableTranslation getTableTranslation(String lang, String tableName) {
1212
        for (TableTranslation tr : this.tableTranslations) {
1251
        for (TableTranslation tr : this.tableTranslations) {
1213
            if (tr.getLocale().equals(lang) && tr.getTableName().equals(tableName)) {
1252
            if (tr.getLocale().equals(lang) && tr.getTableName().equals(tableName)) {
1214
                return tr;
1253
                return tr;
1215
            }
1254
            }
1216
        }
1255
        }
1217
        return null;
1256
        return null;
1218
    }
1257
    }
1219
 
1258
 
1220
    public String getFieldTranslation(String lang, String tableName, String fName) {
1259
    public String getFieldTranslation(String lang, String tableName, String fName) {
1221
        for (FieldTranslation tr : this.fieldTranslations) {
1260
        for (FieldTranslation tr : this.fieldTranslations) {
1222
            if (tr.getLocale().equals(lang) && tr.getTableName().equals(tableName) && tr.getFieldName().equals(fName)) {
1261
            if (tr.getLocale().equals(lang) && tr.getTableName().equals(tableName) && tr.getFieldName().equals(fName)) {
1223
                return tr.getLabel();
1262
                return tr.getLabel();
1224
            }
1263
            }
1225
        }
1264
        }
1226
        return null;
1265
        return null;
1227
    }
1266
    }
1228
 
1267
 
1229
    public List<ComponentDescritor> getCreateComponentList() {
1268
    public List<ComponentDescritor> getCreateComponentList() {
1230
        return this.createComponentList;
1269
        return this.createComponentList;
1231
    }
1270
    }
1232
 
1271
 
1233
    public void addCreateComponent(ComponentDescritor desc) {
1272
    public void addCreateComponent(ComponentDescritor desc) {
1234
        this.createComponentList.add(desc);
1273
        this.createComponentList.add(desc);
1235
    }
1274
    }
1236
 
1275
 
1237
    public void removeCreateComponent(ComponentDescritor desc) {
1276
    public void removeCreateComponent(ComponentDescritor desc) {
1238
        this.createComponentList.remove(desc);
1277
        this.createComponentList.remove(desc);
1239
        setChanged();
1278
        setChanged();
1240
    }
1279
    }
1241
 
1280
 
1242
    public List<MenuDescriptor> getCreateMenuList() {
1281
    public List<MenuDescriptor> getCreateMenuList() {
1243
        return createMenuList;
1282
        return createMenuList;
1244
    }
1283
    }
1245
 
1284
 
1246
    public void addCreateMenu(MenuDescriptor desc) {
1285
    public void addCreateMenu(MenuDescriptor desc) {
1247
        this.createMenuList.add(desc);
1286
        this.createMenuList.add(desc);
1248
    }
1287
    }
1249
 
1288
 
1250
    public MenuDescriptor getCreateMenuItemFromId(String id) {
1289
    public MenuDescriptor getCreateMenuItemFromId(String id) {
1251
        for (MenuDescriptor menuDescriptor : this.createMenuList) {
1290
        for (MenuDescriptor menuDescriptor : this.createMenuList) {
1252
            if (menuDescriptor.getId().equals(id)) {
1291
            if (menuDescriptor.getId().equals(id)) {
1253
                return menuDescriptor;
1292
                return menuDescriptor;
1254
            }
1293
            }
1255
        }
1294
        }
1256
        return null;
1295
        return null;
1257
    }
1296
    }
1258
 
1297
 
1259
    public List<MenuDescriptor> getRemoveMenuList() {
1298
    public List<MenuDescriptor> getRemoveMenuList() {
1260
        return removeMenuList;
1299
        return removeMenuList;
1261
    }
1300
    }
1262
 
1301
 
1263
    public void addRemoveMenu(MenuDescriptor desc) {
1302
    public void addRemoveMenu(MenuDescriptor desc) {
1264
        this.removeRemoveMenuForId(desc.getId());
1303
        this.removeRemoveMenuForId(desc.getId());
1265
        this.removeMenuList.add(desc);
1304
        this.removeMenuList.add(desc);
1266
    }
1305
    }
1267
 
1306
 
1268
    public List<String> getAllKnownFieldName(String tableName) {
1307
    public List<String> getAllKnownFieldName(String tableName) {
1269
        final Set<String> l = new HashSet<String>();
1308
        final Set<String> l = new HashSet<String>();
1270
        // fields created in the extension
1309
        // fields created in the extension
1271
 
1310
 
1272
        final List<TableDescritor> desc = getCreateTableList();
1311
        final List<TableDescritor> desc = getCreateTableList();
1273
        for (TableDescritor tableDescritor : desc) {
1312
        for (TableDescritor tableDescritor : desc) {
1274
            if (tableDescritor.getName().equals(tableName)) {
1313
            if (tableDescritor.getName().equals(tableName)) {
1275
                final List<FieldDescriptor> fDescs = tableDescritor.getFields();
1314
                final List<FieldDescriptor> fDescs = tableDescritor.getFields();
1276
                for (FieldDescriptor fieldDescriptor : fDescs) {
1315
                for (FieldDescriptor fieldDescriptor : fDescs) {
1277
                    l.add(fieldDescriptor.getName());
1316
                    l.add(fieldDescriptor.getName());
1278
                }
1317
                }
1279
            }
1318
            }
1280
        }
1319
        }
1281
        // + champs dans la base
1320
        // + champs dans la base
1282
        final Set<SQLTable> tables = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTables();
1321
        final Set<SQLTable> tables = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTables();
1283
        for (SQLTable sqlTable : tables) {
1322
        for (SQLTable sqlTable : tables) {
1284
            final String tName = sqlTable.getName();
1323
            final String tName = sqlTable.getName();
1285
            if (tName.equals(tableName)) {
1324
            if (tName.equals(tableName)) {
1286
                Set<String> f = sqlTable.getFieldsName();
1325
                Set<String> f = sqlTable.getFieldsName();
1287
                for (String string : f) {
1326
                for (String string : f) {
1288
                    l.add(string);
1327
                    l.add(string);
1289
                }
1328
                }
1290
 
1329
 
1291
            }
1330
            }
1292
        }
1331
        }
1293
 
1332
 
1294
        return new ArrayList<String>(l);
1333
        return new ArrayList<String>(l);
1295
    }
1334
    }
1296
 
1335
 
1297
    public List<String> getAllKnownActionNames() {
1336
    public List<String> getAllKnownActionNames() {
1298
        ArrayList<String> s = new ArrayList<String>();
1337
        ArrayList<String> s = new ArrayList<String>();
1299
        Collection<SQLElement> elements = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElements();
1338
        Collection<SQLElement> elements = ComptaPropsConfiguration.getInstanceCompta().getDirectory().getElements();
1300
        for (SQLElement element : elements) {
1339
        for (SQLElement element : elements) {
1301
            Collection<IListeAction> actions = element.getRowActions();
1340
            Collection<IListeAction> actions = element.getRowActions();
1302
            for (IListeAction action : actions) {
1341
            for (IListeAction action : actions) {
1303
                if (action instanceof RowAction) {
1342
                if (action instanceof RowAction) {
1304
                    RowAction rAction = (RowAction) action;
1343
                    RowAction rAction = (RowAction) action;
1305
                    final String id = rAction.getID();
1344
                    final String id = rAction.getID();
1306
                    if (id != null)
1345
                    if (id != null)
1307
                        s.add(id);
1346
                        s.add(id);
1308
                }
1347
                }
1309
            }
1348
            }
1310
        }
1349
        }
1311
        Collections.sort(s);
1350
        Collections.sort(s);
1312
        return s;
1351
        return s;
1313
    }
1352
    }
1314
 
1353
 
1315
    public List<String> getActionNames() {
1354
    public List<String> getActionNames() {
1316
        ArrayList<String> s = new ArrayList<String>();
1355
        ArrayList<String> s = new ArrayList<String>();
1317
        for (ActionDescriptor action : this.createActionList) {
1356
        for (ActionDescriptor action : this.createActionList) {
1318
            s.add(action.getId());
1357
            s.add(action.getId());
1319
        }
1358
        }
1320
        Collections.sort(s);
1359
        Collections.sort(s);
1321
        return s;
1360
        return s;
1322
    }
1361
    }
1323
 
1362
 
1324
    public List<ActionDescriptor> getActionDescriptors() {
1363
    public List<ActionDescriptor> getActionDescriptors() {
1325
        Collections.sort(this.createActionList, new Comparator<ActionDescriptor>() {
1364
        Collections.sort(this.createActionList, new Comparator<ActionDescriptor>() {
1326
 
1365
 
1327
            @Override
1366
            @Override
1328
            public int compare(ActionDescriptor o1, ActionDescriptor o2) {
1367
            public int compare(ActionDescriptor o1, ActionDescriptor o2) {
1329
                return o1.getId().compareTo(o2.getId());
1368
                return o1.getId().compareTo(o2.getId());
1330
            }
1369
            }
1331
        });
1370
        });
1332
        return this.createActionList;
1371
        return this.createActionList;
1333
    }
1372
    }
1334
 
1373
 
1335
    public boolean isEmpty() {
1374
    public boolean isEmpty() {
1336
        return createTableList.isEmpty() && modifyTableList.isEmpty() && createListList.isEmpty() && tableTranslations.isEmpty() && fieldTranslations.isEmpty() && menuTranslations.isEmpty()
1375
        return createTableList.isEmpty() && modifyTableList.isEmpty() && createListList.isEmpty() && tableTranslations.isEmpty() && fieldTranslations.isEmpty() && menuTranslations.isEmpty()
1337
                && actionTranslations.isEmpty() && createComponentList.isEmpty() && modifyComponentList.isEmpty() && createMenuList.isEmpty() && removeMenuList.isEmpty() && createActionList.isEmpty();
1376
                && actionTranslations.isEmpty() && createComponentList.isEmpty() && modifyComponentList.isEmpty() && createMenuList.isEmpty() && removeMenuList.isEmpty() && createActionList.isEmpty();
1338
    }
1377
    }
1339
 
1378
 
1340
    public String getTableNameForElementId(String id) {
1379
    public String getTableNameForElementId(String id) {
1341
        for (ElementDescriptor element : this.elementList) {
1380
        for (ElementDescriptor element : this.elementList) {
1342
            if (element.getId().equals(id)) {
1381
            if (element.getId().equals(id)) {
1343
                return element.getTableName();
1382
                return element.getTableName();
1344
            }
1383
            }
1345
        }
1384
        }
1346
        return null;
1385
        return null;
1347
    }
1386
    }
1348
 
1387
 
1349
    private String getRefFromTable(String table) {
1388
    private String getRefFromTable(String table) {
1350
        for (ElementDescriptor element : this.elementList) {
1389
        for (ElementDescriptor element : this.elementList) {
1351
            if (element.getTableName().equals(table)) {
1390
            if (element.getTableName().equals(table)) {
1352
                return element.getId();
1391
                return element.getId();
1353
            }
1392
            }
1354
        }
1393
        }
1355
        return null;
1394
        return null;
1356
    }
1395
    }
1357
 
1396
 
1358
    public List<MenuTranslation> getMenuTranslations() {
1397
    public List<MenuTranslation> getMenuTranslations() {
1359
        return menuTranslations;
1398
        return menuTranslations;
1360
    }
1399
    }
1361
 
1400
 
1362
    public List<ActionTranslation> getActionTranslations() {
1401
    public List<ActionTranslation> getActionTranslations() {
1363
        return actionTranslations;
1402
        return actionTranslations;
1364
    }
1403
    }
1365
 
1404
 
1366
    public List<FieldTranslation> getFieldTranslations() {
1405
    public List<FieldTranslation> getFieldTranslations() {
1367
        return fieldTranslations;
1406
        return fieldTranslations;
1368
    }
1407
    }
1369
 
1408
 
1370
    public void removeChangeListener(ChangeListener listener) {
1409
    public void removeChangeListener(ChangeListener listener) {
1371
        this.listeners.remove(listener);
1410
        this.listeners.remove(listener);
1372
 
1411
 
1373
    }
1412
    }
1374
 
1413
 
1375
    public void setName(String name) {
1414
    public void setName(String name) {
1376
        if (!name.equals(this.name)) {
1415
        if (!name.equals(this.name)) {
1377
            deleteFromDB();
1416
            deleteFromDB();
1378
            this.name = name;
1417
            this.name = name;
1379
            save();
1418
            save();
1380
            setChanged();
1419
            setChanged();
1381
        }
1420
        }
1382
    }
1421
    }
1383
 
1422
 
1384
    public void setupMenu(MenuContext ctxt) {
1423
    public void setupMenu(MenuContext ctxt) {
1385
        final Group group = ctxt.getMenuAndActions().getGroup();
1424
        final Group group = ctxt.getMenuAndActions().getGroup();
1386
        initMenuGroup(group);
1425
        initMenuGroup(group);
1387
        registerMenuActions(ctxt.getMenuAndActions());
1426
        registerMenuActions(ctxt.getMenuAndActions());
1388
    }
1427
    }
1389
 
1428
 
1390
    public void removeRemoveMenuForId(String id) {
1429
    public void removeRemoveMenuForId(String id) {
1391
        for (int i = removeMenuList.size() - 1; i >= 0; i--) {
1430
        for (int i = removeMenuList.size() - 1; i >= 0; i--) {
1392
            final MenuDescriptor m = removeMenuList.get(i);
1431
            final MenuDescriptor m = removeMenuList.get(i);
1393
            if (m.getId().equals(id)) {
1432
            if (m.getId().equals(id)) {
1394
                removeMenuList.remove(i);
1433
                removeMenuList.remove(i);
1395
            }
1434
            }
1396
        }
1435
        }
1397
 
1436
 
1398
    }
1437
    }
1399
 
1438
 
1400
    public void removeCreateMenuForId(String id) {
1439
    public void removeCreateMenuForId(String id) {
1401
        for (int i = createMenuList.size() - 1; i >= 0; i--) {
1440
        for (int i = createMenuList.size() - 1; i >= 0; i--) {
1402
            final MenuDescriptor m = createMenuList.get(i);
1441
            final MenuDescriptor m = createMenuList.get(i);
1403
            if (m.getId().equals(id)) {
1442
            if (m.getId().equals(id)) {
1404
                createMenuList.remove(i);
1443
                createMenuList.remove(i);
1405
            }
1444
            }
1406
        }
1445
        }
1407
 
1446
 
1408
    }
1447
    }
1409
 
1448
 
1410
    public MenuDescriptor getRemoveMenuItemFromId(String itemId) {
1449
    public MenuDescriptor getRemoveMenuItemFromId(String itemId) {
1411
        for (MenuDescriptor m : removeMenuList) {
1450
        for (MenuDescriptor m : removeMenuList) {
1412
            if (m.getId().equals(itemId)) {
1451
            if (m.getId().equals(itemId)) {
1413
                return m;
1452
                return m;
1414
            }
1453
            }
1415
        }
1454
        }
1416
        return null;
1455
        return null;
1417
    }
1456
    }
1418
 
1457
 
1419
    public void renameMenuItem(String previousId, String newId) {
1458
    public void renameMenuItem(String previousId, String newId) {
1420
        if (!previousId.equals(newId)) {
1459
        if (!previousId.equals(newId)) {
1421
            final List<MenuDescriptor> descs = new ArrayList<MenuDescriptor>(createMenuList.size() + removeMenuList.size());
1460
            final List<MenuDescriptor> descs = new ArrayList<MenuDescriptor>(createMenuList.size() + removeMenuList.size());
1422
            descs.addAll(createMenuList);
1461
            descs.addAll(createMenuList);
1423
            descs.addAll(removeMenuList);
1462
            descs.addAll(removeMenuList);
1424
            for (MenuDescriptor m : descs) {
1463
            for (MenuDescriptor m : descs) {
1425
                if (m.getId().equals(previousId)) {
1464
                if (m.getId().equals(previousId)) {
1426
                    m.setId(newId);
1465
                    m.setId(newId);
1427
                }
1466
                }
1428
            }
1467
            }
1429
 
1468
 
1430
        }
1469
        }
1431
 
1470
 
1432
    }
1471
    }
1433
 
1472
 
1434
    public void moveMenuItem(String itemId, String parentId) {
1473
    public void moveMenuItem(String itemId, String parentId) {
1435
        for (MenuDescriptor m : createMenuList) {
1474
        for (MenuDescriptor m : createMenuList) {
1436
            if (m.getId().equals(itemId)) {
1475
            if (m.getId().equals(itemId)) {
1437
                m.setInsertInMenu(parentId);
1476
                m.setInsertInMenu(parentId);
1438
            }
1477
            }
1439
        }
1478
        }
1440
 
1479
 
1441
    }
1480
    }
1442
 
1481
 
1443
    public void setMenuTranslation(String id, String text, Locale locale) {
1482
    public void setMenuTranslation(String id, String text, Locale locale) {
1444
        MenuTranslation mTranslation = null;
1483
        MenuTranslation mTranslation = null;
1445
        for (MenuTranslation mTr : this.menuTranslations) {
1484
        for (MenuTranslation mTr : this.menuTranslations) {
1446
            if (mTr.getId().equals(id) && mTr.getLocale().equals(locale.toString())) {
1485
            if (mTr.getId().equals(id) && mTr.getLocale().equals(locale.toString())) {
1447
                mTranslation = mTr;
1486
                mTranslation = mTr;
1448
                break;
1487
                break;
1449
            }
1488
            }
1450
 
1489
 
1451
        }
1490
        }
1452
        if (mTranslation == null) {
1491
        if (mTranslation == null) {
1453
            mTranslation = new MenuTranslation(locale.toString(), id);
1492
            mTranslation = new MenuTranslation(locale.toString(), id);
1454
            this.menuTranslations.add(mTranslation);
1493
            this.menuTranslations.add(mTranslation);
1455
        }
1494
        }
1456
        mTranslation.setLabel(text);
1495
        mTranslation.setLabel(text);
1457
 
1496
 
1458
    }
1497
    }
1459
 
1498
 
1460
    private TableTranslation getTableTranslation(String tableName, Locale locale) {
1499
    private TableTranslation getTableTranslation(String tableName, Locale locale) {
1461
        TableTranslation tTranslation = null;
1500
        TableTranslation tTranslation = null;
1462
        for (TableTranslation mTr : this.tableTranslations) {
1501
        for (TableTranslation mTr : this.tableTranslations) {
1463
            if (mTr.getTableName().equals(tableName) && mTr.getLocale().equals(locale.toString())) {
1502
            if (mTr.getTableName().equals(tableName) && mTr.getLocale().equals(locale.toString())) {
1464
                tTranslation = mTr;
1503
                tTranslation = mTr;
1465
                break;
1504
                break;
1466
            }
1505
            }
1467
        }
1506
        }
1468
        if (tTranslation == null) {
1507
        if (tTranslation == null) {
1469
            tTranslation = new TableTranslation(locale.toString(), tableName);
1508
            tTranslation = new TableTranslation(locale.toString(), tableName);
1470
            this.tableTranslations.add(tTranslation);
1509
            this.tableTranslations.add(tTranslation);
1471
        }
1510
        }
1472
        return tTranslation;
1511
        return tTranslation;
1473
    }
1512
    }
1474
 
1513
 
1475
    public void setTableSingularTranslation(String tableName, Locale locale, String text) {
1514
    public void setTableSingularTranslation(String tableName, Locale locale, String text) {
1476
        TableTranslation mTranslation = getTableTranslation(tableName, locale);
1515
        TableTranslation mTranslation = getTableTranslation(tableName, locale);
1477
        mTranslation.setSingular(text);
1516
        mTranslation.setSingular(text);
1478
 
1517
 
1479
    }
1518
    }
1480
 
1519
 
1481
    public void setTablePluralTranslation(String tableName, Locale locale, String text) {
1520
    public void setTablePluralTranslation(String tableName, Locale locale, String text) {
1482
        TableTranslation mTranslation = getTableTranslation(tableName, locale);
1521
        TableTranslation mTranslation = getTableTranslation(tableName, locale);
1483
        mTranslation.setPlural(text);
1522
        mTranslation.setPlural(text);
1484
 
1523
 
1485
    }
1524
    }
1486
 
1525
 
1487
    public void setFieldTranslation(String tableName, String fieldName, Locale locale, String text) {
1526
    public void setFieldTranslation(String tableName, String fieldName, Locale locale, String text) {
1488
        System.err.println("Extension.setFieldTranslation()" + tableName + " " + fieldName + " " + locale + " : " + text);
-
 
1489
        FieldTranslation fTranslation = null;
1527
        FieldTranslation fTranslation = null;
1490
        for (FieldTranslation mTr : this.fieldTranslations) {
1528
        for (FieldTranslation mTr : this.fieldTranslations) {
1491
            if (mTr.getTableName().equals(tableName) && mTr.getFieldName().equals(fieldName) && mTr.getLocale().equals(locale.toString())) {
1529
            if (mTr.getTableName().equals(tableName) && mTr.getFieldName().equals(fieldName) && mTr.getLocale().equals(locale.toString())) {
1492
                fTranslation = mTr;
1530
                fTranslation = mTr;
1493
                break;
1531
                break;
1494
            }
1532
            }
1495
        }
1533
        }
1496
        if (fTranslation == null) {
1534
        if (fTranslation == null) {
1497
            fTranslation = new FieldTranslation(locale.toString(), tableName, fieldName);
1535
            fTranslation = new FieldTranslation(locale.toString(), tableName, fieldName);
1498
            this.fieldTranslations.add(fTranslation);
1536
            this.fieldTranslations.add(fTranslation);
1499
        }
1537
        }
1500
        fTranslation.setLabel(text);
1538
        fTranslation.setLabel(text);
1501
 
1539
 
1502
    }
1540
    }
1503
}
1541
}