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 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 139 Rev 153
Line 59... Line 59...
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;
Line 80... Line 81...
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
Line 151... Line 156...
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);
Line 164... Line 169...
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
            }
Line 179... Line 184...
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
                        }
Line 214... Line 242...
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
                            }
Line 249... Line 271...
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++) {
Line 269... Line 319...
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
                    }
Line 291... Line 353...
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)) {
Line 1135... Line 1174...
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");
Line 1483... Line 1522...
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;