]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ApplicationSettings.java
Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ApplicationSettings.java
1 package org.splat.simer;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.LinkedHashMap;
9 import java.util.List;
10 import java.util.Locale;
11 import java.util.Map;
12 import java.util.Properties;
13 import java.util.Set;
14 import java.util.Vector;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
20 import org.w3c.dom.NamedNodeMap;
21
22 import org.apache.log4j.Logger;
23 import org.splat.dal.bo.kernel.User;
24 import org.splat.manox.XDOM;
25 import org.splat.dal.bo.som.Document;
26 import org.splat.som.DocumentRights;
27 import org.splat.dal.bo.som.DocumentType;
28 import org.splat.dal.bo.som.KnowledgeElement;
29 import org.splat.dal.bo.som.ProgressState;
30 import org.splat.service.DocumentTypeService;
31 import org.splat.service.technical.ProjectSettingsService;
32 import org.splat.dal.bo.som.SimulationContext;
33 import org.splat.som.Step;
34 import org.splat.som.StudyRights;
35 import org.splat.wapp.MenuItem;
36 import org.splat.wapp.PopupMenu;
37 import org.splat.wapp.PopupItem;
38 import org.splat.wapp.SimpleMenu;
39 import org.splat.wapp.ToolBar;
40
41 public class ApplicationSettings {
42
43         /**
44          * Application settings logger.
45          */
46         protected final static Logger logger = Logger
47                         .getLogger(ApplicationSettings.class);
48
49         private String wappserver;
50         private String wappname;
51         private Properties wapprops; // General properties from the application properties files
52         private Locale locale; // Current user locale
53         private Map<String, SimpleMenu> menus = null; // Application menus
54         private Map<Integer, ToolBar> bars = null; // Study module-bars structured by steps
55         private Map<String, PopupMenu> popups = null;
56         private Map<String, Map<String, Object>> filter = null; // Named search filters
57         private Map<String, DocumentType> defdoctype = null; // Default document types structured by step.formats
58         private Map<String, String> tempfile = null; // Available template files
59         private String[] viewermap = null; // List of file extensions mapped to a viewer
60         private Map<String, Converter> convertmap = null; // Available document format converters
61         private Properties jndprops = null; // JNDI context for launching converters
62
63         private static ApplicationSettings my = null; // Singleton instance
64         /**
65          * Injected project settings service.
66          */
67         private ProjectSettingsService _projectSettingsService;
68         /**
69          * Injected document type service.
70          */
71         private DocumentTypeService _documentTypeService;
72
73         /**
74          * Get the projectSettingsService.
75          * 
76          * @return the projectSettingsService
77          */
78         public ProjectSettingsService getProjectSettings() {
79                 return _projectSettingsService;
80         }
81
82         /**
83          * Set the projectSettingsService.
84          * 
85          * @param projectSettingsService
86          *            the projectSettingsService to set
87          */
88         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
89                 _projectSettingsService = projectSettingsService;
90         }
91
92         private static class NewMenu extends SimpleMenu {
93                 // -----------------------------------------------------------------
94                 private NewMenu() {
95                         super("create");
96                         addItem("new-empty", "menu.new.empty", "image.empty.png",
97                                         "select?menu=create&item=new-empty");
98                         addItem("new-copy", new MenuItem("menu.new.copy")
99                                         .icon("image.copy.png"));
100                         addItem("new-instance", new MenuItem("menu.new.instance")
101                                         .icon("image.hold.gif"));
102                         addItem("new-import", new MenuItem("menu.new.import")
103                                         .icon("icon.upload.png"));
104                         this.selects("new-empty");
105                 }
106         }
107
108         private static class SearchMenu extends SimpleMenu {
109                 // -----------------------------------------------------------------
110                 private SearchMenu() {
111                         super("search");
112                         addItem("search-study", "menu.search.study", "image.study.png",
113                                         "select?menu=search&item=search-study");
114                         addItem("search-knowledge", "menu.search.idea", "image.idea.png",
115                                         "select?menu=search&item=search-knowledge");
116                         addItem("search-document", new MenuItem("menu.search.document")
117                                         .icon("icon.any.png"));
118                         this.selects("search-study");
119                 }
120         }
121
122         private static class PropertiesMenu extends SimpleMenu {
123                 // -----------------------------------------------------------------
124                 private PropertiesMenu() {
125                         super("configuration");
126                         addItem("prop-general", "menu.prop.general", "image.hold.gif",
127                                         "select?menu=properties&item=prop-general");
128                         addItem("prop-scenario", "menu.prop.scenario", "image.hold.gif",
129                                         "select?menu=properties&item=prop-scenario");
130                         addItem("prop-timestamp", new MenuItem("menu.prop.timestamp")
131                                         .icon("image.stamp.png"));
132                         addItem("prop-comlog", new MenuItem("menu.prop.comlog")
133                                         .icon("image.hold.gif"));
134                         addItem("prop-version", new MenuItem("menu.prop.version")
135                                         .icon("image.dirclosed.png"));
136                 }
137         }
138
139         private static class DatadminMenu extends SimpleMenu {
140                 // -----------------------------------------------------------------
141                 private DatadminMenu() {
142                         super("datadmin");
143                         addItem("admin-scontext", "menu.admin.context", "image.hold.gif",
144                                         "select?menu=datadmin&item=admin-scontext");
145                         addItem("admin-knowelm", "menu.admin.knowledge", "image.idea.png",
146                                         "select?menu=datadmin&item=admin-knowelm");
147                         addItem("admin-study", new MenuItem("menu.admin.study")
148                                         .icon("image.study.png"));
149                 }
150         }
151
152         private static class SysadminMenu extends SimpleMenu {
153                 // -----------------------------------------------------------------
154                 private SysadminMenu() {
155                         super("sysadmin");
156                         addItem("admin-indexing", "menu.admin.indexing", "image.index.png",
157                                         "select?menu=sysadmin&item=admin-indexing");
158                         addItem("admin-importuser", "menu.admin.importuser",
159                                         "image.group.png",
160                                         "select?menu=sysadmin&item=admin-importuser");
161                 }
162         }
163
164         private enum Item {
165                 publish, accept, approve, promote, demote, undo, rename, attach, edit, script, version, replace, export, remove, purge
166         };
167
168         // Resources relative to studies
169         private static class EditableStudyPopup extends PopupMenu {
170                 // ----------------------------------------------------------------
171                 private StudyRights user = null;
172
173                 private EditableStudyPopup() {
174                         addItem("publish", new PopupItem("menu.publish").icon(
175                                         "image.publish.png").action("edit-study?action=publish")
176                                         .confirmation("message.publish.study"));
177                         addItem("promote", new PopupItem("menu.archive"));
178                         addSeparator();
179                         addItem("edit", new PopupItem("menu.properties")
180                                         .icon("icon.ed.png").action("../select?menu=properties"));
181                         addSeparator();
182                         addItem("script", new PopupItem("menu.newscenario")
183                                         .action("add-scenario"));
184                         addItem("version", new PopupItem("menu.version").icon(
185                                         "image.version.png").action("notyetimplemented"));
186                         addSeparator();
187                         addItem("purge", new PopupItem("menu.purge")
188                                         .confirmation("message.purge.study"));
189                         addItem("export", new PopupItem("menu.export")
190                                         .icon("image.export.png")); // For future needs
191                         addItem("remove", new PopupItem("menu.remove.version").icon(
192                                         "icon.delete.png").action("notyetimplemented")
193                                         .confirmation("message.delete.study"));
194                 }
195
196                 public boolean isEnabled(String name) {
197                         if (user == null)
198                                 return false; // Should not happen
199                         Item item = Item.valueOf(name);
200                         if (item == Item.publish)
201                                 return user.canPublish();
202                         if (item == Item.edit)
203                                 return user.canEditProperties();
204                         if (item == Item.script)
205                                 return user.canAddScenario();
206                         if (item == Item.version)
207                                 return user.canVersion();
208                         if (item == Item.remove)
209                                 return user.canRemove();
210                         if (item == Item.purge)
211                                 return user.canPurge();
212                         return false;
213                 }
214
215                 public void setContext(String name, Object context) {
216                         if (context instanceof StudyRights) {
217                                 user = (StudyRights) context; // Just for optimizing
218                                 boolean history = user.getOperand().isVersioned();
219                                 PopupItem item = this.item("remove");
220                                 if (history)
221                                         item.rename("menu.remove.version");
222                                 else
223                                         item.rename("menu.remove.study");
224                         }
225                 }
226         }
227
228         // Resources relative to documents
229         private static class EditableDocumentPopup extends PopupMenu { // Popup of In-Work documents
230         // ----------------------------------------------------------------
231                 private DocumentRights user = null;
232
233                 private EditableDocumentPopup() {
234                         addItem("accept", new PopupItem("menu.accept").icon(
235                                         "image.accept.png").action("setDocument?action=accept")
236                                         .confirmation("message.accept.document"));
237                         addItem("promote", new PopupItem("menu.promote").icon(
238                                         "image.publish.png").action("setDocument?action=promote")
239                                         .confirmation("message.promote.document"));
240                         addSeparator();
241                         addItem("rename", new PopupItem("menu.rename")
242                                         .action("edit-document?action=renameDocument"));
243                         addItem("attach", new PopupItem("menu.attach").icon(
244                                         "image.attach.png").action("select-file?nextAction=attach"));
245                         addSeparator();
246                         addItem("version", new PopupItem("menu.version").icon(
247                                         "image.version.png").action(
248                                         "select-file?nextAction=version"));
249                         addItem("replace", new PopupItem("menu.replace").icon(
250                                         "image.replace.png").action(
251                                         "select-file?nextAction=replace"));
252                         addSeparator();
253                         addItem("purge", new PopupItem("menu.purge").action(
254                                         "notyetimplemented").confirmation("message.purge.document"));
255                         addItem("remove", new PopupItem("menu.remove.version").icon(
256                                         "icon.delete.png").action("remove-document").confirmation(
257                                         "message.delete.document"));
258                 }
259
260                 public boolean isEnabled(String name) {
261                         if (user == null)
262                                 return false; // Should not happen
263                         Item item = Item.valueOf(name);
264                         if (item == Item.accept)
265                                 return user.canAccept();
266                         if (item == Item.promote)
267                                 return user.canPromote();
268                         if (item == Item.rename)
269                                 return user.canRename();
270                         if (item == Item.attach)
271                                 return user.canAttach();
272                         if (item == Item.version)
273                                 return user.canVersion();
274                         if (item == Item.replace)
275                                 return user.canReplace();
276                         if (item == Item.purge)
277                                 return user.canPurge();
278                         if (item == Item.remove)
279                                 return user.canRemove();
280                         return false;
281                 }
282
283                 public void setContext(String name, Object context) {
284                         if (context instanceof DocumentRights) {
285                                 user = (DocumentRights) context; // Just for optimizing
286                                 Document downer = user.getOperand();
287                                 PopupItem item = this.item("remove");
288                                 if (downer.isVersioned())
289                                         item.rename("menu.remove.version");
290                                 else
291                                         item.rename("menu.remove.document");
292                         }
293                 }
294         }
295
296         private static class ReviewableDocumentPopup extends PopupMenu { // Popup of In-Draft documents
297         // ----------------------------------------------------------------
298                 private DocumentRights user = null;
299
300                 private ReviewableDocumentPopup() {
301                         addItem("demote", new PopupItem("menu.demote").icon(
302                                         "image.demote.png").action("setDocument?action=demote")
303                                         .confirmation("message.demote.document"));
304                         addItem("promote", new PopupItem("menu.review").icon(
305                                         "image.review.png").action("setDocument?action=review")
306                                         .confirmation("message.review.document"));
307                         addSeparator();
308                         addItem("attach", new PopupItem("menu.attach").icon(
309                                         "image.attach.png").action("select-file?nextAction=attach"));
310                         addSeparator();
311                         addItem("version", new PopupItem("menu.version").icon(
312                                         "image.version.png").action(
313                                         "select-file?nextAction=version"));
314                         addSeparator();
315                         addItem("purge", new PopupItem("menu.purge").action(
316                                         "notyetimplemented").confirmation("message.purge.document"));
317                 }
318
319                 public boolean isEnabled(String name) {
320                         if (user == null)
321                                 return false; // Should not happen
322                         Item item = Item.valueOf(name);
323                         if (item == Item.demote)
324                                 return user.canDemote();
325                         if (item == Item.promote)
326                                 return user.canReview();
327                         if (item == Item.attach)
328                                 return user.canAttach();
329                         if (item == Item.version)
330                                 return user.canVersion();
331                         if (item == Item.purge)
332                                 return user.canPurge();
333                         return false;
334                 }
335
336                 public void setContext(String name, Object context) {
337                         if (context instanceof DocumentRights) {
338                                 user = (DocumentRights) context; // Just for optimizing
339                         }
340                 }
341         }
342
343         private static class NotResultDocumentPopup extends PopupMenu {
344                 // ----------------------------------------------------------------
345                 private DocumentRights user = null;
346
347                 private NotResultDocumentPopup() {
348                         addItem("demote", new PopupItem("menu.demote").icon(
349                                         "image.demote.png").action("setDocument?action=demote")
350                                         .confirmation("message.demote.document"));
351                         addSeparator();
352                         addItem("attach", new PopupItem("menu.attach").icon(
353                                         "image.attach.png").action("select-file?nextAction=attach"));
354                         addSeparator();
355                         addItem("version", new PopupItem("menu.version").icon(
356                                         "image.version.png").action(
357                                         "select-file?nextAction=version"));
358                         addSeparator();
359                         addItem("purge", new PopupItem("menu.purge").action(
360                                         "notyetimplemented").confirmation("message.purge.document"));
361                         addItem("remove", new PopupItem("menu.remove.version").icon(
362                                         "icon.delete.png").action("remove-document").confirmation(
363                                         "message.delete.document"));
364                 }
365         }
366
367         private static class ApprovableDocumentPopup extends PopupMenu { // Popup of In-Check documents
368         // ----------------------------------------------------------------
369                 private DocumentRights user = null;
370
371                 private ApprovableDocumentPopup() {
372                         addItem("undo", new PopupItem("menu.demote").icon(
373                                         "image.invalidate.png").action(
374                                         "setDocument?action=invalidate").confirmation(
375                                         "message.demote.document"));
376                         addItem("demote", new PopupItem("menu.disapprove").icon(
377                                         "image.demote.png").action("setDocument?action=disapprove")
378                                         .confirmation("message.disapprove.document"));
379                         addItem("approve", new PopupItem("menu.approve").icon(
380                                         "icon.APPROVED.png").action("setDocument?action=approve")
381                                         .confirmation("message.approve.document"));
382                 }
383
384                 public boolean isEnabled(String name) {
385                         if (user == null)
386                                 return false; // Should not happen
387                         Item item = Item.valueOf(name);
388                         if (item == Item.undo)
389                                 return user.canInvalidate();
390                         if (item == Item.demote)
391                                 return user.canDemote();
392                         if (item == Item.approve)
393                                 return user.canApprove();
394                         return false;
395                 }
396
397                 public void setContext(String name, Object context) {
398                         if (context instanceof DocumentRights) {
399                                 user = (DocumentRights) context; // Just for optimizing
400                         }
401                 }
402         }
403
404         private static class ApprovedPopup extends PopupMenu { // Popup of Approved documents
405         // ----------------------------------------------------------------
406                 private ApprovedPopup() {
407                         addItem("attach", new PopupItem("menu.attach").icon(
408                                         "image.attach.png").action("select-file?nextAction=attach"));
409                         addSeparator();
410                         addItem("version", new PopupItem("menu.version").icon(
411                                         "image.version.png").action(
412                                         "select-file?nextAction=version"));
413                 }
414         }
415
416         private static class ExternPopup extends PopupMenu { // Popup of Extern documents
417         // ----------------------------------------------------------------
418                 private DocumentRights user = null;
419
420                 private ExternPopup() {
421                         addItem("rename", new PopupItem("menu.rename")
422                                         .action("edit-document?action=renameDocument"));
423                         addItem("replace", new PopupItem("menu.replace").icon(
424                                         "image.replace.png").action(
425                                         "select-file?nextAction=replace"));
426                         addSeparator();
427                         addItem("remove", new PopupItem("menu.remove.document").icon(
428                                         "icon.delete.png").action("remove-document").confirmation(
429                                         "message.delete.document"));
430                 }
431
432                 public boolean isEnabled(String name) {
433                         if (user == null)
434                                 return false; // Should not happen
435                         Item item = Item.valueOf(name);
436                         if (item == Item.rename)
437                                 return user.canRename();
438                         if (item == Item.replace)
439                                 return user.canReplace();
440                         if (item == Item.remove)
441                                 return user.canRemove();
442                         return false;
443                 }
444
445                 public void setContext(String name, Object context) {
446                         if (context instanceof DocumentRights) {
447                                 user = (DocumentRights) context; // Just for optimizing
448                         }
449                 }
450         }
451
452         // Resources relative to simulation contexts
453         private static class ScontextPopup extends PopupMenu {
454                 // --------------------------------------------------------
455                 private SimulationContextFacade owner = null;
456
457                 private ScontextPopup() {
458                         addItem("rename", new PopupItem("menu.rename")
459                                         .action("edit-context?action=renameContext"));
460                         addItem("edit", new PopupItem("menu.edit")
461                                         .action("edit-context?action=editContext"));
462                         addSeparator();
463                         addItem("remove", new PopupItem("menu.remove").icon(
464                                         "icon.delete.png").action("remove-context").confirmation(
465                                         "message.delete.context"));
466                 }
467
468                 public boolean isEnabled(String name) {
469                         Item item = Item.valueOf(name);
470
471                         if (item == Item.rename) {
472                                 return false;
473                         } else if (item == Item.edit) {
474                                 // if (!owner.isEditable())
475                                 return false;
476                         }
477                         return true;
478                 }
479
480                 public void setContext(String name, Object context) {
481                         if (context instanceof SimulationContextFacade) {
482                                 owner = (SimulationContextFacade) context; // Just for optimizing
483                         } else {
484                                 super.setContext(name, context);
485                         }
486                 }
487         }
488
489         // Resources relative to knowledge
490         private static class FeedbexPopup extends PopupMenu {
491                 // --------------------------------------------------------
492                 private KnowledgeElement owner = null;
493
494                 private FeedbexPopup() {
495                         addItem("promote", new PopupItem("menu.promote").icon(
496                                         "image.review.png").action("promote-knowledge")
497                                         .confirmation("message.promote.knowledge"));
498                         addItem("demote", new PopupItem("menu.demote").icon(
499                                         "image.invalidate.png").action("demote-knowledge")
500                                         .confirmation("message.demote.knowledge"));
501                         addSeparator();
502                         addItem("rename", new PopupItem("menu.rename")
503                                         .action("edit-knowledge?action=renameKnowledge"));
504                         addItem("edit", new PopupItem("menu.edit")
505                                         .action("edit-knowledge?action=editKnowledge"));
506                         addSeparator();
507                         addItem("remove", new PopupItem("menu.remove").icon(
508                                         "icon.delete.png").action("remove-knowledge").confirmation(
509                                         "message.delete.knowledge"));
510                 }
511
512                 public boolean isEnabled(String name) {
513                         Item item = Item.valueOf(name);
514
515                         if (item == Item.promote) {
516                                 if (owner.getProgressState() != ProgressState.inDRAFT)
517                                         return false;
518                         } else if (item == Item.demote) {
519                                 if (owner.getProgressState() != ProgressState.inCHECK)
520                                         return false;
521                         }
522                         return true;
523                 }
524
525                 public void setContext(String name, Object context) {
526                         if (context instanceof KnowledgeElement)
527                                 owner = (KnowledgeElement) context; // Just for optimizing
528                         else {
529                                 super.setContext(name, context);
530                         }
531                 }
532         }
533
534         // ==============================================================================================================================
535         // Construction
536         // ==============================================================================================================================
537
538         public static ApplicationSettings getMe() {
539                 // ------------------------------------------
540                 if (my == null) {
541                         my = new ApplicationSettings();
542                 }
543                 return my; // The application is supposed being previously created below
544         }
545
546         /**
547          * @param wappurl
548          * @param lang
549          * @return
550          */
551         public ApplicationSettings init(String wappurl, Locale lang)
552                         throws IOException {
553                 ClassLoader cloader = getClass().getClassLoader();
554                 String[] wurl = wappurl.split("/"); // [0]="http:", [1]="", [2]="{server}:{port}", [3]="name"
555
556                 locale = lang;
557                 wappserver = wurl[2];
558                 wappname = wurl[3];
559                 wapprops = new Properties();
560                 jndprops = new Properties();
561                 wapprops.load(cloader.getResourceAsStream(wappname + ".properties"));
562                 jndprops.load(cloader.getResourceAsStream("jndi.properties"));
563
564                 logger.info("Application root set to "
565                                 + wapprops.getProperty("wapp.root"));
566                 if (my == null) {
567                         my = this;
568                 }
569 //RKV           my = this;
570 //RKV           return this;
571                 return my;
572         }
573
574         // ==============================================================================================================================
575         // Public member functions
576         // ==============================================================================================================================
577
578         public void configure(String filename) {
579                 // ---------------------------------------
580                 // Non customizable settings
581                 menus = new HashMap<String, SimpleMenu>();
582                 SimpleMenu menu = new NewMenu();
583                 menus.put(menu.getName(), menu);
584                 menu = new SearchMenu();
585                 menus.put(menu.getName(), menu);
586                 menu = new DatadminMenu();
587                 menus.put(menu.getName(), menu);
588                 menu = new SysadminMenu();
589                 menus.put(menu.getName(), menu);
590                 menu = new PropertiesMenu();
591                 menus.put(menu.getName(), menu);
592
593                 popups = new HashMap<String, PopupMenu>();
594                 popups.put("steditable", new EditableStudyPopup());
595                 popups.put("editable", new EditableDocumentPopup());
596                 popups.put("notresult", new NotResultDocumentPopup());
597                 popups.put("reviewable", new ReviewableDocumentPopup());
598                 popups.put("approvable", new ApprovableDocumentPopup());
599                 popups.put("approved", new ApprovedPopup());
600                 popups.put("extern", new ExternPopup());
601                 popups.put("scontext", new ScontextPopup());
602                 popups.put("feedbex", new FeedbexPopup());
603
604                 // Default customizable mandatory settings
605                 Map<String, Object> fprop = new HashMap<String, Object>();
606                 fprop.put("visibility", "PRIVATE");
607                 fprop.put("matchamong", "all");
608                 fprop.put("matcontext", "all");
609                 fprop.put("state", "END");
610                 fprop.put("author", "0");
611                 fprop.put("reference", "");
612                 fprop.put("title", "");
613                 fprop.put("context", new Vector<SimulationContext>());
614
615                 Map<String, Object> gprop = new HashMap<String, Object>();
616                 gprop.put("visibility", "PUBLIC");
617                 gprop.put("matchamong", "all");
618                 gprop.put("matcontext", "all");
619                 gprop.put("type", "2"); // TODO: Get the index from the type name
620                 gprop.put("author", "0");
621                 gprop.put("reference", "");
622                 gprop.put("title", "");
623                 gprop.put("context", new Vector<SimulationContext>());
624
625                 defdoctype = new LinkedHashMap<String, DocumentType>();
626                 tempfile = new HashMap<String, String>();
627                 viewermap = new String[0];
628                 convertmap = new HashMap<String, Converter>();
629                 filter = new HashMap<String, Map<String, Object>>();
630                 filter.put("study", fprop);
631                 filter.put("knowledge", gprop);
632
633                 // Customization (must be done after above default settings)
634                 File config = new File(filename);
635                 if (config.exists()) {
636                         loadCustomization(config); // Sets default document types, installed modules and available templates
637                 } else {
638                         logger.info("Could not find the application configuration file \""
639                                         + config.getAbsolutePath() + "\", using default settings");
640                 }
641                 // Settings based on the customization
642                 bars = new HashMap<Integer, ToolBar>(); // May be empty if no module installed
643
644                 List<ProjectSettingsService.Step> steps = getProjectSettings()
645                                 .getAllSteps();
646                 for (Iterator<ProjectSettingsService.Step> i = steps.iterator(); i
647                                 .hasNext();) {
648                         ProjectSettingsService.Step step = i.next();
649                         List<String> formats = getDefaultFormats(step);
650                         if (formats.size() == 0)
651                                 continue;
652
653                         ToolBar bar = new ToolBar(24); // Height of the module-bar
654                         HashSet<String> module = new HashSet<String>(); // For not duplicating modules
655                         for (Iterator<String> j = formats.iterator(); j.hasNext();) {
656                                 String format = j.next();
657                                 String command = getApplicationProperty("executable." + format);
658                                 if (command == null)
659                                         continue; // Module not installed
660                                 if (module.contains(command))
661                                         continue;
662                                 module.add(command);
663                                 String[] parsed = command.split("/");
664                                 String[] name = parsed[parsed.length - 1].split("\\x2E");
665                                 DocumentType dtype = my.defdoctype.get(
666                                                 step.getNumber() + "." + format);
667                                 String docname = "";
668                                 if (dtype != null) {
669                                         docname = dtype.getName();
670                                 }
671                                 if (tempfile.get(docname) == null) { // No available template
672                                         String tool = parsed[parsed.length - 1];
673                                         String icon = name[0];
674                                         if (icon.equals("index")) {
675                                                 tool = parsed[parsed.length - 2];
676                                                 icon = "tool." + tool.toLowerCase() + ".png";
677                                         } else {
678                                                 icon = "tool." + icon.toLowerCase() + ".png";
679                                         }
680                                         File image = new File(ApplicationSettings
681                                                         .getApplicationSkinPath()
682                                                         + icon);
683                                         if (!image.exists())
684                                                 icon = "tool.any.png";
685                                         bar.addTool(tool, icon, command);
686                                 } else {
687                                         docname = "/jsp/newDocument.jsp?type=" + docname;
688                                         String icon = "tool." + name[0].toLowerCase() + ".png";
689                                         File image = new File(ApplicationSettings
690                                                         .getApplicationSkinPath()
691                                                         + icon);
692                                         if (!image.exists())
693                                                 icon = "tool.any.png";
694                                         bar.addTool(name[0], icon, command, docname);
695                                 }
696                         }
697                         if (!bar.isEmpty())
698                                 bars.put(step.getNumber(), bar);
699                 }
700         }
701
702         public String getApplicationProperty(String name) {
703                 // --------------------------------------------------
704                 return wapprops.getProperty(name); // May be null
705         }
706
707         public String getApplicationRootPath() {
708                 // ---------------------------------------
709                 return getApplicationProperty("wapp.root"); // The property is supposed including the Web application name
710         }
711
712         public String getApplicationURL() {
713                 // ----------------------------------
714                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
715                                 .append("/").append(wappname);
716                 return url.toString();
717         }
718
719         public Map<String, Object> getFilter(String name) {
720                 // --------------------------------------------------
721                 return filter.get(name);
722         }
723
724         public ToolBar getModuleBar(Step step) {
725                 // -----------------------------------------
726                 return bars.get(step.getNumber());
727         }
728
729         public Properties getNamingProperties() {
730                 // ----------------------------------------
731                 return jndprops;
732         }
733
734         // ==============================================================================================================================
735         // Public services
736         // ==============================================================================================================================
737
738         public static String getApplicationPluginPath() {
739                 // ------------------------------------------------
740                 return my.getApplicationRootPath() + "plugin/";
741         }
742
743         public static String getApplicationResourcePath() {
744                 // --------------------------------------------------
745                 return my.getApplicationRootPath() + "WEB-INF/classes/";
746         }
747
748         public static String getApplicationSkinPath() {
749                 // ----------------------------------------------
750                 return my.getApplicationRootPath() + "skin/";
751         }
752
753         public static Converter getConverter(DocumentType type, String format) {
754                 // -----------------------------------------------------------------------
755                 return my.convertmap.get(format + type.getName()); // May be null;
756         }
757
758         public static DocumentType getDefaultDocumentType(Step step, String format) {
759                 // ----------------------------------------------------------------------------
760                 String[] table = format.split("\\x2E");
761                 return my.defdoctype.get(step.getNumber() + "."
762                                 + table[table.length - 1]); // May be null
763         }
764
765         public static String getDownloadURL(User user) {
766                 // --------------------------------------------------
767                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
768                                 .append("/download/").append(user.getUsername()).append("/");
769                 return url.toString(); // The download Tomcat context is supposed being defined
770         }
771
772         public static Locale getCurrentLocale() {
773                 // ----------------------------------------
774                 return my.locale;
775         }
776
777         public static SimpleMenu getMenu(String name) {
778                 // ----------------------------------------------
779                 return my.menus.get(name);
780         }
781
782         public static PopupMenu getPopupMenu(String name) {
783                 // --------------------------------------------------
784                 return my.popups.get(name);
785         }
786
787         public static String getRepositoryURL() {
788                 // ----------------------------------------
789                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
790                                 .append("/repository/");
791                 return url.toString(); // The repository Tomcat context is supposed being defined
792         }
793
794         public static Locale[] getSupportedLocales() {
795                 // ---------------------------------------------
796                 String[] code = my.wapprops.getProperty("locale.supported").split(",");
797                 Locale[] result = new Locale[code.length];
798                 for (int i = 0; i < code.length; i++)
799                         result[i] = new Locale(code[i]);
800                 return result;
801         }
802
803         public static String[] getViewersMapping() {
804                 // -------------------------------------------
805                 return my.viewermap;
806         }
807
808         public static String getWebSiteURL() {
809                 // -------------------------------------
810                 return my.getApplicationProperty("wapp.website");
811         }
812
813         public static String getHelpURL() {
814                 // ----------------------------------
815                 return my.getApplicationProperty("wapp.onlinehelp");
816         }
817
818         // ==============================================================================================================================
819         // Private services
820         // ==============================================================================================================================
821
822         private List<String> getDefaultFormats(ProjectSettingsService.Step step) {
823                 // ------------------------------------------------------------------
824                 Set<String> keys = defdoctype.keySet();
825                 int number = step.getNumber();
826                 Vector<String> result = new Vector<String>();
827
828                 for (Iterator<String> i = keys.iterator(); i.hasNext();) {
829                         String[] key = i.next().split("\\x2E");
830                         if (Integer.valueOf(key[0]) != number)
831                                 continue;
832                         if (key[1].equals("pdf"))
833                                 continue; // PDF is not an authoring format
834                         result.add(key[1]); // Formats are unique
835                 }
836                 return result;
837         }
838
839         private void loadCustomization(File config) {
840                 // --------------------------------------------
841                 try {
842                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
843                                         .newInstance();
844                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
845
846                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
847                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
848                                         .getDocumentElement());
849
850                         // Default document types tag
851                         Node child = children.get("default-doctypes");
852                         NodeList nlist = child.getChildNodes();
853
854                         List<DocumentType> listype = getDocumentTypeService()
855                                         .selectAllTypes();
856                         HashMap<String, DocumentType> maptype = new HashMap<String, DocumentType>();
857                         for (Iterator<DocumentType> i = listype.iterator(); i.hasNext();) {
858                                 DocumentType type = i.next();
859                                 maptype.put(type.getName(), type);
860                         }
861                         for (int i = 0; i < nlist.getLength(); i++) {
862                                 child = nlist.item(i);
863                                 if (!child.getNodeName().equals("step"))
864                                         continue;
865
866                                 String nstep = child.getAttributes().getNamedItem("number")
867                                                 .getNodeValue();
868                                 NodeList map = child.getChildNodes();
869                                 for (int j = 0; j < map.getLength(); j++) {
870                                         child = map.item(j);
871                                         if (!child.getNodeName().equals("mapping"))
872                                                 continue;
873                                         NamedNodeMap natr = child.getAttributes();
874                                         String dext = natr.getNamedItem("extension").getNodeValue();
875                                         String type = natr.getNamedItem("type").getNodeValue();
876                                         defdoctype.put(nstep + "." + dext, maptype.get(type));
877                                 }
878                         }
879                         // Modules tag
880                         child = children.get("modules");
881                         nlist = child.getChildNodes();
882                         for (int i = 0; i < nlist.getLength(); i++) {
883                                 child = nlist.item(i);
884                                 if (!child.getNodeName().equals("mapping"))
885                                         continue;
886
887                                 NamedNodeMap natr = child.getAttributes();
888                                 String dext = natr.getNamedItem("extension").getNodeValue();
889                                 String exec = natr.getNamedItem("executable").getNodeValue();
890                                 wapprops.put("executable." + dext, exec);
891                         }
892                         // Viewer mappings tag
893                         child = children.get("viewers");
894                         viewermap = child.getAttributes().getNamedItem("extension")
895                                         .getNodeValue().split(",");
896
897                         // Converters tag
898                         child = children.get("converters");
899                         nlist = child.getChildNodes();
900                         for (int i = 0; i < nlist.getLength(); i++) {
901                                 child = nlist.item(i);
902
903                                 if (child.getNodeName().equals("geometry")) {
904                                         NamedNodeMap natr = child.getAttributes();
905                                         String from = natr.getNamedItem("from").getNodeValue();
906                                         String to = natr.getNamedItem("to").getNodeValue();
907                                         String exec = natr.getNamedItem("executable")
908                                                         .getNodeValue();
909                                         convertmap.put(from + "geometry", new Converter("geometry",
910                                                         from, to, exec));
911                                 }
912                         }
913
914                         // Templates tag
915                         child = children.get("templates");
916                         nlist = child.getChildNodes();
917                         for (int i = 0; i < nlist.getLength(); i++) {
918                                 child = nlist.item(i);
919                                 if (!child.getNodeName().equals("document"))
920                                         continue;
921
922                                 NamedNodeMap natr = child.getAttributes();
923                                 String type = natr.getNamedItem("type").getNodeValue();
924                                 String file = natr.getNamedItem("file").getNodeValue();
925                                 tempfile.put(type, file);
926                         }
927                 } catch (Exception error) {
928                         logger.info("Error in customization", error);
929                 }
930         }
931
932         /**
933          * Get the documentTypeService.
934          * 
935          * @return the documentTypeService
936          */
937         public DocumentTypeService getDocumentTypeService() {
938                 return _documentTypeService;
939         }
940
941         /**
942          * Set the documentTypeService.
943          * 
944          * @param documentTypeService
945          *            the documentTypeService to set
946          */
947         public void setDocumentTypeService(DocumentTypeService documentTypeService) {
948                 _documentTypeService = documentTypeService;
949         }
950 }