Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[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                 return my; // The application is supposed being previously created below
541         }
542
543         /**
544          * @param wappurl
545          * @param lang
546          * @return
547          */
548         public ApplicationSettings init(String wappurl, Locale lang)
549                         throws IOException {
550                 ClassLoader cloader = getClass().getClassLoader();
551                 String[] wurl = wappurl.split("/"); // [0]="http:", [1]="", [2]="{server}:{port}", [3]="name"
552
553                 locale = lang;
554                 wappserver = wurl[2];
555                 wappname = wurl[3];
556                 wapprops = new Properties();
557                 jndprops = new Properties();
558                 wapprops.load(cloader.getResourceAsStream(wappname + ".properties"));
559                 jndprops.load(cloader.getResourceAsStream("jndi.properties"));
560
561                 logger.info("Application root set to "
562                                 + wapprops.getProperty("wapp.root"));
563                 my = this;
564                 return this;
565         }
566
567         // ==============================================================================================================================
568         // Public member functions
569         // ==============================================================================================================================
570
571         public void configure(String filename) {
572                 // ---------------------------------------
573                 // Non customizable settings
574                 menus = new HashMap<String, SimpleMenu>();
575                 SimpleMenu menu = new NewMenu();
576                 menus.put(menu.getName(), menu);
577                 menu = new SearchMenu();
578                 menus.put(menu.getName(), menu);
579                 menu = new DatadminMenu();
580                 menus.put(menu.getName(), menu);
581                 menu = new SysadminMenu();
582                 menus.put(menu.getName(), menu);
583                 menu = new PropertiesMenu();
584                 menus.put(menu.getName(), menu);
585
586                 popups = new HashMap<String, PopupMenu>();
587                 popups.put("steditable", new EditableStudyPopup());
588                 popups.put("editable", new EditableDocumentPopup());
589                 popups.put("notresult", new NotResultDocumentPopup());
590                 popups.put("reviewable", new ReviewableDocumentPopup());
591                 popups.put("approvable", new ApprovableDocumentPopup());
592                 popups.put("approved", new ApprovedPopup());
593                 popups.put("extern", new ExternPopup());
594                 popups.put("scontext", new ScontextPopup());
595                 popups.put("feedbex", new FeedbexPopup());
596
597                 // Default customizable mandatory settings
598                 Map<String, Object> fprop = new HashMap<String, Object>();
599                 fprop.put("visibility", "PRIVATE");
600                 fprop.put("matchamong", "all");
601                 fprop.put("matcontext", "all");
602                 fprop.put("state", "END");
603                 fprop.put("author", "0");
604                 fprop.put("reference", "");
605                 fprop.put("title", "");
606                 fprop.put("context", new Vector<SimulationContext>());
607
608                 Map<String, Object> gprop = new HashMap<String, Object>();
609                 gprop.put("visibility", "PUBLIC");
610                 gprop.put("matchamong", "all");
611                 gprop.put("matcontext", "all");
612                 gprop.put("type", "2"); // TODO: Get the index from the type name
613                 gprop.put("author", "0");
614                 gprop.put("reference", "");
615                 gprop.put("title", "");
616                 gprop.put("context", new Vector<SimulationContext>());
617
618                 defdoctype = new LinkedHashMap<String, DocumentType>();
619                 tempfile = new HashMap<String, String>();
620                 viewermap = new String[0];
621                 convertmap = new HashMap<String, Converter>();
622                 filter = new HashMap<String, Map<String, Object>>();
623                 filter.put("study", fprop);
624                 filter.put("knowledge", gprop);
625
626                 // Customization (must be done after above default settings)
627                 File config = new File(filename);
628                 if (config.exists()) {
629                         loadCustomization(config); // Sets default document types, installed modules and available templates
630                 } else {
631                         logger.info("Could not find the application configuration file \""
632                                         + config.getAbsolutePath() + "\", using default settings");
633                 }
634                 // Settings based on the customization
635                 bars = new HashMap<Integer, ToolBar>(); // May be empty if no module installed
636
637                 List<ProjectSettingsService.Step> steps = getProjectSettings()
638                                 .getAllSteps();
639                 for (Iterator<ProjectSettingsService.Step> i = steps.iterator(); i
640                                 .hasNext();) {
641                         ProjectSettingsService.Step step = i.next();
642                         List<String> formats = getDefaultFormats(step);
643                         if (formats.size() == 0)
644                                 continue;
645
646                         ToolBar bar = new ToolBar(24); // Height of the module-bar
647                         HashSet<String> module = new HashSet<String>(); // For not duplicating modules
648                         for (Iterator<String> j = formats.iterator(); j.hasNext();) {
649                                 String format = j.next();
650                                 String command = getApplicationProperty("executable." + format);
651                                 if (command == null)
652                                         continue; // Module not installed
653                                 if (module.contains(command))
654                                         continue;
655                                 module.add(command);
656                                 String[] parsed = command.split("/");
657                                 String[] name = parsed[parsed.length - 1].split("\\x2E");
658                                 String docname = my.defdoctype.get(
659                                                 step.getNumber() + "." + format).getName();
660                                 if (tempfile.get(docname) == null) { // No available template
661                                         String tool = parsed[parsed.length - 1];
662                                         String icon = name[0];
663                                         if (icon.equals("index")) {
664                                                 tool = parsed[parsed.length - 2];
665                                                 icon = "tool." + tool.toLowerCase() + ".png";
666                                         } else {
667                                                 icon = "tool." + icon.toLowerCase() + ".png";
668                                         }
669                                         File image = new File(ApplicationSettings
670                                                         .getApplicationSkinPath()
671                                                         + icon);
672                                         if (!image.exists())
673                                                 icon = "tool.any.png";
674                                         bar.addTool(tool, icon, command);
675                                 } else {
676                                         docname = "/jsp/newDocument.jsp?type=" + docname;
677                                         String icon = "tool." + name[0].toLowerCase() + ".png";
678                                         File image = new File(ApplicationSettings
679                                                         .getApplicationSkinPath()
680                                                         + icon);
681                                         if (!image.exists())
682                                                 icon = "tool.any.png";
683                                         bar.addTool(name[0], icon, command, docname);
684                                 }
685                         }
686                         if (!bar.isEmpty())
687                                 bars.put(step.getNumber(), bar);
688                 }
689         }
690
691         public String getApplicationProperty(String name) {
692                 // --------------------------------------------------
693                 return wapprops.getProperty(name); // May be null
694         }
695
696         public String getApplicationRootPath() {
697                 // ---------------------------------------
698                 return getApplicationProperty("wapp.root"); // The property is supposed including the Web application name
699         }
700
701         public String getApplicationURL() {
702                 // ----------------------------------
703                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
704                                 .append("/").append(wappname);
705                 return url.toString();
706         }
707
708         public Map<String, Object> getFilter(String name) {
709                 // --------------------------------------------------
710                 return filter.get(name);
711         }
712
713         public ToolBar getModuleBar(Step step) {
714                 // -----------------------------------------
715                 return bars.get(step.getNumber());
716         }
717
718         public Properties getNamingProperties() {
719                 // ----------------------------------------
720                 return jndprops;
721         }
722
723         // ==============================================================================================================================
724         // Public services
725         // ==============================================================================================================================
726
727         public static String getApplicationPluginPath() {
728                 // ------------------------------------------------
729                 return my.getApplicationRootPath() + "plugin/";
730         }
731
732         public static String getApplicationResourcePath() {
733                 // --------------------------------------------------
734                 return my.getApplicationRootPath() + "WEB-INF/classes/";
735         }
736
737         public static String getApplicationSkinPath() {
738                 // ----------------------------------------------
739                 return my.getApplicationRootPath() + "skin/";
740         }
741
742         public static Converter getConverter(DocumentType type, String format) {
743                 // -----------------------------------------------------------------------
744                 return my.convertmap.get(format + type.getName()); // May be null;
745         }
746
747         public static DocumentType getDefaultDocumentType(Step step, String format) {
748                 // ----------------------------------------------------------------------------
749                 String[] table = format.split("\\x2E");
750                 return my.defdoctype.get(step.getNumber() + "."
751                                 + table[table.length - 1]); // May be null
752         }
753
754         public static String getDownloadURL(User user) {
755                 // --------------------------------------------------
756                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
757                                 .append("/download/").append(user.getUsername()).append("/");
758                 return url.toString(); // The download Tomcat context is supposed being defined
759         }
760
761         public static Locale getCurrentLocale() {
762                 // ----------------------------------------
763                 return my.locale;
764         }
765
766         public static SimpleMenu getMenu(String name) {
767                 // ----------------------------------------------
768                 return my.menus.get(name);
769         }
770
771         public static PopupMenu getPopupMenu(String name) {
772                 // --------------------------------------------------
773                 return my.popups.get(name);
774         }
775
776         public static String getRepositoryURL() {
777                 // ----------------------------------------
778                 StringBuffer url = new StringBuffer("http://").append(my.wappserver)
779                                 .append("/repository/");
780                 return url.toString(); // The repository Tomcat context is supposed being defined
781         }
782
783         public static Locale[] getSupportedLocales() {
784                 // ---------------------------------------------
785                 String[] code = my.wapprops.getProperty("locale.supported").split(",");
786                 Locale[] result = new Locale[code.length];
787                 for (int i = 0; i < code.length; i++)
788                         result[i] = new Locale(code[i]);
789                 return result;
790         }
791
792         public static String[] getViewersMapping() {
793                 // -------------------------------------------
794                 return my.viewermap;
795         }
796
797         public static String getWebSiteURL() {
798                 // -------------------------------------
799                 return my.getApplicationProperty("wapp.website");
800         }
801
802         public static String getHelpURL() {
803                 // ----------------------------------
804                 return my.getApplicationProperty("wapp.onlinehelp");
805         }
806
807         // ==============================================================================================================================
808         // Private services
809         // ==============================================================================================================================
810
811         private List<String> getDefaultFormats(ProjectSettingsService.Step step) {
812                 // ------------------------------------------------------------------
813                 Set<String> keys = defdoctype.keySet();
814                 int number = step.getNumber();
815                 Vector<String> result = new Vector<String>();
816
817                 for (Iterator<String> i = keys.iterator(); i.hasNext();) {
818                         String[] key = i.next().split("\\x2E");
819                         if (Integer.valueOf(key[0]) != number)
820                                 continue;
821                         if (key[1].equals("pdf"))
822                                 continue; // PDF is not an authoring format
823                         result.add(key[1]); // Formats are unique
824                 }
825                 return result;
826         }
827
828         private void loadCustomization(File config) {
829                 // --------------------------------------------
830                 try {
831                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
832                                         .newInstance();
833                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
834
835                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
836                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
837                                         .getDocumentElement());
838
839                         // Default document types tag
840                         Node child = children.get("default-doctypes");
841                         NodeList nlist = child.getChildNodes();
842
843                         List<DocumentType> listype = getDocumentTypeService()
844                                         .selectAllTypes();
845                         HashMap<String, DocumentType> maptype = new HashMap<String, DocumentType>();
846                         for (Iterator<DocumentType> i = listype.iterator(); i.hasNext();) {
847                                 DocumentType type = i.next();
848                                 maptype.put(type.getName(), type);
849                         }
850                         for (int i = 0; i < nlist.getLength(); i++) {
851                                 child = nlist.item(i);
852                                 if (!child.getNodeName().equals("step"))
853                                         continue;
854
855                                 String nstep = child.getAttributes().getNamedItem("number")
856                                                 .getNodeValue();
857                                 NodeList map = child.getChildNodes();
858                                 for (int j = 0; j < map.getLength(); j++) {
859                                         child = map.item(j);
860                                         if (!child.getNodeName().equals("mapping"))
861                                                 continue;
862                                         NamedNodeMap natr = child.getAttributes();
863                                         String dext = natr.getNamedItem("extension").getNodeValue();
864                                         String type = natr.getNamedItem("type").getNodeValue();
865                                         defdoctype.put(nstep + "." + dext, maptype.get(type));
866                                 }
867                         }
868                         // Modules tag
869                         child = children.get("modules");
870                         nlist = child.getChildNodes();
871                         for (int i = 0; i < nlist.getLength(); i++) {
872                                 child = nlist.item(i);
873                                 if (!child.getNodeName().equals("mapping"))
874                                         continue;
875
876                                 NamedNodeMap natr = child.getAttributes();
877                                 String dext = natr.getNamedItem("extension").getNodeValue();
878                                 String exec = natr.getNamedItem("executable").getNodeValue();
879                                 wapprops.put("executable." + dext, exec);
880                         }
881                         // Viewer mappings tag
882                         child = children.get("viewers");
883                         viewermap = child.getAttributes().getNamedItem("extension")
884                                         .getNodeValue().split(",");
885
886                         // Converters tag
887                         child = children.get("converters");
888                         nlist = child.getChildNodes();
889                         for (int i = 0; i < nlist.getLength(); i++) {
890                                 child = nlist.item(i);
891
892                                 if (child.getNodeName().equals("geometry")) {
893                                         NamedNodeMap natr = child.getAttributes();
894                                         String from = natr.getNamedItem("from").getNodeValue();
895                                         String to = natr.getNamedItem("to").getNodeValue();
896                                         String exec = natr.getNamedItem("executable")
897                                                         .getNodeValue();
898                                         convertmap.put(from + "geometry", new Converter("geometry",
899                                                         from, to, exec));
900                                 }
901                         }
902
903                         // Templates tag
904                         child = children.get("templates");
905                         nlist = child.getChildNodes();
906                         for (int i = 0; i < nlist.getLength(); i++) {
907                                 child = nlist.item(i);
908                                 if (!child.getNodeName().equals("document"))
909                                         continue;
910
911                                 NamedNodeMap natr = child.getAttributes();
912                                 String type = natr.getNamedItem("type").getNodeValue();
913                                 String file = natr.getNamedItem("file").getNodeValue();
914                                 tempfile.put(type, file);
915                         }
916                 } catch (Exception error) {
917                         logger.info("Error in customization", error);
918                 }
919         }
920
921         /**
922          * Get the documentTypeService.
923          * 
924          * @return the documentTypeService
925          */
926         public DocumentTypeService getDocumentTypeService() {
927                 return _documentTypeService;
928         }
929
930         /**
931          * Set the documentTypeService.
932          * 
933          * @param documentTypeService
934          *            the documentTypeService to set
935          */
936         public void setDocumentTypeService(DocumentTypeService documentTypeService) {
937                 _documentTypeService = documentTypeService;
938         }
939 }