Salome HOME
During checkout call run_salome_siman.sh.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / Action.java
1 package org.splat.simer;
2
3 import java.util.Comparator;
4 import java.util.Map;
5 import java.util.ResourceBundle;
6
7 import javax.security.auth.login.LoginContext;
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.apache.log4j.Logger;
11 import org.apache.struts2.interceptor.ServletRequestAware;
12 import org.apache.struts2.interceptor.SessionAware;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.DocumentType;
15 import org.splat.dal.bo.som.SimulationContextType;
16 import org.splat.dal.bo.som.Study;
17 import org.splat.service.dto.KnowledgeElementDTO;
18 import org.splat.som.ApplicationRights;
19 import org.splat.som.StudyRights;
20 import org.splat.wapp.Menu;
21 import org.splat.wapp.PopupMenu;
22
23 import com.opensymphony.xwork2.ActionSupport;
24
25 /**
26  * Base Siman action.
27  */
28 public class Action extends ActionSupport implements ServletRequestAware,
29                 SessionAware {
30
31         /**
32          * Serial version ID.
33          */
34         private static final long serialVersionUID = -895295026709526501L;
35         /**
36          * Action logger.
37          */
38         protected static final Logger LOG = Logger.getLogger(Action.class);
39
40         /**
41          * Open knowledge key in session.
42          */
43         public static final String KNOWLEDGE_OPEN = "knowledge.open";
44
45         /**
46          * Open study key in session.
47          */
48         public static final String STUDY_OPEN = "study.open";
49
50         /**
51          * User rights key in session.
52          */
53         public static final String USER_RIGHTS = "user.rights";
54
55         /**
56          * Login context key in session.
57          */
58         public static final String LOGIN_CONTEXT = "login.context";
59
60         /**
61          * Http servlet request.
62          */
63         private HttpServletRequest _servletRequest;
64         /**
65          * Http session container.
66          */
67         private Map<String, Object> _session;
68         /**
69          * Error code.
70          */
71         private String _errorCode;
72         /**
73          * The message to show when the page is loaded.
74          */
75         private String _message = "";
76         /**
77          * ActionType for specifying the type of the operaion.
78          */
79         private String _actionType;
80         /**
81          * Current open study facade object.
82          */
83         private OpenStudy _openStudy;
84         /**
85          * Current open knowledge facade object.
86          */
87         private OpenKnowledge _openKnowledge;
88
89         /**
90          * MenuBarSettings bean.
91          */
92         private MenuBarSettings _menuBarSettings;
93
94         /**
95          * TitleBarSettings bean.
96          */
97         private TitleBarSettings _titleBarSettings;
98         /**
99          * ToolBarSettings bean.
100          */
101         private ToolBarSettings _toolBarSettings;
102
103         /**
104          * LeftMenuSettings bean.
105          */
106         private LeftMenuSettings _leftMenuSettings;
107
108         /**
109          * Injected application settings bean.
110          */
111         private ApplicationSettings _applicationSettings;
112
113         /**
114          * Comparator for sorting document types with localized names.
115          */
116         public class DocumentTypeComparator implements Comparator<DocumentType> {
117                 /**
118                  * {@inheritDoc}
119                  * 
120                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
121                  */
122                 public int compare(final DocumentType t1, final DocumentType t2) {
123                         ResourceBundle locale = ResourceBundle.getBundle("som",
124                                         getApplicationSettings().getCurrentLocale());
125                         String name1 = t1.getName();
126                         if (t1.isApproved()) {
127                                 name1 = locale.getString("type.document." + name1);
128                         }
129                         String name2 = t2.getName();
130                         if (t2.isApproved()) {
131                                 name2 = locale.getString("type.document." + name2);
132                         }
133
134                         return name1.compareToIgnoreCase(name2);
135                 }
136         }
137
138         /**
139          * Comparator for sorting simulation context types with localized names.
140          */
141         public class ContextTypeComparator implements
142                         Comparator<SimulationContextType> {
143                 /**
144                  * {@inheritDoc}
145                  * 
146                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
147                  */
148                 public int compare(final SimulationContextType t1,
149                                 final SimulationContextType t2) {
150                         ResourceBundle locale = ResourceBundle.getBundle("som",
151                                         getApplicationSettings().getCurrentLocale());
152                         String name1 = t1.getName();
153                         if (t1.isApproved()) {
154                                 name1 = locale.getString("type.context." + name1);
155                         }
156                         String name2 = t2.getName();
157                         if (t2.isApproved()) {
158                                 name2 = locale.getString("type.context." + name2);
159                         }
160
161                         return name1.compareToIgnoreCase(name2);
162                 }
163         }
164
165         // ==============================================================================================================================
166         // Session services
167         // ==============================================================================================================================
168
169         /**
170          * Remove the currently open knowledge from the session.
171          */
172         protected void closeKnowledge() {
173                 AbstractOpenObject open = (AbstractOpenObject) _session.remove(KNOWLEDGE_OPEN);
174                 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
175                         open.clearFacades(); // For eventually reopening the knowledge from a fresh context
176                 }
177         }
178
179         /**
180          * Remove the currently open study from the session.
181          */
182         protected void closeStudy() {
183                 AbstractOpenObject open = (AbstractOpenObject) _session.remove(STUDY_OPEN);
184                 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
185                         open.clearFacades(); // For eventually reopening the study from a fresh context
186                 }
187         }
188
189         /**
190          * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
191          * 
192          * @param context
193          *            login context
194          * @param user
195          *            the user to connect
196          */
197         protected void connect(final LoginContext context, final User user) {
198                 OpenStudy open = getOpenStudy();
199                 if (open != null) {
200                         open.changeUser(user);
201                 }
202                 _session.put(USER_RIGHTS, new ApplicationRights(user));
203                 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
204         }
205
206         /**
207          * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
208          */
209         protected void disconnect() {
210                 OpenStudy open = getOpenStudy();
211                 if (open != null) {
212                         open.changeUser(null);
213                 }
214                 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
215                 _session.remove(LOGIN_CONTEXT);
216         }
217
218         /**
219          * Get the currently connected user from HTTP session.
220          * 
221          * @return the user
222          */
223         public User getConnectedUser() {
224                 ApplicationRights rights = (ApplicationRights) _session
225                                 .get(USER_RIGHTS);
226                 User connected = null;
227                 if (rights != null) {
228                         connected = rights.getUser();
229                 }
230                 return connected; // May be null
231         }
232
233         /**
234          * Get a menu named as "menu." with the given suffix from HTTP session.
235          * 
236          * @param name
237          *            the menu name suffix
238          * @return the menu
239          */
240         protected Menu getMenu(final String name) {
241                 return (Menu) _session.get("menu." + name);
242         }
243
244         /**
245          * Open knowledge setter.
246          * 
247          * @param kelm
248          *            the OpenKnowledge to set
249          */
250         public void setOpenKnowledge(final OpenKnowledge kelm) {
251                 _openKnowledge = kelm;
252         }
253
254         /**
255          * Open knowledge getter.
256          * 
257          * @return the currently open knowledge wrapper. May be null
258          */
259         protected OpenKnowledge getOpenKnowledge() {
260                 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
261                 return _openKnowledge;
262         }
263
264         /**
265          * Open study setter.
266          * 
267          * @param aStudy
268          *            the OpenStudy to set
269          */
270         public void setOpenStudy(final OpenStudy aStudy) {
271                 _openStudy = aStudy;
272         }
273
274         /**
275          * Open study getter.
276          * 
277          * @return the currently open stydy wrapper. May be null.
278          */
279         public OpenStudy getOpenStudy() {
280                 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
281                 return _openStudy; // May be null
282         }
283
284         /**
285          * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
286          * 
287          * @param kelm
288          *            the knowledge element to open
289          * @return OpenKnowledge wrapper object
290          */
291         protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
292                 OpenKnowledge open = _openKnowledge.open(kelm);
293
294                 closeKnowledge(); // Just in case
295                 _session.put(KNOWLEDGE_OPEN, open);
296                 return open;
297         }
298
299         /**
300          * Open the given study in the current HTTP session. Replace the previose one in the session if any.
301          * 
302          * @param study
303          *            the study to open
304          * @return OpenStudy wrapper object
305          */
306         protected OpenStudy open(final Study study) {
307                 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
308
309                 closeStudy(); // Just in case
310                 _session.put(STUDY_OPEN, open);
311                 return open;
312         }
313
314         /**
315          * Initialization the Context for menubar and toolbar.
316          */
317         public void initializationContext() {
318                 getMenuBarSettings().initializeInitialMenuProperties();
319
320                 if (_session.get(STUDY_OPEN) == null) {
321                         getMenuBarSettings().setIsStudyNull(true);
322                 } else {
323                         getMenuBarSettings().setIsStudyNull(false);
324
325                         // for initialization ToolBarSettings.canUserEdit property
326                         // and ToolBarSettings.isEnabledScript property.
327
328                         OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
329                         PopupMenu popup = currentStudy.getPopup();
330                         StudyRights user = currentStudy.getStudyRights();
331
332                         if (user.canEditProperties()) {
333                                 getToolBarSettings().setCanUserEdit(true);
334                         } else {
335                                 getToolBarSettings().setCanUserEdit(false);
336                         }
337
338                         if (popup == null) {
339                                 getToolBarSettings().setIsEnabledScript(false);
340                         } else if (popup.isEnabled("script")) {
341                                 getToolBarSettings().setIsEnabledScript(true);
342                         } else {
343                                 getToolBarSettings().setIsEnabledScript(false);
344                         }
345                 }
346
347                 if (_session.get(KNOWLEDGE_OPEN) == null) {
348                         getMenuBarSettings().setIsKnowledgeNull(true);
349                 } else {
350                         getMenuBarSettings().setIsKnowledgeNull(false);
351                 }
352
353                 ApplicationRights userRights = (ApplicationRights) _session
354                                 .get(USER_RIGHTS);
355
356                 if ((userRights != null) && userRights.canCreateStudy()) {
357                         getMenuBarSettings().setCanUserCreateStudy(true);
358                 } else {
359                         getMenuBarSettings().setCanUserCreateStudy(false);
360                 }
361
362                 if ((userRights != null) && userRights.canManageDatabase()) {
363                         getMenuBarSettings().setCanUserManageDatabase(true);
364                 } else {
365                         getMenuBarSettings().setCanUserManageDatabase(false);
366                 }
367
368         }
369
370         /**
371          * Initialization the Context for left menu.
372          * 
373          * @param leftMenuProperty -
374          *            the property of the left menu.
375          */
376         public void initializationContextLeftMenus(final String leftMenuProperty) {
377
378                 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
379
380                 getLeftMenuSettings().setMenu(menu);
381                 getLeftMenuSettings().setMenuName(menu.getName());
382                 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
383         }
384
385         /**
386          * Initialization the Context for Menu Bar and Tool Bar.
387          * 
388          * @param titleProperty -
389          *            The title of the open study/knowledge.
390          * @param editDisabledProperty -
391          *            Property that indicates whether the current open study is editable or not.
392          */
393         public void initializationContext(final String titleProperty,
394                         final String editDisabledProperty) {
395
396                 initializationContext();
397
398                 AbstractOpenObject entity = (AbstractOpenObject) _session.get(titleProperty + ".open");
399
400                 if (entity != null) {
401                         getTitleBarSettings().setProgressState(
402                                         entity.getProgressState().toString());
403                         getTitleBarSettings().setSelectionState(entity.getSelection());
404                         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
405                         getTitleBarSettings().setEntryTypeTitle(entity.getType());
406                         getTitleBarSettings().setEntryTitle(entity.getTitle());
407                 }
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          * Set the leftMenuSettings.
667          * 
668          * @param leftMenuSettings
669          *            the leftMenuSettings to set
670          */
671         public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
672                 _leftMenuSettings = leftMenuSettings;
673         }
674         
675         /**
676          * Get the actionType.
677          * @return the actionType
678          */
679         public String getActionType() {
680                 return _actionType;
681         }
682
683         /**
684          * Set the actionType.
685          * @param actionType the actionType to set
686          */
687         public void setActionType(final String actionType) {
688                 _actionType = actionType;
689         }
690
691         /**
692          * Get the message.
693          * @return the message
694          */
695         public String getMessage() {
696                 return _message;
697         }
698
699         /**
700          * Set the message.
701          * @param message the message to set
702          */
703         public void setMessage(final String message) {
704                 _message = message;
705         }
706 }