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