Salome HOME
- Set up-to date functionality is implemented;
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / OpenStudy.java
index b28bc689d0ea50cbf96e0cca4caed2f72a8a03a5..cad212ae8a2be79810024c0f06957ca00138ae8c 100644 (file)
@@ -14,44 +14,72 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.ResourceBundle;
 
-import org.apache.log4j.Logger;
-import org.splat.kernel.Do;
 import org.splat.dal.bo.kernel.User;
-import org.splat.manox.Toolbox;
-import org.splat.manox.Writer;
 import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.DocumentType;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.ProgressState;
+import org.splat.dal.bo.som.Publication;
+import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.UsesRelation;
+import org.splat.kernel.Do;
+import org.splat.log.AppLogger;
+import org.splat.manox.Toolbox;
+import org.splat.manox.Writer;
 import org.splat.service.DocumentService;
 import org.splat.service.DocumentTypeService;
 import org.splat.service.StepService;
 import org.splat.service.StudyService;
+import org.splat.service.dto.KnowledgeElementDTO;
 import org.splat.service.technical.RepositoryService;
-import org.splat.dal.bo.som.Publication;
 import org.splat.som.Revision;
-import org.splat.dal.bo.som.Scenario;
-import org.splat.dal.bo.som.SimulationContext;
 import org.splat.som.Step;
 import org.splat.som.StepRights;
-import org.splat.dal.bo.som.Study;
 import org.splat.som.StudyRights;
+import org.splat.util.BeanHelper;
+import org.splat.wapp.Constants;
 import org.splat.wapp.ToolBar;
 
