1 package org.splat.simer;
4 import java.util.Comparator;
5 import java.util.ResourceBundle;
7 import javax.security.auth.login.LoginContext;
8 import javax.servlet.http.HttpServletRequest;
10 import com.opensymphony.xwork2.ActionSupport;
12 import org.apache.struts2.ServletActionContext;
13 import org.apache.struts2.interceptor.ServletRequestAware;
14 import org.apache.struts2.interceptor.SessionAware;
15 import org.apache.log4j.Logger;
16 import org.splat.dal.bo.kernel.User;
17 import org.splat.som.ApplicationRights;
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;
25 public class Action extends ActionSupport implements ServletRequestAware, SessionAware {
27 private Map<String, Object> session;
28 private String mercode;
29 private OpenStudy _openStudy;
30 private OpenKnowledge _openKnowledge;
32 private MenuBarSettings _menuBarSettings;
34 private HttpServletRequest request;
39 private static final long serialVersionUID = -895295026709526501L;
43 protected static final Logger logger = Logger.getLogger(Action.class);
45 public class DocumentTypeComparator implements Comparator<DocumentType> {
46 // -----------------------------------------------------------------------
47 public int compare(DocumentType t1, DocumentType t2)
49 ResourceBundle locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
50 String name1 = t1.getName();
51 if (t1.isApproved()) name1 = locale.getString("type.document." + name1);
52 String name2 = t2.getName();
53 if (t2.isApproved()) name2 = locale.getString("type.document." + name2);
55 return name1.compareToIgnoreCase(name2);
58 public class ContextTypeComparator implements Comparator<SimulationContextType> {
59 // --------------------------------------------------------------------------------
60 public int compare(SimulationContextType t1, SimulationContextType t2)
62 ResourceBundle locale = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
63 String name1 = t1.getName();
64 if (t1.isApproved()) name1 = locale.getString("type.context." + name1);
65 String name2 = t2.getName();
66 if (t2.isApproved()) name2 = locale.getString("type.context." + name2);
68 return name1.compareToIgnoreCase(name2);
72 // ==============================================================================================================================
74 // ==============================================================================================================================
76 protected void closeKnowledge () {
77 // --------------------------------
78 OpenObject open = (OpenObject)session.remove("knowledge.open");
80 if (session.get("study.open") == null) open.clearFacades(); // For eventually reopening the knowledge from a fresh context
83 protected void closeStudy () {
84 // ----------------------------
85 OpenObject open = (OpenObject)session.remove("study.open");
87 if (session.get("knowledge.open") == null) open.clearFacades(); // For eventually reopening the study from a fresh context
90 protected void connect (LoginContext context, User user) {
91 // --------------------------------------------------------
92 OpenStudy open = getOpenStudy();
94 open.changeUser(user);
96 session.put("user.rights", new ApplicationRights(user) );
97 session.put("login.context", context); // For executing the deconnection, when requested
99 protected void disconnect () {
100 // ----------------------------
101 OpenStudy open = getOpenStudy();
103 open.changeUser(null);
105 session.put("user.rights", new ApplicationRights(null) ); // Disables user rights
106 session.remove("login.context");
108 protected User getConnectedUser () {
109 // ----------------------------------
110 ApplicationRights rights = (ApplicationRights)session.get("user.rights");
111 return rights.getUser(); // May be null
113 protected Menu getMenu (String name) {
114 // ------------------------------------
115 return (Menu)session.get("menu." + name);
117 public void setOpenKnowledge (OpenKnowledge kelm) {
118 _openKnowledge = kelm;
120 protected OpenKnowledge getOpenKnowledge () {
121 // _openKnowledge = (OpenKnowledge)session.get("knowledge.open"); // May be null
122 return _openKnowledge;
124 public void setOpenStudy (OpenStudy aStudy) {
127 public OpenStudy getOpenStudy () {
128 // _openStudy = (OpenStudy)session.get("study.open");
129 return _openStudy; // May be null
131 protected OpenKnowledge open (KnowledgeElement kelm) {
132 // ----------------------------------------------------
133 OpenKnowledge open = _openKnowledge.open(kelm);
135 closeKnowledge(); // Just in case
136 session.put("knowledge.open", open);
139 protected OpenStudy open (Study study) {
140 // --------------------------------------
141 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
143 closeStudy(); // Just in case
144 session.put("study.open", open);
149 * Initialization the Context for menubar.
151 public void initializationContext() {
152 getMenuBarSettings().initializeInitialMenuProperties();
154 if (session.get("study.open") == null) {
155 getMenuBarSettings().setIsStudyNull(true);
156 logger.debug("MKA: setIsStudyNull = true");
158 getMenuBarSettings().setIsStudyNull(false);
159 logger.debug("MKA: setIsStudyNull = false");
162 if (session.get("knowledge.open") == null) {
163 getMenuBarSettings().setIsKnowledgeNull(true);
164 logger.debug("MKA: setIsKnowledgeNull = true");
166 getMenuBarSettings().setIsKnowledgeNull(false);
167 logger.debug("MKA: setIsKnowledgeNull = false");
170 ApplicationRights userRights = (ApplicationRights)session.get("user.rights");
172 if (userRights.canCreateStudy()) {
173 getMenuBarSettings().setCanUserCreateStudy(true);
175 getMenuBarSettings().setCanUserCreateStudy(false);
178 if (userRights.canManageDatabase()) {
179 getMenuBarSettings().setCanUserManageDatabase(true);
181 getMenuBarSettings().setCanUserManageDatabase(false);
187 * Initialization of the screen context for menu bar.
189 public void initializationScreenContext(final String menuProperty) {
191 initializationContext();
192 getMenuBarSettings().intializeMenuBar(menuProperty);
194 // ==============================================================================================================================
195 // Getters and setters
196 // ==============================================================================================================================
198 public void setServletRequest (HttpServletRequest request) {
199 this.request = request;
202 public HttpServletRequest getServletRequest() {
206 public String getErrorCode () {
207 // -----------------------------
210 public Map<String, Object> getSession () {
211 // ----------------------------------------
215 public void setErrorCode (String code) {
216 // --------------------------------------
219 public void setSession (Map<String, Object> session) {
220 // ----------------------------------------------------
221 this.session = session;
225 * Get the menuBarSettings.
226 * @return the menuBarSettings
228 public MenuBarSettings getMenuBarSettings() {
229 return _menuBarSettings;
233 * Set the menuBarSettings.
234 * @param menuBarSettings the menuBarSettings to set
236 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
237 _menuBarSettings = menuBarSettings;