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