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.struts2.interceptor.ServletRequestAware;
11 import org.apache.struts2.interceptor.SessionAware;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.DocumentType;
14 import org.splat.dal.bo.som.SimulationContextType;
15 import org.splat.dal.bo.som.Study;
16 import org.splat.log.AppLogger;
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 AppLogger LOG = AppLogger.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;
94 * Action context key. It is used at least in building a context help URL.
96 protected String _simanContext = "";
99 * TitleBarSettings bean.
101 private TitleBarSettings _titleBarSettings;
103 * ToolBarSettings bean.
105 private ToolBarSettings _toolBarSettings;
108 * LeftMenuSettings bean.
110 private LeftMenuSettings _leftMenuSettings;
113 * Injected application settings bean.
115 private ApplicationSettings _applicationSettings;
117 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
119 private String _menuProperty;
121 * Value of the tool bar property. It can be: none, standard, study, back.
123 private String _toolProperty;
126 * Comparator for sorting document types with localized names.
128 public class DocumentTypeComparator implements Comparator<DocumentType> {
132 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
134 public int compare(final DocumentType t1, final DocumentType t2) {
135 ResourceBundle locale = ResourceBundle.getBundle("som",
136 getApplicationSettings().getCurrentLocale());
137 String name1 = t1.getName();
138 if (t1.isApproved()) {
139 name1 = locale.getString("type.document." + name1);
141 String name2 = t2.getName();
142 if (t2.isApproved()) {
143 name2 = locale.getString("type.document." + name2);
146 return name1.compareToIgnoreCase(name2);
151 * Comparator for sorting simulation context types with localized names.
153 public class ContextTypeComparator implements
154 Comparator<SimulationContextType> {
158 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
160 public int compare(final SimulationContextType t1,
161 final SimulationContextType t2) {
162 ResourceBundle locale = ResourceBundle.getBundle("som",
163 getApplicationSettings().getCurrentLocale());
164 String name1 = t1.getName();
165 if (t1.isApproved()) {
166 name1 = locale.getString("type.context." + name1);
168 String name2 = t2.getName();
169 if (t2.isApproved()) {
170 name2 = locale.getString("type.context." + name2);
173 return name1.compareToIgnoreCase(name2);
177 // ==============================================================================================================================
179 // ==============================================================================================================================
182 * Remove the currently open knowledge from the session.
184 protected void closeKnowledge() {
185 AbstractOpenObject open = (AbstractOpenObject) _session
186 .remove(KNOWLEDGE_OPEN);
187 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
188 open.clearFacades(); // For eventually reopening the knowledge from a fresh context
193 * Remove the currently open study from the session.
195 protected void closeStudy() {
196 AbstractOpenObject open = (AbstractOpenObject) _session
198 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
199 open.clearFacades(); // For eventually reopening the study from a fresh context
204 * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
209 * the user to connect
211 protected void connect(final LoginContext context, final User user) {
212 OpenStudy open = getOpenStudy();
214 open.changeUser(user);
216 _session.put(USER_RIGHTS, new ApplicationRights(user));
217 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
221 * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
223 protected void disconnect() {
224 OpenStudy open = getOpenStudy();
226 open.changeUser(null);
228 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
229 _session.remove(LOGIN_CONTEXT);
233 * Get the currently connected user from HTTP session.
237 public User getConnectedUser() {
238 ApplicationRights rights = (ApplicationRights) _session
240 User connected = null;
241 if (rights != null) {
242 connected = rights.getUser();
244 return connected; // May be null
248 * Get a menu named as "menu." with the given suffix from HTTP session.
251 * the menu name suffix
254 protected Menu getMenu(final String name) {
255 return (Menu) _session.get("menu." + name);
259 * Open knowledge setter.
262 * the OpenKnowledge to set
264 public void setOpenKnowledge(final OpenKnowledge kelm) {
265 _openKnowledge = kelm;
269 * Open knowledge getter.
271 * @return the currently open knowledge wrapper. May be null
273 protected OpenKnowledge getOpenKnowledge() {
274 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
275 return _openKnowledge;
282 * the OpenStudy to set
284 public void setOpenStudy(final OpenStudy aStudy) {
291 * @return the currently open stydy wrapper. May be null.
293 public OpenStudy getOpenStudy() {
294 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
295 return _openStudy; // May be null
299 * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
302 * the knowledge element to open
303 * @return OpenKnowledge wrapper object
305 protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
306 OpenKnowledge open = _openKnowledge.open(kelm);
308 closeKnowledge(); // Just in case
309 _session.put(KNOWLEDGE_OPEN, open);
314 * Open the given study in the current HTTP session. Replace the previose one in the session if any.
318 * @return OpenStudy wrapper object
320 protected OpenStudy open(final Study study) {
321 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
323 closeStudy(); // Just in case
324 _session.put(STUDY_OPEN, open);
329 * Initialization the Context for menubar and toolbar.
331 public void initializationContext() {
332 getMenuBarSettings().initializeInitialMenuProperties();
334 if (_session.get(STUDY_OPEN) == null) {
335 getMenuBarSettings().setIsStudyNull(true);
337 getMenuBarSettings().setIsStudyNull(false);
339 // for initialization ToolBarSettings.canUserEdit property
340 // and ToolBarSettings.isEnabledScript property.
342 OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
343 PopupMenu popup = currentStudy.getPopup();
344 StudyRights user = currentStudy.getStudyRights();
346 if (user.canEditProperties()) {
347 getToolBarSettings().setCanUserEdit(true);
349 getToolBarSettings().setCanUserEdit(false);
353 getToolBarSettings().setIsEnabledScript(false);
354 } else if (popup.isEnabled("script")) {
355 getToolBarSettings().setIsEnabledScript(true);
357 getToolBarSettings().setIsEnabledScript(false);
361 if (_session.get(KNOWLEDGE_OPEN) == null) {
362 getMenuBarSettings().setIsKnowledgeNull(true);
364 getMenuBarSettings().setIsKnowledgeNull(false);
367 ApplicationRights userRights = (ApplicationRights) _session
370 if ((userRights != null) && userRights.canCreateStudy()) {
371 getMenuBarSettings().setCanUserCreateStudy(true);
373 getMenuBarSettings().setCanUserCreateStudy(false);
376 if ((userRights != null) && userRights.canManageDatabase()) {
377 getMenuBarSettings().setCanUserManageDatabase(true);
379 getMenuBarSettings().setCanUserManageDatabase(false);
385 * Initialization the Context for left menu.
387 * @param leftMenuProperty -
388 * the property of the left menu.
390 public void initializationContextLeftMenus(final String leftMenuProperty) {
392 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
394 getLeftMenuSettings().setMenu(menu);
396 getLeftMenuSettings().setMenuName(menu.getName());
397 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
402 * Initialization the Context for Menu Bar and Tool Bar.
404 * @param titleProperty -
405 * The title of the open study/knowledge.
406 * @param editDisabledProperty -
407 * Property that indicates whether the current open study is editable or not.
409 public void initializationContext(final String titleProperty,
410 final String editDisabledProperty) {
412 initializationContext();
414 AbstractOpenObject entity = (AbstractOpenObject) _session
415 .get(titleProperty + ".open");
417 if (entity != null) {
418 getTitleBarSettings().setProgressState(
419 entity.getProgressState().toString());
420 getTitleBarSettings().setSelectionState(entity.getSelection());
421 getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
422 getTitleBarSettings().setEntryTypeTitle(entity.getType());
423 getTitleBarSettings().setEntryTitle(entity.getTitle());
425 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
429 * Initialization of the screen context for menu bar.
431 * @param menuProperty -
432 * the property of the menu bar.
434 public void initializationScreenContext(final String menuProperty) {
436 initializationContext();
437 setMenuProperty(menuProperty);
438 getMenuBarSettings().intializeMenuBar(menuProperty);
442 * Initialization of the screen context for menu bar and title bar.
444 * @param menuProperty -
445 * the property of the menu bar.
446 * @param titleProperty -
447 * The title of the open study/knowledge.
448 * @param editDisabledProperty -
449 * Property that indicates whether the current open study is editable or not.
451 public void initializationScreenContext(final String menuProperty,
452 final String titleProperty, final String editDisabledProperty) {
454 initializationContext(titleProperty, editDisabledProperty);
455 setMenuProperty(menuProperty);
456 getMenuBarSettings().intializeMenuBar(menuProperty);
460 * Initialization of the screen context for menu bar, title bar and tool bar.
462 * @param menuProperty -
463 * the property of the menu bar.
464 * @param titleProperty -
465 * The title of the open study/knowledge.
466 * @param editDisabledProperty -
467 * Property that indicates whether the current open study is editable or not.
468 * @param toolProperty -
469 * the property of the tool bar.
471 public void initializationScreenContext(final String menuProperty,
472 final String titleProperty, final String editDisabledProperty,
473 final String toolProperty) {
475 initializationScreenContext(menuProperty, titleProperty,
476 editDisabledProperty);
477 getToolBarSettings().intializeMenuBar(toolProperty);
481 * Initialization of the screen context for menu bar and tool bar.
483 * @param menuProperty -
484 * the property of the menu bar.
485 * @param toolProperty -
486 * the property of the tool bar.
488 public void initializationScreenContext(final String menuProperty,
489 final String toolProperty) {
491 initializationContext();
492 setMenuProperty(menuProperty);
493 getMenuBarSettings().intializeMenuBar(menuProperty);
494 getToolBarSettings().intializeMenuBar(toolProperty);
498 * Initialization of the screen context for menu bar, title bar and tool bar.
500 * @param menuProperty -
501 * the property of the menu bar.
502 * @param titleProperty -
503 * The title of the open study/knowledge.
504 * @param editDisabledProperty -
505 * Property that indicates whether the current open study is editable or not.
506 * @param toolProperty -
507 * the property of the tool bar.
508 * @param leftMenuProperty -
509 * the property of the left menu.
511 public void initializationFullScreenContext(final String menuProperty,
512 final String titleProperty, final String editDisabledProperty,
513 final String toolProperty, final String leftMenuProperty) {
515 initializationScreenContext(menuProperty, titleProperty,
516 editDisabledProperty);
517 initializationContextLeftMenus(leftMenuProperty);
518 getToolBarSettings().intializeMenuBar(toolProperty);
522 * Initialization of the screen context for menu bar and tool bar.
524 * @param menuProperty -
525 * the property of the menu bar.
526 * @param toolProperty -
527 * the property of the tool bar.
528 * @param leftMenuProperty -
529 * the property of the left menu.
531 public void initializationFullScreenContext(final String menuProperty,
532 final String toolProperty, final String leftMenuProperty) {
534 initializationContext();
535 initializationContextLeftMenus(leftMenuProperty);
536 setMenuProperty(menuProperty);
537 getMenuBarSettings().intializeMenuBar(menuProperty);
538 getToolBarSettings().intializeMenuBar(toolProperty);
541 // ==============================================================================================================================
542 // Getters and setters
543 // ==============================================================================================================================
548 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
550 public void setServletRequest(final HttpServletRequest request) {
551 this._servletRequest = request;
555 * Get current HTTP request.
557 * @return HTTP request
559 public HttpServletRequest getServletRequest() {
560 return _servletRequest;
564 * Get current error code.
568 public String getErrorCode() {
575 * @return session map
577 public Map<String, Object> getSession() {
585 * the error code to set
587 public void setErrorCode(final String code) {
588 this._errorCode = code;
589 this.setMessage(getText(code));
595 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
597 public void setSession(final Map<String, Object> session) {
598 this._session = session;
602 * Get the menuBarSettings.
604 * @return the menuBarSettings
606 public MenuBarSettings getMenuBarSettings() {
607 return _menuBarSettings;
611 * Set the menuBarSettings.
613 * @param menuBarSettings
614 * the menuBarSettings to set
616 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
617 _menuBarSettings = menuBarSettings;
621 * Get the _titleBarSettings.
623 * @return the _titleBarSettings
625 public TitleBarSettings getTitleBarSettings() {
626 return _titleBarSettings;
630 * Set the titleBarSettings.
632 * @param titleBarSettings
633 * the titleBarSettings to set
635 public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
636 _titleBarSettings = titleBarSettings;
640 * Get the toolBarSettings.
642 * @return the toolBarSettings
644 public final ToolBarSettings getToolBarSettings() {
645 return _toolBarSettings;
649 * Set the toolBarSettings.
651 * @param toolBarSettings
652 * the toolBarSettings to set
654 public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
655 _toolBarSettings = toolBarSettings;
659 * Get the applicationSettings.
661 * @return the applicationSettings
663 public ApplicationSettings getApplicationSettings() {
664 return _applicationSettings;
668 * Set the applicationSettings.
670 * @param applicationSettings
671 * the applicationSettings to set
673 public void setApplicationSettings(
674 final ApplicationSettings applicationSettings) {
675 _applicationSettings = applicationSettings;
679 * Get the leftMenuSettings.
681 * @return the leftMenuSettings
683 public LeftMenuSettings getLeftMenuSettings() {
684 return _leftMenuSettings;
688 * Set the leftMenuSettings.
690 * @param leftMenuSettings
691 * the leftMenuSettings to set
693 public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
694 _leftMenuSettings = leftMenuSettings;
698 * Get the actionType.
700 * @return the actionType
702 public String getActionType() {
707 * Set the actionType.
710 * the actionType to set
712 public void setActionType(final String actionType) {
713 _actionType = actionType;
719 * @return the message
721 public String getMessage() {
731 public void setMessage(final String message) {
736 * Get the menuProperty.
738 * @return the menuProperty
740 public String getMenuProperty() {
741 return _menuProperty;
745 * Get the toolProperty.
747 * @return the toolProperty
749 public String getToolProperty() {
750 return _toolProperty;
754 * Set the menuProperty.
756 * @param menuProperty
757 * the menuProperty to set
759 public void setMenuProperty(final String menuProperty) {
760 this._menuProperty = menuProperty;
764 * Set the toolProperty.
766 * @param toolProperty
767 * the toolProperty to set
769 public void setToolProperty(final String toolProperty) {
770 _toolProperty = toolProperty;
774 * Get the simanContext.
775 * @return the simanContext
777 public String getSimanContext() {
778 return _simanContext;
782 * Set the simanContext.
783 * @param simanContext the simanContext to set
785 public void setSimanContext(final String simanContext) {
786 _simanContext = simanContext;