Salome HOME
- Set up-to date functionality is implemented;
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / DocumentFacade.java
index e112a482b52b2b045c09928905b93bb8d9c9aea3..accc56b79e994100c74df290d1a9a453e3d7acbf 100644 (file)
@@ -10,7 +10,6 @@ import java.io.File;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.ResourceBundle;
 
@@ -51,7 +50,7 @@ public class DocumentFacade implements HistoryFacade {
        /**
         * Published document.
         */
-       private final transient Document _my;
+       private final transient Document _mydoc;
        /**
         * Document state.
         */
@@ -61,7 +60,9 @@ public class DocumentFacade implements HistoryFacade {
         */
        private transient String _version;
        /**
-        * Presentation state.
+        * Presentation state: open, deep open, closed.
+        * 
+        * @see DocumentFacade.State
         */
        private transient State _display;
        /**
@@ -167,8 +168,8 @@ public class DocumentFacade implements HistoryFacade {
                setApplicationSettings(applicationSettings);
                _owner = opened;
                _me = represented;
-               _my = _me.value();
-               _state = _my.getProgressState();
+               _mydoc = _me.value();
+               _state = _mydoc.getProgressState();
                _display = State.closed;
 
                refresh(); // Initializes the presentation of my document //NOPMD:RKV: to be reviewed
@@ -198,17 +199,13 @@ public class DocumentFacade implements HistoryFacade {
                setApplicationSettings(applicationSettings);
                _owner = opened;
                _me = null; // Marks the history context
-               _my = represented;
-               _state = _my.getProgressState(); // In reality, HISTORY
+               _mydoc = represented;
+               _state = _mydoc.getProgressState(); // In reality, HISTORY
                _display = State.open; // Because the given document is a history document
                _description = ResourceBundle.getBundle("som",
                                applicationSettings.getCurrentLocale()).getString(
                                "history.creation")
-                               + " " + _my.getAuthor().toString();
-               _uses = null;
-               _exports = null;
-               _history = null;
-               _popup = null;
+                               + " " + _mydoc.getAuthor().toString();
 
                this.refresh(); // Initializes the presentation of my document
        }
@@ -223,40 +220,40 @@ public class DocumentFacade implements HistoryFacade {
        public void develop() {
                if (_display == State.open) { // Opening the history of document, if exist
                        if (_history.isEmpty()) {
-                               collectHistory(_my);
+                               collectHistory(_mydoc);
                        }
                        _display = State.deepopen;
                } else { // Opening the document
                        if (_uses == null) {
-                               List<Publication> relist = _me.getRelations(UsesRelation.class);
+                               List<Relation> relist = _me.value().getRelations(UsesRelation.class);
 
                                _uses = new ArrayList<DocumentFacade>(relist.size());
-                               for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
-                                       Publication used = i.next();
-                                       long index = used.getIndex();
-                                       DocumentFacade facade = _owner._docpres.get(index);
+                               for (Relation relation : relist) {
+                                       Document used = ((UsesRelation)relation).getTo();
+                                       
+                                       DocumentFacade facade = null;
+                                       
+                                       Publication publication = _me.getOwner().getPublication(used);
+                                       if(publication == null) {
+                                               publication = _me.getOwnerStudy().getPublication(used);
+                                       }
+                                       if(publication != null) {
+                                               facade = _owner._docpres.get(publication.getIndex());
+                                       }
+                                       
                                        if (facade == null) {
                                                facade = new DocumentFacade(_owner, used,
                                                                getProjectSettings(), getPublicationService(),
                                                                getApplicationSettings());
-                                               _owner._docpres.put(index, facade);
                                        }
                                        _uses.add(facade);
                                }
                        }
                        if (_exports == null) {
-                               List<Relation> relation = _my
-                                               .getRelations(ConvertsRelation.class);
-
-                               _exports = new ArrayList<FileFacade>(relation.size());
-                               for (Iterator<Relation> i = relation.iterator(); i.hasNext();) {
-                                       ConvertsRelation export = (ConvertsRelation) i.next();
-                                       _exports.add(new FileFacade(export,
-                                                       getApplicationSettings()));
-                               }
+                               updateExports();
                        }
                        if (_history == null) {
-                               if (_my.getPreviousVersion() != null
+                               if (_mydoc.getPreviousVersion() != null
                                                || _state == ProgressState.inCHECK
                                                || _state == ProgressState.APPROVED) {
                                        _history = new ArrayList<HistoryFacade>();
@@ -265,13 +262,33 @@ public class DocumentFacade implements HistoryFacade {
                        _display = State.open;
                }
        }
+       
+       /**
+        * Update the list of export files.
+        */
+       private void updateExports() {
+               List<Relation> relations = _mydoc
+                               .getRelations(ConvertsRelation.class);
+
+               _exports = new ArrayList<FileFacade>(relations.size());
+               for (Relation rel : relations) {
+                       _exports.add(new FileFacade((ConvertsRelation)rel,
+                                       getApplicationSettings()));
+               }
+       }
 
+       /**
+        * Close the presentation tree partially.
+        */
        public void reduce() {
                if (_display == State.deepopen) {
                        _display = State.open;
                }
        }
 
+       /**
+        * Close the presentation tree.
+        */
        public void reduceAll() {
                _display = State.closed;
        }
@@ -280,34 +297,74 @@ public class DocumentFacade implements HistoryFacade {
        // Getters
        // ==============================================================================================================================
 
+       /**
+        * Get a list of attached files presentations.
+        * 
+        * @return list of FileFacades
+        */
        public List<FileFacade> getAttachments() {
                return _exports;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getDate()
+        */
        public String getDate() {
                return _date;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getDescription()
+        */
        public String getDescription() {
                return _description;
        }
 
+       /**
+        * Get a document edit icon file name according to the document state.
+        * 
+        * @return the icon file name
+        */
        public String getEditIcon() {
                return "icon.ed" + _state + ICON_EXT;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getFileIcon()
+        */
        public String getFileIcon() {
                return _icon;
        }
 
+       /**
+        * Get the document history presentation.
+        * 
+        * @return the history facade
+        */
        public List<HistoryFacade> getHistory() {
                return _history;
        }
 
+       /**
+        * Get the document persistent id.
+        * 
+        * @return the document persistent id
+        */
        public String getIndex() {
-               return String.valueOf(_my.getIndex());
+               return String.valueOf(_mydoc.getIndex());
        }
 
+       /**
+        * Get the document contextual popup menu.
+        * 
+        * @return the document popup menu
+        */
        public PopupMenu getPopup() { // Contextualizes the pop-up
                _popup
                                .setContext("document", new DocumentRights(_owner.getUser(),
@@ -315,46 +372,105 @@ public class DocumentFacade implements HistoryFacade {
                return _popup; // callers must "use" the returned pop-up before getting another pop-up
        }
 
+       /**
+        * Get the document presentation state: open, deep open, closed.
+        * 
+        * @return the document presentation state
+        * @see DocumentFacade.State
+        */
        public String getPresentationState() {
                return _display.toString();
        }
 
+       /**
+        * Get document progress state.
+        * 
+        * @return the document state
+        * @see ProgressState
+        */
        public String getProgressState() {
                return _state.toString();
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getSharingIcon()
+        */
        public String getSharingIcon() {
                return _sharing;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getSize()
+        */
        public String getSize() {
                return _size;
        }
 
+       /**
+        * Get a document state icon file name according to the document state.
+        * 
+        * @return the icon file name
+        */
        public String getStateIcon() {
                return "icon." + _state + ICON_EXT;
        }
 
+       /**
+        * Get the document title.
+        * 
+        * @return the document title
+        */
        public String getTitle() {
-               return _my.getTitle();
+               return _mydoc.getTitle();
        }
 
+       /**
+        * Get the URL of the document in the repository.
+        * 
+        * @return the document URL
+        */
        public String getURL() {
                return _surl;
        }
 
+       /**
+        * Get the list of used documents presentations.
+        * 
+        * @return list of DocumentFacades
+        */
        public List<DocumentFacade> getUses() {
                return _uses;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getVersion()
+        */
        public String getVersion() {
                return _version;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.simer.HistoryFacade#getVersioningIcon()
+        */
        public String getVersioningIcon() {
                return _updated;
        }
 
+       /**
+        * Check if this is a representation of the given document publication.
+        * 
+        * @param represented
+        *            the document publication
+        * @return true if this facades represents the given document publication
+        */
        public boolean isFacadeOf(final Publication represented) {
                return _me.equals(represented);
        }
@@ -374,13 +490,12 @@ public class DocumentFacade implements HistoryFacade {
                SimpleDateFormat datstring = new SimpleDateFormat(custom
                                .getString("date.format"), getApplicationSettings()
                                .getCurrentLocale()); // Locale date display format
-               Revision.Format verstring = new Revision.Format(getProjectSettings()
-                               .getRevisionPattern());
-               String path = _my.getSourceFile().getRelativePath();
+               String path = _mydoc.getSourceFile().getRelativePath();
                String[] mapping = ApplicationSettings.getViewersMapping();
 
                for (int i = 0; i < mapping.length; i++) {
-                       org.splat.dal.bo.som.File export = _my.getAttachedFile(mapping[i]);
+                       org.splat.dal.bo.som.File export = _mydoc
+                                       .getAttachedFile(mapping[i]);
                        if (export != null) {
                                path = export.getRelativePath();
                                break;
@@ -390,28 +505,28 @@ public class DocumentFacade implements HistoryFacade {
                _surl = _surl.replaceAll("'", "\\\\'");
 
                // Document state (overridable by the publication - see below)
-               _state = _my.getProgressState();
-               _version = _my.getVersion(); // May be null
+               _state = _mydoc.getProgressState();
+               _version = _mydoc.getVersion(); // May be null
 
                initIcons();
 
                // Document description
-               VersionsRelation versions = (VersionsRelation) _my
+               VersionsRelation versions = (VersionsRelation) _mydoc
                                .getFirstRelation(VersionsRelation.class);
                if (versions != null) {
                        _description = versions.getDescription();
                }
                // File details
-               if (_state != ProgressState.EXTERN) {
+                       Revision.Format verstring = new Revision.Format(
+                                       getProjectSettings().getRevisionPattern());
                        _version = verstring.format(_version);
-               }
-               _size = sizstring.format(_my.getSourceFile().asFile().length() / 1000);
-               _date = datstring.format(_my.getLastModificationDate());
-
+               _size = sizstring.format(_mydoc.getSourceFile().asFile().length() / 1000);
+               _date = datstring.format(_mydoc.getLastModificationDate());
+               updateExports();
                // Refresh of the history in case of promotion
                if (_display == State.deepopen) {
                        _history.clear();
-                       this.collectHistory(_my);
+                       this.collectHistory(_mydoc);
                }
                initPopupMenu();
        }
@@ -421,9 +536,13 @@ public class DocumentFacade implements HistoryFacade {
         */
        private final void initIcons() {
                // Icons definition
-               String format = _my.getFormat();
+               String format = _mydoc.getFormat();
                if ("xml".equals(format)) {
-                       format = XMLDocument.getActualFormat(_my.getSourceFile().asFile());
+                       format = XMLDocument.getActualFormat(_mydoc.getSourceFile()
+                                       .asFile());
+                       if ("schema".equals(_mydoc.getType().getName())) {
+                               format = "schema";
+                       }
                }
 
                _icon = "icon." + format + ICON_EXT;
@@ -436,8 +555,8 @@ public class DocumentFacade implements HistoryFacade {
                }
 
                if (_me == null) { // Facade in the history folder
-                       if (_my.isVersioned()) { // History of the last version
-                               if (_my.isPublished()) {
+                       if (_mydoc.isVersioned()) { // History of the last version
+                               if (_mydoc.isPublished()) {
                                        _sharing = "image.share.png"; // Not correct if published in a previous version of the study
                                        _updated = "icon.hold.png";
                                }
@@ -449,13 +568,13 @@ public class DocumentFacade implements HistoryFacade {
                                }
                        }
                } else {
-                       if (_me.getOwnerStudy().shares(_my)) {
+                       if (_me.getOwnerStudy().shares(_mydoc)) {
                                _sharing = "image.share.png";
                                _updated = "icon.hold.png";
                        }
-                       if (_me.isOutdated()) {
-                               _state = ProgressState.inWORK; // Overrides the document state
-                       }
+                       //if (_me.isOutdated()) {
+                       //      _state = ProgressState.inWORK; // Overrides the document state
+                       //}
                }
        }
 
@@ -497,6 +616,12 @@ public class DocumentFacade implements HistoryFacade {
        // Private services
        // ==============================================================================================================================
 
+       /**
+        * Collect the document's previous versions presentation.
+        * 
+        * @param given
+        *            the document to explore
+        */
        private final void collectHistory(final Document given) {
                VersionsRelation versions = (VersionsRelation) given
                                .getFirstRelation(VersionsRelation.class);