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