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 * The message to show when the page is loaded.
75 private String _message = "";
77 * ActionType for specifying the type of the operaion.
79 private String _actionType;
81 * Current open study facade object.
83 private OpenStudy _openStudy;
85 * Current open knowledge facade object.
87 private OpenKnowledge _openKnowledge;
90 * MenuBarSettings bean.
92 private MenuBarSettings _menuBarSettings;
95 * TitleBarSettings bean.
97 private TitleBarSettings _titleBarSettings;
99 * ToolBarSettings bean.
101 private ToolBarSettings _toolBarSettings;
104 * LeftMenuSettings bean.
106 private LeftMenuSettings _leftMenuSettings;
109 * Injected application settings bean.
111 private ApplicationSettings _applicationSettings;
113 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
115 private String _menuProperty;
117 * Value of the tool bar property. It can be: none, standard, study, back.
119 private String _toolProperty;
122 * Comparator for sorting document types with localized names.
124 public class DocumentTypeComparator implements Comparator<DocumentType> {
128 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
130 public int compare(final DocumentType t1, final DocumentType t2) {
131 ResourceBundle locale = ResourceBundle.getBundle("som",
132 getApplicationSettings().getCurrentLocale());
133 String name1 = t1.getName();
134 if (t1.isApproved()) {
135 name1 = locale.getString("type.document." + name1);
137 String name2 = t2.getName();
138 if (t2.isApproved()) {
139 name2 = locale.getString("type.document." + name2);
142 return name1.compareToIgnoreCase(name2);
147 * Comparator for sorting simulation context types with localized names.
149 public class ContextTypeComparator implements
150 Comparator<SimulationContextType> {
154 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
156 public int compare(final SimulationContextType t1,
157 final SimulationContextType t2) {
158 ResourceBundle locale = ResourceBundle.getBundle("som",
159 getApplicationSettings().getCurrentLocale());
160 String name1 = t1.getName();
161 if (t1.isApproved()) {
162 name1 = locale.getString("type.context." + name1);
164 String name2 = t2.getName();
165 if (t2.isApproved()) {
166 name2 = locale.getString("type.context." + name2);
169 return name1.compareToIgnoreCase(name2);
173 // ==============================================================================================================================
175 // ==============================================================================================================================
178 * Remove the currently open knowledge from the session.
180 protected void closeKnowledge() {
181 AbstractOpenObject open = (AbstractOpenObject) _session
182 .remove(KNOWLEDGE_OPEN);
183 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
184 open.clearFacades(); // For eventually reopening the knowledge from a fresh context
189 * Remove the currently open study from the session.
191 protected void closeStudy() {
192 AbstractOpenObject open = (AbstractOpenObject) _session
194 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
195 open.clearFacades(); // For eventually reopening the study from a fresh context
200 * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
205 * the user to connect
207 protected void connect(final LoginContext context, final User user) {
208 OpenStudy open = getOpenStudy();
210 open.changeUser(user);
212 _session.put(USER_RIGHTS, new ApplicationRights(user));
213 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
217 * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
219 protected void disconnect() {
220 OpenStudy open = getOpenStudy();
222 open.changeUser(null);
224 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
225 _session.remove(LOGIN_CONTEXT);
229 * Get the currently connected user from HTTP session.
233 public User getConnectedUser() {
234 ApplicationRights rights = (ApplicationRights) _session
236 User connected = null;
237 if (rights != null) {
238 connected = rights.getUser();
240 return connected; // May be null
244 * Get a menu named as "menu." with the given suffix from HTTP session.
247 * the menu name suffix
250 protected Menu getMenu(final String name) {
251 return (Menu) _session.get("menu." + name);
255 * Open knowledge setter.
258 * the OpenKnowledge to set
260 public void setOpenKnowledge(final OpenKnowledge kelm) {
261 _openKnowledge = kelm;
265 * Open knowledge getter.
267 * @return the currently open knowledge wrapper. May be null
269 protected OpenKnowledge getOpenKnowledge() {
270 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
271 return _openKnowledge;
278 * the OpenStudy to set
280 public void setOpenStudy(final OpenStudy aStudy) {
287 * @return the currently open stydy wrapper. May be null.
289 public OpenStudy getOpenStudy() {
290 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
291 return _openStudy; // May be null
295 * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
298 * the knowledge element to open
299 * @return OpenKnowledge wrapper object
301 protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
302 OpenKnowledge open = _openKnowledge.open(kelm);
304 closeKnowledge(); // Just in case
305 _session.put(KNOWLEDGE_OPEN, open);
310 * Open the given study in the current HTTP session. Replace the previose one in the session if any.
314 * @return OpenStudy wrapper object
316 protected OpenStudy open(final Study study) {
317 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
319 closeStudy(); // Just in case
320 _session.put(STUDY_OPEN, open);
325 * Initialization the Context for menubar and toolbar.
327 public void initializationContext() {
328 getMenuBarSettings().initializeInitialMenuProperties();
330 if (_session.get(STUDY_OPEN) == null) {
331 getMenuBarSettings().setIsStudyNull(true);
333 getMenuBarSettings().setIsStudyNull(false);
335 // for initialization ToolBarSettings.canUserEdit property
336 // and ToolBarSettings.isEnabledScript property.
338 OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
339 PopupMenu popup = currentStudy.getPopup();
340 StudyRights user = currentStudy.getStudyRights();
342 if (user.canEditProperties()) {
343 getToolBarSettings().setCanUserEdit(true);
345 getToolBarSettings().setCanUserEdit(false);
349 getToolBarSettings().setIsEnabledScript(false);
350 } else if (popup.isEnabled("script")) {
351 getToolBarSettings().setIsEnabledScript(true);
353 getToolBarSettings().setIsEnabledScript(false);
357 if (_session.get(KNOWLEDGE_OPEN) == null) {
358 getMenuBarSettings().setIsKnowledgeNull(true);
360 getMenuBarSettings().setIsKnowledgeNull(false);
363 ApplicationRights userRights = (ApplicationRights) _session
366 if ((userRights != null) && userRights.canCreateStudy()) {
367 getMenuBarSettings().setCanUserCreateStudy(true);
369 getMenuBarSettings().setCanUserCreateStudy(false);
372 if ((userRights != null) && userRights.canManageDatabase()) {
373 getMenuBarSettings().setCanUserManageDatabase(true);
375 getMenuBarSettings().setCanUserManageDatabase(false);
381 * Initialization the Context for left menu.
383 * @param leftMenuProperty -
384 * the property of the left menu.
386 public void initializationContextLeftMenus(final String leftMenuProperty) {
388 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
390 getLeftMenuSettings().setMenu(menu);
392 getLeftMenuSettings().setMenuName(menu.getName());
393 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
398 * Initialization the Context for Menu Bar and Tool Bar.
400 * @param titleProperty -
401 * The title of the open study/knowledge.
402 * @param editDisabledProperty -
403 * Property that indicates whether the current open study is editable or not.
405 public void initializationContext(final String titleProperty,
406 final String editDisabledProperty) {
408 initializationContext();
410 AbstractOpenObject entity = (AbstractOpenObject) _session
411 .get(titleProperty + ".open");
413 if (entity != null) {
414 getTitleBarSettings().setProgressState(
415 entity.getProgressState().toString());
416 getTitleBarSettings().setSelectionState(entity.getSelection());
417 getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
418 getTitleBarSettings().setEntryTypeTitle(entity.getType());
419 getTitleBarSettings().setEntryTitle(entity.getTitle());
421 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
425 * Initialization of the screen context for menu bar.
427 * @param menuProperty -
428 * the property of the menu bar.
430 public void initializationScreenContext(final String menuProperty) {
432 initializationContext();
433 setMenuProperty(menuProperty);
434 getMenuBarSettings().intializeMenuBar(menuProperty);
438 * Initialization of the screen context for menu bar and title bar.
440 * @param menuProperty -
441 * the property of the menu bar.
442 * @param titleProperty -
443 * The title of the open study/knowledge.
444 * @param editDisabledProperty -
445 * Property that indicates whether the current open study is editable or not.
447 public void initializationScreenContext(final String menuProperty,
448 final String titleProperty, final String editDisabledProperty) {
450 initializationContext(titleProperty, editDisabledProperty);
451 setMenuProperty(menuProperty);
452 getMenuBarSettings().intializeMenuBar(menuProperty);
456 * Initialization of the screen context for menu bar, title bar and tool bar.
458 * @param menuProperty -
459 * the property of the menu bar.
460 * @param titleProperty -
461 * The title of the open study/knowledge.
462 * @param editDisabledProperty -
463 * Property that indicates whether the current open study is editable or not.
464 * @param toolProperty -
465 * the property of the tool bar.
467 public void initializationScreenContext(final String menuProperty,
468 final String titleProperty, final String editDisabledProperty,
469 final String toolProperty) {
471 initializationScreenContext(menuProperty, titleProperty,
472 editDisabledProperty);
473 getToolBarSettings().intializeMenuBar(toolProperty);
477 * Initialization of the screen context for menu bar and tool bar.
479 * @param menuProperty -
480 * the property of the menu bar.
481 * @param toolProperty -
482 * the property of the tool bar.
484 public void initializationScreenContext(final String menuProperty,
485 final String toolProperty) {
487 initializationContext();
488 setMenuProperty(menuProperty);
489 getMenuBarSettings().intializeMenuBar(menuProperty);
490 getToolBarSettings().intializeMenuBar(toolProperty);
494 * Initialization of the screen context for menu bar, title bar and tool bar.
496 * @param menuProperty -
497 * the property of the menu bar.
498 * @param titleProperty -
499 * The title of the open study/knowledge.
500 * @param editDisabledProperty -
501 * Property that indicates whether the current open study is editable or not.
502 * @param toolProperty -
503 * the property of the tool bar.
504 * @param leftMenuProperty -
505 * the property of the left menu.
507 public void initializationFullScreenContext(final String menuProperty,
508 final String titleProperty, final String editDisabledProperty,
509 final String toolProperty, final String leftMenuProperty) {
511 initializationScreenContext(menuProperty, titleProperty,
512 editDisabledProperty);
513 initializationContextLeftMenus(leftMenuProperty);
514 getToolBarSettings().intializeMenuBar(toolProperty);
518 * Initialization of the screen context for menu bar and tool bar.
520 * @param menuProperty -
521 * the property of the menu bar.
522 * @param toolProperty -
523 * the property of the tool bar.
524 * @param leftMenuProperty -
525 * the property of the left menu.
527 public void initializationFullScreenContext(final String menuProperty,
528 final String toolProperty, final String leftMenuProperty) {
530 initializationContext();
531 initializationContextLeftMenus(leftMenuProperty);
532 setMenuProperty(menuProperty);
533 getMenuBarSettings().intializeMenuBar(menuProperty);
534 getToolBarSettings().intializeMenuBar(toolProperty);
537 // ==============================================================================================================================
538 // Getters and setters
539 // ==============================================================================================================================
544 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
546 public void setServletRequest(final HttpServletRequest request) {
547 this._servletRequest = request;
551 * Get current HTTP request.
553 * @return HTTP request
555 public HttpServletRequest getServletRequest() {
556 return _servletRequest;
560 * Get current error code.
564 public String getErrorCode() {
571 * @return session map
573 public Map<String, Object> getSession() {
581 * the error code to set
583 public void setErrorCode(final String code) {
584 this._errorCode = code;
590 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
592 public void setSession(final Map<String, Object> session) {
593 this._session = session;
597 * Get the menuBarSettings.
599 * @return the menuBarSettings
601 public MenuBarSettings getMenuBarSettings() {
602 return _menuBarSettings;
606 * Set the menuBarSettings.
608 * @param menuBarSettings
609 * the menuBarSettings to set
611 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
612 _menuBarSettings = menuBarSettings;
616 * Get the _titleBarSettings.
618 * @return the _titleBarSettings
620 public TitleBarSettings getTitleBarSettings() {
621 return _titleBarSettings;
625 * Set the titleBarSettings.
627 * @param titleBarSettings
628 * the titleBarSettings to set
630 public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
631 _titleBarSettings = titleBarSettings;
635 * Get the toolBarSettings.
637 * @return the toolBarSettings
639 public final ToolBarSettings getToolBarSettings() {
640 return _toolBarSettings;
644 * Set the toolBarSettings.
646 * @param toolBarSettings
647 * the toolBarSettings to set
649 public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
650 _toolBarSettings = toolBarSettings;
654 * Get the applicationSettings.
656 * @return the applicationSettings
658 public ApplicationSettings getApplicationSettings() {
659 return _applicationSettings;
663 * Set the applicationSettings.
665 * @param applicationSettings
666 * the applicationSettings to set
668 public void setApplicationSettings(
669 final ApplicationSettings applicationSettings) {
670 _applicationSettings = applicationSettings;
674 * Get the leftMenuSettings.
676 * @return the leftMenuSettings
678 public LeftMenuSettings getLeftMenuSettings() {
679 return _leftMenuSettings;
683 * Set the leftMenuSettings.
685 * @param leftMenuSettings
686 * the leftMenuSettings to set
688 public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
689 _leftMenuSettings = leftMenuSettings;
693 * Get the actionType.
695 * @return the actionType
697 public String getActionType() {
702 * Set the actionType.
705 * the actionType to set
707 public void setActionType(final String actionType) {
708 _actionType = actionType;
714 * @return the message
716 public String getMessage() {
726 public void setMessage(final String message) {
731 * Get the menuProperty.
733 * @return the menuProperty
735 public String getMenuProperty() {
736 return _menuProperty;
740 * Get the toolProperty.
742 * @return the toolProperty
744 public String getToolProperty() {
745 return _toolProperty;
749 * Set the menuProperty.
751 * @param menuProperty
752 * the menuProperty to set
754 public void setMenuProperty(final String menuProperty) {
755 this._menuProperty = menuProperty;
759 * Set the toolProperty.
761 * @param toolProperty
762 * the toolProperty to set
764 public void setToolProperty(final String toolProperty) {
765 _toolProperty = toolProperty;