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