Salome HOME
Update copyrights 2014.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / OpenStudy.java
index 86aaf534495bbe544ec3204ed31d06ddc8c4fd9a..50e3c30dd09733f79980fa71b37f5f182c9004ad 100644 (file)
@@ -3,7 +3,7 @@ package org.splat.simer;
 /**
  * 
  * @author    Daniel Brunier-Coulin
- * @copyright OPEN CASCADE 2012
+ * @copyright OPEN CASCADE 2012-2014
  */
 
 import java.io.File;
@@ -14,7 +14,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.ResourceBundle;
 
-import org.apache.log4j.Logger;
 import org.splat.dal.bo.kernel.User;
 import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.DocumentType;
@@ -24,7 +23,9 @@ 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;
@@ -38,6 +39,7 @@ import org.splat.som.Step;
 import org.splat.som.StepRights;
 import org.splat.som.StudyRights;
 import org.splat.util.BeanHelper;
+import org.splat.wapp.Constants;
 import org.splat.wapp.ToolBar;
 
 /**
@@ -48,7 +50,7 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
        /**
         * Serial version ID.
         */
-       protected final static Logger LOGGER = org.splat.simer.Action.LOG;
+       protected final static AppLogger LOGGER = org.splat.simer.Action.LOG;
 
        /**
         * The currently open study.
@@ -62,9 +64,21 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
         * 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.
@@ -87,9 +101,9 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
         */
        private StudyService _studyService;
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Constructor
-       // ==============================================================================================================================
+       // =========================================================================
 
        /**
         * Open the given study in the current http session.
@@ -104,25 +118,34 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                ResourceBundle custom = ResourceBundle.getBundle("som",
                                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
+               if(!study.equals(_mystudy)) {
+                       _selection = "0.1"; // Default selection
+               }
                _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
+               _lasdate = datstring.format(_mystudy.getLastModificationDate());
                _description = _mystudy.getDescription();
-               _involving = new ArrayList<Step>(1);
+               //_involving = new ArrayList<Step>(1);
                _context = new ArrayList<SimulationContextFacade>();
-               _ustep = getProjectElementService().getFirstStep(_mystudy);
-               _ustep.setActor(_cuser);
-               _involving.add(_ustep);
+               if (_selection.equals("0.1")) {
+                       _ustep = getProjectElementService().getFirstStep(_mystudy);
+               }
+               _ustep.setActor(_cuser);        
+               if ((_involving.isEmpty()) || (_involving.get(0).getOwnerStudy().getIndex()
+                               != _ustep.getOwnerStudy().getIndex())) {
+                       _involving.clear();
+                       _involving.add(_ustep);
+               }
                for (Iterator<SimulationContext> i = _ustep.getAllSimulationContexts()
                                .iterator(); i.hasNext();) {
                        _context.add(new SimulationContextFacade(i.next(),
@@ -135,27 +158,45 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                        // 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");
+                                                       "steditableunmarkprivate");
                                } else {
                                        _popup = getApplicationSettings().getPopupMenu(
                                                        "steditableunmarkpublic");
                                }
                        } else {
-                               
+
                                if (_mystudy.isPublic()) {
                                        _popup = getApplicationSettings().getPopupMenu(
-                                               "steditablemarkprivate");
+                                                       "steditablemarkprivate");
                                } else {
-                                       _popup = getApplicationSettings().getPopupMenu(
-                                                       "steditablemarkpublic");
+                                       String key = null;
+                                       switch(_mystudy.getProgressState()) {
+                                               case inWORK:
+                                                       key = "steditable";
+                                                       break;
+                                               case inDRAFT:
+                                                       key = "streviewable";
+                                                       break;
+                                               case inCHECK:
+                                                       key = "stapprovable";
+                                                       break;
+                                               default: // APPROVED
+                                                       key = "steditablemarkpublic";
+                                                       break;
+                                       }
+                                       if(Long.valueOf(getSelectedScenarioId()) != 0) {
+                                               key += "ScenarioSelected";
+                                       }
+                                       _popup = getApplicationSettings().getPopupMenu(key);
                                }
 
                        }
-                       _popup.setContext("study", new StudyRights(_cuser, _mystudy));
+                       _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
+                                       _mystudy));
                }
                _urightstudy = new StudyRights(_cuser, _mystudy);
                _urightstep = new StepRights(_cuser, _ustep);
@@ -167,92 +208,184 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                return this;
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Getters
-       // ==============================================================================================================================
+       // =========================================================================
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getAuthorName()
+        */
        @Override
        public String getAuthorName() {
                return _mystudy.getAuthor().toString();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getIndex()
+        */
        @Override
        public Long getIndex() {
                return _mystudy.getIndex();
        }
 
+       /**
+        * Get creation date.
+        * 
+        * @return the date
+        */
        public String getDate() {
                return _credate;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.AbstractOpenObject#getMenu()
+        */
        @Override
        public StudyMenu getMenu() {
                return (StudyMenu) _menu;
        }
 
+       /**
+        * 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();
        }
 
+       /**
+        * Get last modification date.
+        * 
+        * @return the date
+        */
        public String getLastModificationDate() {
                return _lasdate;
        }
 
+       /**
+        * Get the toolbar with buttons of available modules.
+        * 
+        * @return the toolbar
+        */
        public ToolBar getModuleBar() {
                return getApplicationSettings().getModuleBar(getSelectedStep());
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getReference()
+        */
        @Override
        public String getReference() {
                return _mystudy.getReference();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.OpenStudyServices#getSelectedDocument()
+        */
        @Override
        public Publication getSelectedDocument() {
                return _selecdoc;
        }
 
+       /**
+        * Get user rights for the selected step.
+        * 
+        * @return user step rights
+        */
        public StepRights getSelectedStepRights() {
                return _urightstep;
        }
 
+       /**
+        * Get user rights for the study.
+        * 
+        * @return user study rights
+        */
        public StudyRights getStudyRights() {
                return _urightstudy;
        }
 
+       /**
+        * Get the detached study object.
+        * 
+        * @return the detached study object
+        */
        public Study getStudyObject() {
                return _mystudy;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getTitle()
+        */
        @Override
        public String getTitle() {
                return _mystudy.getTitle();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.dto.Proxy#getType()
+        */
        @Override
        public String getType() {
-               /*
-                * RKV: return ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()).getString( "label.study");
-                */
-               return "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;
        }
 
+       /**
+        * 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();
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Public services
-       // ==============================================================================================================================
+       // =========================================================================
 
        @Override
        public URL newTemplateBasedDocument(final String typename, final User author) {
@@ -291,7 +424,8 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                        ResourceBundle locale = ResourceBundle.getBundle("som",
                                        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();) {
@@ -305,7 +439,7 @@ public class OpenStudy extends AbstractOpenObject 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());
@@ -337,6 +471,12 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                }
        }
 
+       /**
+        * 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;
@@ -351,24 +491,39 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                setupContents(); // The contents may have changed even if the selection is the same
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Protected services
-       // ==============================================================================================================================
+       // =========================================================================
 
+       /**
+        * 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(),
                                getApplicationSettings());
                boolean first = (_contents.size() == 0);
 
-               docpres.put(doc.getIndex(), facade);
+               _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);
+               }
        }
 
+       /**
+        * 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(), getApplicationSettings());
@@ -376,6 +531,12 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                _context.add(facade);
        }
 
+       /**
+        * 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),
@@ -392,11 +553,17 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                        }
                }
                if (known != null) { // RKV:End
-                       knowpres.put(kelm.getIndex(), facade);
+                       _knowpres.put(kelm.getIndex(), facade);
                        known._list.add(facade); // Insert the new knowledge at the end of the corresponding knowledge type
                }
        }
 
+       /**
+        * 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();
@@ -410,12 +577,20 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                }
        }
 
+       /**
+        * 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("study", new StudyRights(_cuser, _mystudy));
+                       _popup = getApplicationSettings().getPopupMenu(
+                                       "steditablemarkpublic");
+                       _popup.setContext(Constants.STUDY_MENU, new StudyRights(_cuser,
+                                       _mystudy));
                }
                // ustep = getProjectElementService().getFirstStep(mystudy);
                if (_ustep != null) {
@@ -425,18 +600,29 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                _urightstep = new StepRights(_cuser, _ustep);
        }
 
+       /**
+        * 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;
+                       if (facade.isFacadeOf(contex)) {
+                               i.remove();
+                               break;
                        }
-                       i.remove();
-                       break;
                }
        }
 
+       /**
+        * 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));
@@ -451,7 +637,7 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                        }
                }
                if (known != null) { // RKV:End
-                       knowpres.remove(kelm.getIndex());
+                       _knowpres.remove(kelm.getIndex());
                        for (Iterator<KnowledgeElementFacade> i = known._list.iterator(); i
                                        .hasNext();) {
                                KnowledgeElementFacade facade = i.next();
@@ -465,20 +651,40 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                }
        }
 
+       /**
+        * Refresh the document presentation facade.
+        * 
+        * @param doc
+        *            the document publication
+        */
        protected void update(final Publication doc) {
-               DocumentFacade facade = docpres.get(doc.getIndex());
+               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);
                }
        }
 
+       /**
+        * Refresh the knowledge element presentation facade.
+        * 
+        * @param kelm
+        *            the knowledge element DTO
+        */
        protected void update(final KnowledgeElementDTO kelm) {
-               KnowledgeElementFacade facade = knowpres.get(kelm.getIndex());
+               KnowledgeElementFacade facade = _knowpres.get(kelm.getIndex());
                if (facade != null) {
                        facade.refresh(kelm);
                }
        }
 
+       /**
+        * Refresh simulation contexts presentation facades.
+        */
        protected void updateSimulationContexts() {
                _context.clear();
                for (Iterator<Step> i = _involving.iterator(); i.hasNext();) {
@@ -491,9 +697,9 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                }
        }
 
-       // ==============================================================================================================================
+       // =========================================================================
        // Private services
-       // ==============================================================================================================================
+       // =========================================================================
 
        private void setupPreviousToSelectedSteps() {
                String[] item = _selection.split("\\x2E");
@@ -521,10 +727,9 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                step = getProjectElementService().getSteps(_mystudy);
                for (int i = step.length - 1; i > -1; i--) {
                        Step firstep = step[i];
-                       if (firstep.getNumber() > base) {
-                               continue;
+                       if (firstep.getNumber() <= base) {
+                               _involving.add(0, firstep);
                        }
-                       _involving.add(0, firstep);
                }
        }
 
@@ -624,11 +829,31 @@ public class OpenStudy extends AbstractOpenObject implements OpenStudyServices {
                _documentService = documentService;
        }
 
+       /**
+        * Get the detached study object.
+        * 
+        * @return the detached study object
+        */
        public Study getMystudy() {
                return _mystudy;
        }
 
+       /**
+        * Set the study to present.
+        * 
+        * @param mystudy
+        *            the selected study detached object
+        */
        public void setMystudy(final Study mystudy) {
                this._mystudy = mystudy;
        }
+
+       /**
+        * Update the current step.
+        *
+        */
+       public final void updateCurrentStep() {
+               setupPreviousToSelectedSteps();
+               _ustep = _involving.get(_involving.size() - 1);
+       }
 }
\ No newline at end of file