1 package org.splat.simer;
3 import java.util.Comparator;
5 import java.util.ResourceBundle;
7 import javax.security.auth.login.LoginContext;
8 import javax.servlet.http.HttpServletRequest;
10 import org.apache.log4j.Logger;
11 import org.apache.struts2.interceptor.ServletRequestAware;
12 import org.apache.struts2.interceptor.SessionAware;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.DocumentType;
15 import org.splat.dal.bo.som.SimulationContextType;
16 import org.splat.dal.bo.som.Study;
17 import org.splat.service.dto.KnowledgeElementDTO;
18 import org.splat.som.ApplicationRights;
19 import org.splat.som.StudyRights;
20 import org.splat.wapp.Menu;
21 import org.splat.wapp.PopupMenu;
23 import com.opensymphony.xwork2.ActionSupport;
28 public class Action extends ActionSupport implements ServletRequestAware,
34 private static final long serialVersionUID = -895295026709526501L;
38 protected static final Logger LOG = Logger.getLogger(Action.class);
41 * Open knowledge key in session.
43 public static final String KNOWLEDGE_OPEN = "knowledge.open";
46 * Open study key in session.
48 public static final String STUDY_OPEN = "study.open";
51 * User rights key in session.
53 public static final String USER_RIGHTS = "user.rights";
56 * Login context key in session.
58 public static final String LOGIN_CONTEXT = "login.context";
61 * Http servlet request.
63 private HttpServletRequest _servletRequest;
65 * Http session container.
67 private Map<String, Object> _session;
71 private String _errorCode;
73 * ActionType for specifying the type of the operaion.
75 private String _actionType;
77 * Current open study facade object.
79 private OpenStudy _openStudy;
81 * Current open knowledge facade object.
83 private OpenKnowledge _openKnowledge;
86 * MenuBarSettings bean.
88 private MenuBarSettings _menuBarSettings;
91 * TitleBarSettings bean.
93 private TitleBarSettings _titleBarSettings;
95 * ToolBarSettings bean.
97 private ToolBarSettings _toolBarSettings;
100 * LeftMenuSettings bean.
102 private LeftMenuSettings _leftMenuSettings;
105 * Injected application settings bean.
107 private ApplicationSettings _applicationSettings;
110 * Comparator for sorting document types with localized names.
112 public class DocumentTypeComparator implements Comparator<DocumentType> {
116 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
118 public int compare(final DocumentType t1, final DocumentType t2) {
119 ResourceBundle locale = ResourceBundle.getBundle("som",
120 ApplicationSettings.getCurrentLocale());
121 String name1 = t1.getName();
122 if (t1.isApproved()) {
123 name1 = locale.getString("type.document." + name1);
125 String name2 = t2.getName();
126 if (t2.isApproved()) {
127 name2 = locale.getString("type.document." + name2);
130 return name1.compareToIgnoreCase(name2);
135 * Comparator for sorting simulation context types with localized names.
137 public class ContextTypeComparator implements
138 Comparator<SimulationContextType> {
142 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
144 public int compare(final SimulationContextType t1,
145 final SimulationContextType t2) {
146 ResourceBundle locale = ResourceBundle.getBundle("som",
147 ApplicationSettings.getCurrentLocale());
148 String name1 = t1.getName();
149 if (t1.isApproved()) {
150 name1 = locale.getString("type.context." + name1);
152 String name2 = t2.getName();
153 if (t2.isApproved()) {
154 name2 = locale.getString("type.context." + name2);
157 return name1.compareToIgnoreCase(name2);
161 // ==============================================================================================================================
163 // ==============================================================================================================================
166 * Remove the currently open knowledge from the session.
168 protected void closeKnowledge() {
169 AbstractOpenObject open = (AbstractOpenObject) _session.remove(KNOWLEDGE_OPEN);
170 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
171 open.clearFacades(); // For eventually reopening the knowledge from a fresh context
176 * Remove the currently open study from the session.
178 protected void closeStudy() {
179 AbstractOpenObject open = (AbstractOpenObject) _session.remove(STUDY_OPEN);
180 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
181 open.clearFacades(); // For eventually reopening the study from a fresh context
186 * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
191 * the user to connect
193 protected void connect(final LoginContext context, final User user) {
194 OpenStudy open = getOpenStudy();
196 open.changeUser(user);
198 _session.put(USER_RIGHTS, new ApplicationRights(user));
199 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
203 * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
205 protected void disconnect() {
206 OpenStudy open = getOpenStudy();
208 open.changeUser(null);
210 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
211 _session.remove(LOGIN_CONTEXT);
215 * Get the currently connected user from HTTP session.
219 public User getConnectedUser() {
220 ApplicationRights rights = (ApplicationRights) _session
222 User connected = null;
223 if (rights != null) {
224 connected = rights.getUser();
226 return connected; // May be null
230 * Get a menu named as "menu." with the given suffix from HTTP session.
233 * the menu name suffix
236 protected Menu getMenu(final String name) {
237 return (Menu) _session.get("menu." + name);
241 * Open knowledge setter.
244 * the OpenKnowledge to set
246 public void setOpenKnowledge(final OpenKnowledge kelm) {
247 _openKnowledge = kelm;
251 * Open knowledge getter.
253 * @return the currently open knowledge wrapper. May be null
255 protected OpenKnowledge getOpenKnowledge() {
256 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
257 return _openKnowledge;
264 * the OpenStudy to set
266 public void setOpenStudy(final OpenStudy aStudy) {
273 * @return the currently open stydy wrapper. May be null.
275 public OpenStudy getOpenStudy() {
276 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
277 return _openStudy; // May be null
281 * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
284 * the knowledge element to open
285 * @return OpenKnowledge wrapper object
287 protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
288 OpenKnowledge open = _openKnowledge.open(kelm);
290 closeKnowledge(); // Just in case
291 _session.put(KNOWLEDGE_OPEN, open);
296 * Open the given study in the current HTTP session. Replace the previose one in the session if any.
300 * @return OpenStudy wrapper object
302 protected OpenStudy open(final Study study) {
303 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
305 closeStudy(); // Just in case
306 _session.put(STUDY_OPEN, open);
311 * Initialization the Context for menubar and toolbar.
313 public void initializationContext() {
314 getMenuBarSettings().initializeInitialMenuProperties();
316 if (_session.get(STUDY_OPEN) == null) {
317 getMenuBarSettings().setIsStudyNull(true);
319 getMenuBarSettings().setIsStudyNull(false);
321 // for initialization ToolBarSettings.canUserEdit property
322 // and ToolBarSettings.isEnabledScript property.
324 OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
325 PopupMenu popup = currentStudy.getPopup();
326 StudyRights user = currentStudy.getStudyRights();
328 if (user.canEditProperties()) {
329 getToolBarSettings().setCanUserEdit(true);
331 getToolBarSettings().setCanUserEdit(false);
335 getToolBarSettings().setIsEnabledScript(false);
336 } else if (popup.isEnabled("script")) {
337 getToolBarSettings().setIsEnabledScript(true);
339 getToolBarSettings().setIsEnabledScript(false);
343 if (_session.get(KNOWLEDGE_OPEN) == null) {
344 getMenuBarSettings().setIsKnowledgeNull(true);
346 getMenuBarSettings().setIsKnowledgeNull(false);
349 ApplicationRights userRights = (ApplicationRights) _session
352 if ((userRights != null) && userRights.canCreateStudy()) {
353 getMenuBarSettings().setCanUserCreateStudy(true);
355 getMenuBarSettings().setCanUserCreateStudy(false);
358 if ((userRights != null) && userRights.canManageDatabase()) {
359 getMenuBarSettings().setCanUserManageDatabase(true);
361 getMenuBarSettings().setCanUserManageDatabase(false);
367 * Initialization the Context for left menu.
369 * @param leftMenuProperty -
370 * the property of the left menu.
372 public void initializationContextLeftMenus(final String leftMenuProperty) {
374 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
376 getLeftMenuSettings().setMenu(menu);
377 getLeftMenuSettings().setMenuName(menu.getName());
378 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
382 * Initialization the Context for Menu Bar and Tool Bar.
384 * @param titleProperty -
385 * The title of the open study/knowledge.
386 * @param editDisabledProperty -
387 * Property that indicates whether the current open study is editable or not.
389 public void initializationContext(final String titleProperty,
390 final String editDisabledProperty) {
392 initializationContext();
394 AbstractOpenObject entity = (AbstractOpenObject) _session.get(titleProperty + ".open");
396 getTitleBarSettings().setProgressState(
397 entity.getProgressState().toString());
398 getTitleBarSettings().setSelectionState(entity.getSelection());
399 getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
400 getTitleBarSettings().setEntryTypeTitle(entity.getType());
401 getTitleBarSettings().setEntryTitle(entity.getTitle());
402 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
406 * Initialization of the screen context for menu bar.
408 * @param menuProperty -
409 * the property of the menu bar.
411 public void initializationScreenContext(final String menuProperty) {
413 initializationContext();
414 getMenuBarSettings().intializeMenuBar(menuProperty);
418 * Initialization of the screen context for menu bar and title bar.
420 * @param menuProperty -
421 * the property of the menu bar.
422 * @param titleProperty -
423 * The title of the open study/knowledge.
424 * @param editDisabledProperty -
425 * Property that indicates whether the current open study is editable or not.
427 public void initializationScreenContext(final String menuProperty,
428 final String titleProperty, final String editDisabledProperty) {
430 initializationContext(titleProperty, editDisabledProperty);
431 getMenuBarSettings().intializeMenuBar(menuProperty);
435 * Initialization of the screen context for menu bar, title bar and tool bar.
437 * @param menuProperty -
438 * the property of the menu bar.
439 * @param titleProperty -
440 * The title of the open study/knowledge.
441 * @param editDisabledProperty -
442 * Property that indicates whether the current open study is editable or not.
443 * @param toolProperty -
444 * the property of the tool bar.
446 public void initializationScreenContext(final String menuProperty,
447 final String titleProperty, final String editDisabledProperty,
448 final String toolProperty) {
450 initializationScreenContext(menuProperty, titleProperty,
451 editDisabledProperty);
452 getToolBarSettings().intializeMenuBar(toolProperty);
456 * Initialization of the screen context for menu bar and tool bar.
458 * @param menuProperty -
459 * the property of the menu bar.
460 * @param toolProperty -
461 * the property of the tool bar.
463 public void initializationScreenContext(final String menuProperty,
464 final String toolProperty) {
466 initializationContext();
467 getMenuBarSettings().intializeMenuBar(menuProperty);
468 getToolBarSettings().intializeMenuBar(toolProperty);
472 * Initialization of the screen context for menu bar, title bar and tool bar.
474 * @param menuProperty -
475 * the property of the menu bar.
476 * @param titleProperty -
477 * The title of the open study/knowledge.
478 * @param editDisabledProperty -
479 * Property that indicates whether the current open study is editable or not.
480 * @param toolProperty -
481 * the property of the tool bar.
482 * @param leftMenuProperty -
483 * the property of the left menu.
485 public void initializationFullScreenContext(final String menuProperty,
486 final String titleProperty, final String editDisabledProperty,
487 final String toolProperty, final String leftMenuProperty) {
489 initializationScreenContext(menuProperty, titleProperty,
490 editDisabledProperty);
491 initializationContextLeftMenus(leftMenuProperty);
492 getToolBarSettings().intializeMenuBar(toolProperty);
496 * Initialization of the screen context for menu bar and tool bar.
498 * @param menuProperty -
499 * the property of the menu bar.
500 * @param toolProperty -
501 * the property of the tool bar.
502 * @param leftMenuProperty -
503 * the property of the left menu.
505 public void initializationFullScreenContext(final String menuProperty,
506 final String toolProperty, final String leftMenuProperty) {
508 initializationContext();
509 initializationContextLeftMenus(leftMenuProperty);
510 getMenuBarSettings().intializeMenuBar(menuProperty);
511 getToolBarSettings().intializeMenuBar(toolProperty);
514 // ==============================================================================================================================
515 // Getters and setters
516 // ==============================================================================================================================
521 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
523 public void setServletRequest(final HttpServletRequest request) {
524 this._servletRequest = request;
528 * Get current HTTP request.
530 * @return HTTP request
532 public HttpServletRequest getServletRequest() {
533 return _servletRequest;
537 * Get current error code.
541 public String getErrorCode() {
548 * @return session map
550 public Map<String, Object> getSession() {
558 * the error code to set
560 public void setErrorCode(final String code) {
561 this._errorCode = code;
567 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
569 public void setSession(final Map<String, Object> session) {
570 this._session = session;
574 * Get the menuBarSettings.
576 * @return the menuBarSettings
578 public MenuBarSettings getMenuBarSettings() {
579 return _menuBarSettings;
583 * Set the menuBarSettings.
585 * @param menuBarSettings
586 * the menuBarSettings to set
588 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
589 _menuBarSettings = menuBarSettings;
593 * Get the _titleBarSettings.
595 * @return the _titleBarSettings
597 public TitleBarSettings getTitleBarSettings() {
598 return _titleBarSettings;
602 * Set the titleBarSettings.
604 * @param titleBarSettings
605 * the titleBarSettings to set
607 public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
608 _titleBarSettings = titleBarSettings;
612 * Get the toolBarSettings.
614 * @return the toolBarSettings
616 public final ToolBarSettings getToolBarSettings() {
617 return _toolBarSettings;
621 * Set the toolBarSettings.
623 * @param toolBarSettings
624 * the toolBarSettings to set
626 public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
627 _toolBarSettings = toolBarSettings;
631 * Get the applicationSettings.
633 * @return the applicationSettings
635 public ApplicationSettings getApplicationSettings() {
636 return _applicationSettings;
640 * Set the applicationSettings.
642 * @param applicationSettings
643 * the applicationSettings to set
645 public void setApplicationSettings(
646 final ApplicationSettings applicationSettings) {
647 _applicationSettings = applicationSettings;
651 * Get the leftMenuSettings.
653 * @return the leftMenuSettings
655 public LeftMenuSettings getLeftMenuSettings() {
656 return _leftMenuSettings;
660 * Set the leftMenuSettings.
662 * @param leftMenuSettings
663 * the leftMenuSettings to set
665 public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
666 _leftMenuSettings = leftMenuSettings;
670 * Get the actionType.
671 * @return the actionType
673 public String getActionType() {
678 * Set the actionType.
679 * @param actionType the actionType to set
681 public void setActionType(final String actionType) {
682 _actionType = actionType;