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