Salome HOME
28f0b2c91c05ddbe48c425a4b815663f4c685c71
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / DocumentFacade.java
1 package org.splat.simer;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.File;
9 import java.text.DecimalFormat;
10 import java.text.SimpleDateFormat;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.ResourceBundle;
15
16 import org.splat.manox.XMLDocument;
17 import org.splat.kernel.Relation;
18 import org.splat.som.ConvertsRelation;
19 import org.splat.som.Document;
20 import org.splat.som.DocumentRights;
21 import org.splat.som.DocumentType;
22 import org.splat.som.ProgressState;
23 import org.splat.som.ProjectSettings;
24 import org.splat.som.Publication;
25 import org.splat.som.Revision;
26 import org.splat.som.Step;
27 import org.splat.som.Timestamp;
28 import org.splat.som.UsesRelation;
29 import org.splat.som.VersionsRelation;
30 import org.splat.wapp.PopupMenu;
31
32
33 public class DocumentFacade implements HistoryFacade {
34
35     private OpenObject           owner;
36     private Publication          me;
37     private Document             my;        // Published document
38     private ProgressState        state;     // Document state
39     private String               version;   // My document version in customized format
40     private State                display;   // Presentation state
41     private String               surl;      // URL of the source file
42     private String               format;    // Extension of the source file
43     private String               icon;      // Corresponding icon
44     private String               sharing;   // Icon qualifying sharing between scenarios of a same study
45     private String               updated;   // Icon qualifying versioning from the previous study version
46     private String               size;
47     private String               date;
48     private String               description;
49     private List<DocumentFacade> uses;
50     private List<FileFacade>     exports;
51     private List<HistoryFacade>  history;
52     private PopupMenu            popup;
53     
54     private enum State { closed, open, deepopen }
55
56 //  ==============================================================================================================================
57 //  Constructors
58 //  ==============================================================================================================================
59
60     public DocumentFacade (OpenObject opened, Publication represented) {
61 //  ------------------------------------------------------------------
62       owner       = opened;
63       me          = represented;
64       my          = me.value();
65       state       = my.getProgressState();
66       display     = State.closed;
67       description = null;
68       uses        = null;
69       exports     = null;
70       history     = null;
71       popup       = null;
72
73       this.refresh();                       // Initializes the presentation of my document
74     }
75 /**
76  * Constructs the facade of a document presented in the history folder.
77  * 
78  * @param represented the represented history document
79  */
80     private DocumentFacade (OpenObject opened, Document represented) {
81 //  ----------------------------------------------------------------
82       owner       = opened;
83       me          = null;                   // Marks the history context
84       my          = represented;
85       state       = my.getProgressState();  // In reality, HISTORY
86       display     = State.open;             // Because the given document is a history document
87       description = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale()).getString("history.creation") + " " + my.getAuthor().toString();
88       uses        = null;
89       exports     = null;
90       history     = null;
91       popup       = null;
92
93       this.refresh();                       // Initializes the presentation of my document
94     }
95
96 //  ==============================================================================================================================
97 //  Public member functions
98 //  ==============================================================================================================================
99
100     public void develop () {
101 //  -------------------
102       if (display != State.open) {   // Opening the document
103         if (uses == null) {
104           List<Publication> relist = me.getRelations(UsesRelation.class);
105
106           uses = new ArrayList<DocumentFacade>(relist.size());
107           for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
108                 Publication     used   = i.next();
109             Integer         index  = used.getIndex();
110             DocumentFacade  facade = OpenObject.docpres.get(index);
111             if (facade == null) {
112                 facade = new DocumentFacade(owner, used);
113               OpenObject.docpres.put(index, facade);
114             }
115             uses.add(facade);
116           }
117         }
118         if (exports == null) {
119           List<Relation> relation = my.getRelations(ConvertsRelation.class);
120
121           exports = new ArrayList<FileFacade>(relation.size());
122           for (Iterator<Relation> i=relation.iterator(); i.hasNext();) {
123                 ConvertsRelation export = (ConvertsRelation)i.next();
124             exports.add( new FileFacade(export) );
125           }
126         }
127         if (history == null) {
128           if (my.getPreviousVersion() != null || state == ProgressState.inCHECK || state == ProgressState.APPROVED) history = new ArrayList<HistoryFacade>();
129         }
130         display = State.open;
131       } else {                     // Opening the history of document, if exist
132         if (history.isEmpty()) collectHistory(my);
133         display = State.deepopen;
134       }
135     }
136
137     public void reduce () {
138 //  ---------------------
139       if (display == State.deepopen) display = State.open;
140     }
141
142     public void reduceAll () {
143 //  ------------------------
144         display = State.closed;
145     }
146
147 //  ==============================================================================================================================
148 //  Getters
149 //  ==============================================================================================================================
150
151     public List<FileFacade> getAttachments () {
152 //  ------------------------------------------
153       return exports;
154     }
155     public String getDate () {
156 //  ------------------------
157       return date;
158     }
159     public String getDescription () {
160 //  -------------------------------
161       return description;
162     }
163     public String getEditIcon () {
164 //  ----------------------------
165       return "icon.ed" + state + ".png";
166     }
167     public String getFileIcon () {
168 //  ----------------------------
169       return icon;
170     }
171     public List<HistoryFacade> getHistory () {
172 //  ----------------------------------------
173       return history;
174     }
175     public String getIndex () {
176 //  -------------------------
177       return String.valueOf(my.getIndex());
178     }
179     public PopupMenu getPopup () {          // Contextualizes the pop-up
180 //  ----------------------------
181       popup.setContext("document", new DocumentRights(owner.getUser(), me));
182       return popup;                         // callers must "use" the returned pop-up before getting another pop-up
183     }
184     public String getPresentationState () {
185 //  -------------------------------------
186       return display.toString();
187     }
188     public String getProgressState () {
189 //  ---------------------------------
190       return state.toString();
191     }
192     public String getSharingIcon () {
193 //  -------------------------------
194       return sharing;
195     }
196     public String getSize () {
197 //  ------------------------
198       return size;
199     }
200     public String getStateIcon () {
201 //  -----------------------------
202       return "icon." + state + ".png";
203     }
204     public String getTitle () {
205 //  -------------------------
206       return my.getTitle();
207     }
208     public String getURL () {
209 //  -----------------------
210       return surl;
211     }
212     public List<DocumentFacade> getUses () {
213 //  ---------------------------------------
214       return uses;
215     }
216     public String getVersion () {
217 //  ---------------------------
218       return version;
219     }
220     public String getVersioningIcon () {
221 //  ----------------------------------
222       return updated;
223     }
224     public boolean isFacadeOf (Publication represented) {
225 //  ---------------------------------------------------
226       return  me.equals(represented);
227     }
228 //  ==============================================================================================================================
229 //  Protected services
230 //  ==============================================================================================================================
231
232     protected void refresh () {
233 //  -------------------------
234       ResourceBundle      custom    = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
235           DecimalFormat       sizstring = new DecimalFormat(custom.getString("size.format"));     // Locale size display format
236           SimpleDateFormat    datstring = new SimpleDateFormat(custom.getString("date.format"));  // Locale date display format
237           Revision.Format     verstring = new Revision.Format(ProjectSettings.getRevisionPattern());
238       String              path      = my.getSourceFile().getRelativePath();
239       String[]            mapping   = ApplicationSettings.getViewersMapping();
240
241       for (int i=0; i<mapping.length; i++) {
242         org.splat.som.File  export  = my.getAttachedFile(mapping[i]);
243         if (export == null) continue;
244         path = export.getRelativePath();
245         break;
246       }
247       surl   = ApplicationSettings.getRepositoryURL() + path;
248       surl   = surl.replaceAll("'", "\\\\'");
249       format = my.getFormat();
250       if (format.equals("xml")) format = XMLDocument.getActualFormat(my.getSourceFile().asFile());
251
252 //    Document state (overridable by the publication - see below)
253       state   = my.getProgressState();
254       version = my.getVersion();                              // May be null
255
256 //    Icons definition
257       icon    = "icon." + format + ".png";
258       sharing = "image.hold.gif";
259       updated = "image.hold.gif";
260       File image = new File(ApplicationSettings.getApplicationSkinPath() + icon);
261       if (!image.exists()) icon = "icon.any.png";
262
263       if (me != null) {
264         if (me.getOwnerStudy().shares(my)) {
265                              sharing = "image.share.png";
266                              updated = "icon.hold.png";
267         }
268         if (me.isOutdated()) state   = ProgressState.inWORK;  // Overrides the document state
269
270       } else {            // Facade in the history folder
271         if (!my.isVersioned()) {                              // History of the last version
272           if (state != ProgressState.inWORK) icon = "icon.inWORK.png";
273           else                               icon = "icon." + state.toString() + ".png";
274         } else
275         if ( my.isPublished()) {
276                                  sharing = "image.share.png";     // Not correct if published in a previous version of the study
277                              updated = "icon.hold.png";
278         }
279       }
280 //    Document description
281       VersionsRelation  versions = (VersionsRelation)my.getFirstRelation(VersionsRelation.class);
282       if (versions != null) {
283         description = versions.getDescription();
284       }
285 //    File details
286       if (state != ProgressState.EXTERN) version = verstring.format(version);
287           size = sizstring.format(my.getSourceFile().asFile().length()/1000);
288           date = datstring.format(my.getLastModificationDate());
289
290 //    Refresh of the history in case of promotion
291       if (display == State.deepopen) {
292         history.clear();
293         collectHistory(my);
294       }
295 //    Popup menus
296           if (me == null) return;                                 // No pop-up (yet) in the history folder
297           if       (state == ProgressState.EXTERN)   popup = ApplicationSettings.getPopupMenu("extern");
298           else if  (state == ProgressState.inWORK)   popup = ApplicationSettings.getPopupMenu("editable");
299           else if  (state == ProgressState.inDRAFT)  popup = ApplicationSettings.getPopupMenu("reviewable");
300           else if  (state == ProgressState.APPROVED) popup = ApplicationSettings.getPopupMenu("approved");
301           else { //(state == ProgressState.inCHECK)
302         DocumentType mytype = me.value().getType();           // Only result documents need to be approved
303         Step         mystep = me.getInvolvedStep();
304                 if (mytype.isResultOf(mystep.getStep())) popup = ApplicationSettings.getPopupMenu("approvable");
305                 else                                     popup = ApplicationSettings.getPopupMenu("notresult");
306           }
307     }
308
309     protected void setVersioned () {
310 //  ------------------------------
311       updated = "image.modified.png";
312     }
313
314 //  ==============================================================================================================================
315 //  Private services
316 //  ==============================================================================================================================
317
318     private void collectHistory (Document given) {
319 //  --------------------------------------------
320       VersionsRelation  versions = (VersionsRelation)given.getFirstRelation(VersionsRelation.class);
321       Timestamp[]       stamp    = given.getStamps();         // Stamps in ascending order of date
322
323       for  (int i=stamp.length-1; i>-1; i--) history.add( new StampFacade(stamp[i]) );
324       history.add( new DocumentFacade(owner, given) );
325       if (versions != null) collectHistory(versions.getTo());
326     }
327 }