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