Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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
9 import com.opensymphony.xwork2.ActionSupport;
10
11 import org.apache.struts2.interceptor.SessionAware;
12 import org.apache.log4j.Logger;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.som.ApplicationRights;
15 import org.splat.dal.bo.som.KnowledgeElement;
16 import org.splat.dal.bo.som.SimulationContextType;
17 import org.splat.dal.bo.som.Study;
18 import org.splat.dal.bo.som.DocumentType;
19 import org.splat.wapp.Menu;
20
21
22 public class Action extends ActionSupport implements SessionAware {
23
24         private   Map<String, Object>  session;
25         private   String               mercode;
26
27         private   static final long    serialVersionUID = -895295026709526501L;
28     protected static final Logger  logger           = Logger.getLogger(Action.class);
29
30     public class DocumentTypeComparator implements Comparator<DocumentType> {
31 //  -----------------------------------------------------------------------
32       public int compare(DocumentType t1, DocumentType t2)
33       {
34         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
35         String               name1  = t1.getName();
36         if (t1.isApproved()) name1  = locale.getString("type.document." + name1);
37         String               name2  = t2.getName();
38         if (t2.isApproved()) name2  = locale.getString("type.document." + name2);
39
40         return name1.compareToIgnoreCase(name2);
41       }
42     }
43     public class ContextTypeComparator  implements Comparator<SimulationContextType> {
44 //  --------------------------------------------------------------------------------
45       public int compare(SimulationContextType t1, SimulationContextType t2)
46       {
47         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
48         String               name1  = t1.getName();
49         if (t1.isApproved()) name1  = locale.getString("type.context." + name1);
50         String               name2  = t2.getName();
51         if (t2.isApproved()) name2  = locale.getString("type.context." + name2);
52
53         return name1.compareToIgnoreCase(name2);
54       }
55     }
56
57 //  ==============================================================================================================================
58 //  Session services
59 //  ==============================================================================================================================
60
61     protected void closeKnowledge () {
62 //  --------------------------------
63       OpenObject open = (OpenObject)session.remove("knowledge.open");
64       if (open != null) {
65         if (session.get("study.open") == null) open.clearFacades();      // For eventually reopening the knowledge from a fresh context
66       }
67     }
68     protected void closeStudy () {
69 //  ----------------------------
70       OpenObject open = (OpenObject)session.remove("study.open");
71       if (open != null) {
72         if (session.get("knowledge.open") == null) open.clearFacades();  // For eventually reopening the study from a fresh context
73       }
74     }
75     protected void connect (LoginContext context, User user) {
76 //  --------------------------------------------------------
77       OpenStudy  open = getOpenStudy();
78       if (open != null) {
79           open.changeUser(user);
80       }
81       session.put("user.rights", new ApplicationRights(user) );
82       session.put("login.context", context);                             // For executing the deconnection, when requested
83     }
84     protected void disconnect () {
85 //  ----------------------------
86       OpenStudy  open = getOpenStudy();
87       if (open != null) {
88           open.changeUser(null);
89       }
90       session.put("user.rights", new ApplicationRights(null) );          // Disables user rights
91       session.remove("login.context");
92     }
93     protected User getConnectedUser () {
94 //  ----------------------------------
95       ApplicationRights  rights = (ApplicationRights)session.get("user.rights");
96       return rights.getUser();                                           // May be null
97     }
98     protected Menu getMenu (String name) {
99 //  ------------------------------------
100       return (Menu)session.get("menu." + name);
101     }
102     protected OpenKnowledge getOpenKnowledge () {
103 //  -------------------------------------------
104       return (OpenKnowledge)session.get("knowledge.open");               // May be null
105     }
106     protected OpenStudy getOpenStudy () {
107 //  -----------------------------------
108       return (OpenStudy)session.get("study.open");                       // May be null
109     }
110     protected OpenKnowledge open (KnowledgeElement kelm) {
111 //  ----------------------------------------------------
112       OpenKnowledge open = new OpenKnowledge(kelm);
113
114       closeKnowledge();   // Just in case
115           session.put("knowledge.open", open);
116           return open;
117     }
118     protected OpenStudy open (Study study) {
119 //  --------------------------------------
120           OpenStudy open = new OpenStudy(getConnectedUser(), study);         // The connected user may be null
121
122       closeStudy();       // Just in case
123           session.put("study.open", open);
124           return open;
125     }
126 //  ==============================================================================================================================
127 //  Getters and setters
128 //  ==============================================================================================================================
129
130     public String getErrorCode () {
131 //  -----------------------------
132       return mercode;
133     }
134     public Map<String, Object> getSession () {
135 //  ----------------------------------------
136       return session;
137     }
138
139     public void setErrorCode (String code) {
140 //  --------------------------------------
141       this.mercode = code;
142     }
143         public void setSession (Map<String, Object> session) {
144 //  ----------------------------------------------------                
145           this.session = session;
146         }
147 }