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.remove(KNOWLEDGE_OPEN);
182 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
183 open.clearFacades(); // For eventually reopening the knowledge from a fresh context
188 * Remove the currently open study from the session.
190 protected void closeStudy() {
191 AbstractOpenObject open = (AbstractOpenObject) _session.remove(STUDY_OPEN);
192 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
193 open.clearFacades(); // For eventually reopening the study from a fresh context
198 * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
203 * the user to connect
205 protected void connect(final LoginContext context, final User user) {
206 OpenStudy open = getOpenStudy();
208 open.changeUser(user);
210 _session.put(USER_RIGHTS, new ApplicationRights(user));
211 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
215 * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
217 protected void disconnect() {
218 OpenStudy open = getOpenStudy();
220 open.changeUser(null);
222 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
223 _session.remove(LOGIN_CONTEXT);
227 * Get the currently connected user from HTTP session.
231 public User getConnectedUser() {
232 ApplicationRights rights = (ApplicationRights) _session
234 User connected = null;
235 if (rights != null) {
236 connected = rights.getUser();
238 return connected; // May be null
242 * Get a menu named as "menu." with the given suffix from HTTP session.
245 * the menu name suffix
248 protected Menu getMenu(final String name) {
249 return (Menu) _session.get("menu." + name);
253 * Open knowledge setter.
256 * the OpenKnowledge to set
258 public void setOpenKnowledge(final OpenKnowledge kelm) {
259 _openKnowledge = kelm;
263 * Open knowledge getter.
265 * @return the currently open knowledge wrapper. May be null
267 protected OpenKnowledge getOpenKnowledge() {
268 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
269 return _openKnowledge;
276 * the OpenStudy to set
278 public void setOpenStudy(final OpenStudy aStudy) {
285 * @return the currently open stydy wrapper. May be null.
287 public OpenStudy getOpenStudy() {
288 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
289 return _openStudy; // May be null
293 * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
296 * the knowledge element to open
297 * @return OpenKnowledge wrapper object
299 protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
300 OpenKnowledge open = _openKnowledge.open(kelm);
302 closeKnowledge(); // Just in case
303 _session.put(KNOWLEDGE_OPEN, open);
308 * Open the given study in the current HTTP session. Replace the previose one in the session if any.
312 * @return OpenStudy wrapper object
314 protected OpenStudy open(final Study study) {
315 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
317 closeStudy(); // Just in case
318 _session.put(STUDY_OPEN, open);
323 * Initialization the Context for menubar and toolbar.
325 public void initializationContext() {
326 getMenuBarSettings().initializeInitialMenuProperties();
328 if (_session.get(STUDY_OPEN) == null) {
329 getMenuBarSettings().setIsStudyNull(true);
331 getMenuBarSettings().setIsStudyNull(false);
333 // for initialization ToolBarSettings.canUserEdit property
334 // and ToolBarSettings.isEnabledScript property.
336 OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
337 PopupMenu popup = currentStudy.getPopup();
338 StudyRights user = currentStudy.getStudyRights();
340 if (user.canEditProperties()) {
341 getToolBarSettings().setCanUserEdit(true);
343 getToolBarSettings().setCanUserEdit(false);
347 getToolBarSettings().setIsEnabledScript(false);
348 } else if (popup.isEnabled("script")) {
349 getToolBarSettings().setIsEnabledScript(true);
351 getToolBarSettings().setIsEnabledScript(false);
355 if (_session.get(KNOWLEDGE_OPEN) == null) {
356 getMenuBarSettings().setIsKnowledgeNull(true);
358 getMenuBarSettings().setIsKnowledgeNull(false);
361 ApplicationRights userRights = (ApplicationRights) _session
364 if ((userRights != null) && userRights.canCreateStudy()) {
365 getMenuBarSettings().setCanUserCreateStudy(true);
367 getMenuBarSettings().setCanUserCreateStudy(false);
370 if ((userRights != null) && userRights.canManageDatabase()) {
371 getMenuBarSettings().setCanUserManageDatabase(true);
373 getMenuBarSettings().setCanUserManageDatabase(false);
379 * Initialization the Context for left menu.
381 * @param leftMenuProperty -
382 * the property of the left menu.
384 public void initializationContextLeftMenus(final String leftMenuProperty) {
386 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
388 getLeftMenuSettings().setMenu(menu);
389 getLeftMenuSettings().setMenuName(menu.getName());
390 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
394 * Initialization the Context for Menu Bar and Tool Bar.
396 * @param titleProperty -
397 * The title of the open study/knowledge.
398 * @param editDisabledProperty -
399 * Property that indicates whether the current open study is editable or not.
401 public void initializationContext(final String titleProperty,
402 final String editDisabledProperty) {
404 initializationContext();
406 AbstractOpenObject entity = (AbstractOpenObject) _session.get(titleProperty + ".open");
408 if (entity != null) {
409 getTitleBarSettings().setProgressState(
410 entity.getProgressState().toString());
411 getTitleBarSettings().setSelectionState(entity.getSelection());
412 getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
413 getTitleBarSettings().setEntryTypeTitle(entity.getType());
414 getTitleBarSettings().setEntryTitle(entity.getTitle());
416 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
420 * Initialization of the screen context for menu bar.
422 * @param menuProperty -
423 * the property of the menu bar.
425 public void initializationScreenContext(final String menuProperty) {
427 initializationContext();
428 setMenuProperty(menuProperty);
429 getMenuBarSettings().intializeMenuBar(menuProperty);
433 * Initialization of the screen context for menu bar and title bar.
435 * @param menuProperty -
436 * the property of the menu bar.
437 * @param titleProperty -
438 * The title of the open study/knowledge.
439 * @param editDisabledProperty -
440 * Property that indicates whether the current open study is editable or not.
442 public void initializationScreenContext(final String menuProperty,
443 final String titleProperty, final String editDisabledProperty) {
445 initializationContext(titleProperty, editDisabledProperty);
446 setMenuProperty(menuProperty);
447 getMenuBarSettings().intializeMenuBar(menuProperty);
451 * Initialization of the screen context for menu bar, title bar and tool bar.
453 * @param menuProperty -
454 * the property of the menu bar.
455 * @param titleProperty -
456 * The title of the open study/knowledge.
457 * @param editDisabledProperty -
458 * Property that indicates whether the current open study is editable or not.
459 * @param toolProperty -
460 * the property of the tool bar.
462 public void initializationScreenContext(final String menuProperty,
463 final String titleProperty, final String editDisabledProperty,
464 final String toolProperty) {
466 initializationScreenContext(menuProperty, titleProperty,
467 editDisabledProperty);
468 getToolBarSettings().intializeMenuBar(toolProperty);
472 * Initialization of the screen context for menu bar and tool bar.
474 * @param menuProperty -
475 * the property of the menu bar.
476 * @param toolProperty -
477 * the property of the tool bar.
479 public void initializationScreenContext(final String menuProperty,
480 final String toolProperty) {
482 initializationContext();
483 setMenuProperty(menuProperty);
484 getMenuBarSettings().intializeMenuBar(menuProperty);
485 getToolBarSettings().intializeMenuBar(toolProperty);
489 * Initialization of the screen context for menu bar, title bar and tool bar.
491 * @param menuProperty -
492 * the property of the menu bar.
493 * @param titleProperty -
494 * The title of the open study/knowledge.
495 * @param editDisabledProperty -
496 * Property that indicates whether the current open study is editable or not.
497 * @param toolProperty -
498 * the property of the tool bar.
499 * @param leftMenuProperty -
500 * the property of the left menu.
502 public void initializationFullScreenContext(final String menuProperty,
503 final String titleProperty, final String editDisabledProperty,
504 final String toolProperty, final String leftMenuProperty) {
506 initializationScreenContext(menuProperty, titleProperty,
507 editDisabledProperty);
508 initializationContextLeftMenus(leftMenuProperty);
509 getToolBarSettings().intializeMenuBar(toolProperty);
513 * Initialization of the screen context for menu bar and tool bar.
515 * @param menuProperty -
516 * the property of the menu bar.
517 * @param toolProperty -
518 * the property of the tool bar.
519 * @param leftMenuProperty -
520 * the property of the left menu.
522 public void initializationFullScreenContext(final String menuProperty,
523 final String toolProperty, final String leftMenuProperty) {
525 initializationContext();
526 initializationContextLeftMenus(leftMenuProperty);
527 setMenuProperty(menuProperty);
528 getMenuBarSettings().intializeMenuBar(menuProperty);
529 getToolBarSettings().intializeMenuBar(toolProperty);
532 // ==============================================================================================================================
533 // Getters and setters
534 // ==============================================================================================================================
539 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
541 public void setServletRequest(final HttpServletRequest request) {
542 this._servletRequest = request;
546 * Get current HTTP request.
548 * @return HTTP request
550 public HttpServletRequest getServletRequest() {
551 return _servletRequest;
555 * Get current error code.
559 public String getErrorCode() {
566 * @return session map
568 public Map<String, Object> getSession() {
576 * the error code to set
578 public void setErrorCode(final String code) {
579 this._errorCode = code;
585 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
587 public void setSession(final Map<String, Object> session) {
588 this._session = session;
592 * Get the menuBarSettings.
594 * @return the menuBarSettings
596 public MenuBarSettings getMenuBarSettings() {
597 return _menuBarSettings;
601 * Set the menuBarSettings.
603 * @param menuBarSettings
604 * the menuBarSettings to set
606 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
607 _menuBarSettings = menuBarSettings;
611 * Get the _titleBarSettings.
613 * @return the _titleBarSettings
615 public TitleBarSettings getTitleBarSettings() {
616 return _titleBarSettings;
620 * Set the titleBarSettings.
622 * @param titleBarSettings
623 * the titleBarSettings to set
625 public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
626 _titleBarSettings = titleBarSettings;
630 * Get the toolBarSettings.
632 * @return the toolBarSettings
634 public final ToolBarSettings getToolBarSettings() {
635 return _toolBarSettings;
639 * Set the toolBarSettings.
641 * @param toolBarSettings
642 * the toolBarSettings to set
644 public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
645 _toolBarSettings = toolBarSettings;
649 * Get the applicationSettings.
651 * @return the applicationSettings
653 public ApplicationSettings getApplicationSettings() {
654 return _applicationSettings;
658 * Set the applicationSettings.
660 * @param applicationSettings
661 * the applicationSettings to set
663 public void setApplicationSettings(
664 final ApplicationSettings applicationSettings) {
665 _applicationSettings = applicationSettings;
669 * Get the leftMenuSettings.
671 * @return the leftMenuSettings
673 public LeftMenuSettings getLeftMenuSettings() {
674 return _leftMenuSettings;
678 * Set the leftMenuSettings.
680 * @param leftMenuSettings
681 * the leftMenuSettings to set
683 public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
684 _leftMenuSettings = leftMenuSettings;
688 * Get the actionType.
689 * @return the actionType
691 public String getActionType() {
696 * Set the actionType.
697 * @param actionType the actionType to set
699 public void setActionType(final String actionType) {
700 _actionType = actionType;
705 * @return the message
707 public String getMessage() {
713 * @param message the message to set
715 public void setMessage(final String message) {
720 * Get the menuProperty.
722 * @return the menuProperty
724 public String getMenuProperty() {
725 return _menuProperty;
729 * Get the toolProperty.
731 * @return the toolProperty
733 public String getToolProperty() {
734 return _toolProperty;
738 * Set the menuProperty.
740 * @param menuProperty
741 * the menuProperty to set
743 public void setMenuProperty(final String menuProperty) {
744 this._menuProperty = menuProperty;
748 * Set the toolProperty.
750 * @param toolProperty
751 * the toolProperty to set
753 public void setToolProperty(final String toolProperty) {
754 _toolProperty = toolProperty;