Salome HOME
aef69cf121b3a2ba7897de8da2352d34753c60bc
[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          * Injected application settings bean.
78          */
79         private ApplicationSettings _applicationSettings;
80
81     public class DocumentTypeComparator implements Comparator<DocumentType> {
82       public int compare(DocumentType t1, DocumentType t2)
83       {
84         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
85         String               name1  = t1.getName();
86         if (t1.isApproved()) name1  = locale.getString("type.document." + name1);
87         String               name2  = t2.getName();
88         if (t2.isApproved()) name2  = locale.getString("type.document." + name2);
89
90         return name1.compareToIgnoreCase(name2);
91       }
92     }
93     public class ContextTypeComparator  implements Comparator<SimulationContextType> {
94       public int compare(SimulationContextType t1, SimulationContextType t2)
95       {
96         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
97         String               name1  = t1.getName();
98         if (t1.isApproved()) name1  = locale.getString("type.context." + name1);
99         String               name2  = t2.getName();
100         if (t2.isApproved()) name2  = locale.getString("type.context." + name2);
101
102         return name1.compareToIgnoreCase(name2);
103       }
104     }
105
106 //  ==============================================================================================================================
107 //  Session services
108 //  ==============================================================================================================================
109
110     protected void closeKnowledge () {
111       OpenObject open = (OpenObject)session.remove("knowledge.open");
112       if (open != null) {
113         if (session.get("study.open") == null) open.clearFacades();      // For eventually reopening the knowledge from a fresh context
114       }
115     }
116     protected void closeStudy () {
117       OpenObject open = (OpenObject)session.remove("study.open");
118       if (open != null) {
119         if (session.get("knowledge.open") == null) open.clearFacades();  // For eventually reopening the study from a fresh context
120       }
121     }
122     protected void connect (LoginContext context, User user) {
123       OpenStudy  open = getOpenStudy();
124       if (open != null) {
125           open.changeUser(user);
126       }
127       session.put("user.rights", new ApplicationRights(user) );
128       session.put("login.context", context);                             // For executing the deconnection, when requested
129     }
130     protected void disconnect () {
131       OpenStudy  open = getOpenStudy();
132       if (open != null) {
133           open.changeUser(null);
134       }
135       session.put("user.rights", new ApplicationRights(null) );          // Disables user rights
136       session.remove("login.context");
137     }
138     public User getConnectedUser () {
139       ApplicationRights  rights = (ApplicationRights)session.get("user.rights");
140       User connected = null;
141       if (rights != null) {
142           connected = rights.getUser();
143       }
144       return connected;                                           // May be null
145     }
146     protected Menu getMenu (String name) {
147       return (Menu)session.get("menu." + name);
148     }
149     public void setOpenKnowledge (OpenKnowledge kelm) {
150         _openKnowledge = kelm;
151       }
152     protected OpenKnowledge getOpenKnowledge () {
153 //      _openKnowledge = (OpenKnowledge)session.get("knowledge.open");               // May be null
154         return _openKnowledge;
155     }
156     public void setOpenStudy (OpenStudy aStudy) {
157       _openStudy = aStudy;
158     }
159     public OpenStudy getOpenStudy () {
160 //      _openStudy = (OpenStudy)session.get("study.open");
161           return _openStudy;                       // May be null
162         }
163     protected OpenKnowledge open (KnowledgeElement kelm) {
164       OpenKnowledge open = _openKnowledge.open(kelm);
165
166       closeKnowledge();   // Just in case
167           session.put("knowledge.open", open);
168           return open;
169     }
170     protected OpenStudy open (Study study) {
171           OpenStudy open = _openStudy.open(getConnectedUser(), study);         // The connected user may be null
172
173       closeStudy();       // Just in case
174           session.put("study.open", open);
175           return open;
176     }
177     
178     /**
179      * Initialization the Context for menubar.
180      */
181     public void initializationContext() {
182         getMenuBarSettings().initializeInitialMenuProperties();
183         
184         if (session.get("study.open") == null) {
185                 getMenuBarSettings().setIsStudyNull(true);
186         } else {
187                 getMenuBarSettings().setIsStudyNull(false);
188                 
189                 //for initialization ToolBarSettings.canUserEdit property
190                 // and ToolBarSettings.isEnabledScript property.
191                 
192                 OpenStudy currentStudy = (OpenStudy)session.get("study.open");
193                 PopupMenu    popup = currentStudy.getPopup();
194             StudyRights  user  = currentStudy.getStudyRights();
195             
196             if(user.canEditProperties()) {
197                 getToolBarSettings().setCanUserEdit(true);
198             } else {
199                 getToolBarSettings().setCanUserEdit(false);
200             }
201             
202             if (popup == null) {
203                 getToolBarSettings().setIsEnabledScript(false);
204             } else if (popup.isEnabled("script")) {
205                 getToolBarSettings().setIsEnabledScript(true);
206             } else {
207                 getToolBarSettings().setIsEnabledScript(false);
208             }
209         }
210         
211         if (session.get("knowledge.open") == null) {
212                 getMenuBarSettings().setIsKnowledgeNull(true);
213         } else {
214                 getMenuBarSettings().setIsKnowledgeNull(false);
215         }
216         
217         ApplicationRights userRights  = (ApplicationRights)session.get("user.rights");
218         
219         if ((userRights != null) && userRights.canCreateStudy()) {
220                 getMenuBarSettings().setCanUserCreateStudy(true);
221         } else {
222                 getMenuBarSettings().setCanUserCreateStudy(false);
223         }
224         
225         if ((userRights != null) && userRights.canManageDatabase()) {
226                 getMenuBarSettings().setCanUserManageDatabase(true);
227         } else {
228                 getMenuBarSettings().setCanUserManageDatabase(false);
229         } 
230         
231     }
232     
233     /**
234      * Initialization the Context for Menu Bar and Tool Bar.
235      * 
236      * @param titleProperty - The title of the open study/knowledge.
237      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
238      */
239     public void initializationContext(final String titleProperty, final String editDisabledProperty) {
240         
241         initializationContext();
242         
243         OpenObject entity = (OpenObject)session.get(titleProperty + ".open");
244         
245         getTitleBarSettings().setProgressState(entity.getProgressState().toString());
246         getTitleBarSettings().setSelectionState(entity.getSelection());
247         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
248         getTitleBarSettings().setEntryTypeTitle(entity.getType());
249         getTitleBarSettings().setEntryTitle(entity.getTitle());
250         getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
251     }
252     
253     /**
254          * Initialization of the screen context for menu bar.
255          * 
256          * @param menuProperty - the property of the menu bar.
257          */
258         public void initializationScreenContext(final String menuProperty) {
259                 
260                 initializationContext();
261                 getMenuBarSettings().intializeMenuBar(menuProperty);
262         }
263         
264         /**
265          * Initialization of the screen context for menu bar and title bar.
266          * 
267          * @param menuProperty - the property of the menu bar.
268          * @param titleProperty - The title of the open study/knowledge.
269      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
270          */
271         public void initializationScreenContext(final String menuProperty, 
272                         final String titleProperty, final String editDisabledProperty) {
273                 
274                 initializationContext(titleProperty, editDisabledProperty);
275                 getMenuBarSettings().intializeMenuBar(menuProperty);
276         }
277         
278         /**
279          * Initialization of the screen context for menu bar, title bar and tool bar.
280          * 
281          * @param menuProperty - the property of the menu bar.
282          * @param titleProperty - The title of the open study/knowledge.
283      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
284      * @param toolProperty - the property of the tool bar.
285          */
286         public void initializationScreenContext(final String menuProperty, 
287                         final String titleProperty, final String editDisabledProperty,
288                         final String toolProperty) {
289                 
290                 initializationScreenContext(menuProperty, titleProperty, editDisabledProperty);
291                 getToolBarSettings().intializeMenuBar(toolProperty);
292         }
293         
294         /**
295          * Initialization of the screen context for menu bar and tool bar.
296          * 
297          * @param menuProperty - the property of the menu bar.
298      * @param toolProperty - the property of the tool bar.
299          */
300         public void initializationScreenContext(final String menuProperty, 
301                         final String toolProperty) {
302                 
303                 initializationContext();
304                 getMenuBarSettings().intializeMenuBar(menuProperty);
305                 getToolBarSettings().intializeMenuBar(toolProperty);
306         }
307 //  ==============================================================================================================================
308 //  Getters and setters
309 //  ==============================================================================================================================
310     
311         public void setServletRequest (HttpServletRequest request) {
312                 this.request = request;
313         }
314         
315         public HttpServletRequest getServletRequest() {
316                 return request;
317         }
318
319     public String getErrorCode () {
320       return mercode;
321     }
322     public Map<String, Object> getSession () {
323       return session;
324     }
325
326     public void setErrorCode (String code) {
327       this.mercode = code;
328     }
329         public void setSession (Map<String, Object> session) {
330           this.session = session;
331         }
332         
333         /**
334          * Get the menuBarSettings.
335          * @return the menuBarSettings
336          */
337         public MenuBarSettings getMenuBarSettings() {
338                 return _menuBarSettings;
339         }
340
341         /**
342          * Set the menuBarSettings.
343          * @param menuBarSettings the menuBarSettings to set
344          */
345         public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
346                 _menuBarSettings = menuBarSettings;
347         }
348         
349         /**
350          * Get the _titleBarSettings.
351          * @return the _titleBarSettings
352          */
353         public TitleBarSettings getTitleBarSettings() {
354                 return _titleBarSettings;
355         }
356         /**
357          * Set the titleBarSettings.
358          * @param titleBarSettings the titleBarSettings to set
359          */
360         public void setTitleBarSettings(TitleBarSettings titleBarSettings) {
361                 _titleBarSettings = titleBarSettings;
362         }
363         /**
364          * Get the toolBarSettings.
365          * @return the toolBarSettings
366          */
367         public final ToolBarSettings getToolBarSettings() {
368                 return _toolBarSettings;
369         }
370         /**
371          * Set the toolBarSettings.
372          * @param toolBarSettings the toolBarSettings to set
373          */
374         public final void setToolBarSettings(ToolBarSettings toolBarSettings) {
375                 _toolBarSettings = toolBarSettings;
376         }
377         
378         /**
379          * Get the applicationSettings.
380          * @return the applicationSettings
381          */
382         public ApplicationSettings getApplicationSettings() {
383                 return _applicationSettings;
384         }
385         /**
386          * Set the applicationSettings.
387          * @param applicationSettings the applicationSettings to set
388          */
389         public void setApplicationSettings(ApplicationSettings applicationSettings) {
390                 _applicationSettings = applicationSettings;
391         }
392 }