]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ApplicationSettings.java
Salome HOME
Fix: document removing is fixed. Unit tests for StudyDAO and StepService.removeDocume...
[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.List;
10 import java.util.Locale;
11 import java.util.Map;
12 import java.util.Properties;
13 import java.util.Set;
14
15 import javax.servlet.http.HttpServletRequest;
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18
19 import org.apache.log4j.Logger;
20 import org.splat.dal.bo.kernel.User;
21 import org.splat.dal.bo.som.Document;
22 import org.splat.dal.bo.som.DocumentType;
23 import org.splat.dal.bo.som.KnowledgeElement;
24 import org.splat.dal.bo.som.ProgressState;
25 import org.splat.dal.bo.som.SimulationContext;
26 import org.splat.manox.XDOM;
27 import org.splat.service.DocumentTypeService;
28 import org.splat.service.technical.ProjectSettingsService;
29 import org.splat.som.DocumentRights;
30 import org.splat.som.Step;
31 import org.splat.som.StudyRights;
32 import org.splat.wapp.MenuItem;
33 import org.splat.wapp.PopupItem;
34 import org.splat.wapp.PopupMenu;
35 import org.splat.wapp.SimpleMenu;
36 import org.splat.wapp.ToolBar;
37 import org.springframework.web.context.request.RequestContextHolder;
38 import org.springframework.web.context.request.ServletRequestAttributes;
39 import org.w3c.dom.NamedNodeMap;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42
43 /**
44  * SIMAN project settings service. Provides settings according to XML customization.
45  */
46 public class ApplicationSettings {
47
48         /**
49          * Application settings logger.
50          */
51         protected final static Logger LOG = Logger
52                         .getLogger(ApplicationSettings.class);
53         /**
54          * JNDI context for launching converters.
55          */
56         private transient static final Properties _JNDPROPS;
57         /**
58          * Siman web application name.
59          */
60         private transient static String wappname;
61         /**
62          * General properties from the application properties files.
63          */
64         private transient static final Properties _WAPPROPS = new Properties();
65         /**
66          * Siman web application root path on the server.
67          */
68         private transient static String wapproot;
69         /**
70          * Available template files.
71          */
72         private transient static Map<String, String> tempfile;
73         /**
74          * List of file extensions mapped to a viewer.
75          */
76         private transient static String[] viewermap;
77         /**
78          * Available document format converters.
79          */
80         private transient static Map<String, Converter> convertmap;
81
82         static {
83                 synchronized (_WAPPROPS) {
84                         // Do common configuration for all users
85
86                         _JNDPROPS = new Properties();
87                         tempfile = new HashMap<String, String>();
88                         viewermap = new String[0];
89                         convertmap = new HashMap<String, Converter>();
90
91                         ClassLoader cloader = Thread.currentThread()
92                                         .getContextClassLoader();
93                         HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder
94                                         .currentRequestAttributes()).getRequest();
95                         String appname = curRequest.getContextPath();
96                         if (appname.startsWith("/")) {
97                                 wappname = appname.substring(1);
98                         }
99                         // Set local path on the server to the application root.
100                         wapproot = curRequest.getSession().getServletContext().getRealPath(
101                                         "/");
102
103                         try {
104                                 _JNDPROPS.load(cloader.getResourceAsStream("jndi.properties"));
105                                 _WAPPROPS.load(cloader.getResourceAsStream(wappname
106                                                 + ".properties"));
107                         } catch (IOException e) {
108                                 LOG.info(
109                                                 "Can't load application properties: " + e.getMessage(),
110                                                 e);
111                         }
112
113                         // Configure login security
114                         System.setProperty("java.security.auth.login.config", wapproot
115                                         + ApplicationSettings.getApplicationProperty("wapp.login"));
116
117                         // Customization (must be done after above default settings)
118                         File config = new File(wapproot
119                                         + getApplicationProperty("wapp.customization"));
120                         if (config.exists()) {
121                                 loadCustomization(config); // Sets default document types, installed modules and available templates
122                         } else {
123                                 LOG.info("Could not find the application configuration file \""
124                                                 + config.getAbsolutePath()
125                                                 + "\", using default settings");
126                         }
127                 }
128         }
129
130         /**
131          * All value.
132          */
133         private static final String ALL = "all";
134
135         /**
136          * Hold icon file name.
137          */
138         private static final String IMG_HOLD = "image.hold.gif";
139         /**
140          * Attach icon file name.
141          */
142         private static final String IMG_ATTACH = "image.attach.png";
143         /**
144          * Version icon file name.
145          */
146         private static final String IMG_VERSION = "image.version.png";
147         /**
148          * Delete icon file name.
149          */
150         private static final String IMG_DELETE = "icon.delete.png";
151         /**
152          * Attach menu item name.
153          */
154         private static final String MNU_ATTACH = "attach";
155         /**
156          * Demote menu item name.
157          */
158         private static final String MNU_DEMOTE = "demote";
159         /**
160          * Promote menu item name.
161          */
162         private static final String MNU_PROMOTE = "promote";
163         /**
164          * Publish menu item name.
165          */
166         private static final String MNU_PUBLISH = "publish";
167         /**
168          * Edit menu item name.
169          */
170         private static final String MNU_EDIT = "edit";
171         /**
172          * Script menu item name.
173          */
174         private static final String MNU_SCRIPT = "script";
175         /**
176          * Version menu item name.
177          */
178         private static final String MNU_VERSION = "version";
179         /**
180          * Purge menu item name.
181          */
182         private static final String MNU_PURGE = "purge";
183         /**
184          * Remove menu item name.
185          */
186         private static final String MNU_REMOVE = "remove";
187         /**
188          * Rename menu item name.
189          */
190         private static final String MNU_RENAME = "rename";
191
192         /**
193          * Attach menu item name.
194          */
195         private static final String MNU_NAME_ATTACH = "menu.attach";
196         /**
197          * Demote menu item name.
198          */
199         private static final String MNU_NAME_DEMOTE = "menu.demote";
200         /**
201          * Promote menu item name.
202          */
203         private static final String MNU_NAME_PROMOTE = "menu.promote";
204         /**
205          * Publish menu item name.
206          */
207         private static final String MNU_NAME_PUBLISH = "menu.publish";
208         /**
209          * Edit menu item name.
210          */
211         private static final String MNU_NAME_EDIT = "menu.edit";
212         /**
213          * Script menu item name.
214          */
215         private static final String MNU_NAME_SCRIPT = "menu.newscenario";
216         /**
217          * Version menu item name.
218          */
219         private static final String MNU_NAME_VERSION = "menu.version";
220         /**
221          * Purge menu item name.
222          */
223         private static final String MNU_NAME_PURGE = "menu.purge";
224         /**
225          * Remove menu item name.
226          */
227         private static final String MNU_NAME_REMOVE = "menu.remove.version";
228         /**
229          * Rename menu item name.
230          */
231         private static final String MNU_NAME_RENAME = "menu.rename";
232         /**
233          * Mark as reference menu item name.
234          */
235         private static final String MNU_MARK_AS_REFERENCE = "markasreference";
236         /**
237          * Mark as reference menu item label key.
238          */
239         private static final String MNU_NAME_MARK_AS_REFERENCE = "menu.markasreference";
240         /**
241          * Not yet implemented action name.
242          */
243         private static final String ACT_NOT_YET_IMPLEMENTED = "notyetimplemented";
244         /**
245          * Attach action name.
246          */
247         private static final String ACT_ATTACH = "select-file?nextAction=attach";
248         /**
249          * Version action name.
250          */
251         private static final String ACT_VERSION = "select-file?nextAction=version";
252
253         /**
254          * Siman application server name.
255          */
256         private transient final String _wappserver;
257         /**
258          * Current user locale.
259          */
260         private transient Locale _locale;
261         /**
262          * Application menus.
263          */
264         private transient final Map<String, SimpleMenu> _menus = new HashMap<String, SimpleMenu>();
265         /**
266          * Study module-bars structured by steps.
267          */
268         private transient Map<Integer, ToolBar> _bars = null;
269         /**
270          * Popup menus.
271          */
272         private transient Map<String, PopupMenu> _popups = null;
273         /**
274          * Named search filters.
275          */
276         private transient Map<String, Map<String, Object>> _filter = null;
277         /**
278          * Injected project settings service.
279          */
280         private ProjectSettingsService _projectSettings;
281         /**
282          * Injected document type service.
283          */
284         private DocumentTypeService _documentTypeService;
285
286         /**
287          * Application settings constructor.
288          */
289         public ApplicationSettings() {
290                 HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder
291                                 .currentRequestAttributes()).getRequest();
292                 _wappserver = curRequest.getServerName() + ":"
293                                 + curRequest.getServerPort();
294
295                 LOG.info("Application server is set to " + _wappserver);
296                 LOG.info("Application name is set to " + wappname);
297         }
298
299         /**
300          * Get the projectSettingsService.
301          * 
302          * @return the projectSettingsService
303          */
304         public ProjectSettingsService getProjectSettings() {
305                 return _projectSettings;
306         }
307
308         /**
309          * Set the projectSettingsService.
310          * 
311          * @param projectSettingsService
312          *            the projectSettingsService to set
313          */
314         public void setProjectSettings(
315                         final ProjectSettingsService projectSettingsService) {
316                 _projectSettings = projectSettingsService;
317         }
318
319         private static class NewMenu extends SimpleMenu {
320                 private NewMenu() {
321                         super("create");
322                         addItem("new-empty", "menu.new.empty", "image.empty.png",
323                                         "select?menu=create&item=new-empty");
324                         addItem("new-copy", new MenuItem("menu.new.copy")
325                                         .icon("image.copy.png"));
326                         /*
327                          * addItem("new-instance", new MenuItem("menu.new.instance") .icon(IMG_HOLD)); addItem("new-import", new
328                          * MenuItem("menu.new.import") .icon("icon.upload.png"));
329                          */
330                         this.selects("new-empty");
331                 }
332         }
333
334         private static class SearchMenu extends SimpleMenu {
335                 private SearchMenu() {
336                         super("search");
337                         addItem("search-study", "menu.search.study", "image.study.png",
338                                         "select?menu=search&item=search-study");
339                         addItem("search-knowledge", "menu.search.idea", "image.idea.png",
340                                         "select?menu=search&item=search-knowledge");
341                         addItem("search-document", new MenuItem("menu.search.document")
342                                         .icon("icon.any.png"));
343                         this.selects("search-study");
344                 }
345         }
346
347         private static class PropertiesMenu extends SimpleMenu {
348                 private PropertiesMenu() {
349                         super("configuration");
350                         addItem("prop-general", "menu.prop.general", IMG_HOLD,
351                                         "select?menu=configuration&item=prop-general");
352                         addItem("prop-scenario", "menu.prop.scenario", IMG_HOLD,
353                                         "select?menu=configuration&item=prop-scenario");
354                         // These menu items will not be implemented in the current version.
355                         /*
356                          * addItem("prop-timestamp", new MenuItem("menu.prop.timestamp") .icon("image.stamp.png")); addItem("prop-comlog", new
357                          * MenuItem("menu.prop.comlog") .icon(IMG_HOLD)); addItem("prop-version", new MenuItem("menu.prop.version")
358                          * .icon("image.dirclosed.png"));
359                          */
360                 }
361         }
362
363         private static class DatadminMenu extends SimpleMenu {
364                 private DatadminMenu() {
365                         super("datadmin");
366                         addItem("admin-scontext", "menu.admin.context", IMG_HOLD,
367                                         "select?menu=datadmin&item=admin-scontext");
368                         addItem("admin-knowelm", "menu.admin.knowledge", "image.idea.png",
369                                         "select?menu=datadmin&item=admin-knowelm");
370                         addItem("admin-study", new MenuItem("menu.admin.study")
371                                         .icon("image.study.png"));
372                 }
373         }
374
375         private static class SysadminMenu extends SimpleMenu {
376                 private SysadminMenu() {
377                         super("sysadmin");
378                         addItem("admin-indexing", "menu.admin.indexing", "image.index.png",
379                                         "select?menu=sysadmin&item=admin-indexing");
380                         addItem("admin-importuser", "menu.admin.importuser",
381                                         "image.group.png",
382                                         "select?menu=sysadmin&item=admin-importuser");
383                 }
384         }
385
386         /**
387          * Menu items enumeration.
388          */
389         private enum Item {
390                 /**
391                  * Publish the study.
392                  */
393                 publish,
394                 /**
395                  * Accept the document.
396                  */
397                 accept, 
398                 /**
399                  * Approve the document.
400                  */
401                 approve, 
402                 /**
403                  * Promote the document.
404                  */
405                 promote, 
406                 /**
407                  * Demote the docuemnt.
408                  */
409                 demote, 
410                 /**
411                  * Undo the last operation.
412                  */
413                 undo, 
414                 /**
415                  * Rename the document.
416                  */
417                 rename, 
418                 /**
419                  * Attach a file to the document.
420                  */
421                 attach, 
422                 /**
423                  * Edit the document.
424                  */
425                 edit, 
426                 /**
427                  * script
428                  */
429                 script, 
430                 /**
431                  * Version the document.
432                  */
433                 version, 
434                 /**
435                  * replace
436                  */
437                 replace, 
438                 /**
439                  * export
440                  */
441                 export, 
442                 /**
443                  * Remove the document.
444                  */
445                 remove, 
446                 /**
447                  * purge
448                  */
449                 purge, 
450                 /**
451                  * Mark the study as reference.
452                  */
453                 markasreference
454         };
455
456         // Resources relative to studies
457         private static class EditableStudyPopup extends PopupMenu {
458                 private transient StudyRights _user = null;
459
460                 private EditableStudyPopup() {
461                         super();
462                         addItem(MNU_MARK_AS_REFERENCE, new PopupItem(MNU_NAME_MARK_AS_REFERENCE)
463                                         .action(ACT_NOT_YET_IMPLEMENTED).confirmation(
464                                                         "message.markasreference.study"));
465                         addItem(MNU_PUBLISH, new PopupItem(MNU_NAME_PUBLISH).icon(
466                                         "image.publish.png").action("edit-study?action=publish")
467                                         .confirmation("message.publish.study"));
468                         /* addItem(MNU_PROMOTE, new PopupItem("menu.archive")); */
469                         addSeparator();
470                         addItem(MNU_EDIT, new PopupItem("menu.properties").icon(
471                                         "icon.ed.png").action("../select?menu=properties"));
472                         addSeparator();
473                         addItem(MNU_SCRIPT, new PopupItem(MNU_NAME_SCRIPT)
474                                         .action("add-scenario"));
475                         /*
476                          * addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon( IMG_VERSION).action(ACT_NOT_YET_IMPLEMENTED));
477                          */
478                         addSeparator();
479                         /*
480                          * addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE) .confirmation("message.purge.study")); addItem("export", new
481                          * PopupItem("menu.export") .icon("image.export.png")); // For future needs
482                          */addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(
483                                         IMG_DELETE).action(ACT_NOT_YET_IMPLEMENTED).confirmation(
484                                         "message.delete.study"));
485                 }
486
487                 /** 
488                  * {@inheritDoc}
489                  * @see org.splat.wapp.ContextualMenu#isEnabled(java.lang.String)
490                  */
491                 @Override
492                 public boolean isEnabled(final String name) {
493                         boolean res = (_user != null);
494                         if (res) {
495                                 Item item = Item.valueOf(name);
496                                 switch (item) {
497                                         case publish:
498                                                 res = _user.canPublish();
499                                                 break;
500                                         case edit:
501                                                 res = _user.canEditProperties();
502                                                 break;
503                                         case script:
504                                                 res = _user.canAddScenario();
505                                                 break;
506                                         case version:
507                                                 res = _user.canVersion();
508                                                 break;
509                                         case remove:
510                                                 res = _user.canRemove();
511                                                 break;
512                                         case purge:
513                                                 res = _user.canPurge();
514                                                 break;
515                                         default:
516                                                 res = false;
517                                 }
518                         }
519                         return res;
520                 }
521
522                 @Override
523                 public void setContext(final String name, final Object context) {
524                         if (context instanceof StudyRights) {
525                                 _user = (StudyRights) context; // Just for optimizing
526                                 boolean history = _user.getOperand().isVersioned();
527                                 PopupItem item = this.item(MNU_REMOVE);
528                                 if (history) {
529                                         item.rename(MNU_NAME_REMOVE);
530                                 } else {
531                                         item.rename("menu.remove.study");
532                                 }
533                         }
534                 }
535         }
536
537         // Resources relative to documents
538         /**
539          * Popup of In-Work documents.
540          */
541         private static class EditableDocumentPopup extends PopupMenu {
542                 private transient DocumentRights _user = null;
543
544                 private EditableDocumentPopup() {
545                         super();
546                         addItem("accept", new PopupItem("menu.accept").icon(
547                                         "image.accept.png").action("setDocument?action=accept")
548                                         .confirmation("message.accept.document"));
549                         addItem(MNU_PROMOTE, new PopupItem(MNU_NAME_PROMOTE).icon(
550                                         "image.publish.png").action("setDocument?action=promote")
551                                         .confirmation("message.promote.document"));
552                         addSeparator();
553                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
554                                         .action("edit-document?action=renameDocument"));
555                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
556                                         .action(ACT_ATTACH));
557                         addSeparator();
558                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
559                                         IMG_VERSION).action(ACT_VERSION));
560                         addItem("replace", new PopupItem("menu.replace").icon(
561                                         "image.replace.png").action(
562                                         "select-file?nextAction=replace"));
563                         addSeparator();
564                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
565                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
566                                         "message.purge.document"));
567                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
568                                         .action("remove-document").confirmation(
569                                                         "message.delete.document"));
570                 }
571
572                 @Override
573                 public boolean isEnabled(final String name) {
574                         boolean res = (_user != null);
575                         if (res) {
576                                 Item item = Item.valueOf(name);
577                                 switch (item) {
578                                         case accept:
579                                                 res = _user.canAccept();
580                                                 break;
581                                         case promote:
582                                                 res = _user.canPromote();
583                                                 break;
584                                         case rename:
585                                                 res = _user.canRename();
586                                                 break;
587                                         case attach:
588                                                 res = _user.canAttach();
589                                                 break;
590                                         case version:
591                                                 res = _user.canVersion();
592                                                 break;
593                                         case replace:
594                                                 res = _user.canReplace();
595                                                 break;
596                                         case purge:
597                                                 res = _user.canPurge();
598                                                 break;
599                                         case remove:
600                                                 res = _user.canRemove();
601                                                 break;
602                                         default:
603                                                 res = false;
604                                 }
605                         }
606                         return res;
607                 }
608
609                 @Override
610                 public void setContext(final String name, final Object context) {
611                         if (context instanceof DocumentRights) {
612                                 _user = (DocumentRights) context; // Just for optimizing
613                                 Document downer = _user.getOperand();
614                                 PopupItem item = this.item(MNU_REMOVE);
615                                 if (downer.isVersioned()) {
616                                         item.rename(MNU_NAME_REMOVE);
617                                 } else {
618                                         item.rename("menu.remove.document");
619                                 }
620                         }
621                 }
622         }
623
624         /**
625          * Popup of In-Draft documents.
626          */
627         private static class ReviewableDocumentPopup extends PopupMenu {
628                 private transient DocumentRights _user = null;
629
630                 private ReviewableDocumentPopup() {
631                         super();
632                         addItem(MNU_DEMOTE, new PopupItem(MNU_NAME_DEMOTE).icon(
633                                         "image.demote.png").action("setDocument?action=demote")
634                                         .confirmation("message.demote.document"));
635                         addItem(MNU_PROMOTE, new PopupItem("menu.review").icon(
636                                         "image.review.png").action("setDocument?action=review")
637                                         .confirmation("message.review.document"));
638                         addSeparator();
639                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
640                                         .action(ACT_ATTACH));
641                         addSeparator();
642                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
643                                         IMG_VERSION).action(ACT_VERSION));
644                         addSeparator();
645                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
646                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
647                                         "message.purge.document"));
648                 }
649
650                 @Override
651                 public boolean isEnabled(final String name) {
652                         boolean res = (_user != null);
653                         if (res) {
654                                 Item item = Item.valueOf(name);
655                                 switch (item) {
656                                         case demote:
657                                                 res = _user.canDemote();
658                                                 break;
659                                         case promote:
660                                                 res = _user.canReview();
661                                                 break;
662                                         case attach:
663                                                 res = _user.canAttach();
664                                                 break;
665                                         case version:
666                                                 res = _user.canVersion();
667                                                 break;
668                                         case purge:
669                                                 res = _user.canPurge();
670                                                 break;
671                                         default:
672                                                 res = false;
673                                 }
674                         }
675                         return res;
676                 }
677
678                 @Override
679                 public void setContext(final String name, final Object context) {
680                         if (context instanceof DocumentRights) {
681                                 _user = (DocumentRights) context; // Just for optimizing
682                         }
683                 }
684         }
685
686         private static class NotResultDocumentPopup extends PopupMenu {
687                 // private final DocumentRights _user = null;
688
689                 private NotResultDocumentPopup() {
690                         super();
691                         addItem(MNU_DEMOTE, new PopupItem(MNU_NAME_DEMOTE).icon(
692                                         "image.demote.png").action("setDocument?action=demote")
693                                         .confirmation("message.demote.document"));
694                         addSeparator();
695                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
696                                         .action(ACT_ATTACH));
697                         addSeparator();
698                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
699                                         IMG_VERSION).action(ACT_VERSION));
700                         addSeparator();
701                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
702                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
703                                         "message.purge.document"));
704                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
705                                         .action("remove-document").confirmation(
706                                                         "message.delete.document"));
707                 }
708         }
709
710         /**
711          * Popup of In-Check documents.
712          */
713         private static class ApprovableDocumentPopup extends PopupMenu {
714                 private transient DocumentRights _user = null;
715
716                 private ApprovableDocumentPopup() {
717                         super();
718                         addItem("undo", new PopupItem(MNU_NAME_DEMOTE).icon(
719                                         "image.invalidate.png").action(
720                                         "setDocument?action=invalidate").confirmation(
721                                         "message.demote.document"));
722                         addItem(MNU_DEMOTE, new PopupItem("menu.disapprove").icon(
723                                         "image.demote.png").action("setDocument?action=disapprove")
724                                         .confirmation("message.disapprove.document"));
725                         addItem("approve", new PopupItem("menu.approve").icon(
726                                         "icon.APPROVED.png").action("setDocument?action=approve")
727                                         .confirmation("message.approve.document"));
728                 }
729
730                 @Override
731                 public boolean isEnabled(final String name) {
732                         boolean res = (_user != null);
733                         if (res) {
734                                 Item item = Item.valueOf(name);
735                                 switch (item) {
736                                         case undo:
737                                                 res = _user.canInvalidate();
738                                                 break;
739                                         case demote:
740                                                 res = _user.canDemote();
741                                                 break;
742                                         case approve:
743                                                 res = _user.canApprove();
744                                                 break;
745                                         default:
746                                                 res = false;
747                                 }
748                         }
749                         return res;
750                 }
751
752                 @Override
753                 public void setContext(final String name, final Object context) {
754                         if (context instanceof DocumentRights) {
755                                 _user = (DocumentRights) context; // Just for optimizing
756                         }
757                 }
758         }
759
760         /**
761          * Popup of Approved documents.
762          */
763         private static class ApprovedPopup extends PopupMenu {
764                 private ApprovedPopup() {
765                         super();
766                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
767                                         .action(ACT_ATTACH));
768                         addSeparator();
769                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
770                                         IMG_VERSION).action(ACT_VERSION));
771                 }
772         }
773
774         /**
775          * Popup of Extern documents.
776          */
777         private static class ExternPopup extends PopupMenu {
778                 private transient DocumentRights _user = null;
779
780                 private ExternPopup() {
781                         super();
782                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
783                                         .action("edit-document?action=renameDocument"));
784                         addSeparator();
785                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
786                                         IMG_VERSION).action(ACT_VERSION));
787                         addSeparator();
788                         addItem("replace", new PopupItem("menu.replace").icon(
789                                         "image.replace.png").action(
790                                         "select-file?nextAction=replace"));
791                         addSeparator();
792                         addItem(MNU_REMOVE, new PopupItem("menu.remove.document").icon(
793                                         IMG_DELETE).action("remove-document").confirmation(
794                                         "message.delete.document"));
795                 }
796
797                 @Override
798                 public boolean isEnabled(final String name) {
799                         boolean res = (_user != null);
800                         if (res) {
801                                 Item item = Item.valueOf(name);
802                                 switch (item) {
803                                         case rename:
804                                                 res = _user.canRename();
805                                                 break;
806                                         case version:
807                                                 res = _user.canVersion();
808                                                 break;
809                                         case replace:
810                                                 res = _user.canReplace();
811                                                 break;
812                                         case remove:
813                                                 res = _user.canRemove();
814                                                 break;
815                                         default:
816                                                 res = false;
817                                 }
818                         }
819                         return res;
820                 }
821
822                 @Override
823                 public void setContext(final String name, final Object context) {
824                         if (context instanceof DocumentRights) {
825                                 _user = (DocumentRights) context; // Just for optimizing
826                         }
827                 }
828         }
829
830         // Resources relative to simulation contexts
831         private static class ScontextPopup extends PopupMenu {
832                 private SimulationContextFacade _owner = null; // RKV: NOPMD: TODO: Refine the usage of this field or remove it.
833
834                 private ScontextPopup() {
835                         super();
836                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
837                                         .action("edit-context?action=renameContext"));
838                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT)
839                                         .action("edit-context?action=editContext"));
840                         addSeparator();
841                         addItem(MNU_REMOVE, new PopupItem("menu.remove").icon(IMG_DELETE)
842                                         .action("remove-context").confirmation(
843                                                         "message.delete.context"));
844                 }
845
846                 @Override
847                 public boolean isEnabled(final String name) {
848                         Item item = Item.valueOf(name);
849                         boolean res = true;
850
851                         if (item == Item.rename) {
852                                 res = false;
853                         } else if (item == Item.edit) {
854                                 // if (!owner.isEditable())
855                                 res = false;
856                         }
857                         return res;
858                 }
859
860                 @Override
861                 public void setContext(final String name, final Object context) {
862                         if (context instanceof SimulationContextFacade) {
863                                 _owner = (SimulationContextFacade) context; // Just for optimizing
864                         } else {
865                                 super.setContext(name, context);
866                         }
867                 }
868         }
869
870         // Resources relative to knowledge
871         private static class FeedbexPopup extends PopupMenu {
872                 private transient KnowledgeElement _owner = null;
873
874                 private FeedbexPopup() {
875                         super();
876                         addItem(MNU_PROMOTE, new PopupItem(MNU_NAME_PROMOTE).icon(
877                                         "image.review.png").action("promote-knowledge")
878                                         .confirmation("message.promote.knowledge"));
879                         addItem(MNU_DEMOTE, new PopupItem(MNU_NAME_DEMOTE).icon(
880                                         "image.invalidate.png").action("demote-knowledge")
881                                         .confirmation("message.demote.knowledge"));
882                         addSeparator();
883                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
884                                         .action("edit-knowledge?action=renameKnowledge"));
885                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT)
886                                         .action("edit-knowledge?action=editKnowledge"));
887                         addSeparator();
888                         addItem(MNU_REMOVE, new PopupItem("menu.remove").icon(IMG_DELETE)
889                                         .action("remove-knowledge").confirmation(
890                                                         "message.delete.knowledge"));
891                 }
892
893                 @Override
894                 public boolean isEnabled(final String name) {
895                         Item item = Item.valueOf(name);
896                         boolean res = true;
897
898                         if (item == Item.promote) {
899                                 if (_owner.getProgressState() != ProgressState.inDRAFT) {
900                                         res = false;
901                                 }
902                         } else if ((item == Item.demote)
903                                         && (_owner.getProgressState() != ProgressState.inCHECK)) {
904                                 res = false;
905                         }
906                         return res;
907                 }
908
909                 @Override
910                 public void setContext(final String name, final Object context) {
911                         if (context instanceof KnowledgeElement) {
912                                 _owner = (KnowledgeElement) context; // Just for optimizing
913                         } else {
914                                 super.setContext(name, context);
915                         }
916                 }
917         }
918
919         // ==============================================================================================================================
920         // Construction
921         // ==============================================================================================================================
922
923         // ==============================================================================================================================
924         // Public member functions
925         // ==============================================================================================================================
926
927         public void configure() {
928                 // Non customizable settings
929                 _menus.clear();
930                 SimpleMenu menu = new NewMenu();
931                 _menus.put(menu.getName(), menu);
932                 menu = new SearchMenu();
933                 _menus.put(menu.getName(), menu);
934                 menu = new DatadminMenu();
935                 _menus.put(menu.getName(), menu);
936                 menu = new SysadminMenu();
937                 _menus.put(menu.getName(), menu);
938                 menu = new PropertiesMenu();
939                 _menus.put(menu.getName(), menu);
940
941                 _popups = new HashMap<String, PopupMenu>();
942                 _popups.put("steditable", new EditableStudyPopup());
943                 _popups.put("editable", new EditableDocumentPopup());
944                 _popups.put("notresult", new NotResultDocumentPopup());
945                 _popups.put("reviewable", new ReviewableDocumentPopup());
946                 _popups.put("approvable", new ApprovableDocumentPopup());
947                 _popups.put("approved", new ApprovedPopup());
948                 _popups.put("extern", new ExternPopup());
949                 _popups.put("scontext", new ScontextPopup());
950                 _popups.put("feedbex", new FeedbexPopup());
951
952                 // Default customizable mandatory settings
953                 Map<String, Object> fprop = new HashMap<String, Object>();
954                 fprop.put("visibility", "PRIVATE");
955                 fprop.put("matchamong", ALL);
956                 fprop.put("matcontext", ALL);
957                 fprop.put("state", "APPROVED");
958                 fprop.put("author", "0");
959                 fprop.put("reference", "");
960                 fprop.put("title", "");
961                 fprop.put("context", new ArrayList<SimulationContext>());
962
963                 Map<String, Object> gprop = new HashMap<String, Object>();
964                 gprop.put("visibility", "PUBLIC");
965                 gprop.put("matchamong", ALL);
966                 gprop.put("matcontext", ALL);
967                 gprop.put("type", "2"); // TODO: Get the index from the type name
968                 gprop.put("author", "0");
969                 gprop.put("reference", "");
970                 gprop.put("title", "");
971                 gprop.put("context", new ArrayList<SimulationContext>());
972
973                 _filter = new HashMap<String, Map<String, Object>>();
974                 _filter.put("study", fprop);
975                 _filter.put("knowledge", gprop);
976
977                 // Settings based on the customization
978                 configureToolbars();
979         }
980
981         /**
982          * Configure toolbars for steps.
983          */
984         private void configureToolbars() {
985                 _bars = new HashMap<Integer, ToolBar>(); // May be empty if no module installed
986
987                 List<ProjectSettingsService.Step> steps = getProjectSettings()
988                                 .getAllSteps();
989                 for (Iterator<ProjectSettingsService.Step> i = steps.iterator(); i
990                                 .hasNext();) {
991                         ProjectSettingsService.Step step = i.next();
992                         List<String> formats = getProjectSettings().getDefaultFormats(step);
993                         if (formats.isEmpty()) {
994                                 continue;
995                         }
996
997                         ToolBar bar = new ToolBar(24); // Height of the module-bar
998                         Set<String> module = new HashSet<String>(); // For not duplicating modules
999                         for (String format : formats) {
1000                                 String command = getApplicationProperty("executable." + format);
1001                                 if (command == null) {
1002                                         continue; // Module not installed
1003                                 }
1004                                 if (module.contains(command)) {
1005                                         continue;
1006                                 }
1007                                 module.add(command);
1008                                 String[] parsed = command.split("/");
1009                                 String[] name = parsed[parsed.length - 1].split("\\x2E");
1010                                 DocumentType dtype = getProjectSettings()
1011                                                 .getDefaultDocumentType(step, format);
1012                                 String docname = "";
1013                                 if (dtype != null) {
1014                                         docname = dtype.getName();
1015                                 }
1016                                 if (tempfile.get(docname) == null) { // No available template
1017                                         String tool = parsed[parsed.length - 1];
1018                                         String icon = name[0];
1019                                         if ("index".equals(icon)) {
1020                                                 tool = parsed[parsed.length - 2];
1021                                                 icon = "tool." + tool.toLowerCase() + ".png";
1022                                         } else {
1023                                                 icon = "tool." + icon.toLowerCase() + ".png";
1024                                         }
1025                                         File image = new File(ApplicationSettings
1026                                                         .getApplicationSkinPath()
1027                                                         + icon);
1028                                         if (!image.exists()) {
1029                                                 icon = "tool.any.png";
1030                                         }
1031                                         bar.addTool(tool, icon, command);
1032                                 } else {
1033                                         docname = "/jsp/newDocument.jsp?type=" + docname;
1034                                         String icon = "tool." + name[0].toLowerCase() + ".png";
1035                                         File image = new File(ApplicationSettings
1036                                                         .getApplicationSkinPath()
1037                                                         + icon);
1038                                         if (!image.exists()) {
1039                                                 icon = "tool.any.png";
1040                                         }
1041                                         bar.addTool(name[0], icon, command, docname);
1042                                 }
1043                         }
1044                         if (!bar.isEmpty()) {
1045                                 _bars.put(step.getNumber(), bar);
1046                         }
1047                 }
1048         }
1049
1050         public static String getApplicationProperty(final String name) {
1051                 return _WAPPROPS.getProperty(name); // May be null
1052         }
1053
1054         public static String getApplicationRootPath() {
1055                 // The property is supposed including the Web application name
1056                 return wapproot;
1057         }
1058
1059         public String getApplicationURL() {
1060                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1061                                 .append("/").append(wappname);
1062                 return url.toString();
1063         }
1064
1065         public Map<String, Object> getFilter(final String name) {
1066                 return _filter.get(name);
1067         }
1068
1069         public ToolBar getModuleBar(final Step step) {
1070                 return _bars.get(step.getNumber());
1071         }
1072
1073         static public Properties getNamingProperties() {
1074                 return _JNDPROPS;
1075         }
1076
1077         // ==============================================================================================================================
1078         // Public services
1079         // ==============================================================================================================================
1080
1081         public static String getApplicationPluginPath() {
1082                 return getApplicationRootPath() + "plugin/";
1083         }
1084
1085         public static String getApplicationResourcePath() {
1086                 return getApplicationRootPath() + "WEB-INF/classes/";
1087         }
1088
1089         public static String getApplicationSkinPath() {
1090                 return getApplicationRootPath() + "skin/";
1091         }
1092
1093         public static Converter getConverter(final DocumentType type,
1094                         final String format) {
1095                 return convertmap.get(format + type.getName()); // May be null;
1096         }
1097
1098         public DocumentType getDefaultDocumentType(final Step step,
1099                         final String format) {
1100                 return getProjectSettings().getDefaultDocumentType(step.getStep(),
1101                                 format); // May be null
1102         }
1103
1104         public String getDownloadURL(final User user) {
1105                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1106                                 .append("/download/").append(user.getUsername()).append("/");
1107                 return url.toString(); // The download Tomcat context is supposed being defined
1108         }
1109
1110         public Locale getCurrentLocale() {
1111                 return _locale;
1112         }
1113
1114         public SimpleMenu getMenu(final String name) {
1115                 return _menus.get(name);
1116         }
1117
1118         public PopupMenu getPopupMenu(final String name) {
1119                 return _popups.get(name);
1120         }
1121
1122         public String getRepositoryURL() {
1123                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1124                                 .append("/repository/");
1125                 return url.toString(); // The repository Tomcat context is supposed being defined
1126         }
1127
1128         public static Locale[] getSupportedLocales() {
1129                 String[] code = _WAPPROPS.getProperty("locale.supported").split(",");
1130                 Locale[] result = new Locale[code.length];
1131                 for (int i = 0; i < code.length; i++) {
1132                         result[i] = new Locale(code[i]);
1133                 }
1134                 return result;
1135         }
1136
1137         public static String[] getViewersMapping() {
1138                 return viewermap;
1139         }
1140
1141         public static String getWebSiteURL() {
1142                 return getApplicationProperty("wapp.website");
1143         }
1144
1145         public static String getHelpURL() {
1146                 return getApplicationProperty("wapp.onlinehelp");
1147         }
1148
1149         // ==============================================================================================================================
1150         // Private services
1151         // ==============================================================================================================================
1152
1153         /**
1154          * Load application custom configuration from the given XML file (see conf/my.xml).
1155          * 
1156          * @param config
1157          *            the XML configuration file
1158          */
1159         private static void loadCustomization(final File config) {
1160                 try {
1161                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
1162                                         .newInstance();
1163                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
1164
1165                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
1166                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
1167                                         .getDocumentElement());
1168
1169                         // Modules tag
1170                         loadModules(children);
1171
1172                         // Converters tag
1173                         loadConverters(children);
1174
1175                         // Templates tag
1176                         loadTemplates(children);
1177                 } catch (Exception error) {
1178                         LOG.info("Error in customization", error);
1179                 }
1180         }
1181
1182         /**
1183          * Load modules from XML configuration.
1184          * 
1185          * @param children
1186          *            XML nodes
1187          */
1188         private static void loadModules(final Map<String, Node> children) {
1189                 Node child = children.get("modules");
1190                 NodeList nlist = child.getChildNodes();
1191                 for (int i = 0; i < nlist.getLength(); i++) {
1192                         child = nlist.item(i);
1193                         if (!child.getNodeName().equals("mapping")) {
1194                                 continue;
1195                         }
1196
1197                         NamedNodeMap natr = child.getAttributes();
1198                         String dext = natr.getNamedItem("extension").getNodeValue();
1199                         String exec = natr.getNamedItem("executable").getNodeValue();
1200                         _WAPPROPS.put("executable." + dext, exec);
1201                 }
1202                 // Viewer mappings tag
1203                 child = children.get("viewers");
1204                 viewermap = child.getAttributes().getNamedItem("extension")
1205                                 .getNodeValue().split(",");
1206         }
1207
1208         /**
1209          * Load converters from XML configuration.
1210          * 
1211          * @param children
1212          *            XML nodes
1213          */
1214         private static void loadConverters(final Map<String, Node> children) {
1215                 Node child = children.get("converters");
1216                 NodeList nlist = child.getChildNodes();
1217                 for (int i = 0; i < nlist.getLength(); i++) {
1218                         child = nlist.item(i);
1219
1220                         if (child.getNodeName().equals("geometry")) {
1221                                 NamedNodeMap natr = child.getAttributes();
1222                                 String from = natr.getNamedItem("from").getNodeValue();
1223                                 String to = natr.getNamedItem("to").getNodeValue();
1224                                 String exec = natr.getNamedItem("executable").getNodeValue();
1225                                 convertmap.put(from + "geometry", new Converter("geometry",
1226                                                 from, to, exec));
1227                         }
1228                 }
1229         }
1230
1231         /**
1232          * Load templates from XML configuration.
1233          * 
1234          * @param children
1235          *            XML nodes
1236          */
1237         private static void loadTemplates(final Map<String, Node> children) {
1238                 Node child = children.get("templates");
1239                 NodeList nlist = child.getChildNodes();
1240                 for (int i = 0; i < nlist.getLength(); i++) {
1241                         child = nlist.item(i);
1242                         if (!child.getNodeName().equals("document")) {
1243                                 continue;
1244                         }
1245
1246                         NamedNodeMap natr = child.getAttributes();
1247                         String type = natr.getNamedItem("type").getNodeValue();
1248                         String file = natr.getNamedItem("file").getNodeValue();
1249                         tempfile.put(type, file);
1250                 }
1251         }
1252
1253         /**
1254          * Get the documentTypeService.
1255          * 
1256          * @return the documentTypeService
1257          */
1258         public DocumentTypeService getDocumentTypeService() {
1259                 return _documentTypeService;
1260         }
1261
1262         /**
1263          * Set the documentTypeService.
1264          * 
1265          * @param documentTypeService
1266          *            the documentTypeService to set
1267          */
1268         public void setDocumentTypeService(
1269                         final DocumentTypeService documentTypeService) {
1270                 _documentTypeService = documentTypeService;
1271         }
1272
1273         /**
1274          * Get the locale.
1275          * 
1276          * @return the locale
1277          */
1278         public Locale getLocale() {
1279                 return _locale;
1280         }
1281
1282         /**
1283          * Set the locale.
1284          * 
1285          * @param locale
1286          *            the locale to set
1287          */
1288         public void setLocale(final Locale locale) {
1289                 _locale = locale;
1290         }
1291
1292 }