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