Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / Action.java
1 package org.splat.simer;
2
3 import java.util.Comparator;
4 import java.util.Map;
5 import java.util.ResourceBundle;
6
7 import javax.security.auth.login.LoginContext;
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.apache.log4j.Logger;
11 import org.apache.struts2.interceptor.ServletRequestAware;
12 import org.apache.struts2.interceptor.SessionAware;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.DocumentType;
15 import org.splat.dal.bo.som.SimulationContextType;
16 import org.splat.dal.bo.som.Study;
17 import org.splat.service.dto.KnowledgeElementDTO;
18 import org.splat.som.ApplicationRights;
19 import org.splat.som.StudyRights;
20 import org.splat.wapp.Menu;
21 import org.splat.wapp.PopupMenu;
22
23 import com.opensymphony.xwork2.ActionSupport;
24
25 /**
26  * Base Siman action.
27  */
28 public class Action extends ActionSupport implements ServletRequestAware,
29                 SessionAware {
30
31         /**
32          * Serial version ID.
33          */
34         private static final long serialVersionUID = -895295026709526501L;
35         /**
36          * Action logger.
37          */
38         protected static final Logger LOG = Logger.getLogger(Action.class);
39
40         /**
41          * Open knowledge key in session.
42          */
43         public static final String KNOWLEDGE_OPEN = "knowledge.open";
44
45         /**
46          * Open study key in session.
47          */
48         public static final String STUDY_OPEN = "study.open";
49
50         /**
51          * User rights key in session.
52          */
53         public static final String USER_RIGHTS = "user.rights";
54
55         /**
56          * Login context key in session.
57          */
58         public static final String LOGIN_CONTEXT = "login.context";
59
60         /**
61          * Http servlet request.
62          */
63         private HttpServletRequest _servletRequest;
64         /**
65          * Http session container.
66          */
67         private Map<String, Object> _session;
68         /**
69          * Error code.
70          */
71         private String _errorCode;
72         /**
73          * The message to show when the page is loaded.
74          */
75         private String _message = "";
76         /**
77          * ActionType for specifying the type of the operaion.
78          */
79         private String _actionType;
80         /**
81          * Current open study facade object.
82          */
83         private OpenStudy _openStudy;
84         /**
85          * Current open knowledge facade object.
86          */
87         private OpenKnowledge _openKnowledge;
88
89         /**
90          * MenuBarSettings bean.
91          */
92         private MenuBarSettings _menuBarSettings;
93
94         /**
95          * TitleBarSettings bean.
96          */
97         private TitleBarSettings _titleBarSettings;
98         /**
99          * ToolBarSettings bean.
100          */
101         private ToolBarSettings _toolBarSettings;
102
103         /**
104          * LeftMenuSettings bean.
105          */
106         private LeftMenuSettings _leftMenuSettings;
107
108         /**
109          * Injected application settings bean.
110          */
111         private ApplicationSettings _applicationSettings;
112         /**
113          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
114          */
115         private String _menuProperty;
116         /**
117          * Value of the tool bar property. It can be: none, standard, study, back.
118          */
119         private String _toolProperty;
120
121         /**
122          * Comparator for sorting document types with localized names.
123          */
124         public class DocumentTypeComparator implements Comparator<DocumentType> {
125                 /**
126                  * {@inheritDoc}
127                  * 
128                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
129                  */
130                 public int compare(final DocumentType t1, final DocumentType t2) {
131                         ResourceBundle locale = ResourceBundle.getBundle("som",
132                                         getApplicationSettings().getCurrentLocale());
133                         String name1 = t1.getName();
134                         if (t1.isApproved()) {
135                                 name1 = locale.getString("type.document." + name1);
136                         }
137                         String name2 = t2.getName();
138                         if (t2.isApproved()) {
139                                 name2 = locale.getString("type.document." + name2);
140                         }
141
142                         return name1.compareToIgnoreCase(name2);
143                 }
144         }
145
146         /**
147          * Comparator for sorting simulation context types with localized names.
148          */
149         public class ContextTypeComparator implements
150                         Comparator<SimulationContextType> {
151                 /**
152                  * {@inheritDoc}
153                  * 
154                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
155                  */
156                 public int compare(final SimulationContextType t1,
157                                 final SimulationContextType t2) {
158                         ResourceBundle locale = ResourceBundle.getBundle("som",
159                                         getApplicationSettings().getCurrentLocale());
160                         String name1 = t1.getName();
161                         if (t1.isApproved()) {
162                                 name1 = locale.getString("type.context." + name1);
163                         }
164                         String name2 = t2.getName();
165                         if (t2.isApproved()) {
166                                 name2 = locale.getString("type.context." + name2);
167                         }
168
169                         return name1.compareToIgnoreCase(name2);
170                 }
171         }
172
173         // ==============================================================================================================================
174         // Session services
175         // ==============================================================================================================================
176
177         /**
178          * Remove the currently open knowledge from the session.
179          */
180         protected void closeKnowledge() {
181                 AbstractOpenObject open = (AbstractOpenObject) _session
182                                 .remove(KNOWLEDGE_OPEN);
183                 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
184                         open.clearFacades(); // For eventually reopening the knowledge from a fresh context
185                 }
186         }
187
188         /**
189          * Remove the currently open study from the session.
190          */
191         protected void closeStudy() {
192                 AbstractOpenObject open = (AbstractOpenObject) _session
193                                 .remove(STUDY_OPEN);
194                 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
195                         open.clearFacades(); // For eventually reopening the study from a fresh context
196                 }
197         }
198
199         /**
200          * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
201          * 
202          * @param context
203          *            login context
204          * @param user
205          *            the user to connect
206          */
207         protected void connect(final LoginContext context, final User user) {
208                 OpenStudy open = getOpenStudy();
209                 if (open != null) {
210                         open.changeUser(user);
211                 }
212                 _session.put(USER_RIGHTS, new ApplicationRights(user));
213                 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
214         }
215
216         /**
217          * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
218          */
219         protected void disconnect() {
220                 OpenStudy open = getOpenStudy();
221                 if (open != null) {
222                         open.changeUser(null);
223                 }
224                 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
225                 _session.remove(LOGIN_CONTEXT);
226         }
227
228         /**
229          * Get the currently connected user from HTTP session.
230          * 
231          * @return the user
232          */
233         public User getConnectedUser() {
234                 ApplicationRights rights = (ApplicationRights) _session
235                                 .get(USER_RIGHTS);
236                 User connected = null;
237                 if (rights != null) {
238                         connected = rights.getUser();
239                 }
240                 return connected; // May be null
241         }
242
243         /**
244          * Get a menu named as "menu." with the given suffix from HTTP session.
245          * 
246          * @param name
247          *            the menu name suffix
248          * @return the menu
249          */
250         protected Menu getMenu(final String name) {
251                 return (Menu) _session.get("menu." + name);
252         }
253
254         /**
255          * Open knowledge setter.
256          * 
257          * @param kelm
258          *            the OpenKnowledge to set
259          */
260         public void setOpenKnowledge(final OpenKnowledge kelm) {
261                 _openKnowledge = kelm;
262         }
263
264         /**
265          * Open knowledge getter.
266          * 
267          * @return the currently open knowledge wrapper. May be null
268          */
269         protected OpenKnowledge getOpenKnowledge() {
270                 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
271                 return _openKnowledge;
272         }
273
274         /**
275          * Open study setter.
276          * 
277          * @param aStudy
278          *            the OpenStudy to set
279          */
280         public void setOpenStudy(final OpenStudy aStudy) {
281                 _openStudy = aStudy;
282         }
283
284         /**
285          * Open study getter.
286          * 
287          * @return the currently open stydy wrapper. May be null.
288          */
289         public OpenStudy getOpenStudy() {
290                 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
291                 return _openStudy; // May be null
292         }
293
294         /**
295          * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
296          * 
297          * @param kelm
298          *            the knowledge element to open
299          * @return OpenKnowledge wrapper object
300          */
301         protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
302                 OpenKnowledge open = _openKnowledge.open(kelm);
303
304                 closeKnowledge(); // Just in case
305                 _session.put(KNOWLEDGE_OPEN, open);
306                 return open;
307         }
308
309         /**
310          * Open the given study in the current HTTP session. Replace the previose one in the session if any.
311          * 
312          * @param study
313          *            the study to open
314          * @return OpenStudy wrapper object
315          */
316         protected OpenStudy open(final Study study) {
317                 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
318
319                 closeStudy(); // Just in case
320                 _session.put(STUDY_OPEN, open);
321                 return open;
322         }
323
324         /**
325          * Initialization the Context for menubar and toolbar.
326          */
327         public void initializationContext() {
328                 getMenuBarSettings().initializeInitialMenuProperties();
329
330                 if (_session.get(STUDY_OPEN) == null) {
331                         getMenuBarSettings().setIsStudyNull(true);
332                 } else {
333                         getMenuBarSettings().setIsStudyNull(false);
334
335                         // for initialization ToolBarSettings.canUserEdit property
336                         // and ToolBarSettings.isEnabledScript property.
337
338                         OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
339                         PopupMenu popup = currentStudy.getPopup();
340                         StudyRights user = currentStudy.getStudyRights();
341
342                         if (user.canEditProperties()) {
343                                 getToolBarSettings().setCanUserEdit(true);
344                         } else {
345                                 getToolBarSettings().setCanUserEdit(false);
346                         }
347
348                         if (popup == null) {
349                                 getToolBarSettings().setIsEnabledScript(false);
350                         } else if (popup.isEnabled("script")) {
351                                 getToolBarSettings().setIsEnabledScript(true);
352                         } else {
353                                 getToolBarSettings().setIsEnabledScript(false);
354                         }
355                 }
356
357                 if (_session.get(KNOWLEDGE_OPEN) == null) {
358                         getMenuBarSettings().setIsKnowledgeNull(true);
359                 } else {
360                         getMenuBarSettings().setIsKnowledgeNull(false);
361                 }
362
363                 ApplicationRights userRights = (ApplicationRights) _session
364                                 .get(USER_RIGHTS);
365
366                 if ((userRights != null) && userRights.canCreateStudy()) {
367                         getMenuBarSettings().setCanUserCreateStudy(true);
368                 } else {
369                         getMenuBarSettings().setCanUserCreateStudy(false);
370                 }
371
372                 if ((userRights != null) && userRights.canManageDatabase()) {
373                         getMenuBarSettings().setCanUserManageDatabase(true);
374                 } else {
375                         getMenuBarSettings().setCanUserManageDatabase(false);
376                 }
377
378         }
379
380         /**
381          * Initialization the Context for left menu.
382          * 
383          * @param leftMenuProperty -
384          *            the property of the left menu.
385          */
386         public void initializationContextLeftMenus(final String leftMenuProperty) {
387
388                 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
389
390                 getLeftMenuSettings().setMenu(menu);
391                 if (menu != null) {
392                         getLeftMenuSettings().setMenuName(menu.getName());
393                         getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
394                 }
395         }
396
397         /**
398          * Initialization the Context for Menu Bar and Tool Bar.
399          * 
400          * @param titleProperty -
401          *            The title of the open study/knowledge.
402          * @param editDisabledProperty -
403          *            Property that indicates whether the current open study is editable or not.
404          */
405         public void initializationContext(final String titleProperty,
406                         final String editDisabledProperty) {
407
408                 initializationContext();
409
410                 AbstractOpenObject entity = (AbstractOpenObject) _session
411                                 .get(titleProperty + ".open");
412
413                 if (entity != null) {
414                         getTitleBarSettings().setProgressState(
415                                         entity.getProgressState().toString());
416                         getTitleBarSettings().setSelectionState(entity.getSelection());
417                         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
418                         getTitleBarSettings().setEntryTypeTitle(entity.getType());
419                         getTitleBarSettings().setEntryTitle(entity.getTitle());
420                 }
421                 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
422         }
423
424         /**
425          * Initialization of the screen context for menu bar.
426          * 
427          * @param menuProperty -
428          *            the property of the menu bar.
429          */
430         public void initializationScreenContext(final String menuProperty) {
431
432                 initializationContext();
433                 setMenuProperty(menuProperty);
434                 getMenuBarSettings().intializeMenuBar(menuProperty);
435         }
436
437         /**
438          * Initialization of the screen context for menu bar and title bar.
439          * 
440          * @param menuProperty -
441          *            the property of the menu bar.
442          * @param titleProperty -
443          *            The title of the open study/knowledge.
444          * @param editDisabledProperty -
445          *            Property that indicates whether the current open study is editable or not.
446          */
447         public void initializationScreenContext(final String menuProperty,
448                         final String titleProperty, final String editDisabledProperty) {
449
450                 initializationContext(titleProperty, editDisabledProperty);
451                 setMenuProperty(menuProperty);
452                 getMenuBarSettings().intializeMenuBar(menuProperty);
453         }
454
455         /**
456          * Initialization of the screen context for menu bar, title bar and tool bar.
457          * 
458          * @param menuProperty -
459          *            the property of the menu bar.
460          * @param titleProperty -
461          *            The title of the open study/knowledge.
462          * @param editDisabledProperty -
463          *            Property that indicates whether the current open study is editable or not.
464          * @param toolProperty -
465          *            the property of the tool bar.
466          */
467         public void initializationScreenContext(final String menuProperty,
468                         final String titleProperty, final String editDisabledProperty,
469                         final String toolProperty) {
470
471                 initializationScreenContext(menuProperty, titleProperty,
472                                 editDisabledProperty);
473                 getToolBarSettings().intializeMenuBar(toolProperty);
474         }
475
476         /**
477          * Initialization of the screen context for menu bar and tool bar.
478          * 
479          * @param menuProperty -
480          *            the property of the menu bar.
481          * @param toolProperty -
482          *            the property of the tool bar.
483          */
484         public void initializationScreenContext(final String menuProperty,
485                         final String toolProperty) {
486
487                 initializationContext();
488                 setMenuProperty(menuProperty);
489                 getMenuBarSettings().intializeMenuBar(menuProperty);
490                 getToolBarSettings().intializeMenuBar(toolProperty);
491         }
492
493         /**
494          * Initialization of the screen context for menu bar, title bar and tool bar.
495          * 
496          * @param menuProperty -
497          *            the property of the menu bar.
498          * @param titleProperty -
499          *            The title of the open study/knowledge.
500          * @param editDisabledProperty -
501          *            Property that indicates whether the current open study is editable or not.
502          * @param toolProperty -
503          *            the property of the tool bar.
504          * @param leftMenuProperty -
505          *            the property of the left menu.
506          */
507         public void initializationFullScreenContext(final String menuProperty,
508                         final String titleProperty, final String editDisabledProperty,
509                         final String toolProperty, final String leftMenuProperty) {
510
511                 initializationScreenContext(menuProperty, titleProperty,
512                                 editDisabledProperty);
513                 initializationContextLeftMenus(leftMenuProperty);
514                 getToolBarSettings().intializeMenuBar(toolProperty);
515         }
516
517         /**
518          * Initialization of the screen context for menu bar and tool bar.
519          * 
520          * @param menuProperty -
521          *            the property of the menu bar.
522          * @param toolProperty -
523          *            the property of the tool bar.
524          * @param leftMenuProperty -
525          *            the property of the left menu.
526          */
527         public void initializationFullScreenContext(final String menuProperty,
528                         final String toolProperty, final String leftMenuProperty) {
529
530                 initializationContext();
531                 initializationContextLeftMenus(leftMenuProperty);
532                 setMenuProperty(menuProperty);
533                 getMenuBarSettings().intializeMenuBar(menuProperty);
534                 getToolBarSettings().intializeMenuBar(toolProperty);
535         }
536
537         // ==============================================================================================================================
538         // Getters and setters
539         // ==============================================================================================================================
540
541         /**
542          * {@inheritDoc}
543          * 
544          * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
545          */
546         public void setServletRequest(final HttpServletRequest request) {
547                 this._servletRequest = request;
548         }
549
550         /**
551          * Get current HTTP request.
552          * 
553          * @return HTTP request
554          */
555         public HttpServletRequest getServletRequest() {
556                 return _servletRequest;
557         }
558
559         /**
560          * Get current error code.
561          * 
562          * @return error code
563          */
564         public String getErrorCode() {
565                 return _errorCode;
566         }
567
568         /**
569          * Get session map.
570          * 
571          * @return session map
572          */
573         public Map<String, Object> getSession() {
574                 return _session;
575         }
576
577         /**
578          * Set error code.
579          * 
580          * @param code
581          *            the error code to set
582          */
583         public void setErrorCode(final String code) {
584                 this._errorCode = code;
585                 this.setMessage(getText(code));
586         }
587
588         /**
589          * {@inheritDoc}
590          * 
591          * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
592          */
593         public void setSession(final Map<String, Object> session) {
594                 this._session = session;
595         }
596
597         /**
598          * Get the menuBarSettings.
599          * 
600          * @return the menuBarSettings
601          */
602         public MenuBarSettings getMenuBarSettings() {
603                 return _menuBarSettings;
604         }
605
606         /**
607          * Set the menuBarSettings.
608          * 
609          * @param menuBarSettings
610          *            the menuBarSettings to set
611          */
612         public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
613                 _menuBarSettings = menuBarSettings;
614         }
615
616         /**
617          * Get the _titleBarSettings.
618          * 
619          * @return the _titleBarSettings
620          */
621         public TitleBarSettings getTitleBarSettings() {
622                 return _titleBarSettings;
623         }
624
625         /**
626          * Set the titleBarSettings.
627          * 
628          * @param titleBarSettings
629          *            the titleBarSettings to set
630          */
631         public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
632                 _titleBarSettings = titleBarSettings;
633         }
634
635         /**
636          * Get the toolBarSettings.
637          * 
638          * @return the toolBarSettings
639          */
640         public final ToolBarSettings getToolBarSettings() {
641                 return _toolBarSettings;
642         }
643
644         /**
645          * Set the toolBarSettings.
646          * 
647          * @param toolBarSettings
648          *            the toolBarSettings to set
649          */
650         public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
651                 _toolBarSettings = toolBarSettings;
652         }
653
654         /**
655          * Get the applicationSettings.
656          * 
657          * @return the applicationSettings
658          */
659         public ApplicationSettings getApplicationSettings() {
660                 return _applicationSettings;
661         }
662
663         /**
664          * Set the applicationSettings.
665          * 
666          * @param applicationSettings
667          *            the applicationSettings to set
668          */
669         public void setApplicationSettings(
670                         final ApplicationSettings applicationSettings) {
671                 _applicationSettings = applicationSettings;
672         }
673
674         /**
675          * Get the leftMenuSettings.
676          * 
677          * @return the leftMenuSettings
678          */
679         public LeftMenuSettings getLeftMenuSettings() {
680                 return _leftMenuSettings;
681         }
682
683         /**
684          * Set the leftMenuSettings.
685          * 
686          * @param leftMenuSettings
687          *            the leftMenuSettings to set
688          */
689         public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
690                 _leftMenuSettings = leftMenuSettings;
691         }
692
693         /**
694          * Get the actionType.
695          * 
696          * @return the actionType
697          */
698         public String getActionType() {
699                 return _actionType;
700         }
701
702         /**
703          * Set the actionType.
704          * 
705          * @param actionType
706          *            the actionType to set
707          */
708         public void setActionType(final String actionType) {
709                 _actionType = actionType;
710         }
711
712         /**
713          * Get the message.
714          * 
715          * @return the message
716          */
717         public String getMessage() {
718                 return _message;
719         }
720
721         /**
722          * Set the message.
723          * 
724          * @param message
725          *            the message to set
726          */
727         public void setMessage(final String message) {
728                 _message = message;
729         }
730
731         /**
732          * Get the menuProperty.
733          * 
734          * @return the menuProperty
735          */
736         public String getMenuProperty() {
737                 return _menuProperty;
738         }
739
740         /**
741          * Get the toolProperty.
742          * 
743          * @return the toolProperty
744          */
745         public String getToolProperty() {
746                 return _toolProperty;
747         }
748
749         /**
750          * Set the menuProperty.
751          * 
752          * @param menuProperty
753          *            the menuProperty to set
754          */
755         public void setMenuProperty(final String menuProperty) {
756                 this._menuProperty = menuProperty;
757         }
758
759         /**
760          * Set the toolProperty.
761          * 
762          * @param toolProperty
763          *            the toolProperty to set
764          */
765         public void setToolProperty(final String toolProperty) {
766                 _toolProperty = toolProperty;
767         }
768 }