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