+ /**
+ * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
+ */
+ private String _menuProperty;
+ /**
+ * Value of the tool bar property. It can be: none, standard, study, back.
+ */
+ private String _toolProperty;
+
+ /**
+ * Comparator for sorting document types with localized names.
+ */
+ public class DocumentTypeComparator implements Comparator<DocumentType> {
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(final DocumentType t1, final DocumentType t2) {
+ ResourceBundle locale = ResourceBundle.getBundle("som",
+ getApplicationSettings().getCurrentLocale());
+ String name1 = t1.getName();
+ if (t1.isApproved()) {
+ name1 = locale.getString("type.document." + name1);
+ }
+ String name2 = t2.getName();
+ if (t2.isApproved()) {
+ name2 = locale.getString("type.document." + name2);
+ }
+
+ return name1.compareToIgnoreCase(name2);
+ }
+ }
+
+ /**
+ * Comparator for sorting simulation context types with localized names.
+ */
+ public class ContextTypeComparator implements
+ Comparator<SimulationContextType> {
+ /**
+ * {@inheritDoc}
+ *
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(final SimulationContextType t1,
+ final SimulationContextType t2) {
+ ResourceBundle locale = ResourceBundle.getBundle("som",
+ getApplicationSettings().getCurrentLocale());
+ String name1 = t1.getName();
+ if (t1.isApproved()) {
+ name1 = locale.getString("type.context." + name1);
+ }
+ String name2 = t2.getName();
+ if (t2.isApproved()) {
+ name2 = locale.getString("type.context." + name2);
+ }
+
+ return name1.compareToIgnoreCase(name2);
+ }
+ }
+
+ // ==============================================================================================================================
+ // Session services
+ // ==============================================================================================================================
+
+ /**
+ * Remove the currently open knowledge from the session.
+ */
+ protected void closeKnowledge() {
+ AbstractOpenObject open = (AbstractOpenObject) _session
+ .remove(KNOWLEDGE_OPEN);
+ if ((open != null) && (_session.get(STUDY_OPEN) == null)) {
+ open.clearFacades(); // For eventually reopening the knowledge from a fresh context
+ }
+ }
+
+ /**
+ * Remove the currently open study from the session.
+ */
+ protected void closeStudy() {
+ AbstractOpenObject open = (AbstractOpenObject) _session
+ .remove(STUDY_OPEN);
+ if ((open != null) && (_session.get(KNOWLEDGE_OPEN) == null)) {
+ open.clearFacades(); // For eventually reopening the study from a fresh context
+ }
+ }
+
+ /**
+ * Connect the given user to SIMAN. Store his rights and login context in HTTP session.
+ *
+ * @param context
+ * login context
+ * @param user
+ * the user to connect
+ */
+ protected void connect(final LoginContext context, final User user) {
+ OpenStudy open = getOpenStudy();
+ if (open != null) {
+ open.changeUser(user);
+ }
+ _session.put(USER_RIGHTS, new ApplicationRights(user));
+ _session.put(LOGIN_CONTEXT, context); // For executing the deconnection, when requested
+ }
+
+ /**
+ * Disconnect the currently connected user from SIMAN. Remove his rihgts and login context from the session.
+ */
+ protected void disconnect() {
+ OpenStudy open = getOpenStudy();
+ if (open != null) {
+ open.changeUser(null);
+ }
+ _session.put(USER_RIGHTS, new ApplicationRights(null)); // Disables user rights
+ _session.remove(LOGIN_CONTEXT);
+ }
+
+ /**
+ * Get the currently connected user from HTTP session.
+ *
+ * @return the user
+ */
+ public User getConnectedUser() {
+ ApplicationRights rights = (ApplicationRights) _session
+ .get(USER_RIGHTS);
+ User connected = null;
+ if (rights != null) {
+ connected = rights.getUser();
+ }
+ return connected; // May be null
+ }
+
+ /**
+ * Get a menu named as "menu." with the given suffix from HTTP session.
+ *
+ * @param name
+ * the menu name suffix
+ * @return the menu
+ */
+ protected Menu getMenu(final String name) {
+ return (Menu) _session.get("menu." + name);
+ }
+
+ /**
+ * Open knowledge setter.
+ *
+ * @param kelm
+ * the OpenKnowledge to set
+ */
+ public void setOpenKnowledge(final OpenKnowledge kelm) {
+ _openKnowledge = kelm;
+ }
+
+ /**
+ * Open knowledge getter.
+ *
+ * @return the currently open knowledge wrapper. May be null
+ */
+ protected OpenKnowledge getOpenKnowledge() {
+ // _openKnowledge = (OpenKnowledge)session.get(KNOWLEDGE_OPEN); // May be null
+ return _openKnowledge;
+ }
+
+ /**
+ * Open study setter.
+ *
+ * @param aStudy
+ * the OpenStudy to set
+ */
+ public void setOpenStudy(final OpenStudy aStudy) {
+ _openStudy = aStudy;
+ }
+
+ /**
+ * Open study getter.
+ *
+ * @return the currently open stydy wrapper. May be null.
+ */
+ public OpenStudy getOpenStudy() {
+ // _openStudy = (OpenStudy)session.get(STUDY_OPEN);
+ return _openStudy; // May be null
+ }
+
+ /**
+ * Open the given knowledge in the current HTTP session. Replace the previose one in the session if any.
+ *
+ * @param kelm
+ * the knowledge element to open
+ * @return OpenKnowledge wrapper object
+ */
+ protected OpenKnowledge open(final KnowledgeElementDTO kelm) {
+ OpenKnowledge open = _openKnowledge.open(kelm);
+
+ closeKnowledge(); // Just in case
+ _session.put(KNOWLEDGE_OPEN, open);
+ return open;
+ }
+
+ /**
+ * Open the given study in the current HTTP session. Replace the previose one in the session if any.
+ *
+ * @param study
+ * the study to open
+ * @return OpenStudy wrapper object
+ */
+ protected OpenStudy open(final Study study) {
+ OpenStudy open = _openStudy.open(getConnectedUser(), study); // The connected user may be null
+
+ closeStudy(); // Just in case
+ _session.put(STUDY_OPEN, open);
+ return open;
+ }
+
+ /**
+ * Initialization the Context for menubar and toolbar.
+ */
+ public void initializationContext() {
+ getMenuBarSettings().initializeInitialMenuProperties();
+
+ if (_session.get(STUDY_OPEN) == null) {
+ getMenuBarSettings().setIsStudyNull(true);
+ } else {
+ getMenuBarSettings().setIsStudyNull(false);
+
+ // for initialization ToolBarSettings.canUserEdit property
+ // and ToolBarSettings.isEnabledScript property.
+
+ OpenStudy currentStudy = (OpenStudy) _session.get(STUDY_OPEN);
+ PopupMenu popup = currentStudy.getPopup();
+ StudyRights user = currentStudy.getStudyRights();
+
+ if (user.canEditProperties()) {
+ getToolBarSettings().setCanUserEdit(true);
+ } else {
+ getToolBarSettings().setCanUserEdit(false);
+ }
+
+ if (popup == null) {
+ getToolBarSettings().setIsEnabledScript(false);
+ } else if (popup.isEnabled("script")) {
+ getToolBarSettings().setIsEnabledScript(true);
+ } else {
+ getToolBarSettings().setIsEnabledScript(false);
+ }
+ }
+
+ if (_session.get(KNOWLEDGE_OPEN) == null) {
+ getMenuBarSettings().setIsKnowledgeNull(true);
+ } else {
+ getMenuBarSettings().setIsKnowledgeNull(false);
+ }
+
+ ApplicationRights userRights = (ApplicationRights) _session
+ .get(USER_RIGHTS);
+
+ if ((userRights != null) && userRights.canCreateStudy()) {
+ getMenuBarSettings().setCanUserCreateStudy(true);
+ } else {
+ getMenuBarSettings().setCanUserCreateStudy(false);
+ }
+
+ if ((userRights != null) && userRights.canManageDatabase()) {
+ getMenuBarSettings().setCanUserManageDatabase(true);
+ } else {
+ getMenuBarSettings().setCanUserManageDatabase(false);
+ }
+
+ }