-public class OpenStudy extends OpenObject implements OpenStudyServices {
+/**
+ * Presentation of the currently open study.
+ */
+public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
 
        /**
         * Serial version ID.
         */
-       protected final static Logger logger = org.splat.simer.Action.logger;
+       protected final static AppLogger LOGGER = org.splat.simer.Action.LOG;
 
-       private Study mystudy;
-       private StudyRights urightstudy; // User rights on the open study
-       private StepRights urightstep; // User rights on the selected step
-       private String version;
-       private String credate;
-       private String lasdate;
-       private Publication selecdoc;
+       /**
+        * The currently open study.
+        */
+       private transient Study _mystudy;
+       /**
+        * User rights on the open study.
+        */
+       private transient StudyRights _urightstudy;
+       /**
+        * User rights on the selected step.
+        */
+       private transient StepRights _urightstep;
+       /**
+        * The study version.
+        */
+       private transient String _version;
+       /**
+        * The study creation date.
+        */
+       private transient String _credate;
+       /**
+        * The study modification date.
+        */
+       private transient String _lasdate;
+       /**
+        * The selected document publication.
+        */
+       private transient Publication _selecdoc;
        /**
         * Injected step service.
         */
@@ -73,9 +101,9 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
         */
        private StudyService _studyService;
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Constructor
-       // ==============================================================================================================================
+       // =========================================================================
 
        /**
         * Open the given study in the current http session.
@@ -86,157 +114,276 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
         *            the study to open
         * @return this open study object
         */
-       public OpenStudy open(User user, Study study) {
-               // -----------------------------------------
+       public OpenStudy open(final User user, final Study study) {
                ResourceBundle custom = ResourceBundle.getBundle("som",
-                               ApplicationSettings.getCurrentLocale());
+                               getApplicationSettings().getCurrentLocale());
                SimpleDateFormat datstring = new SimpleDateFormat(custom
-                               .getString("date.format"));
+                               .getString("date.format"), getApplicationSettings()
+                               .getCurrentLocale());
                Revision.Format verstring = new Revision.Format(getProjectSettings()
                                .getRevisionPattern());
 
-               cuser = user; // May be null if nobody connected
-               mystudy = study;
-               selection = "0.1"; // Default selection
-               selecdoc = null;
+               _cuser = user; // May be null if nobody connected
+               _mystudy = study;
+               _selection = "0.1"; // Default selection
+               _selecdoc = null;
 
                // Preparation of the display
-               version = verstring.format(mystudy.getVersion());
-               credate = datstring.format(mystudy.getDate());
-               lasdate = ""; // Not yet supported
-               description = mystudy.getDescription();
-               involving = new ArrayList<Step>(1);
-               context = new ArrayList<SimulationContextFacade>();
-               ustep = getProjectElementService().getFirstStep(mystudy);
-               ustep.setActor(cuser);
-               involving.add(ustep);
-               for (Iterator<SimulationContext> i = ustep.getAllSimulationContexts()
+               _version = verstring.format(_mystudy.getVersion());
+               _credate = datstring.format(_mystudy.getDate());
+               _lasdate = datstring.format(_mystudy.getLastModificationDate());
+               _description = _mystudy.getDescription();
+               _involving = new ArrayList<Step>(1);
+               _context = new ArrayList<SimulationContextFacade>();
+               _ustep = getProjectElementService().getFirstStep(_mystudy);
+               _ustep.setActor(_cuser);
+               _involving.add(_ustep);
+               for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
                                .iterator(); i.hasNext();) {
-                       context.add(new SimulationContextFacade(i.next(),
-                                       getProjectSettings().getAllSteps()));
+                       _context.add(new SimulationContextFacade(i.next(),
+                                       getProjectSettings().getAllSteps(),
+                                       getApplicationSettings()));
                }
-               if (getStudyService().isStaffedBy(mystudy, cuser) || getStudyService().hasActor(mystudy, cuser)) {
+               if (getStudyService().isStaffedBy(_mystudy, _cuser)
+                               || getStudyService().hasActor(_mystudy, _cuser)) {
                        // ProgressState state = mystudy.getProgressState();
-                       // if (state == ProgressState.inCHECK) popup = ApplicationSettings.getPopupMenu("stapprovable");
-                       // else if (state == ProgressState.APPROVED) popup = ApplicationSettings.getPopupMenu("stapproved");
-                       /* else */popup = ApplicationSettings
-                                       .getPopupMenu("steditable");
-                       popup.setContext("study", new StudyRights(cuser, mystudy));
+                       // if (state == ProgressState.inCHECK) popup = getApplicationSettings().getPopupMenu("stapprovable");
+                       // else if (state == ProgressState.APPROVED) popup = getApplicationSettings().getPopupMenu("stapproved");
+                       /* else */
+
+                       if (_mystudy.getProgressState() == ProgressState.TEMPLATE) {
+                               if (_mystudy.isPublic()) {
+                                       _popup = getApplicationSettings().getPopupMenu(
+                                                       "steditableunmarkprivate");
+                               } else {
+                                       _popup = getApplicationSettings().getPopupMenu(
+                                                       "steditableunmarkpublic");
+                               }
+                       } else {
+
+                               if (_mystudy.isPublic()) {
+                                       _popup = getApplicationSettings().getPopupMenu(
+                                                       "steditablemarkprivate");
+                               } else {
+                                       if (_mystudy.getProgressState() == ProgressState.inWORK) {
+                                               _popup = getApplicationSettings().getPopupMenu(
+                                                               "steditable");
+                                       } else if (_mystudy.getProgressState() == ProgressState.inDRAFT) {
+                                               _popup = getApplicationSettings().getPopupMenu(
+                                                               "streviewable");
+                                       } else if (_mystudy.getProgressState() == ProgressState.inCHECK) {
+                                               _popup = getApplicationSettings().getPopupMenu(
+                                                               "stapprovable");
+                                       } else { // APPROVED
+                                               _popup = getApplicationSettings().getPopupMenu(
+                                                               "steditablemarkpublic");
+                                       }
+                               }
+
+                       }
+                       _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
+                                       _mystudy));
                }
-               urightstudy = new StudyRights(cuser, mystudy);
-               urightstep = new StepRights(cuser, ustep);
+               _urightstudy = new StudyRights(_cuser, _mystudy);
+               _urightstep = new StepRights(_cuser, _ustep);
 
                // RKV menu = new StudyMenu(mystudy);
