Salome HOME
ff3235524a018b4b2a0307789998e168a3dbbfba
[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.remove(KNOWLEDGE_OPEN);
182                 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
183                         open.clearFacades(); // For eventually reopening the knowledge from a fresh context
184                 }
185         }
186
187         /**
188          * Remove the currently open study from the session.
189          */
190         protected void closeStudy() {
191                 AbstractOpenObject open = (AbstractOpenObject) _session.remove(STUDY_OPEN);
192                 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
193                         open.clearFacades(); // For eventually reopening the study from a fresh context
194                 }
195         }
196
197         /**
198          * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
199          * 
200          * @param context
201          *            login context
202          * @param user
203          *            the user to connect
204          */
205         protected void connect(final LoginContext context, final User user) {
206                 OpenStudy open = getOpenStudy();
207                 if (open != null) {
208                         open.changeUser(user);
209                 }
210                 _session.put(USER_RIGHTS, new ApplicationRights(user));
211                 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
212         }
213
214         /**
215          * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
216          */
217         protected void disconnect() {
218                 OpenStudy open = getOpenStudy();
219                 if (open != null) {
220                         open.changeUser(null);
221                 }
222                 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
223                 _session.remove(LOGIN_CONTEXT);
224         }
225
226         /**
227          * Get the currently connected user from HTTP session.
228          * 
229          * @return the user
230          */
231         public User getConnectedUser() {
232                 ApplicationRights rights = (ApplicationRights) _session
233                                 .get(USER_RIGHTS);
234                 User connected = null;
235                 if (rights != null) {
236                         connected = rights.getUser();
237                 }
238                 return connected; // May be null
239         }
240
241         /**
242          * Get a menu named as "menu." with the given suffix from HTTP session.
243          * 
244          * @param name
245          *            the menu name suffix
246          * @return the menu
247          */
248         protected Menu getMenu(final String name) {
249                 return (Menu) _session.get("menu." + name);
250         }
251
252         /**
253          * Open knowledge setter.
254          * 
255          * @param kelm
256          *            the OpenKnowledge to set
257          */
258         public void setOpenKnowledge(final OpenKnowledge kelm) {
259                 _openKnowledge = kelm;
260         }
261
262         /**
263          * Open knowledge getter.
264          * 
265          * @return the currently open knowledge wrapper. May be null
266          */
267         protected OpenKnowledge getOpenKnowledge() {
268                 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
269                 return _openKnowledge;
270         }
271
272         /**
273          * Open study setter.
274          * 
275          * @param aStudy
276          *            the OpenStudy to set
277          */
278         public void setOpenStudy(final OpenStudy aStudy) {
279                 _openStudy = aStudy;
280         }
281
282         /**
283          * Open study getter.
284          * 
285          * @return the currently open stydy wrapper. May be null.
286          */
287         public OpenStudy getOpenStudy() {
288                 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
289                 return _openStudy; // May be null
290         }
291
292         /**
293          * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
294          * 
295          * @param kelm
296          *            the knowledge element to open
297          * @return OpenKnowledge wrapper object
298          */
299         protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
300                 OpenKnowledge open = _openKnowledge.open(kelm);
301
302                 closeKnowledge(); // Just in case
303                 _session.put(KNOWLEDGE_OPEN, open);
304                 return open;
305         }
306
307         /**
308          * Open the given study in the current HTTP session. Replace the previose one in the session if any.
309          * 
310          * @param study
311          *            the study to open
312          * @return OpenStudy wrapper object
313          */
314         protected OpenStudy open(final Study study) {
315                 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
316
317                 closeStudy(); // Just in case
318                 _session.put(STUDY_OPEN, open);
319                 return open;
320         }
321
322         /**
323          * Initialization the Context for menubar and toolbar.
324          */
325         public void initializationContext() {
326                 getMenuBarSettings().initializeInitialMenuProperties();
327
328                 if (_session.get(STUDY_OPEN) == null) {
329                         getMenuBarSettings().setIsStudyNull(true);
330                 } else {
331                         getMenuBarSettings().setIsStudyNull(false);
332
333                         // for initialization ToolBarSettings.canUserEdit property
334                         // and ToolBarSettings.isEnabledScript property.
335
336                         OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
337                         PopupMenu popup = currentStudy.getPopup();
338                         StudyRights user = currentStudy.getStudyRights();
339
340                         if (user.canEditProperties()) {
341                                 getToolBarSettings().setCanUserEdit(true);
342                         } else {
343                                 getToolBarSettings().setCanUserEdit(false);
344                         }
345
346                         if (popup == null) {
347                                 getToolBarSettings().setIsEnabledScript(false);
348                         } else if (popup.isEnabled("script")) {
349                                 getToolBarSettings().setIsEnabledScript(true);
350                         } else {
351                                 getToolBarSettings().setIsEnabledScript(false);
352                         }
353                 }
354
355                 if (_session.get(KNOWLEDGE_OPEN) == null) {
356                         getMenuBarSettings().setIsKnowledgeNull(true);
357                 } else {
358                         getMenuBarSettings().setIsKnowledgeNull(false);
359                 }
360
361                 ApplicationRights userRights = (ApplicationRights) _session
362                                 .get(USER_RIGHTS);
363
364                 if ((userRights != null) && userRights.canCreateStudy()) {
365                         getMenuBarSettings().setCanUserCreateStudy(true);
366                 } else {
367                         getMenuBarSettings().setCanUserCreateStudy(false);
368                 }
369
370                 if ((userRights != null) && userRights.canManageDatabase()) {
371                         getMenuBarSettings().setCanUserManageDatabase(true);
372                 } else {
373                         getMenuBarSettings().setCanUserManageDatabase(false);
374                 }
375
376         }
377
378         /**
379          * Initialization the Context for left menu.
380          * 
381          * @param leftMenuProperty -
382          *            the property of the left menu.
383          */
384         public void initializationContextLeftMenus(final String leftMenuProperty) {
385
386                 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
387
388                 getLeftMenuSettings().setMenu(menu);
389                 getLeftMenuSettings().setMenuName(menu.getName());
390                 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
391         }
392
393         /**
394          * Initialization the Context for Menu Bar and Tool Bar.
395          * 
396          * @param titleProperty -
397          *            The title of the open study/knowledge.
398          * @param editDisabledProperty -
399          *            Property that indicates whether the current open study is editable or not.
400          */
401         public void initializationContext(final String titleProperty,
402                         final String editDisabledProperty) {
403
404                 initializationContext();
405
406                 AbstractOpenObject entity = (AbstractOpenObject) _session.get(titleProperty + ".open");
407
408                 if (entity != null) {
409                         getTitleBarSettings().setProgressState(
410                                         entity.getProgressState().toString());
411                         getTitleBarSettings().setSelectionState(entity.getSelection());
412                         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
413                         getTitleBarSettings().setEntryTypeTitle(entity.getType());
414                         getTitleBarSettings().setEntryTitle(entity.getTitle());
415                 }
416                 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
417         }
418
419         /**
420          * Initialization of the screen context for menu bar.
421          * 
422          * @param menuProperty -
423          *            the property of the menu bar.
424          */
425         public void initializationScreenContext(final String menuProperty) {
426
427                 initializationContext();
428                 setMenuProperty(menuProperty);
429                 getMenuBarSettings().intializeMenuBar(menuProperty);
430         }
431
432         /**
433          * Initialization of the screen context for menu bar and title bar.
434          * 
435          * @param menuProperty -
436          *            the property of the menu bar.
437          * @param titleProperty -
438          *            The title of the open study/knowledge.
439          * @param editDisabledProperty -
440          *            Property that indicates whether the current open study is editable or not.
441          */
442         public void initializationScreenContext(final String menuProperty,
443                         final String titleProperty, final String editDisabledProperty) {
444
445                 initializationContext(titleProperty, editDisabledProperty);
446                 setMenuProperty(menuProperty);
447                 getMenuBarSettings().intializeMenuBar(menuProperty);
448         }
449
450         /**
451          * Initialization of the screen context for menu bar, title bar and tool bar.
452          * 
453          * @param menuProperty -
454          *            the property of the menu bar.
455          * @param titleProperty -
456          *            The title of the open study/knowledge.
457          * @param editDisabledProperty -
458          *            Property that indicates whether the current open study is editable or not.
459          * @param toolProperty -
460          *            the property of the tool bar.
461          */
462         public void initializationScreenContext(final String menuProperty,
463                         final String titleProperty, final String editDisabledProperty,
464                         final String toolProperty) {
465
466                 initializationScreenContext(menuProperty, titleProperty,
467                                 editDisabledProperty);
468                 getToolBarSettings().intializeMenuBar(toolProperty);
469         }
470
471         /**
472          * Initialization of the screen context for menu bar and tool bar.
473          * 
474          * @param menuProperty -
475          *            the property of the menu bar.
476          * @param toolProperty -
477          *            the property of the tool bar.
478          */
479         public void initializationScreenContext(final String menuProperty,
480                         final String toolProperty) {
481
482                 initializationContext();
483                 setMenuProperty(menuProperty);
484                 getMenuBarSettings().intializeMenuBar(menuProperty);
485                 getToolBarSettings().intializeMenuBar(toolProperty);
486         }
487
488         /**
489          * Initialization of the screen context for menu bar, title bar and tool bar.
490          * 
491          * @param menuProperty -
492          *            the property of the menu bar.
493          * @param titleProperty -
494          *            The title of the open study/knowledge.
495          * @param editDisabledProperty -
496          *            Property that indicates whether the current open study is editable or not.
497          * @param toolProperty -
498          *            the property of the tool bar.
499          * @param leftMenuProperty -
500          *            the property of the left menu.
501          */
502         public void initializationFullScreenContext(final String menuProperty,
503                         final String titleProperty, final String editDisabledProperty,
504                         final String toolProperty, final String leftMenuProperty) {
505
506                 initializationScreenContext(menuProperty, titleProperty,
507                                 editDisabledProperty);
508                 initializationContextLeftMenus(leftMenuProperty);
509                 getToolBarSettings().intializeMenuBar(toolProperty);
510         }
511
512         /**
513          * Initialization of the screen context for menu bar and tool bar.
514          * 
515          * @param menuProperty -
516          *            the property of the menu bar.
517          * @param toolProperty -
518          *            the property of the tool bar.
519          * @param leftMenuProperty -
520          *            the property of the left menu.
521          */
522         public void initializationFullScreenContext(final String menuProperty,
523                         final String toolProperty, final String leftMenuProperty) {
524
525                 initializationContext();
526                 initializationContextLeftMenus(leftMenuProperty);
527                 setMenuProperty(menuProperty);
528                 getMenuBarSettings().intializeMenuBar(menuProperty);
529                 getToolBarSettings().intializeMenuBar(toolProperty);
530         }
531
532         // ==============================================================================================================================
533         // Getters and setters
534         // ==============================================================================================================================
535
536         /**
537          * {@inheritDoc}
538          * 
539          * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
540          */
541         public void setServletRequest(final HttpServletRequest request) {
542                 this._servletRequest = request;
543         }
544
545         /**
546          * Get current HTTP request.
547          * 
548          * @return HTTP request
549          */
550         public HttpServletRequest getServletRequest() {
551                 return _servletRequest;
552         }
553
554         /**
555          * Get current error code.
556          * 
557          * @return error code
558          */
559         public String getErrorCode() {
560                 return _errorCode;
561         }
562
563         /**
564          * Get session map.
565          * 
566          * @return session map
567          */
568         public Map<String, Object> getSession() {
569                 return _session;
570         }
571
572         /**
573          * Set error code.
574          * 
575          * @param code
576          *            the error code to set
577          */
578         public void setErrorCode(final String code) {
579                 this._errorCode = code;
580         }
581
582         /**
583          * {@inheritDoc}
584          * 
585          * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
586          */
587         public void setSession(final Map<String, Object> session) {
588                 this._session = session;
589         }
590
591         /**
592          * Get the menuBarSettings.
593          * 
594          * @return the menuBarSettings
595          */
596         public MenuBarSettings getMenuBarSettings() {
597                 return _menuBarSettings;
598         }
599
600         /**
601          * Set the menuBarSettings.
602          * 
603          * @param menuBarSettings
604          *            the menuBarSettings to set
605          */
606         public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
607                 _menuBarSettings = menuBarSettings;
608         }
609
610         /**
611          * Get the _titleBarSettings.
612          * 
613          * @return the _titleBarSettings
614          */
615         public TitleBarSettings getTitleBarSettings() {
616                 return _titleBarSettings;
617         }
618
619         /**
620          * Set the titleBarSettings.
621          * 
622          * @param titleBarSettings
623          *            the titleBarSettings to set
624          */
625         public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
626                 _titleBarSettings = titleBarSettings;
627         }
628
629         /**
630          * Get the toolBarSettings.
631          * 
632          * @return the toolBarSettings
633          */
634         public final ToolBarSettings getToolBarSettings() {
635                 return _toolBarSettings;
636         }
637
638         /**
639          * Set the toolBarSettings.
640          * 
641          * @param toolBarSettings
642          *            the toolBarSettings to set
643          */
644         public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
645                 _toolBarSettings = toolBarSettings;
646         }
647
648         /**
649          * Get the applicationSettings.
650          * 
651          * @return the applicationSettings
652          */
653         public ApplicationSettings getApplicationSettings() {
654                 return _applicationSettings;
655         }
656
657         /**
658          * Set the applicationSettings.
659          * 
660          * @param applicationSettings
661          *            the applicationSettings to set
662          */
663         public void setApplicationSettings(
664                         final ApplicationSettings applicationSettings) {
665                 _applicationSettings = applicationSettings;
666         }
667
668         /**
669          * Get the leftMenuSettings.
670          * 
671          * @return the leftMenuSettings
672          */
673         public LeftMenuSettings getLeftMenuSettings() {
674                 return _leftMenuSettings;
675         }
676
677         /**
678          * Set the leftMenuSettings.
679          * 
680          * @param leftMenuSettings
681          *            the leftMenuSettings to set
682          */
683         public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
684                 _leftMenuSettings = leftMenuSettings;
685         }
686         
687         /**
688          * Get the actionType.
689          * @return the actionType
690          */
691         public String getActionType() {
692                 return _actionType;
693         }
694
695         /**
696          * Set the actionType.
697          * @param actionType the actionType to set
698          */
699         public void setActionType(final String actionType) {
700                 _actionType = actionType;
701         }
702
703         /**
704          * Get the message.
705          * @return the message
706          */
707         public String getMessage() {
708                 return _message;
709         }
710
711         /**
712          * Set the message.
713          * @param message the message to set
714          */
715         public void setMessage(final String message) {
716                 _message = message;
717         }
718
719         /**
720          * Get the menuProperty.
721          * 
722          * @return the menuProperty
723          */
724         public String getMenuProperty() {
725                 return _menuProperty;
726         }
727
728         /**
729          * Get the toolProperty.
730          * 
731          * @return the toolProperty
732          */
733         public String getToolProperty() {
734                 return _toolProperty;
735         }
736
737         /**
738          * Set the menuProperty.
739          * 
740          * @param menuProperty
741          *            the menuProperty to set
742          */
743         public void setMenuProperty(final String menuProperty) {
744                 this._menuProperty = menuProperty;
745         }
746
747         /**
748          * Set the toolProperty.
749          * 
750          * @param toolProperty
751          *            the toolProperty to set
752          */
753         public void setToolProperty(final String toolProperty) {
754                 _toolProperty = toolProperty;
755         }
756 }