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