]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/Action.java
Salome HOME
Fix for the bug: urls to documents in the repository are cached for all users with...
[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                                         getApplicationSettings().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                                         getApplicationSettings().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                 if (entity != null) {
397                         getTitleBarSettings().setProgressState(
398                                         entity.getProgressState().toString());
399                         getTitleBarSettings().setSelectionState(entity.getSelection());
400                         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
401                         getTitleBarSettings().setEntryTypeTitle(entity.getType());
402                         getTitleBarSettings().setEntryTitle(entity.getTitle());
403                 }
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          * Set the leftMenuSettings.
663          * 
664          * @param leftMenuSettings
665          *            the leftMenuSettings to set
666          */
667         public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
668                 _leftMenuSettings = leftMenuSettings;
669         }
670         
671         /**
672          * Get the actionType.
673          * @return the actionType
674          */
675         public String getActionType() {
676                 return _actionType;
677         }
678
679         /**
680          * Set the actionType.
681          * @param actionType the actionType to set
682          */
683         public void setActionType(final String actionType) {
684                 _actionType = actionType;
685         }
686 }