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;
114 * Comparator for sorting document types with localized names.
116 public class DocumentTypeComparator implements Comparator<DocumentType> {
120 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
122 public int compare(final DocumentType t1, final DocumentType t2) {
123 ResourceBundle locale = ResourceBundle.getBundle("som",
124 getApplicationSettings().getCurrentLocale());
125 String name1 = t1.getName();
126 if (t1.isApproved()) {
127 name1 = locale.getString("type.document." + name1);
129 String name2 = t2.getName();
130 if (t2.isApproved()) {
131 name2 = locale.getString("type.document." + name2);
134 return name1.compareToIgnoreCase(name2);
139 * Comparator for sorting simulation context types with localized names.
141 public class ContextTypeComparator implements
142 Comparator<SimulationContextType> {
146 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
148 public int compare(final SimulationContextType t1,
149 final SimulationContextType t2) {
150 ResourceBundle locale = ResourceBundle.getBundle("som",
151 getApplicationSettings().getCurrentLocale());
152 String name1 = t1.getName();
153 if (t1.isApproved()) {
154 name1 = locale.getString("type.context." + name1);
156 String name2 = t2.getName();
157 if (t2.isApproved()) {
158 name2 = locale.getString("type.context." + name2);
161 return name1.compareToIgnoreCase(name2);
165 // ==============================================================================================================================
167 // ==============================================================================================================================
170 * Remove the currently open knowledge from the session.
172 protected void closeKnowledge() {
173 AbstractOpenObject open = (AbstractOpenObject) _session.remove(KNOWLEDGE_OPEN);
174 if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
175 open.clearFacades(); // For eventually reopening the knowledge from a fresh context
180 * Remove the currently open study from the session.
182 protected void closeStudy() {
183 AbstractOpenObject open = (AbstractOpenObject) _session.remove(STUDY_OPEN);
184 if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
185 open.clearFacades(); // For eventually reopening the study from a fresh context
190 * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
195 * the user to connect
197 protected void connect(final LoginContext context, final User user) {
198 OpenStudy open = getOpenStudy();
200 open.changeUser(user);
202 _session.put(USER_RIGHTS, new ApplicationRights(user));
203 _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
207 * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
209 protected void disconnect() {
210 OpenStudy open = getOpenStudy();
212 open.changeUser(null);
214 _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
215 _session.remove(LOGIN_CONTEXT);
219 * Get the currently connected user from HTTP session.
223 public User getConnectedUser() {
224 ApplicationRights rights = (ApplicationRights) _session
226 User connected = null;
227 if (rights != null) {
228 connected = rights.getUser();
230 return connected; // May be null
234 * Get a menu named as "menu." with the given suffix from HTTP session.
237 * the menu name suffix
240 protected Menu getMenu(final String name) {
241 return (Menu) _session.get("menu." + name);
245 * Open knowledge setter.
248 * the OpenKnowledge to set
250 public void setOpenKnowledge(final OpenKnowledge kelm) {
251 _openKnowledge = kelm;
255 * Open knowledge getter.
257 * @return the currently open knowledge wrapper. May be null
259 protected OpenKnowledge getOpenKnowledge() {
260 // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
261 return _openKnowledge;
268 * the OpenStudy to set
270 public void setOpenStudy(final OpenStudy aStudy) {
277 * @return the currently open stydy wrapper. May be null.
279 public OpenStudy getOpenStudy() {
280 // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
281 return _openStudy; // May be null
285 * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
288 * the knowledge element to open
289 * @return OpenKnowledge wrapper object
291 protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
292 OpenKnowledge open = _openKnowledge.open(kelm);
294 closeKnowledge(); // Just in case
295 _session.put(KNOWLEDGE_OPEN, open);
300 * Open the given study in the current HTTP session. Replace the previose one in the session if any.
304 * @return OpenStudy wrapper object
306 protected OpenStudy open(final Study study) {
307 OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
309 closeStudy(); // Just in case
310 _session.put(STUDY_OPEN, open);
315 * Initialization the Context for menubar and toolbar.
317 public void initializationContext() {
318 getMenuBarSettings().initializeInitialMenuProperties();
320 if (_session.get(STUDY_OPEN) == null) {
321 getMenuBarSettings().setIsStudyNull(true);
323 getMenuBarSettings().setIsStudyNull(false);
325 // for initialization ToolBarSettings.canUserEdit property
326 // and ToolBarSettings.isEnabledScript property.
328 OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
329 PopupMenu popup = currentStudy.getPopup();
330 StudyRights user = currentStudy.getStudyRights();
332 if (user.canEditProperties()) {
333 getToolBarSettings().setCanUserEdit(true);
335 getToolBarSettings().setCanUserEdit(false);
339 getToolBarSettings().setIsEnabledScript(false);
340 } else if (popup.isEnabled("script")) {
341 getToolBarSettings().setIsEnabledScript(true);
343 getToolBarSettings().setIsEnabledScript(false);
347 if (_session.get(KNOWLEDGE_OPEN) == null) {
348 getMenuBarSettings().setIsKnowledgeNull(true);
350 getMenuBarSettings().setIsKnowledgeNull(false);
353 ApplicationRights userRights = (ApplicationRights) _session
356 if ((userRights != null) && userRights.canCreateStudy()) {
357 getMenuBarSettings().setCanUserCreateStudy(true);
359 getMenuBarSettings().setCanUserCreateStudy(false);
362 if ((userRights != null) && userRights.canManageDatabase()) {
363 getMenuBarSettings().setCanUserManageDatabase(true);
365 getMenuBarSettings().setCanUserManageDatabase(false);
371 * Initialization the Context for left menu.
373 * @param leftMenuProperty -
374 * the property of the left menu.
376 public void initializationContextLeftMenus(final String leftMenuProperty) {
378 Menu menu = (Menu) _session.get("menu." + leftMenuProperty);
380 getLeftMenuSettings().setMenu(menu);
381 getLeftMenuSettings().setMenuName(menu.getName());
382 getLeftMenuSettings().setMenuNamespace(menu.getNamespace());
386 * Initialization the Context for Menu Bar and Tool Bar.
388 * @param titleProperty -
389 * The title of the open study/knowledge.
390 * @param editDisabledProperty -
391 * Property that indicates whether the current open study is editable or not.
393 public void initializationContext(final String titleProperty,
394 final String editDisabledProperty) {
396 initializationContext();
398 AbstractOpenObject entity = (AbstractOpenObject) _session.get(titleProperty + ".open");
400 if (entity != null) {
401 getTitleBarSettings().setProgressState(
402 entity.getProgressState().toString());
403 getTitleBarSettings().setSelectionState(entity.getSelection());
404 getTitleBarSettings().setEntryType(entity.getType().toLowerCase());
405 getTitleBarSettings().setEntryTypeTitle(entity.getType());
406 getTitleBarSettings().setEntryTitle(entity.getTitle());
408 getTitleBarSettings().setEditDisabledProperty(editDisabledProperty);
412 * Initialization of the screen context for menu bar.
414 * @param menuProperty -
415 * the property of the menu bar.
417 public void initializationScreenContext(final String menuProperty) {
419 initializationContext();
420 getMenuBarSettings().intializeMenuBar(menuProperty);
424 * Initialization of the screen context for menu bar and title bar.
426 * @param menuProperty -
427 * the property of the menu bar.
428 * @param titleProperty -
429 * The title of the open study/knowledge.
430 * @param editDisabledProperty -
431 * Property that indicates whether the current open study is editable or not.
433 public void initializationScreenContext(final String menuProperty,
434 final String titleProperty, final String editDisabledProperty) {
436 initializationContext(titleProperty, editDisabledProperty);
437 getMenuBarSettings().intializeMenuBar(menuProperty);
441 * Initialization of the screen context for menu bar, title bar and tool bar.
443 * @param menuProperty -
444 * the property of the menu bar.
445 * @param titleProperty -
446 * The title of the open study/knowledge.
447 * @param editDisabledProperty -
448 * Property that indicates whether the current open study is editable or not.
449 * @param toolProperty -
450 * the property of the tool bar.
452 public void initializationScreenContext(final String menuProperty,
453 final String titleProperty, final String editDisabledProperty,
454 final String toolProperty) {
456 initializationScreenContext(menuProperty, titleProperty,
457 editDisabledProperty);
458 getToolBarSettings().intializeMenuBar(toolProperty);
462 * Initialization of the screen context for menu bar and tool bar.
464 * @param menuProperty -
465 * the property of the menu bar.
466 * @param toolProperty -
467 * the property of the tool bar.
469 public void initializationScreenContext(final String menuProperty,
470 final String toolProperty) {
472 initializationContext();
473 getMenuBarSettings().intializeMenuBar(menuProperty);
474 getToolBarSettings().intializeMenuBar(toolProperty);
478 * Initialization of the screen context for menu bar, title bar and tool bar.
480 * @param menuProperty -
481 * the property of the menu bar.
482 * @param titleProperty -
483 * The title of the open study/knowledge.
484 * @param editDisabledProperty -
485 * Property that indicates whether the current open study is editable or not.
486 * @param toolProperty -
487 * the property of the tool bar.
488 * @param leftMenuProperty -
489 * the property of the left menu.
491 public void initializationFullScreenContext(final String menuProperty,
492 final String titleProperty, final String editDisabledProperty,
493 final String toolProperty, final String leftMenuProperty) {
495 initializationScreenContext(menuProperty, titleProperty,
496 editDisabledProperty);
497 initializationContextLeftMenus(leftMenuProperty);
498 getToolBarSettings().intializeMenuBar(toolProperty);
502 * Initialization of the screen context for menu bar and tool bar.
504 * @param menuProperty -
505 * the property of the menu bar.
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 toolProperty, final String leftMenuProperty) {
514 initializationContext();
515 initializationContextLeftMenus(leftMenuProperty);
516 getMenuBarSettings().intializeMenuBar(menuProperty);
517 getToolBarSettings().intializeMenuBar(toolProperty);
520 // ==============================================================================================================================
521 // Getters and setters
522 // ==============================================================================================================================
527 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
529 public void setServletRequest(final HttpServletRequest request) {
530 this._servletRequest = request;
534 * Get current HTTP request.
536 * @return HTTP request
538 public HttpServletRequest getServletRequest() {
539 return _servletRequest;
543 * Get current error code.
547 public String getErrorCode() {
554 * @return session map
556 public Map<String, Object> getSession() {
564 * the error code to set
566 public void setErrorCode(final String code) {
567 this._errorCode = code;
573 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
575 public void setSession(final Map<String, Object> session) {
576 this._session = session;
580 * Get the menuBarSettings.
582 * @return the menuBarSettings
584 public MenuBarSettings getMenuBarSettings() {
585 return _menuBarSettings;
589 * Set the menuBarSettings.
591 * @param menuBarSettings
592 * the menuBarSettings to set
594 public void setMenuBarSettings(final MenuBarSettings menuBarSettings) {
595 _menuBarSettings = menuBarSettings;
599 * Get the _titleBarSettings.
601 * @return the _titleBarSettings
603 public TitleBarSettings getTitleBarSettings() {
604 return _titleBarSettings;
608 * Set the titleBarSettings.
610 * @param titleBarSettings
611 * the titleBarSettings to set
613 public void setTitleBarSettings(final TitleBarSettings titleBarSettings) {
614 _titleBarSettings = titleBarSettings;
618 * Get the toolBarSettings.
620 * @return the toolBarSettings
622 public final ToolBarSettings getToolBarSettings() {
623 return _toolBarSettings;
627 * Set the toolBarSettings.
629 * @param toolBarSettings
630 * the toolBarSettings to set
632 public final void setToolBarSettings(final ToolBarSettings toolBarSettings) {
633 _toolBarSettings = toolBarSettings;
637 * Get the applicationSettings.
639 * @return the applicationSettings
641 public ApplicationSettings getApplicationSettings() {
642 return _applicationSettings;
646 * Set the applicationSettings.
648 * @param applicationSettings
649 * the applicationSettings to set
651 public void setApplicationSettings(
652 final ApplicationSettings applicationSettings) {
653 _applicationSettings = applicationSettings;
657 * Get the leftMenuSettings.
659 * @return the leftMenuSettings
661 public LeftMenuSettings getLeftMenuSettings() {
662 return _leftMenuSettings;
666 * Set the leftMenuSettings.
668 * @param leftMenuSettings
669 * the leftMenuSettings to set
671 public void setLeftMenuSettings(final LeftMenuSettings leftMenuSettings) {
672 _leftMenuSettings = leftMenuSettings;
676 * Get the actionType.
677 * @return the actionType
679 public String getActionType() {
684 * Set the actionType.
685 * @param actionType the actionType to set
687 public void setActionType(final String actionType) {
688 _actionType = actionType;
693 * @return the message
695 public String getMessage() {
701 * @param message the message to set
703 public void setMessage(final String message) {