Salome HOME
Pop-up menu for knowledge is changed in accordance with specification
[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.SimulationContext;
25 import org.splat.dal.bo.som.Visibility;
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          * Protect menu item name.
210          */
211         private static final String MNU_NAME_PROTECT = "menu.protect";
212         /**
213          * Edit menu item name.
214          */
215         private static final String MNU_NAME_EDIT = "menu.edit";
216         /**
217          * Edit menu item name.
218          */
219         private static final String MNU_NAME_REMOVE = "menu.remove";
220         /**
221          * Script menu item name.
222          */
223         private static final String MNU_NAME_SCRIPT = "menu.newscenario";
224         /**
225          * Version menu item name.
226          */
227         private static final String MNU_NAME_VERSION = "menu.version";
228         /**
229          * Purge menu item name.
230          */
231         private static final String MNU_NAME_PURGE = "menu.purge";
232         /**
233          * Remove menu item name.
234          */
235         private static final String MNU_NAME_REMOVE_VERSION = "menu.remove.version";
236         /**
237          * Rename menu item name.
238          */
239         private static final String MNU_NAME_RENAME = "menu.rename";
240         /**
241          * Mark as reference menu item name.
242          */
243         private static final String MNU_MARK_AS_REFERENCE = "markasreference";
244         /**
245          * Mark as reference menu item label key.
246          */
247         private static final String MNU_NAME_MARK_AS_REFERENCE = "menu.markasreference";
248         /**
249          * Remove as reference menu item label key.
250          */
251         private static final String MNU_NAME_REMOVE_AS_REFERENCE = "menu.removeasreference";
252         /**
253          * Not yet implemented action name.
254          */
255         private static final String ACT_NOT_YET_IMPLEMENTED = "notyetimplemented";
256         /**
257          * Attach action name.
258          */
259         private static final String ACT_ATTACH = "select-file?nextAction=attach";
260         /**
261          * Version action name.
262          */
263         private static final String ACT_VERSION = "select-file?nextAction=version";
264         /**
265          * Mark as reference action name.
266          */
267         private static final String ACT_MARK_AS_REFERENCE = "markasref-study";
268         /**
269          * Remove as reference action name.
270          */
271         private static final String ACT_REMOVE_AS_REFERENCE = "removeasref-study";
272
273         /**
274          * Siman application server name.
275          */
276         private transient final String _wappserver;
277         /**
278          * Current user locale.
279          */
280         private transient Locale _locale;
281         /**
282          * Application menus.
283          */
284         private transient final Map<String, SimpleMenu> _menus = new HashMap<String, SimpleMenu>();
285         /**
286          * Study module-bars structured by steps.
287          */
288         private transient Map<Integer, ToolBar> _bars = null;
289         /**
290          * Popup menus.
291          */
292         private transient Map<String, PopupMenu> _popups = null;
293         /**
294          * Named search filters.
295          */
296         private transient Map<String, Map<String, Object>> _filter = null;
297         /**
298          * Injected project settings service.
299          */
300         private ProjectSettingsService _projectSettings;
301         /**
302          * Injected document type service.
303          */
304         private DocumentTypeService _documentTypeService;
305
306         /**
307          * Application settings constructor.
308          */
309         public ApplicationSettings() {
310                 HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder
311                                 .currentRequestAttributes()).getRequest();
312                 _wappserver = curRequest.getServerName() + ":"
313                                 + curRequest.getServerPort();
314
315                 LOG.info("Application server is set to " + _wappserver);
316                 LOG.info("Application name is set to " + wappname);
317         }
318
319         /**
320          * Get the projectSettingsService.
321          * 
322          * @return the projectSettingsService
323          */
324         public ProjectSettingsService getProjectSettings() {
325                 return _projectSettings;
326         }
327
328         /**
329          * Set the projectSettingsService.
330          * 
331          * @param projectSettingsService
332          *            the projectSettingsService to set
333          */
334         public void setProjectSettings(
335                         final ProjectSettingsService projectSettingsService) {
336                 _projectSettings = projectSettingsService;
337         }
338
339         /**
340          * New study menu.
341          */
342         private static class NewMenu extends SimpleMenu {
343                 /**
344                  * New study menu constructor.
345                  */
346                 private NewMenu() {
347                         super("create");
348                         addItem("new-empty", "menu.new.empty", "image.empty.png",
349                                         "select?menu=create&item=new-empty");
350                         addItem("new-copy", "menu.new.copy", "image.copy.png",
351                                         "select?menu=create&item=new-copy");
352                         /*
353                          * addItem("new-instance", new MenuItem("menu.new.instance") .icon(IMG_HOLD)); addItem("new-import", new
354                          * MenuItem("menu.new.import") .icon("icon.upload.png"));
355                          */
356                         this.selects("new-empty");
357                 }
358         }
359
360         /**
361          * Search menu.
362          */
363         private static class SearchMenu extends SimpleMenu {
364                 /**
365                  * Search menu constructor.
366                  */
367                 private SearchMenu() {
368                         super("search");
369                         addItem("search-study", "menu.search.study", "image.study.png",
370                                         "select?menu=search&item=search-study");
371                         addItem("search-knowledge", "menu.search.idea", "image.idea.png",
372                                         "select?menu=search&item=search-knowledge");
373                         addItem("search-document", new MenuItem("menu.search.document")
374                                         .icon("icon.any.png"));
375                         this.selects("search-study");
376                 }
377         }
378
379         /**
380          * Configuration menu.
381          */
382         private static class PropertiesMenu extends SimpleMenu {
383                 /**
384                  * Configuration menu constructor.
385                  */
386                 private PropertiesMenu() {
387                         super("configuration");
388                         addItem("prop-general", "menu.prop.general", IMG_HOLD,
389                                         "select?menu=configuration&item=prop-general");
390                         addItem("prop-scenario", "menu.prop.scenario", IMG_HOLD,
391                                         "select?menu=configuration&item=prop-scenario");
392                         // These menu items will not be implemented in the current version.
393                         /*
394                          * addItem("prop-timestamp", new MenuItem("menu.prop.timestamp") .icon("image.stamp.png")); addItem("prop-comlog", new
395                          * MenuItem("menu.prop.comlog") .icon(IMG_HOLD)); addItem("prop-version", new MenuItem("menu.prop.version")
396                          * .icon("image.dirclosed.png"));
397                          */
398                 }
399         }
400
401         /**
402          * Data administrator menu.
403          */
404         private static class DatadminMenu extends SimpleMenu {
405                 /**
406                  * Data administrator menu constructor.
407                  */
408                 private DatadminMenu() {
409                         super("datadmin");
410                         addItem("admin-scontext", "menu.admin.context", IMG_HOLD,
411                                         "select?menu=datadmin&item=admin-scontext");
412                         addItem("admin-knowelm", "menu.admin.knowledge", "image.idea.png",
413                                         "select?menu=datadmin&item=admin-knowelm");
414                         addItem("admin-study", new MenuItem("menu.admin.study")
415                                         .icon("image.study.png"));
416                 }
417         }
418
419         /**
420          * System administrator menu.
421          */
422         private static class SysadminMenu extends SimpleMenu {
423                 /**
424                  * System administrator menu constructor.
425                  */
426                 private SysadminMenu() {
427                         super("sysadmin");
428                         addItem("admin-indexing", "menu.admin.indexing", "image.index.png",
429                                         "select?menu=sysadmin&item=admin-indexing");
430                         addItem("admin-importuser", "menu.admin.importuser",
431                                         "image.group.png",
432                                         "select?menu=sysadmin&item=admin-importuser");
433                 }
434         }
435
436         /**
437          * Menu items enumeration.
438          */
439         private enum Item {
440                 /**
441                  * Publish the study.
442                  */
443                 publish,
444                 /**
445                  * Accept the document.
446                  */
447                 accept,
448                 /**
449                  * Approve the document.
450                  */
451                 approve,
452                 /**
453                  * Promote the document.
454                  */
455                 promote,
456                 /**
457                  * Demote the docuemnt.
458                  */
459                 demote,
460                 /**
461                  * Undo the last operation.
462                  */
463                 undo,
464                 /**
465                  * Rename the document.
466                  */
467                 rename,
468                 /**
469                  * Attach a file to the document.
470                  */
471                 attach,
472                 /**
473                  * Edit the document.
474                  */
475                 edit,
476                 /**
477                  * script
478                  */
479                 script,
480                 /**
481                  * Version the document.
482                  */
483                 version,
484                 /**
485                  * replace
486                  */
487                 replace,
488                 /**
489                  * export
490                  */
491                 export,
492                 /**
493                  * Remove the document.
494                  */
495                 remove,
496                 /**
497                  * purge
498                  */
499                 purge,
500                 /**
501                  * Mark the study as reference.
502                  */
503                 markasreference
504         };
505
506         // Resources relative to studies
507         /**
508          * Study popup menu.
509          */
510         private static class EditableMarkedStudyPopup extends PopupMenu {
511                 /**
512                  * User rights for the selected study.
513                  */
514                 private transient StudyRights _user = null;
515
516                 /**
517                  * Study popup menu constructor.
518                  * 
519                  * @param isPublic
520                  *            public study flag
521                  * @param isMarked
522                  *            "marked as reference" study flag
523                  */
524                 private EditableMarkedStudyPopup(final boolean isPublic,
525                                 final boolean isMarked) {
526                         super();
527
528                         if (isMarked) {
529                                 addItem(MNU_MARK_AS_REFERENCE, new PopupItem(
530                                                 MNU_NAME_REMOVE_AS_REFERENCE).action(
531                                                 ACT_REMOVE_AS_REFERENCE).confirmation(
532                                                 "message.removeasreference.study"));
533                         } else {
534                                 addItem(MNU_MARK_AS_REFERENCE, new PopupItem(
535                                                 MNU_NAME_MARK_AS_REFERENCE).action(
536                                                 ACT_MARK_AS_REFERENCE).confirmation(
537                                                 "message.markasreference.study"));
538                         }
539
540                         if (isPublic) {
541                                 addItem(MNU_PUBLISH, new PopupItem(MNU_NAME_PROTECT).icon(
542                                                 "image.publish.png")
543                                                 .action("edit-study?action=protect").confirmation(
544                                                                 "message.protect.study"));
545                         } else {
546                                 addItem(MNU_PUBLISH, new PopupItem(MNU_NAME_PUBLISH).icon(
547                                                 "image.publish.png")
548                                                 .action("edit-study?action=publish").confirmation(
549                                                                 "message.publish.study"));
550                         }
551
552                         /* addItem(MNU_PROMOTE, new PopupItem("menu.archive")); */
553                         addSeparator();
554                         addItem(MNU_EDIT, new PopupItem("menu.properties").icon(
555                                         "icon.ed.png").action(
556                                         "../select?menu=configuration&item=prop-general"));
557                         addSeparator();
558                         addItem(MNU_SCRIPT, new PopupItem(MNU_NAME_SCRIPT)
559                                         .action("add-scenario"));
560                         /*
561                          * addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon( IMG_VERSION).action(ACT_NOT_YET_IMPLEMENTED));
562                          */
563                         addSeparator();
564                         /*
565                          * addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE) .confirmation("message.purge.study")); addItem("export", new
566                          * PopupItem("menu.export") .icon("image.export.png")); // For future needs
567                          */addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE_VERSION).icon(
568                                         IMG_DELETE).action("remove-study").confirmation(
569                                         "message.delete.study"));
570                 }
571
572                 /**
573                  * {@inheritDoc}
574                  * 
575                  * @see org.splat.wapp.ContextualMenu#isEnabled(java.lang.String)
576                  */
577                 @Override
578                 public boolean isEnabled(final String name) {
579                         boolean res = (_user != null);
580                         if (res) {
581                                 Item item = Item.valueOf(name);
582                                 switch (item) {
583                                         case publish:
584                                                 if (_user.getOperand().getVisibility() == Visibility.PRIVATE) {
585                                                         res = _user.canPublish();
586                                                 } else {
587                                                         res = _user.canProtect();
588                                                 }
589                                                 break;
590                                         case edit:
591                                                 res = _user.canEditProperties();
592                                                 break;
593                                         case script:
594                                                 res = _user.canAddScenario();
595                                                 break;
596                                         case version:
597                                                 res = _user.canVersion();
598                                                 break;
599                                         case remove:
600                                                 res = _user.canRemove();
601                                                 break;
602                                         case purge:
603                                                 res = _user.canPurge();
604                                                 break;
605                                         case markasreference:
606                                                 if (_user.getOperand().getMarkreference() == 0) {
607                                                         res = _user.canMarkStudyAsReference();
608                                                 } else {
609                                                         res = _user.canRemoveStudyAsReference();
610                                                 }
611                                                 break;
612                                         default:
613                                                 res = false;
614                                 }
615                         }
616                         return res;
617                 }
618
619                 @Override
620                 public void setContext(final String name, final Object context) {
621                         if (context instanceof StudyRights) {
622                                 _user = (StudyRights) context; // Just for optimizing
623                                 boolean history = _user.getOperand().isVersioned();
624                                 PopupItem item = this.item(MNU_REMOVE);
625                                 if (history) {
626                                         item.rename(MNU_NAME_REMOVE_VERSION);
627                                 } else {
628                                         item.rename("menu.remove.study");
629                                 }
630                         }
631                 }
632         }
633
634         // Resources relative to documents
635         /**
636          * Popup of In-Work documents.
637          */
638         private static class EditableDocumentPopup extends PopupMenu {
639                 /**
640                  * User rights for the selected document.
641                  */
642                 private transient DocumentRights _user = null;
643
644                 /**
645                  * Document popup menu constructor.
646                  */
647                 private EditableDocumentPopup() {
648                         super();
649                         addItem("accept", new PopupItem("menu.accept").icon(
650                                         "image.accept.png").action("setDocument?action=accept")
651                                         .confirmation("message.accept.document"));
652                         addItem(MNU_PROMOTE, new PopupItem(MNU_NAME_PROMOTE).icon(
653                                         "image.publish.png").action("setDocument?action=promote")
654                                         .confirmation("message.promote.document"));
655                         addSeparator();
656                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
657                                         .action("edit-document?action=renameDocument"));
658                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
659                                         .action(ACT_ATTACH));
660                         addSeparator();
661                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
662                                         IMG_VERSION).action(ACT_VERSION));
663                         addItem("replace", new PopupItem("menu.replace").icon(
664                                         "image.replace.png").action(
665                                         "select-file?nextAction=replace"));
666                         addSeparator();
667                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
668                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
669                                         "message.purge.document"));
670                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE_VERSION).icon(
671                                         IMG_DELETE).action("remove-document").confirmation(
672                                         "message.delete.document"));
673                 }
674
675                 @Override
676                 public boolean isEnabled(final String name) {
677                         boolean res = (_user != null);
678                         if (res) {
679                                 Item item = Item.valueOf(name);
680                                 switch (item) {
681                                         case accept:
682                                                 res = _user.canAccept();
683                                                 break;
684                                         case promote:
685                                                 res = _user.canPromote();
686                                                 break;
687                                         case rename:
688                                                 res = _user.canRename();
689                                                 break;
690                                         case attach:
691                                                 res = _user.canAttach();
692                                                 break;
693                                         case version:
694                                                 res = _user.canVersion();
695                                                 break;
696                                         case replace:
697                                                 res = _user.canReplace();
698                                                 break;
699                                         case purge:
700                                                 res = _user.canPurge();
701                                                 break;
702                                         case remove:
703                                                 res = _user.canRemove();
704                                                 break;
705                                         default:
706                                                 res = false;
707                                 }
708                         }
709                         return res;
710                 }
711
712                 @Override
713                 public void setContext(final String name, final Object context) {
714                         if (context instanceof DocumentRights) {
715                                 _user = (DocumentRights) context; // Just for optimizing
716                                 Document downer = _user.getOperand();
717                                 PopupItem item = this.item(MNU_REMOVE);
718                                 if (downer.isVersioned()) {
719                                         item.rename(MNU_NAME_REMOVE_VERSION);
720                                 } else {
721                                         item.rename("menu.remove.document");
722                                 }
723                         }
724                 }
725         }
726
727         /**
728          * Popup of In-Draft documents.
729          */
730         private static class ReviewableDocumentPopup extends PopupMenu {
731                 /**
732                  * User rights for the selected document.
733                  */
734                 private transient DocumentRights _user = null;
735
736                 /**
737                  * Document popup menu constructor.
738                  */
739                 private ReviewableDocumentPopup() {
740                         super();
741                         addItem(MNU_DEMOTE, new PopupItem(MNU_NAME_DEMOTE).icon(
742                                         "image.demote.png").action("setDocument?action=demote")
743                                         .confirmation("message.demote.document"));
744                         addItem(MNU_PROMOTE, new PopupItem("menu.review").icon(
745                                         "image.review.png").action("setDocument?action=review")
746                                         .confirmation("message.review.document"));
747                         addSeparator();
748                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
749                                         .action(ACT_ATTACH));
750                         addSeparator();
751                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
752                                         IMG_VERSION).action(ACT_VERSION));
753                         addSeparator();
754                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
755                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
756                                         "message.purge.document"));
757                 }
758
759                 @Override
760                 public boolean isEnabled(final String name) {
761                         boolean res = (_user != null);
762                         if (res) {
763                                 Item item = Item.valueOf(name);
764                                 switch (item) {
765                                         case demote:
766                                                 res = _user.canDemote();
767                                                 break;
768                                         case promote:
769                                                 res = _user.canReview();
770                                                 break;
771                                         case attach:
772                                                 res = _user.canAttach();
773                                                 break;
774                                         case version:
775                                                 res = _user.canVersion();
776                                                 break;
777                                         case purge:
778                                                 res = _user.canPurge();
779                                                 break;
780                                         default:
781                                                 res = false;
782                                 }
783                         }
784                         return res;
785                 }
786
787                 @Override
788                 public void setContext(final String name, final Object context) {
789                         if (context instanceof DocumentRights) {
790                                 _user = (DocumentRights) context; // Just for optimizing
791                         }
792                 }
793         }
794
795         private static class NotResultDocumentPopup extends PopupMenu {
796                 // private final DocumentRights _user = null;
797
798                 private NotResultDocumentPopup() {
799                         super();
800                         addItem(MNU_DEMOTE, new PopupItem(MNU_NAME_DEMOTE).icon(
801                                         "image.demote.png").action("setDocument?action=demote")
802                                         .confirmation("message.demote.document"));
803                         addSeparator();
804                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
805                                         .action(ACT_ATTACH));
806                         addSeparator();
807                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
808                                         IMG_VERSION).action(ACT_VERSION));
809                         addSeparator();
810                         addItem(MNU_PURGE, new PopupItem(MNU_NAME_PURGE).action(
811                                         ACT_NOT_YET_IMPLEMENTED).confirmation(
812                                         "message.purge.document"));
813                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE_VERSION).icon(
814                                         IMG_DELETE).action("remove-document").confirmation(
815                                         "message.delete.document"));
816                 }
817         }
818
819         /**
820          * Popup of In-Check documents.
821          */
822         private static class ApprovableDocumentPopup extends PopupMenu {
823                 private transient DocumentRights _user = null;
824
825                 private ApprovableDocumentPopup() {
826                         super();
827                         addItem("undo", new PopupItem(MNU_NAME_DEMOTE).icon(
828                                         "image.invalidate.png").action(
829                                         "setDocument?action=invalidate").confirmation(
830                                         "message.demote.document"));
831                         addItem(MNU_DEMOTE, new PopupItem("menu.disapprove").icon(
832                                         "image.demote.png").action("setDocument?action=disapprove")
833                                         .confirmation("message.disapprove.document"));
834                         addItem("approve", new PopupItem("menu.approve").icon(
835                                         "icon.APPROVED.png").action("setDocument?action=approve")
836                                         .confirmation("message.approve.document"));
837                 }
838
839                 @Override
840                 public boolean isEnabled(final String name) {
841                         boolean res = (_user != null);
842                         if (res) {
843                                 Item item = Item.valueOf(name);
844                                 switch (item) {
845                                         case undo:
846                                                 res = _user.canInvalidate();
847                                                 break;
848                                         case demote:
849                                                 res = _user.canDemote();
850                                                 break;
851                                         case approve:
852                                                 res = _user.canApprove();
853                                                 break;
854                                         default:
855                                                 res = false;
856                                 }
857                         }
858                         return res;
859                 }
860
861                 @Override
862                 public void setContext(final String name, final Object context) {
863                         if (context instanceof DocumentRights) {
864                                 _user = (DocumentRights) context; // Just for optimizing
865                         }
866                 }
867         }
868
869         /**
870          * Popup of Approved documents.
871          */
872         private static class ApprovedPopup extends PopupMenu {
873                 private ApprovedPopup() {
874                         super();
875                         addItem(MNU_ATTACH, new PopupItem(MNU_NAME_ATTACH).icon(IMG_ATTACH)
876                                         .action(ACT_ATTACH));
877                         addSeparator();
878                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
879                                         IMG_VERSION).action(ACT_VERSION));
880                 }
881         }
882
883         /**
884          * Popup of Extern documents.
885          */
886         private static class ExternPopup extends PopupMenu {
887                 private transient DocumentRights _user = null;
888
889                 private ExternPopup() {
890                         super();
891                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
892                                         .action("edit-document?action=renameDocument"));
893                         addSeparator();
894                         addItem(MNU_VERSION, new PopupItem(MNU_NAME_VERSION).icon(
895                                         IMG_VERSION).action(ACT_VERSION));
896                         addSeparator();
897                         addItem("replace", new PopupItem("menu.replace").icon(
898                                         "image.replace.png").action(
899                                         "select-file?nextAction=replace"));
900                         addSeparator();
901                         addItem(MNU_REMOVE, new PopupItem("menu.remove.document").icon(
902                                         IMG_DELETE).action("remove-document").confirmation(
903                                         "message.delete.document"));
904                 }
905
906                 @Override
907                 public boolean isEnabled(final String name) {
908                         boolean res = (_user != null);
909                         if (res) {
910                                 Item item = Item.valueOf(name);
911                                 switch (item) {
912                                         case rename:
913                                                 res = _user.canRename();
914                                                 break;
915                                         case version:
916                                                 res = _user.canVersion();
917                                                 break;
918                                         case replace:
919                                                 res = _user.canReplace();
920                                                 break;
921                                         case remove:
922                                                 res = _user.canRemove();
923                                                 break;
924                                         default:
925                                                 res = false;
926                                 }
927                         }
928                         return res;
929                 }
930
931                 @Override
932                 public void setContext(final String name, final Object context) {
933                         if (context instanceof DocumentRights) {
934                                 _user = (DocumentRights) context; // Just for optimizing
935                         }
936                 }
937         }
938
939         // Resources relative to simulation contexts
940         private static class ScontextPopup extends PopupMenu {
941                 private SimulationContextFacade _owner = null; // RKV: NOPMD: TODO: Refine the usage of this field or remove it.
942
943                 private ScontextPopup() {
944                         super();
945                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
946                                         .action("edit-context?action=renameContext"));
947                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT)
948                                         .action("edit-context?action=editContext"));
949                         addSeparator();
950                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
951                                         .action("remove-context").confirmation(
952                                                         "message.delete.context"));
953                 }
954
955                 @Override
956                 public boolean isEnabled(final String name) {
957                         Item item = Item.valueOf(name);
958                         boolean res = true;
959
960                         if (item == Item.rename) {
961                                 res = false;
962                         } else if (item == Item.edit) {
963                                 // if (!owner.isEditable())
964                                 res = false;
965                         }
966                         return res;
967                 }
968
969                 @Override
970                 public void setContext(final String name, final Object context) {
971                         if (context instanceof SimulationContextFacade) {
972                                 _owner = (SimulationContextFacade) context; // Just for optimizing
973                         } else {
974                                 super.setContext(name, context);
975                         }
976                 }
977         }
978
979         // Resources relative to knowledge
980         private static class FeedbexPopup extends PopupMenu {
981                 private transient KnowledgeElement _owner = null;
982
983                 private FeedbexPopup() {
984                         super();
985                         addItem(MNU_RENAME, new PopupItem(MNU_NAME_RENAME)
986                                         .action("edit-knowledge?action=renameKnowledge"));
987                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT)
988                                         .action("edit-knowledge?action=editKnowledge"));
989                         addSeparator();
990                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
991                                         .action("remove-knowledge").confirmation(
992                                                         "message.delete.knowledge"));
993                 }
994
995                 @Override
996                 public boolean isEnabled(final String name) {
997                         boolean res = true;
998                         return res;
999                 }
1000
1001                 @Override
1002                 public void setContext(final String name, final Object context) {
1003                         if (context instanceof KnowledgeElement) {
1004                                 _owner = (KnowledgeElement) context; // Just for optimizing
1005                         } else {
1006                                 super.setContext(name, context);
1007                         }
1008                 }
1009         }
1010
1011         /**
1012          * Pop-up menu for comments.
1013          */
1014         private static class CommentPopup extends PopupMenu {
1015                 /**
1016                  * Default constructor.
1017                  */
1018                 private CommentPopup() {
1019                         super();
1020                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT).icon("icon.ed.png")
1021                                         .action("editComment(entity_index)"));
1022                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
1023                                         .action("removeComment(entity_index)").confirmation(
1024                                                         "message.delete.comment"));
1025                 }
1026
1027                 /**
1028                  * Is enabled.
1029                  * 
1030                  * @param name
1031                  *            the entry name
1032                  * @return true
1033                  */
1034                 @Override
1035                 public boolean isEnabled(final String name) {
1036                         return true;
1037                 }
1038         }
1039
1040         /**
1041          * Pop-up menu for comments.
1042          */
1043         private static class DescritptionPopup extends PopupMenu {
1044                 /**
1045                  * Default constructor.
1046                  */
1047                 private DescritptionPopup() {
1048                         super();
1049                         addItem(MNU_EDIT, new PopupItem(MNU_NAME_EDIT).icon("icon.ed.png")
1050                                         .action("showDescriptionEditor()"));
1051                         addItem(MNU_REMOVE, new PopupItem(MNU_NAME_REMOVE).icon(IMG_DELETE)
1052                                         .action("removeDescription()").confirmation(
1053                                                         "message.delete.description"));
1054                 }
1055
1056                 /**
1057                  * Is enable.
1058                  * 
1059                  * @param name
1060                  *            the entry name
1061                  * @return true
1062                  */
1063                 @Override
1064                 public boolean isEnabled(final String name) {
1065                         return true;
1066                 }
1067         }
1068
1069         // ==============================================================================================================================
1070         // Construction
1071         // ==============================================================================================================================
1072
1073         // ==============================================================================================================================
1074         // Public member functions
1075         // ==============================================================================================================================
1076
1077         public void configure() {
1078                 // Non customizable settings
1079                 _menus.clear();
1080                 SimpleMenu menu = new NewMenu();
1081                 _menus.put(menu.getName(), menu);
1082                 menu = new SearchMenu();
1083                 _menus.put(menu.getName(), menu);
1084                 menu = new DatadminMenu();
1085                 _menus.put(menu.getName(), menu);
1086                 menu = new SysadminMenu();
1087                 _menus.put(menu.getName(), menu);
1088                 menu = new PropertiesMenu();
1089                 _menus.put(menu.getName(), menu);
1090
1091                 _popups = new HashMap<String, PopupMenu>();
1092                 _popups.put("steditablemarkpublic", new EditableMarkedStudyPopup(false,
1093                                 false));
1094                 _popups.put("steditableunmarkpublic", new EditableMarkedStudyPopup(
1095                                 false, true));
1096                 _popups.put("steditablemarkprivate", new EditableMarkedStudyPopup(true,
1097                                 false));
1098                 _popups.put("steditableunmarkprivate", new EditableMarkedStudyPopup(
1099                                 true, true));
1100                 _popups.put("editable", new EditableDocumentPopup());
1101                 _popups.put("notresult", new NotResultDocumentPopup());
1102                 _popups.put("reviewable", new ReviewableDocumentPopup());
1103                 _popups.put("approvable", new ApprovableDocumentPopup());
1104                 _popups.put("approved", new ApprovedPopup());
1105                 _popups.put("extern", new ExternPopup());
1106                 _popups.put("scontext", new ScontextPopup());
1107                 _popups.put("feedbex", new FeedbexPopup());
1108                 _popups.put("comment", new CommentPopup());
1109                 _popups.put("description", new DescritptionPopup());
1110
1111                 // Default customizable mandatory settings
1112                 Map<String, Object> fprop = new HashMap<String, Object>();
1113                 fprop.put("visibility", "PRIVATE");
1114                 fprop.put("matchamong", ALL);
1115                 fprop.put("matcontext", ALL);
1116                 fprop.put("state", "APPROVED");
1117                 fprop.put("author", "0");
1118                 fprop.put("reference", "");
1119                 fprop.put("title", "");
1120                 fprop.put("context", new ArrayList<SimulationContext>());
1121
1122                 Map<String, Object> gprop = new HashMap<String, Object>();
1123                 gprop.put("visibility", "PUBLIC");
1124                 gprop.put("matchamong", ALL);
1125                 gprop.put("matcontext", ALL);
1126                 gprop.put("type", "2"); // TODO: Get the index from the type name
1127                 gprop.put("author", "0");
1128                 gprop.put("reference", "");
1129                 gprop.put("title", "");
1130                 gprop.put("context", new ArrayList<SimulationContext>());
1131
1132                 _filter = new HashMap<String, Map<String, Object>>();
1133                 _filter.put("study", fprop);
1134                 _filter.put("knowledge", gprop);
1135
1136                 // Settings based on the customization
1137                 configureToolbars();
1138         }
1139
1140         /**
1141          * Configure toolbars for steps.
1142          */
1143         private void configureToolbars() {
1144                 _bars = new HashMap<Integer, ToolBar>(); // May be empty if no module installed
1145
1146                 List<ProjectSettingsService.Step> steps = getProjectSettings()
1147                                 .getAllSteps();
1148                 for (Iterator<ProjectSettingsService.Step> i = steps.iterator(); i
1149                                 .hasNext();) {
1150                         ProjectSettingsService.Step step = i.next();
1151                         List<String> formats = getProjectSettings().getDefaultFormats(step);
1152                         if (formats.isEmpty()) {
1153                                 continue;
1154                         }
1155
1156                         ToolBar bar = new ToolBar(24); // Height of the module-bar
1157                         Set<String> module = new HashSet<String>(); // For not duplicating modules
1158                         for (String format : formats) {
1159                                 String command = getApplicationProperty("executable." + format);
1160                                 if (command == null) {
1161                                         continue; // Module not installed
1162                                 }
1163                                 if (module.contains(command)) {
1164                                         continue;
1165                                 }
1166                                 module.add(command);
1167                                 String[] parsed = command.split("/");
1168                                 String[] name = parsed[parsed.length - 1].split("\\x2E");
1169                                 DocumentType dtype = getProjectSettings()
1170                                                 .getDefaultDocumentType(step, format);
1171                                 String docname = "";
1172                                 if (dtype != null) {
1173                                         docname = dtype.getName();
1174                                 }
1175                                 if (tempfile.get(docname) == null) { // No available template
1176                                         String tool = parsed[parsed.length - 1];
1177                                         String icon = name[0];
1178                                         if ("index".equals(icon)) {
1179                                                 tool = parsed[parsed.length - 2];
1180                                                 icon = "tool." + tool.toLowerCase() + ".png";
1181                                         } else {
1182                                                 icon = "tool." + icon.toLowerCase() + ".png";
1183                                         }
1184                                         File image = new File(ApplicationSettings
1185                                                         .getApplicationSkinPath()
1186                                                         + icon);
1187                                         if (!image.exists()) {
1188                                                 icon = "tool.any.png";
1189                                         }
1190                                         bar.addTool(tool, icon, command);
1191                                 } else {
1192                                         docname = "/jsp/newDocument.jsp?type=" + docname;
1193                                         String icon = "tool." + name[0].toLowerCase() + ".png";
1194                                         File image = new File(ApplicationSettings
1195                                                         .getApplicationSkinPath()
1196                                                         + icon);
1197                                         if (!image.exists()) {
1198                                                 icon = "tool.any.png";
1199                                         }
1200                                         bar.addTool(name[0], icon, command, docname);
1201                                 }
1202                         }
1203                         if (!bar.isEmpty()) {
1204                                 _bars.put(step.getNumber(), bar);
1205                         }
1206                 }
1207         }
1208
1209         public static String getApplicationProperty(final String name) {
1210                 return _WAPPROPS.getProperty(name); // May be null
1211         }
1212
1213         public static String getApplicationRootPath() {
1214                 // The property is supposed including the Web application name
1215                 return wapproot;
1216         }
1217
1218         public String getApplicationURL() {
1219                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1220                                 .append("/").append(wappname);
1221                 return url.toString();
1222         }
1223
1224         public Map<String, Object> getFilter(final String name) {
1225                 return _filter.get(name);
1226         }
1227
1228         public ToolBar getModuleBar(final Step step) {
1229                 return _bars.get(step.getNumber());
1230         }
1231
1232         static public Properties getNamingProperties() {
1233                 return _JNDPROPS;
1234         }
1235
1236         // ==============================================================================================================================
1237         // Public services
1238         // ==============================================================================================================================
1239
1240         public static String getApplicationPluginPath() {
1241                 return getApplicationRootPath() + "plugin/";
1242         }
1243
1244         public static String getApplicationResourcePath() {
1245                 return getApplicationRootPath() + "WEB-INF/classes/";
1246         }
1247
1248         public static String getApplicationSkinPath() {
1249                 return getApplicationRootPath() + "skin/";
1250         }
1251
1252         public static Converter getConverter(final DocumentType type,
1253                         final String format) {
1254                 return convertmap.get(format + type.getName()); // May be null;
1255         }
1256
1257         /**
1258          * Get default document type for the given format on the given study step.
1259          * 
1260          * @param step
1261          *            the study step
1262          * @param format
1263          *            the file format
1264          * @return default document type or null if not defined in the configuration
1265          */
1266         public DocumentType getDefaultDocumentType(final Step step,
1267                         final String format) {
1268                 return getProjectSettings().getDefaultDocumentType(step.getStep(),
1269                                 format); // May be null
1270         }
1271
1272         public String getDownloadURL(final User user) {
1273                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1274                                 .append("/download/").append(user.getUsername()).append("/");
1275                 return url.toString(); // The download Tomcat context is supposed being defined
1276         }
1277
1278         public Locale getCurrentLocale() {
1279                 return _locale;
1280         }
1281
1282         public SimpleMenu getMenu(final String name) {
1283                 return _menus.get(name);
1284         }
1285
1286         public PopupMenu getPopupMenu(final String name) {
1287                 return _popups.get(name);
1288         }
1289
1290         public String getRepositoryURL() {
1291                 StringBuffer url = new StringBuffer("http://").append(_wappserver)
1292                                 .append("/repository/");
1293                 return url.toString(); // The repository Tomcat context is supposed being defined
1294         }
1295
1296         public static Locale[] getSupportedLocales() {
1297                 String[] code = _WAPPROPS.getProperty("locale.supported").split(",");
1298                 Locale[] result = new Locale[code.length];
1299                 for (int i = 0; i < code.length; i++) {
1300                         result[i] = new Locale(code[i]);
1301                 }
1302                 return result;
1303         }
1304
1305         public static String[] getViewersMapping() {
1306                 return viewermap;
1307         }
1308
1309         public static String getWebSiteURL() {
1310                 return getApplicationProperty("wapp.website");
1311         }
1312
1313         public static String getHelpURL() {
1314                 return getApplicationProperty("wapp.onlinehelp");
1315         }
1316
1317         // ==============================================================================================================================
1318         // Private services
1319         // ==============================================================================================================================
1320
1321         /**
1322          * Load application custom configuration from the given XML file (see conf/my.xml).
1323          * 
1324          * @param config
1325          *            the XML configuration file
1326          */
1327         private static void loadCustomization(final File config) {
1328                 try {
1329                         DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
1330                                         .newInstance();
1331                         DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
1332
1333                         org.w3c.dom.Document conf = dBuilder.parse(config.getPath());
1334                         HashMap<String, Node> children = XDOM.getNamedChildNodes(conf
1335                                         .getDocumentElement());
1336
1337                         // Modules tag
1338                         loadModules(children);
1339
1340                         // Converters tag
1341                         loadConverters(children);
1342
1343                         // Templates tag
1344                         loadTemplates(children);
1345                 } catch (Exception error) {
1346                         LOG.info("Error in customization", error);
1347                 }
1348         }
1349
1350         /**
1351          * Load modules from XML configuration.
1352          * 
1353          * @param children
1354          *            XML nodes
1355          */
1356         private static void loadModules(final Map<String, Node> children) {
1357                 Node child = children.get("modules");
1358                 NodeList nlist = child.getChildNodes();
1359                 for (int i = 0; i < nlist.getLength(); i++) {
1360                         child = nlist.item(i);
1361                         if (!child.getNodeName().equals("mapping")) {
1362                                 continue;
1363                         }
1364
1365                         NamedNodeMap natr = child.getAttributes();
1366                         String dext = natr.getNamedItem("extension").getNodeValue();
1367                         String exec = natr.getNamedItem("executable").getNodeValue();
1368                         _WAPPROPS.put("executable." + dext, exec);
1369                 }
1370                 // Viewer mappings tag
1371                 child = children.get("viewers");
1372                 viewermap = child.getAttributes().getNamedItem("extension")
1373                                 .getNodeValue().split(",");
1374         }
1375
1376         /**
1377          * Load converters from XML configuration.
1378          * 
1379          * @param children
1380          *            XML nodes
1381          */
1382         private static void loadConverters(final Map<String, Node> children) {
1383                 Node child = children.get("converters");
1384                 NodeList nlist = child.getChildNodes();
1385                 for (int i = 0; i < nlist.getLength(); i++) {
1386                         child = nlist.item(i);
1387
1388                         if (child.getNodeName().equals("geometry")) {
1389                                 NamedNodeMap natr = child.getAttributes();
1390                                 String from = natr.getNamedItem("from").getNodeValue();
1391                                 String to = natr.getNamedItem("to").getNodeValue();
1392                                 String exec = natr.getNamedItem("executable").getNodeValue();
1393                                 convertmap.put(from + "geometry", new Converter("geometry",
1394                                                 from, to, exec));
1395                         }
1396                 }
1397         }
1398
1399         /**
1400          * Load templates from XML configuration.
1401          * 
1402          * @param children
1403          *            XML nodes
1404          */
1405         private static void loadTemplates(final Map<String, Node> children) {
1406                 Node child = children.get("templates");
1407                 NodeList nlist = child.getChildNodes();
1408                 for (int i = 0; i < nlist.getLength(); i++) {
1409                         child = nlist.item(i);
1410                         if (!child.getNodeName().equals("document")) {
1411                                 continue;
1412                         }
1413
1414                         NamedNodeMap natr = child.getAttributes();
1415                         String type = natr.getNamedItem("type").getNodeValue();
1416                         String file = natr.getNamedItem("file").getNodeValue();
1417                         tempfile.put(type, file);
1418                 }
1419         }
1420
1421         /**
1422          * Get the documentTypeService.
1423          * 
1424          * @return the documentTypeService
1425          */
1426         public DocumentTypeService getDocumentTypeService() {
1427                 return _documentTypeService;
1428         }
1429
1430         /**
1431          * Set the documentTypeService.
1432          * 
1433          * @param documentTypeService
1434          *            the documentTypeService to set
1435          */
1436         public void setDocumentTypeService(
1437                         final DocumentTypeService documentTypeService) {
1438                 _documentTypeService = documentTypeService;
1439         }
1440
1441         /**
1442          * Get the locale.
1443          * 
1444          * @return the locale
1445          */
1446         public Locale getLocale() {
1447                 return _locale;
1448         }
1449
1450         /**
1451          * Set the locale.
1452          * 
1453          * @param locale
1454          *            the locale to set
1455          */
1456         public void setLocale(final Locale locale) {
1457                 _locale = locale;
1458         }
1459
1460 }