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