]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/Action.java
Salome HOME
Tool bar is improved.
[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     protected User getConnectedUser () {
139       ApplicationRights  rights = (ApplicationRights)session.get("user.rights");
140       return rights.getUser();                                           // May be null
141     }
142     protected Menu getMenu (String name) {
143       return (Menu)session.get("menu." + name);
144     }
145     public void setOpenKnowledge (OpenKnowledge kelm) {
146         _openKnowledge = kelm;
147       }
148     protected OpenKnowledge getOpenKnowledge () {
149 //      _openKnowledge = (OpenKnowledge)session.get("knowledge.open");               // May be null
150         return _openKnowledge;
151     }
152     public void setOpenStudy (OpenStudy aStudy) {
153       _openStudy = aStudy;
154     }
155     public OpenStudy getOpenStudy () {
156 //      _openStudy = (OpenStudy)session.get("study.open");
157           return _openStudy;                       // May be null
158         }
159     protected OpenKnowledge open (KnowledgeElement kelm) {
160       OpenKnowledge open = _openKnowledge.open(kelm);
161
162       closeKnowledge();   // Just in case
163           session.put("knowledge.open", open);
164           return open;
165     }
166     protected OpenStudy open (Study study) {
167           OpenStudy open = _openStudy.open(getConnectedUser(), study);         // The connected user may be null
168
169       closeStudy();       // Just in case
170           session.put("study.open", open);
171           return open;
172     }
173     
174     /**
175      * Initialization the Context for menubar.
176      */
177     public void initializationContext() {
178         getMenuBarSettings().initializeInitialMenuProperties();
179         
180         if (session.get("study.open") == null) {
181                 getMenuBarSettings().setIsStudyNull(true);
182         } else {
183                 getMenuBarSettings().setIsStudyNull(false);
184                 
185                 //for initialization ToolBarSettings.canUserEdit property
186                 // and ToolBarSettings.isEnabledScript property.
187                 
188                 OpenStudy currentStudy = (OpenStudy)session.get("study.open");
189                 PopupMenu    popup = currentStudy.getPopup();
190             StudyRights  user  = currentStudy.getStudyRights();
191             
192             if(user.canEditProperties()) {
193                 getToolBarSettings().setCanUserEdit(true);
194             } else {
195                 getToolBarSettings().setCanUserEdit(false);
196             }
197             
198             if (popup == null) {
199                 getToolBarSettings().setIsEnabledScript(false);
200             } else if (popup.isEnabled("script")) {
201                 getToolBarSettings().setIsEnabledScript(true);
202             } else {
203                 getToolBarSettings().setIsEnabledScript(false);
204             }
205         }
206         
207         if (session.get("knowledge.open") == null) {
208                 getMenuBarSettings().setIsKnowledgeNull(true);
209         } else {
210                 getMenuBarSettings().setIsKnowledgeNull(false);
211         }
212         
213         ApplicationRights userRights  = (ApplicationRights)session.get("user.rights");
214         
215         if (userRights.canCreateStudy()) {
216                 getMenuBarSettings().setCanUserCreateStudy(true);
217         } else {
218                 getMenuBarSettings().setCanUserCreateStudy(false);
219         }
220         
221         if (userRights.canManageDatabase()) {
222                 getMenuBarSettings().setCanUserManageDatabase(true);
223         } else {
224                 getMenuBarSettings().setCanUserManageDatabase(false);
225         } 
226         
227     }
228     
229     /**
230      * Initialization the Context for Menu Bar and Tool Bar.
231      * 
232      * @param titleProperty - The title of the open study/knowledge.
233      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
234      */
235     public void initializationContext(final String titleProperty, final String editDisabledProperty) {
236         
237         initializationContext();
238         
239         OpenObject entity = (OpenObject)session.get(titleProperty + ".open");
240         
241         getTitleBarSettings().setProgressState(entity.getProgressState().toString());
242         getTitleBarSettings().setSelectionState(entity.getSelection());
243         getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
244         getTitleBarSettings().setEntryTypeTitle(entity.getType());
245         getTitleBarSettings().setEntryTitle(entity.getTitle());
246         getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
247     }
248     
249     /**
250          * Initialization of the screen context for menu bar.
251          * 
252          * @param menuProperty - the property of the menu bar.
253          */
254         public void initializationScreenContext(final String menuProperty) {
255                 
256                 initializationContext();
257                 getMenuBarSettings().intializeMenuBar(menuProperty);
258         }
259         
260         /**
261          * Initialization of the screen context for menu bar and title bar.
262          * 
263          * @param menuProperty - the property of the menu bar.
264          * @param titleProperty - The title of the open study/knowledge.
265      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
266          */
267         public void initializationScreenContext(final String menuProperty, 
268                         final String titleProperty, final String editDisabledProperty) {
269                 
270                 initializationContext(titleProperty, editDisabledProperty);
271                 getMenuBarSettings().intializeMenuBar(menuProperty);
272         }
273         
274         /**
275          * Initialization of the screen context for menu bar, title bar and tool bar.
276          * 
277          * @param menuProperty - the property of the menu bar.
278          * @param titleProperty - The title of the open study/knowledge.
279      * @param editDisabledProperty - Property that indicates whether the current open study is editable or not.
280      * @param toolProperty - the property of the tool bar.
281          */
282         public void initializationScreenContext(final String menuProperty, 
283                         final String titleProperty, final String editDisabledProperty,
284                         final String toolProperty) {
285                 
286                 initializationScreenContext(menuProperty, titleProperty, editDisabledProperty);
287                 getToolBarSettings().intializeMenuBar(toolProperty);
288         }
289         
290         /**
291          * Initialization of the screen context for menu bar and tool bar.
292          * 
293          * @param menuProperty - the property of the menu bar.
294      * @param toolProperty - the property of the tool bar.
295          */
296         public void initializationScreenContext(final String menuProperty, 
297                         final String toolProperty) {
298                 
299                 initializationContext();
300                 getMenuBarSettings().intializeMenuBar(menuProperty);
301                 getToolBarSettings().intializeMenuBar(toolProperty);
302         }
303 //  ==============================================================================================================================
304 //  Getters and setters
305 //  ==============================================================================================================================
306     
307         public void setServletRequest (HttpServletRequest request) {
308                 this.request = request;
309         }
310         
311         public HttpServletRequest getServletRequest() {
312                 return request;
313         }
314
315     public String getErrorCode () {
316       return mercode;
317     }
318     public Map<String, Object> getSession () {
319       return session;
320     }
321
322     public void setErrorCode (String code) {
323       this.mercode = code;
324     }
325         public void setSession (Map<String, Object> session) {
326           this.session = session;
327         }
328         
329         /**
330          * Get the menuBarSettings.
331          * @return the menuBarSettings
332          */
333         public MenuBarSettings getMenuBarSettings() {
334                 return _menuBarSettings;
335         }
336
337         /**
338          * Set the menuBarSettings.
339          * @param menuBarSettings the menuBarSettings to set
340          */
341         public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
342                 _menuBarSettings = menuBarSettings;
343         }
344         
345         /**
346          * Get the _titleBarSettings.
347          * @return the _titleBarSettings
348          */
349         public TitleBarSettings getTitleBarSettings() {
350                 return _titleBarSettings;
351         }
352         /**
353          * Set the titleBarSettings.
354          * @param titleBarSettings the titleBarSettings to set
355          */
356         public void setTitleBarSettings(TitleBarSettings titleBarSettings) {
357                 _titleBarSettings = titleBarSettings;
358         }
359         /**
360          * Get the toolBarSettings.
361          * @return the toolBarSettings
362          */
363         public final ToolBarSettings getToolBarSettings() {
364                 return _toolBarSettings;
365         }
366         /**
367          * Set the toolBarSettings.
368          * @param toolBarSettings the toolBarSettings to set
369          */
370         public final void setToolBarSettings(ToolBarSettings toolBarSettings) {
371                 _toolBarSettings = toolBarSettings;
372         }
373         
374         /**
375          * Get the applicationSettings.
376          * @return the applicationSettings
377          */
378         public ApplicationSettings getApplicationSettings() {
379                 return _applicationSettings;
380         }
381         /**
382          * Set the applicationSettings.
383          * @param applicationSettings the applicationSettings to set
384          */
385         public void setApplicationSettings(ApplicationSettings applicationSettings) {
386                 _applicationSettings = applicationSettings;
387         }
388 }