Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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         private OpenStudy _openStudy;
27         private OpenKnowledge _openKnowledge;
28
29         /**
30          * Serial version ID.
31          */
32         private   static final long    serialVersionUID = -895295026709526501L;
33         /**
34          * Action logger.
35          */
36     protected static final Logger  logger           = Logger.getLogger(Action.class);
37
38     public class DocumentTypeComparator implements Comparator<DocumentType> {
39 //  -----------------------------------------------------------------------
40       public int compare(DocumentType t1, DocumentType t2)
41       {
42         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
43         String               name1  = t1.getName();
44         if (t1.isApproved()) name1  = locale.getString("type.document." + name1);
45         String               name2  = t2.getName();
46         if (t2.isApproved()) name2  = locale.getString("type.document." + name2);
47
48         return name1.compareToIgnoreCase(name2);
49       }
50     }
51     public class ContextTypeComparator  implements Comparator<SimulationContextType> {
52 //  --------------------------------------------------------------------------------
53       public int compare(SimulationContextType t1, SimulationContextType t2)
54       {
55         ResourceBundle       locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
56         String               name1  = t1.getName();
57         if (t1.isApproved()) name1  = locale.getString("type.context." + name1);
58         String               name2  = t2.getName();
59         if (t2.isApproved()) name2  = locale.getString("type.context." + name2);
60
61         return name1.compareToIgnoreCase(name2);
62       }
63     }
64
65 //  ==============================================================================================================================
66 //  Session services
67 //  ==============================================================================================================================
68
69     protected void closeKnowledge () {
70 //  --------------------------------
71       OpenObject open = (OpenObject)session.remove("knowledge.open");
72       if (open != null) {
73         if (session.get("study.open") == null) open.clearFacades();      // For eventually reopening the knowledge from a fresh context
74       }
75     }
76     protected void closeStudy () {
77 //  ----------------------------
78       OpenObject open = (OpenObject)session.remove("study.open");
79       if (open != null) {
80         if (session.get("knowledge.open") == null) open.clearFacades();  // For eventually reopening the study from a fresh context
81       }
82     }
83     protected void connect (LoginContext context, User user) {
84 //  --------------------------------------------------------
85       OpenStudy  open = getOpenStudy();
86       if (open != null) {
87           open.changeUser(user);
88       }
89       session.put("user.rights", new ApplicationRights(user) );
90       session.put("login.context", context);                             // For executing the deconnection, when requested
91     }
92     protected void disconnect () {
93 //  ----------------------------
94       OpenStudy  open = getOpenStudy();
95       if (open != null) {
96           open.changeUser(null);
97       }
98       session.put("user.rights", new ApplicationRights(null) );          // Disables user rights
99       session.remove("login.context");
100     }
101     protected User getConnectedUser () {
102 //  ----------------------------------
103       ApplicationRights  rights = (ApplicationRights)session.get("user.rights");
104       return rights.getUser();                                           // May be null
105     }
106     protected Menu getMenu (String name) {
107 //  ------------------------------------
108       return (Menu)session.get("menu." + name);
109     }
110     public void setOpenKnowledge (OpenKnowledge kelm) {
111         _openKnowledge = kelm;
112       }
113     protected OpenKnowledge getOpenKnowledge () {
114 //      _openKnowledge = (OpenKnowledge)session.get("knowledge.open");               // May be null
115         return _openKnowledge;
116     }
117     public void setOpenStudy (OpenStudy aStudy) {
118       _openStudy = aStudy;
119     }
120     public OpenStudy getOpenStudy () {
121 //      _openStudy = (OpenStudy)session.get("study.open");
122           return _openStudy;                       // May be null
123         }
124     protected OpenKnowledge open (KnowledgeElement kelm) {
125 //  ----------------------------------------------------
126       OpenKnowledge open = _openKnowledge.open(kelm);
127
128       closeKnowledge();   // Just in case
129           session.put("knowledge.open", open);
130           return open;
131     }
132     protected OpenStudy open (Study study) {
133 //  --------------------------------------
134           OpenStudy open = _openStudy.open(getConnectedUser(), study);         // The connected user may be null
135
136       closeStudy();       // Just in case
137           session.put("study.open", open);
138           return open;
139     }
140 //  ==============================================================================================================================
141 //  Getters and setters
142 //  ==============================================================================================================================
143
144     public String getErrorCode () {
145 //  -----------------------------
146       return mercode;
147     }
148     public Map<String, Object> getSession () {
149 //  ----------------------------------------
150       return session;
151     }
152
153     public void setErrorCode (String code) {
154 //  --------------------------------------
155       this.mercode = code;
156     }
157         public void setSession (Map<String, Object> session) {
158 //  ----------------------------------------------------                
159           this.session = session;
160         }
161 }