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