-               menu = ((StudyMenu) getMenu()).init(mystudy); // RKV
-               menu.selects(selection); // Initializes menu items to be displayed
+               _menu = (getMenu()).init(_mystudy); // RKV
+               _menu.selects(_selection); // Initializes menu items to be displayed
                setupContents(); // Initializes documents and knowledge at ustep
                return this;
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Getters
-       // ==============================================================================================================================
+       // =========================================================================
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getAuthorName()
+        */
+       @Override
        public String getAuthorName() {
-               // -----------------------------
-               return mystudy.getAuthor().toString();
+               return _mystudy.getAuthor().toString();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getIndex()
+        */
+       @Override
        public Long getIndex() {
-               // -------------------------
-               return mystudy.getIndex();
+               return _mystudy.getIndex();
        }
 
+       /**
+        * Get creation date.
+        * 
+        * @return the date
+        */
        public String getDate() {
-               // ------------------------
-               return credate;
+               return _credate;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.AbstractOpenObject#getMenu()
+        */
+       @Override
        public StudyMenu getMenu() {
-               // ---------------------------
-               return (StudyMenu) menu;
+               return (StudyMenu) _menu;
        }
 
-       public void setMenu(StudyMenu aMenu) {
-               menu = aMenu;
+       /**
+        * Set study menu.
+        * 
+        * @param aMenu
+        *            the study menu
+        */
+       public void setMenu(final StudyMenu aMenu) {
+               _menu = aMenu;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getProgressState()
+        */
+       @Override
        public ProgressState getProgressState() {
-               // ----------------------------------------
-               return mystudy.getProgressState();
+               return _mystudy.getProgressState();
        }
 
+       /**
+        * Get last modification date.
+        * 
+        * @return the date
+        */
        public String getLastModificationDate() {
-               // ----------------------------------------
-               return lasdate;
+               return _lasdate;
        }
 
+       /**
+        * Get the toolbar with buttons of available modules.
+        * 
+        * @return the toolbar
+        */
        public ToolBar getModuleBar() {
-               // ------------------------------
-               return ApplicationSettings.getMe().getModuleBar(getSelectedStep());
+               return getApplicationSettings().getModuleBar(getSelectedStep());
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getReference()
+        */
+       @Override
        public String getReference() {
-               // ----------------------------
-               return mystudy.getReference();
+               return _mystudy.getReference();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.OpenStudyServices#getSelectedDocument()
+        */
+       @Override
        public Publication getSelectedDocument() {
-               // -----------------------------------------
-               return selecdoc;
+               return _selecdoc;
        }
 
+       /**
+        * Get user rights for the selected step.
+        * 
+        * @return user step rights
+        */
        public StepRights getSelectedStepRights() {
-               // ------------------------------------------
-               return urightstep;
+               return _urightstep;
        }
 
+       /**
+        * Get user rights for the study.
+        * 
+        * @return user study rights
+        */
        public StudyRights getStudyRights() {
-               // ------------------------------------
-               return urightstudy;
+               return _urightstudy;
        }
 
+       /**
+        * Get the detached study object.
+        * 
+        * @return the detached study object
+        */
        public Study getStudyObject() {
-               // ------------------------------
-               return mystudy;
+               return _mystudy;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getTitle()
+        */
+       @Override
        public String getTitle() {
-               // ------------------------
-               return mystudy.getTitle();
+               return _mystudy.getTitle();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getType()
+        */
+       @Override
        public String getType() {
-               // ------------------------
-               return ResourceBundle.getBundle("labels",
-                               ApplicationSettings.getCurrentLocale())
-                               .getString("label.study");
+               return Constants.STUDY_MENU;
+       }
+       
+       /** 
+        * {@inheritDoc}
+        * @see org.splat.simer.AbstractOpenObject#getTypeName()
+        */
+       @Override
+       public String getTypeName() {
+               return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
        }
 
+       /**
+        * Get the study version.
+        * 
+        * @return the version string
+        */
        public String getVersion() {
-               // ---------------------------
-               return version;
+               return _version;
        }
 
+       /**
+        * Check if the selected step is enabled for writing.
+        * 
+        * @return true if the selected step is enabled for writing
+        */
        public boolean isStepEnabled() {
-               // -------------------------------
-               return urightstep.isEnabled();
+               return _urightstep.isEnabled();
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Public services
-       // ==============================================================================================================================
+       // =========================================================================
 
-       public URL newTemplateBasedDocument(String typename, User author) {
-               // ------------------------------------------------------------------
+       @Override
+       public URL newTemplateBasedDocument(final String typename, final User author) {
                String filename = typename + ".xml"; // Only XML templates are writeable
                File template = new File(getRepositoryService().getTemplatePath()
                                + filename);
-               if (!template.exists())
+               if (!template.exists()) {
                        return null;
+               }
 
-//             Session connex = Database.getCurSession();
-//             Transaction transax = connex.beginTransaction();
+               // Session connex = Database.getCurSession();
+               // Transaction transax = connex.beginTransaction();
                try {
                        File udir = getRepositoryService().getDownloadDirectory(author);
                        File credoc = new File(udir.getPath() + "/" + filename);
@@ -248,20 +395,23 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
                        Document medoc = getStepService().createDocument(step,
                                        dprop.setType(type).setFormat("xml").setAuthor(author))
                                        .value();
-//                     transax.commit();
+                       // transax.commit();
 
                        // Instantiation of the template into the user download directory
-                       if (!udir.exists())
+                       if (!udir.exists()) {
                                udir.mkdir();
-                       if (credoc.exists())
+                       }
+                       if (credoc.exists()) {
                                credoc.delete();
+                       }
                        Do.copy(template, credoc);
 
                        // Transfer to the document of all known properties
                        ResourceBundle locale = ResourceBundle.getBundle("som",
-                                       ApplicationSettings.getCurrentLocale());
+                                       getApplicationSettings().getCurrentLocale());
                        SimpleDateFormat get = new SimpleDateFormat(locale
-                                       .getString("date.format"));
+                                       .getString("date.format"), getApplicationSettings()
+                                       .getCurrentLocale());
                        Writer tool = Toolbox.getWriter(credoc);
                        List<Step> slist = getInvolvedSteps();
                        for (Iterator<Step> i = slist.iterator(); i.hasNext();) {
@@ -275,7 +425,7 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
                                }
                        }
                        tool.updateProperty("reference", medoc.getReference());
-                       tool.updateProperty("study", mystudy.getTitle());
+                       tool.updateProperty(Constants.STUDY_MENU, _mystudy.getTitle());
                        tool.updateProperty("step", locale.getString(
                                        "folder.step." + step.getNumber()).replaceAll("''", "'"));
                        tool.updateProperty("author", author.getUsername().toUpperCase());
@@ -284,77 +434,104 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
                                        .replaceAll("''", "'"));
                        tool.save();
 
-                       return new URL(ApplicationSettings.getDownloadURL(author)
+                       return new URL(getApplicationSettings().getDownloadURL(author)
                                        + filename);
                } catch (Exception saverror) {
-                       logger.error("Reason:", saverror);
+                       LOGGER.error("Reason:", saverror);
                        return null;
                }
        }
 
-       public void selectDocument(String docurl) {
-               // ------------------------------------------
-               String prefix = ApplicationSettings.getRepositoryURL();
+       public void selectDocument(final String docurl) {
+               String prefix = getApplicationSettings().getRepositoryURL();
 
-               if (docurl.startsWith(prefix))
+               if (docurl.startsWith(prefix)) {
                        try {
                                String path = docurl.substring(prefix.length());
                                Document value = getDocumentService().getDocumentByPath(path);
 
-                               selecdoc = ustep.getDocument(value.getIndex());
+                               _selecdoc = _ustep.getDocument(value.getIndex());
                        } catch (Exception error) {
-                               logger.error("Reason:", error);
+                               LOGGER.error("Reason:", error);
                        }
+               }
        }
 
-       public void setSelection(String step) {
-               // --------------------------------------
-               if (!step.equals(selection)) {
-                       selection = step;
-                       selecdoc = null;
+       /**
+        * Select an activity (step).
+        * 
+        * @param step
+        *            the key of the step to select
+        */
+       public void setSelection(final String step) {
+               if (!step.equals(_selection)) {
+                       _selection = step;
+                       _selecdoc = null;
                        setupPreviousToSelectedSteps();
                        updateSimulationContexts(); // Initializes contexts according to the selected steps
                }
-               ustep = involving.get(involving.size() - 1);
-               urightstep = new StepRights(cuser, ustep);
-               ustep.setActor(cuser);
-               menu.selects(selection); // Updates menu items to be displayed
+               _ustep = _involving.get(_involving.size() - 1);
+               _urightstep = new StepRights(_cuser, _ustep);
+               _ustep.setActor(_cuser);
+               _menu.selects(_selection); // Updates menu items to be displayed
                setupContents(); // The contents may have changed even if the selection is the same
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Protected services
-       // ==============================================================================================================================
+       // =========================================================================
 
-       protected void add(Publication doc) {
-               // ------------------------------------
+       /**
+        * Add a new document presentation facade.
+        * 
+        * @param doc
+        *            the document publication
+        */
+       protected void add(final Publication doc) {
                DocumentFacade facade = new DocumentFacade(this, doc,
-                               getProjectSettings(), getPublicationService());
-               boolean first = (contents.size() == 0);
+                               getProjectSettings(), getPublicationService(),
+                               getApplicationSettings());
+               boolean first = (_contents.size() == 0);
 
-               docpres.put(doc.getIndex(), facade);
-               contents.add(0, facade); // Prepend the new publication
-               if (first)
+               _docpres.put(doc.getIndex(), facade);
+               _contents.add(0, facade); // Prepend the new publication
+               if (first) {
                        this.getMenu().refreshSelectedItem();
-
+               }
+               // Refresh dependencies. They can not be removed until removing this document.
+               for (Publication pub : doc.getRelations(UsesRelation.class)) {
+                       update(pub);
+               }
        }
 
-       protected void add(SimulationContext contex) {
-               // ---------------------------------------------
+       /**
+        * Add a simulation context presentation facade.
+        * 
+        * @param contex
+        *            the simulation context to add
+        */
+       protected void add(final SimulationContext contex) {
                SimulationContextFacade facade = new SimulationContextFacade(contex,
-                               getProjectSettings().getAllSteps());
+                               getProjectSettings().getAllSteps(), getApplicationSettings());
 
-               context.add(facade);
+               _context.add(facade);
        }
 
-       protected void add(KnowledgeElement kelm) {
-               // ------------------------------------------
-               KnowledgeElementFacade facade = new KnowledgeElementFacade(kelm);
+       /**
+        * Add a knowledge element presentation facade.
+        * 
+        * @param kelm
+        *            the knowledge element to add
+        */
+       protected void add(final KnowledgeElement kelm) {
+               KnowledgeElementFacade facade = new KnowledgeElementFacade(BeanHelper
+                               .copyBean(kelm, KnowledgeElementDTO.class),
+                               getApplicationSettings());
                // RKV KnowledgeIterator known = knowledge.get(kelm.getType().getIndex() - 2);
                // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
                // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
                KnowledgeIterator known = null;
-               for (KnowledgeIterator aKnowledgeSection : knowledge) {
+               for (KnowledgeIterator aKnowledgeSection : _knowledge) {
                        if (aKnowledgeSection.getIndex().equals(
                                        String.valueOf(kelm.getType().getIndex()))) {
                                known = aKnowledgeSection;
@@ -362,129 +539,183 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
                        }
                }
                if (known != null) { // RKV:End
-                       knowpres.put(kelm.getIndex(), facade);
-                       known.list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
+                       _knowpres.put(kelm.getIndex(), facade);
+                       known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
                }
        }
 
-       protected void remove(Publication doctag) {
-               // ------------------------------------------
-               for (Iterator<DocumentFacade> i = contents.iterator(); i.hasNext();) {
+       /**
+        * Remove the document presentation facade.
+        * 
+        * @param doctag
+        *            the document publication to remove
+        */
+       protected void remove(final Publication doctag) {
+               for (Iterator<DocumentFacade> i = _contents.iterator(); i.hasNext();) {
                        DocumentFacade facade = i.next();
-                       if (!facade.isFacadeOf(doctag))
-                               continue;
-                       i.remove();
-                       break;
+                       if (facade.isFacadeOf(doctag)) {
+                               i.remove();
+                               break;
+                       }
                }
-               if (contents.size() == 0)
+               if (_contents.size() == 0) {
                        this.getMenu().refreshSelectedItem();
+               }
        }
 
-       protected void changeUser(User user) {
-               // -------------------------------------
-               cuser = user;
-               popup = null;
-               if (getStudyService().isStaffedBy(mystudy, cuser)) {
-                       popup = ApplicationSettings.getPopupMenu("steditable");
-                       popup.setContext("study", new StudyRights(cuser, mystudy));
+       /**
+        * Change the currently connected user and refresh user's rights.
+        * 
+        * @param user
+        *            the new connected user
+        */
+       protected void changeUser(final User user) {
+               _cuser = user;
+               _popup = null;
+               if (getStudyService().isStaffedBy(_mystudy, _cuser)) {
+                       _popup = getApplicationSettings().getPopupMenu(
+                                       "steditablemarkpublic");
+                       _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
+                                       _mystudy));
                }
-               //ustep = getProjectElementService().getFirstStep(mystudy);
-               if (ustep != null) {
-                       ustep.setActor(cuser);
+               // ustep = getProjectElementService().getFirstStep(mystudy);
+               if (_ustep != null) {
+                       _ustep.setActor(_cuser);
                }
-               urightstudy = new StudyRights(cuser, mystudy);
-               urightstep = new StepRights(cuser, ustep);
+               _urightstudy = new StudyRights(_cuser, _mystudy);
+               _urightstep = new StepRights(_cuser, _ustep);
        }
 
-       protected void remove(SimulationContext contex) {
-               // ------------------------------------------------
-               for (Iterator<SimulationContextFacade> i = context.iterator(); i
+       /**
+        * Remove the simulation context presentation facade.
+        * 
+        * @param contex
+        *            the simulation context to remove
+        */
+       protected void remove(final SimulationContext contex) {
+               for (Iterator<SimulationContextFacade> i = _context.iterator(); i
                                .hasNext();) {
                        SimulationContextFacade facade = i.next();
-                       if (!facade.isFacadeOf(contex))
-                               continue;
-                       i.remove();
-                       break;
+                       if (facade.isFacadeOf(contex)) {
+                               i.remove();
+                               break;
+                       }
                }
        }
 
-       protected void remove(KnowledgeElement kelm) {
-               // ---------------------------------------------
-               KnowledgeIterator known = knowledge.get((int) (kelm.getType()
-                               .getIndex() - 2));
+       /**
+        * Remove the knowledge element presentation facade.
+        * 
+        * @param kelm
+        *            the knowledge element to remove
+        */
+       protected void remove(final KnowledgeElement kelm) {
+               // RKV: KnowledgeIterator known = _knowledge.get((int) (kelm.getType()
+               // RKV: .getIndex() - 2));
                // Knowledges are ordered by type index, from 0 to n-1, the first one being reserved (reason for -2)
-               knowpres.remove(kelm.getIndex());
-               for (Iterator<KnowledgeElementFacade> i = known.list.iterator(); i
-                               .hasNext();) {
-                       KnowledgeElementFacade facade = i.next();
-                       if (!facade.isFacadeOf(kelm))
-                               continue;
-                       i.remove();
-                       break;
+               // RKV:Begin: Find a knowledge iterator for appropriate knowledge type
+               KnowledgeIterator known = null;
+               for (KnowledgeIterator aKnowledgeSection : _knowledge) {
+                       if (aKnowledgeSection.getIndex().equals(
+                                       String.valueOf(kelm.getType().getIndex()))) {
+                               known = aKnowledgeSection;
+                               break;
+                       }
+               }
+               if (known != null) { // RKV:End
+                       _knowpres.remove(kelm.getIndex());
+                       for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
+                                       .hasNext();) {
+                               KnowledgeElementFacade facade = i.next();
+                               if (!facade.isFacadeOf(BeanHelper.copyBean(kelm,
+                                               KnowledgeElementDTO.class))) {
+                                       continue;
+                               }
+                               i.remove();
+                               break;
+                       }
                }
        }
 
-       protected void update(Publication doc) {
-               // ---------------------------------------
-               DocumentFacade facade = docpres.get(doc.getIndex());
+       /**
+        * Refresh the document presentation facade.
+        * 
+        * @param doc
+        *            the document publication
+        */
+       protected void update(final Publication doc) {
+               DocumentFacade facade = _docpres.get(doc.getIndex());
                if (facade != null) {
-                       facade.refresh();
+                       _contents.remove(facade);
+                       facade = new DocumentFacade(this, doc,
+                                       getProjectSettings(), getPublicationService(),
+                                       getApplicationSettings());
+                       _docpres.put(doc.getIndex(), facade);
+                       _contents.add(facade);
                }
        }
 
-       protected void update(KnowledgeElement kelm) {
-               // ---------------------------------------------
-               KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
+       /**
+        * Refresh the knowledge element presentation facade.
+        * 
+        * @param kelm
+        *            the knowledge element DTO
+        */
+       protected void update(final KnowledgeElementDTO kelm) {
+               KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
                if (facade != null) {
-                       facade.refresh();
+                       facade.refresh(kelm);
                }
        }
 
+       /**
+        * Refresh simulation contexts presentation facades.
+        */
        protected void updateSimulationContexts() {
-               // ------------------------------------------
-               context.clear();
-               for (Iterator<Step> i = involving.iterator(); i.hasNext();) {
+               _context.clear();
+               for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
                        for (Iterator<SimulationContext> j = i.next()
                                        .getAllSimulationContexts().iterator(); j.hasNext();) {
-                               context.add(new SimulationContextFacade(j.next(),
-                                               getProjectSettings().getAllSteps()));
+                               _context.add(new SimulationContextFacade(j.next(),
+                                               getProjectSettings().getAllSteps(),
+                                               getApplicationSettings()));
                        }
                }
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Private services
-       // ==============================================================================================================================
+       // =========================================================================
 
        private void setupPreviousToSelectedSteps() {
-               // --------------------------------------------
-               String[] item = selection.split("\\x2E");
+               String[] item = _selection.split("\\x2E");
                int major = Integer.valueOf(item[0]);
                int minor = Integer.valueOf(item[1]);
                int base = minor;
                Step[] step;
 
-               involving.clear();
+               _involving.clear();
                if (major > 0) {
-                       Scenario[] branch = mystudy.getScenarii();
+                       Scenario[] branch = _mystudy.getScenarii();
                        Scenario scenar = branch[0];
                        for (int i = 0; i < branch.length; i++) {
                                scenar = branch[i];
-                               if (scenar.getIndex() == major)
+                               if (scenar.getIndex() == major) {
                                        break; // Supposed exist
+                               }
                        }
                        step = getProjectElementService().getSteps(scenar);
                        base = step[0].getNumber() - 1;
                        for (int i = 0; i + base < minor; i++) {
-                               involving.add(step[i]);
+                               _involving.add(step[i]);
                        }
                }
-               step = getProjectElementService().getSteps(mystudy);
+               step = getProjectElementService().getSteps(_mystudy);
                for (int i = step.length - 1; i > -1; i--) {
                        Step firstep = step[i];
-                       if (firstep.getNumber() > base)
-                               continue;
-                       involving.add(0, firstep);
+                       if (firstep.getNumber() <= base) {
+                               _involving.add(0, firstep);
+                       }
                }
        }
 
@@ -503,7 +734,7 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
         * @param stepService
         *            the stepService to set
         */
-       public void setStepService(StepService stepService) {
+       public void setStepService(final StepService stepService) {
                _stepService = stepService;
        }
 
@@ -522,12 +753,13 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
         * @param repositoryService
         *            the repositoryService to set
         */
-       public void setRepositoryService(RepositoryService repositoryService) {
+       public void setRepositoryService(final RepositoryService repositoryService) {
                _repositoryService = repositoryService;
        }
 
        /**
         * Get the documentTypeService.
+        * 
         * @return the documentTypeService
         */
        public DocumentTypeService getDocumentTypeService() {
@@ -536,9 +768,12 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
 
        /**
         * Set the documentTypeService.
-        * @param documentTypeService the documentTypeService to set
+        * 
+        * @param documentTypeService
+        *            the documentTypeService to set
         */
-       public void setDocumentTypeService(DocumentTypeService documentTypeService) {
+       public void setDocumentTypeService(
+                       final DocumentTypeService documentTypeService) {
                _documentTypeService = documentTypeService;
        }
 
@@ -557,12 +792,13 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
         * @param studyService
         *            the studyService to set
         */
-       public void setStudyService(StudyService studyService) {
+       public void setStudyService(final StudyService studyService) {
                _studyService = studyService;
        }
 
        /**
         * Get the documentService.
+        * 
         * @return the documentService
         */
        public DocumentService getDocumentService() {
@@ -571,17 +807,30 @@ public class OpenStudy extends OpenObject implements OpenStudyServices {
 
        /**
         * Set the documentService.
-        * @param documentService the documentService to set
+        * 
+        * @param documentService
+        *            the documentService to set
         */
-       public void setDocumentService(DocumentService documentService) {
+       public void setDocumentService(final DocumentService documentService) {
                _documentService = documentService;
        }
-       
+
+       /**
+        * Get the detached study object.
+        * 
+        * @return the detached study object
+        */
        public Study getMystudy() {
-               return mystudy;
+               return _mystudy;
        }
 
-       public void setMystudy(Study mystudy) {
-               this.mystudy = mystudy;
+       /**
+        * Set the study to present.
+        * 
+        * @param mystudy
+        *            the selected study detached object
+        */
+       public void setMystudy(final Study mystudy) {
+               this._mystudy = mystudy;
        }
 }
\ No newline at end of file