Salome HOME
7b56bc4c3d9e579374c1d201bd4169892606929d
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / Action.java
1 package org.splat.simer;
2
3 import java.util.Map;
4 import java.util.Comparator;
5 import java.util.ResourceBundle;
6
7 import javax.security.auth.login.LoginContext;
8 import javax.servlet.http.HttpServletRequest;
9
10 import com.opensymphony.xwork2.ActionSupport;
11
12 import org.apache.struts2.interceptor.ServletRequestAware;
13 import org.apache.struts2.interceptor.SessionAware;
14 import org.apache.log4j.Logger;
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.som.ApplicationRights;
17 import org.splat.som.StudyRights;
18 import org.splat.dal.bo.som.KnowledgeElement;
19 import org.splat.dal.bo.som.SimulationContextType;
20 import org.splat.dal.bo.som.Study;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.wapp.Menu;
23 import org.splat.wapp.PopupMenu;
24
25
26 /**
27  * Base Siman action.
28  */
29 public class Action extends ActionSupport implements ServletRequestAware, 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  logger           = Logger.getLogger(Action.class);
39
40         /**
41          * Http servlet request.
42          */
43         private HttpServletRequest  request;
44         /**
45          * Http session container.
46          */
47         private   Map<String, Object>  session;
48         /**
49          * Error code.
50          */
51         private   String               mercode;
52         /**
53          * Current open study facade object.
54          */
55         private OpenStudy _openStudy;
56         /**
57          * Current open knowledge facade object.
58          */
59         private OpenKnowledge _openKnowledge;
60         
61         
62         /**
63          * MenuBarSettings bean.
64          */
65         private MenuBarSettings _menuBarSettings;
66         
67         /**
68          * TitleBarSettings bean.
69          */
70         private TitleBarSettings _titleBarSettings;
71         /**
72          * ToolBarSettings bean.
73          */
74         private ToolBarSettings _toolBarSettings;
75         
76         /**
77          * LeftMenuSettings bean.
78          */
79         private LeftMenuSettings _leftMenuSettings;
80         
81         /**
82          * Injected application settings bean.
83          */
84         private ApplicationSettings _applicationSettings;
85         
86         /**
87          * Will be removed!!!
88          * It's temporary solution for menuitem.jsp
89          */
90         private static Menu staticMenu; 
91
92     public class DocumentTypeComparator implements Comparator<DocumentType> {
93       public int compare(DocumentType t1, DocumentType t2)
94       {
95         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
96         String               name1  = t1.getName();
97         if (t1.isApproved()) name1  = locale.getString("type.document." + name1);
98         String               name2  = t2.getName();
99         if (t2.isApproved()) name2  = locale.getString("type.document." + name2);
100
101         return name1.compareToIgnoreCase(name2);
102       }
103     }
104     public class ContextTypeComparator  implements Comparator<SimulationContextType> {
105       public int compare(SimulationContextType t1, SimulationContextType t2)
106       {
107         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
108         String               name1  = t1.getName();
109         if (t1.isApproved()) name1  = locale.getString("type.context." + name1);
110         String               name2  = t2.getName();
111         if (t2.isApproved()) name2  = locale.getString("type.context." + name2);
112
113         return name1.compareToIgnoreCase(name2);
114       }
115     }
116
117 //  ==============================================================================================================================
118 //  Session services
119 //  ==============================================================================================================================
120
121     protected void closeKnowledge () {
122       OpenObject open = (OpenObject)session.remove("knowledge.open");
123       if (open != null) {
124         if (session.get("study.open") == null) open.clearFacades();      // For eventually reopening the knowledge from a fresh context
125       }
126     }
127     protected void closeStudy () {
128       OpenObject open = (OpenObject)session.remove("study.open");
129       if (open != null) {
130         if (session.get("knowledge.open") == null) open.clearFacades();  // For eventually reopening the study from a fresh context
131       }
132     }
133     protected void connect (LoginContext context, User user) {
134       OpenStudy  open = getOpenStudy();
135       if (open != null) {
136           open.changeUser(user);
137       }
138       session.put("user.rights", new ApplicationRights(user) );
139       session.put("login.context", context);                             // For executing the deconnection, when requested
140     }
141     protected void disconnect () {
142       OpenStudy  open = getOpenStudy();
143       if (open != null) {
144           open.changeUser(null);
145       }
146       session.put("user.rights", new ApplicationRights(null) );          // Disables user rights
147       session.remove("login.context");
148     }
149     public User getConnectedUser () {
150       ApplicationRights  rights = (ApplicationRights)session.get("user.rights");
151       User connected = null;
152       if (rights != null) {
153           connected = rights.getUser();
154       }
155       return connected;                                           // May be null
156     }
157     protected Menu getMenu (String name) {
158       return (Menu)session.get("menu." + name);
159     }
160     public void setOpenKnowledge (OpenKnowledge kelm) {
161         _openKnowledge = kelm;
162       }
163     protected OpenKnowledge getOpenKnowledge () {
164 //      _openKnowledge = (OpenKnowledge)session.get("knowledge.open");               // May be null
165         return _openKnowledge;
166     }
167     public void setOpenStudy (OpenStudy aStudy) {
168       _openStudy = aStudy;
169     }
170     public OpenStudy getOpenStudy () {
171 //      _openStudy = (OpenStudy)session.get("study.open");
172           return _openStudy;                       // May be null
173         }
174     protected OpenKnowledge open (KnowledgeElement kelm) {
175       OpenKnowledge open = _openKnowledge.open(kelm);
176
177       closeKnowledge();   // Just in case
178           session.put("knowledge.open", open);
179           return open;
180     }
181     protected OpenStudy open (Study study) {
182           OpenStudy open = _openStudy.open(getConnectedUser(), study);         // The connected user may be null
183
184       closeStudy();       // Just in case
185           session.put("study.open", open);
186           return open;
187     }
188     
189     /**
190      * Initialization the Context for menubar and toolbar.
191      */
192     public void initializationContext() {
193         getMenuBarSettings().initializeInitialMenuProperties();
194         
195         if (session.get("study.open") == null) {
196                 getMenuBarSettings().setIsStudyNull(true);
197         } else {
198                 getMenuBarSettings().setIsStudyNull(false);
199                 
200                 //for initialization ToolBarSettings.canUserEdit property
201                 // and ToolBarSettings.isEnabledScript property.
202                 
203                 OpenStudy currentStudy = (OpenStudy)session.get("study.open");
204                 PopupMenu    popup = currentStudy.getPopup();
205             StudyRights  user  = currentStudy.getStudyRights();
206             
207             if(user.canEditProperties()) {
208                 getToolBarSettings().setCanUserEdit(true);
209             } else {
210                 getToolBarSettings().setCanUserEdit(false);
211             }
212             
213             if (popup == null) {
214                 getToolBarSettings().setIsEnabledScript(false);
215             } else if (popup.isEnabled("script")) {
216                 getToolBarSettings().setIsEnabledScript(true);
217             } else {
218                 getToolBarSettings().setIsEnabledScript(false);
219             }
220         }
221         
222         if (session.get("knowledge.open") == null) {
223                 getMenuBarSettings().setIsKnowledgeNull(true);
224         } else {
225                 getMenuBarSettings().setIsKnowledgeNull(false);
226         }
227         
228         ApplicationRights userRights  = (ApplicationRights)session.get("user.rights");
229         
230         if ((userRights != null) && userRights.canCreateStudy()) {
231                 getMenuBarSettings().setCanUserCreateStudy(true);
232         } else {
233                 getMenuBarSettings().setCanUserCreateStudy(false);
234         }
235         
236         if ((userRights != null) && userRights.canManageDatabase()) {
237                 getMenuBarSettings().setCanUserManageDatabase(true);
238         } else {
239                 getMenuBarSettings().setCanUserManageDatabase(false);
240         } 
241         
242     }
243     
244     /**
245      * Initialization the Context for left menu.
246      * @param leftMenuProperty - the property of the left menu.
247      */
248     public void initializationContextLeftMenus(final String leftMenuProperty) {
249                   
250         Menu    menu   = (Menu)session.get("menu." + leftMenuProperty);
251         
252         getLeftMenuSettings().setMenu(menu);
253         setStaticMenu(menu);
254         getLeftMenuSettings().setMenuName(menu.getName());
255         getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
256     }
257     
258     /**s
259      * Initialization the Context for Menu Bar and Tool Bar.
260      * 
261      * @param titleProperty - The title of the open study/knowledge.
262      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
263      */
264     public void initializationContext(final String titleProperty, final String editDisabledProperty) {
265         
266         initializationContext();
267         
268         OpenObject entity = (OpenObject)session.get(titleProperty + ".open");
269         
270         getTitleBarSettings().setProgressState(entity.getProgressState().toString());
271         getTitleBarSettings().setSelectionState(entity.getSelection());
272         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
273         getTitleBarSettings().setEntryTypeTitle(entity.getType());
274         getTitleBarSettings().setEntryTitle(entity.getTitle());
275         getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
276     }
277     
278     /**
279          * Initialization of the screen context for menu bar.
280          * 
281          * @param menuProperty - the property of the menu bar.
282          */
283         public void initializationScreenContext(final String menuProperty) {
284                 
285                 initializationContext();
286                 getMenuBarSettings().intializeMenuBar(menuProperty);
287         }
288         
289         /**
290          * Initialization of the screen context for menu bar and title bar.
291          * 
292          * @param menuProperty - the property of the menu bar.
293          * @param titleProperty - The title of the open study/knowledge.
294      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
295          */
296         public void initializationScreenContext(final String menuProperty, 
297                         final String titleProperty, final String editDisabledProperty) {
298                 
299                 initializationContext(titleProperty, editDisabledProperty);
300                 getMenuBarSettings().intializeMenuBar(menuProperty);
301         }
302         
303         /**
304          * Initialization of the screen context for menu bar, title bar and tool bar.
305          * 
306          * @param menuProperty - the property of the menu bar.
307          * @param titleProperty - The title of the open study/knowledge.
308      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
309      * @param toolProperty - the property of the tool bar.
310          */
311         public void initializationScreenContext(final String menuProperty, 
312                         final String titleProperty, final String editDisabledProperty,
313                         final String toolProperty) {
314                 
315                 initializationScreenContext(menuProperty, titleProperty, editDisabledProperty);
316                 getToolBarSettings().intializeMenuBar(toolProperty);
317         }
318         
319         /**
320          * Initialization of the screen context for menu bar and tool bar.
321          * 
322          * @param menuProperty - the property of the menu bar.
323      * @param toolProperty - the property of the tool bar.
324          */
325         public void initializationScreenContext(final String menuProperty, 
326                         final String toolProperty) {
327                 
328                 initializationContext();
329                 getMenuBarSettings().intializeMenuBar(menuProperty);
330                 getToolBarSettings().intializeMenuBar(toolProperty);
331         }
332         
333         /**
334          * Initialization of the screen context for menu bar, title bar and tool bar.
335          * 
336          * @param menuProperty - the property of the menu bar.
337          * @param titleProperty - The title of the open study/knowledge.
338      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
339      * @param toolProperty - the property of the tool bar.
340      * @param leftMenuProperty - the property of the left menu.
341          */
342         public void initializationFullScreenContext(final String menuProperty, 
343                         final String titleProperty, final String editDisabledProperty,
344                         final String toolProperty, final String leftMenuProperty) {
345                 
346                 initializationScreenContext(menuProperty, titleProperty, editDisabledProperty);
347                 initializationContextLeftMenus(leftMenuProperty);
348                 getToolBarSettings().intializeMenuBar(toolProperty);
349         }
350         
351         /**
352          * Initialization of the screen context for menu bar and tool bar.
353          * 
354          * @param menuProperty - the property of the menu bar.
355      * @param toolProperty - the property of the tool bar.
356      * @param leftMenuProperty - the property of the left menu.
357          */
358         public void initializationFullScreenContext(final String menuProperty, 
359                         final String toolProperty, final String leftMenuProperty) {
360                 
361                 initializationContext();
362                 initializationContextLeftMenus(leftMenuProperty);
363                 getMenuBarSettings().intializeMenuBar(menuProperty);
364                 getToolBarSettings().intializeMenuBar(toolProperty);
365         }
366 //  ==============================================================================================================================
367 //  Getters and setters
368 //  ==============================================================================================================================
369     
370         public void setServletRequest (HttpServletRequest request) {
371                 this.request = request;
372         }
373         
374         public HttpServletRequest getServletRequest() {
375                 return request;
376         }
377
378     public String getErrorCode () {
379       return mercode;
380     }
381     public Map<String, Object> getSession () {
382       return session;
383     }
384
385     public void setErrorCode (String code) {
386       this.mercode = code;
387     }
388         public void setSession (Map<String, Object> session) {
389           this.session = session;
390         }
391         
392         /**
393          * Get the menuBarSettings.
394          * @return the menuBarSettings
395          */
396         public MenuBarSettings getMenuBarSettings() {
397                 return _menuBarSettings;
398         }
399
400         /**
401          * Set the menuBarSettings.
402          * @param menuBarSettings the menuBarSettings to set
403          */
404         public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
405                 _menuBarSettings = menuBarSettings;
406         }
407         
408         /**
409          * Get the _titleBarSettings.
410          * @return the _titleBarSettings
411          */
412         public TitleBarSettings getTitleBarSettings() {
413                 return _titleBarSettings;
414         }
415         /**
416          * Set the titleBarSettings.
417          * @param titleBarSettings the titleBarSettings to set
418          */
419         public void setTitleBarSettings(TitleBarSettings titleBarSettings) {
420                 _titleBarSettings = titleBarSettings;
421         }
422         /**
423          * Get the toolBarSettings.
424          * @return the toolBarSettings
425          */
426         public final ToolBarSettings getToolBarSettings() {
427                 return _toolBarSettings;
428         }
429         /**
430          * Set the toolBarSettings.
431          * @param toolBarSettings the toolBarSettings to set
432          */
433         public final void setToolBarSettings(ToolBarSettings toolBarSettings) {
434                 _toolBarSettings = toolBarSettings;
435         }
436         
437         /**
438          * Get the applicationSettings.
439          * @return the applicationSettings
440          */
441         public ApplicationSettings getApplicationSettings() {
442                 return _applicationSettings;
443         }
444         /**
445          * Set the applicationSettings.
446          * @param applicationSettings the applicationSettings to set
447          */
448         public void setApplicationSettings(ApplicationSettings applicationSettings) {
449                 _applicationSettings = applicationSettings;
450         }
451         /**
452          * Get the leftMenuSettings.
453          * @return the leftMenuSettings
454          */
455         public LeftMenuSettings getLeftMenuSettings() {
456                 return _leftMenuSettings;
457         }
458                 
459         /**
460          * Get the staticMenu.
461          * @return the staticMenu
462          */
463         public static Menu getStaticMenu() {
464                 return staticMenu;
465         }
466         /**
467          * Set the staticMenu.
468          * @param staticMenu the staticMenu to set
469          */
470         public static void setStaticMenu(Menu staticMenu) {
471                 Action.staticMenu = staticMenu;
472         }
473         /**
474          * Set the leftMenuSettings.
475          * @param leftMenuSettings the leftMenuSettings to set
476          */
477         public void setLeftMenuSettings(LeftMenuSettings leftMenuSettings) {
478                 _leftMenuSettings = leftMenuSettings;
479         }
480         
481         
482 }