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