Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[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         /**
40          * Icon extension.
41          */
42         static private final String ICON_EXT = ".png";
43         /**
44          * Document's owner.
45          */
46         private final transient AbstractOpenObject _owner;
47         /**
48          * Wrapped document publication.
49          */
50         private final transient Publication _me;
51         /**
52          * Published document.
53          */
54         private final transient Document _my;
55         /**
56          * Document state.
57          */
58         private transient ProgressState _state;
59         /**
60          * My document version in customized format.
61          */
62         private transient String _version;
63         /**
64          * Presentation state.
65          */
66         private transient State _display;
67         /**
68          * URL of the source file.
69          */
70         private transient String _surl;
71         /**
72          * Corresponding icon.
73          */
74         private transient String _icon;
75         /**
76          * Icon qualifying sharing between scenarios of a same study.
77          */
78         private transient String _sharing;
79         /**
80          * Icon qualifying versioning from the previous study version.
81          */
82         private transient String _updated;
83         /**
84          * Document size.
85          */
86         private transient String _size;
87         /**
88          * Document last modification date.
89          */
90         private transient String _date;
91         /**
92          * Document's description.
93          */
94         private transient String _description;
95         /**
96          * List of used documents presentations.
97          */
98         private transient List<DocumentFacade> _uses;
99         /**
100          * List of attached files presentations.
101          */
102         private transient List<FileFacade> _exports;
103         /**
104          * List of the document's history nodes.
105          */
106         private transient List<HistoryFacade> _history;
107         /**
108          * Document's popup menu.
109          */
110         private transient PopupMenu _popup;
111         /**
112          * Injected project settings service.
113          */
114         private ProjectSettingsService _projectSettings;
115         /**
116          * Injected publication service.
117          */
118         private PublicationService _publicationService;
119         /**
120          * Injected application settings.
121          */
122         private ApplicationSettings _applicationSettings;
123
124         /**
125          * Document presentation state enumeration.
126          */
127         private enum State {
128                 /**
129                  * Closed presentation state.
130                  */
131                 closed,
132                 /**
133                  * Open presentation state (history and attached files nodes are visible).
134                  */
135                 open,
136                 /**
137                  * Deeply open presentation.
138                  */
139                 deepopen
140         }
141
142         // ==============================================================================================================================
143         // Constructors
144         // ==============================================================================================================================
145
146         /**
147          * Constructor.
148          * 
149          * @param opened
150          *            document owner
151          * @param represented
152          *            document presentation
153          * @param projectSettings
154          *            project settings service
155          * @param publicationService
156          *            publication service
157          * @param applicationSettings
158          *            application settings
159          */
160         public DocumentFacade(final AbstractOpenObject opened,
161                         final Publication represented,
162                         final ProjectSettingsService projectSettings,
163                         final PublicationService publicationService,
164                         final ApplicationSettings applicationSettings) {
165                 setProjectSettings(projectSettings);
166                 setPublicationService(publicationService);
167                 setApplicationSettings(applicationSettings);
168                 _owner = opened;
169                 _me = represented;
170                 _my = _me.value();
171                 _state = _my.getProgressState();
172                 _display = State.closed;
173
174                 refresh(); // Initializes the presentation of my document //NOPMD:RKV: to be reviewed
175         }
176
177         /**
178          * Constructs the facade of a document presented in the history folder.
179          * 
180          * @param opened
181          *            document owner
182          * @param represented
183          *            document presentation
184          * @param projectSettings
185          *            project settings service
186          * @param publicationService
187          *            publication service
188          * @param applicationSettings
189          *            application settings
190          */
191         private DocumentFacade(final AbstractOpenObject opened,
192                         final Document represented,
193                         final ProjectSettingsService projectSettings,
194                         final PublicationService publicationService,
195                         final ApplicationSettings applicationSettings) {
196                 setProjectSettings(projectSettings);
197                 setPublicationService(publicationService);
198                 setApplicationSettings(applicationSettings);
199                 _owner = opened;
200                 _me = null; // Marks the history context
201                 _my = represented;
202                 _state = _my.getProgressState(); // In reality, HISTORY
203                 _display = State.open; // Because the given document is a history document
204                 _description = ResourceBundle.getBundle("som",
205                                 applicationSettings.getCurrentLocale()).getString(
206                                 "history.creation")
207                                 + " " + _my.getAuthor().toString();
208                 _uses = null;
209                 _exports = null;
210                 _history = null;
211                 _popup = null;
212
213                 this.refresh(); // Initializes the presentation of my document
214         }
215
216         // ==============================================================================================================================
217         // Public member functions
218         // ==============================================================================================================================
219
220         /**
221          * Open the presentation subtree.
222          */
223         public void develop() {
224                 if (_display == State.open) { // Opening the history of document, if exist
225                         if (_history.isEmpty()) {
226                                 collectHistory(_my);
227                         }
228                         _display = State.deepopen;
229                 } else { // Opening the document
230                         if (_uses == null) {
231                                 List<Publication> relist = _me.getRelations(UsesRelation.class);
232
233                                 _uses = new ArrayList<DocumentFacade>(relist.size());
234                                 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
235                                         Publication used = i.next();
236                                         long index = used.getIndex();
237                                         DocumentFacade facade = _owner._docpres.get(index);
238                                         if (facade == null) {
239                                                 facade = new DocumentFacade(_owner, used,
240                                                                 getProjectSettings(), getPublicationService(),
241                                                                 getApplicationSettings());
242                                                 _owner._docpres.put(index, facade);
243                                         }
244                                         _uses.add(facade);
245                                 }
246                         }
247                         if (_exports == null) {
248                                 List<Relation> relation = _my
249                                                 .getRelations(ConvertsRelation.class);
250
251                                 _exports = new ArrayList<FileFacade>(relation.size());
252                                 for (Iterator<Relation> i = relation.iterator(); i.hasNext();) {
253                                         ConvertsRelation export = (ConvertsRelation) i.next();
254                                         _exports.add(new FileFacade(export,
255                                                         getApplicationSettings()));
256                                 }
257                         }
258                         if (_history == null) {
259                                 if (_my.getPreviousVersion() != null
260                                                 || _state == ProgressState.inCHECK
261                                                 || _state == ProgressState.APPROVED) {
262                                         _history = new ArrayList<HistoryFacade>();
263                                 }
264                         }
265                         _display = State.open;
266                 }
267         }
268
269         public void reduce() {
270                 if (_display == State.deepopen) {
271                         _display = State.open;
272                 }
273         }
274
275         public void reduceAll() {
276                 _display = State.closed;
277         }
278
279         // ==============================================================================================================================
280         // Getters
281         // ==============================================================================================================================
282
283         public List<FileFacade> getAttachments() {
284                 return _exports;
285         }
286
287         public String getDate() {
288                 return _date;
289         }
290
291         public String getDescription() {
292                 return _description;
293         }
294
295         public String getEditIcon() {
296                 return "icon.ed" + _state + ICON_EXT;
297         }
298
299         public String getFileIcon() {
300                 return _icon;
301         }
302
303         public List<HistoryFacade> getHistory() {
304                 return _history;
305         }
306
307         public String getIndex() {
308                 return String.valueOf(_my.getIndex());
309         }
310
311         public PopupMenu getPopup() { // Contextualizes the pop-up
312                 _popup
313                                 .setContext("document", new DocumentRights(_owner.getUser(),
314                                                 _me));
315                 return _popup; // callers must "use" the returned pop-up before getting another pop-up
316         }
317
318         public String getPresentationState() {
319                 return _display.toString();
320         }
321
322         public String getProgressState() {
323                 return _state.toString();
324         }
325
326         public String getSharingIcon() {
327                 return _sharing;
328         }
329
330         public String getSize() {
331                 return _size;
332         }
333
334         public String getStateIcon() {
335                 return "icon." + _state + ICON_EXT;
336         }
337
338         public String getTitle() {
339                 return _my.getTitle();
340         }
341
342         public String getURL() {
343                 return _surl;
344         }
345
346         public List<DocumentFacade> getUses() {
347                 return _uses;
348         }
349
350         public String getVersion() {
351                 return _version;
352         }
353
354         public String getVersioningIcon() {
355                 return _updated;
356         }
357
358         public boolean isFacadeOf(final Publication represented) {
359                 return _me.equals(represented);
360         }
361
362         // ==============================================================================================================================
363         // Protected services
364         // ==============================================================================================================================
365
366         /**
367          * Refresh the document presentation.
368          */
369         protected final void refresh() {
370                 ResourceBundle custom = ResourceBundle.getBundle("som",
371                                 getApplicationSettings().getCurrentLocale());
372                 DecimalFormat sizstring = new DecimalFormat(custom
373                                 .getString("size.format")); // Locale size display format
374                 SimpleDateFormat datstring = new SimpleDateFormat(custom
375                                 .getString("date.format"), getApplicationSettings()
376                                 .getCurrentLocale()); // Locale date display format
377                 Revision.Format verstring = new Revision.Format(getProjectSettings()
378                                 .getRevisionPattern());
379                 String path = _my.getSourceFile().getRelativePath();
380                 String[] mapping = ApplicationSettings.getViewersMapping();
381
382                 for (int i = 0; i < mapping.length; i++) {
383                         org.splat.dal.bo.som.File export = _my.getAttachedFile(mapping[i]);
384                         if (export != null) {
385                                 path = export.getRelativePath();
386                                 break;
387                         }
388                 }
389                 _surl = getApplicationSettings().getRepositoryURL() + path;
390                 _surl = _surl.replaceAll("'", "\\\\'");
391
392                 // Document state (overridable by the publication - see below)
393                 _state = _my.getProgressState();
394                 _version = _my.getVersion(); // May be null
395
396                 initIcons();
397
398                 // Document description
399                 VersionsRelation versions = (VersionsRelation) _my
400                                 .getFirstRelation(VersionsRelation.class);
401                 if (versions != null) {
402                         _description = versions.getDescription();
403                 }
404                 // File details
405                 if (_state != ProgressState.EXTERN) {
406                         _version = verstring.format(_version);
407                 }
408                 _size = sizstring.format(_my.getSourceFile().asFile().length() / 1000);
409                 _date = datstring.format(_my.getLastModificationDate());
410
411                 // Refresh of the history in case of promotion
412                 if (_display == State.deepopen) {
413                         _history.clear();
414                         this.collectHistory(_my);
415                 }
416                 initPopupMenu();
417         }
418
419         /**
420          * Initialize document presentation icons.
421          */
422         private final void initIcons() {
423                 // Icons definition
424                 String format = _my.getFormat();
425                 if ("xml".equals(format)) {
426                         format = XMLDocument.getActualFormat(_my.getSourceFile().asFile());
427                         if ("schema".equals(_my.getType().getName())) {
428                                 format = "schema";
429                         }
430                 }
431
432                 _icon = "icon." + format + ICON_EXT;
433                 _sharing = "image.hold.gif";
434                 _updated = "image.hold.gif";
435                 File image = new File(ApplicationSettings.getApplicationSkinPath()
436                                 + _icon);
437                 if (!image.exists()) {
438                         _icon = "icon.any.png";
439                 }
440
441                 if (_me == null) { // Facade in the history folder
442                         if (_my.isVersioned()) { // History of the last version
443                                 if (_my.isPublished()) {
444                                         _sharing = "image.share.png"; // Not correct if published in a previous version of the study
445                                         _updated = "icon.hold.png";
446                                 }
447                         } else {
448                                 if (_state == ProgressState.inWORK) {
449                                         _icon = "icon." + _state.toString() + ICON_EXT;
450                                 } else {
451                                         _icon = "icon.inWORK.png";
452                                 }
453                         }
454                 } else {
455                         if (_me.getOwnerStudy().shares(_my)) {
456                                 _sharing = "image.share.png";
457                                 _updated = "icon.hold.png";
458                         }
459                         if (_me.isOutdated()) {
460                                 _state = ProgressState.inWORK; // Overrides the document state
461                         }
462                 }
463         }
464
465         /**
466          * Initialize document's popup menu.
467          */
468         private final void initPopupMenu() {
469                 // Popup menus
470                 if (_me != null) { // There is a pop-up
471                         if (_state == ProgressState.EXTERN) {
472                                 _popup = getApplicationSettings().getPopupMenu("extern");
473                         } else if (_state == ProgressState.inWORK) {
474                                 _popup = getApplicationSettings().getPopupMenu("editable");
475                         } else if (_state == ProgressState.inDRAFT) {
476                                 _popup = getApplicationSettings().getPopupMenu("reviewable");
477                         } else if (_state == ProgressState.APPROVED) {
478                                 _popup = getApplicationSettings().getPopupMenu("approved");
479                         } else { // (state == ProgressState.inCHECK)
480                                 DocumentType aType = _me.value().getType(); // Only result documents need to be approved
481                                 Step aStep = getPublicationService().getInvolvedStep(_me);
482                                 if (aType.isResultOf(aStep.getStep())) {
483                                         _popup = getApplicationSettings()
484                                                         .getPopupMenu("approvable");
485                                 } else {
486                                         _popup = getApplicationSettings().getPopupMenu("notresult");
487                                 }
488                         }
489                 }
490         }
491
492         /**
493          * Turn on the versioning icon.
494          */
495         protected void setVersioned() {
496                 _updated = "image.modified.png";
497         }
498
499         // ==============================================================================================================================
500         // Private services
501         // ==============================================================================================================================
502
503         private final void collectHistory(final Document given) {
504                 VersionsRelation versions = (VersionsRelation) given
505                                 .getFirstRelation(VersionsRelation.class);
506                 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
507
508                 for (int i = stamp.length - 1; i > -1; i--) {
509                         _history.add(new StampFacade(stamp[i], getApplicationSettings()
510                                         .getCurrentLocale()));
511                 }
512                 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
513                                 getPublicationService(), getApplicationSettings()));
514                 if (versions != null) {
515                         this.collectHistory(versions.getTo());
516                 }
517         }
518
519         /**
520          * Get project settings.
521          * 
522          * @return Project settings service
523          */
524         private ProjectSettingsService getProjectSettings() {
525                 return _projectSettings;
526         }
527
528         /**
529          * Set project settings service.
530          * 
531          * @param projectSettingsService
532          *            project settings service
533          */
534         public final void setProjectSettings(
535                         final ProjectSettingsService projectSettingsService) {
536                 _projectSettings = projectSettingsService;
537         }
538
539         /**
540          * Get the publicationService.
541          * 
542          * @return the publicationService
543          */
544         public PublicationService getPublicationService() {
545                 return _publicationService;
546         }
547
548         /**
549          * Set the publicationService.
550          * 
551          * @param publicationService
552          *            the publicationService to set
553          */
554         public final void setPublicationService(
555                         final PublicationService publicationService) {
556                 _publicationService = publicationService;
557         }
558
559         /**
560          * Get the applicationSettings.
561          * 
562          * @return the applicationSettings
563          */
564         public ApplicationSettings getApplicationSettings() {
565                 return _applicationSettings;
566         }
567
568         /**
569          * Set the applicationSettings.
570          * 
571          * @param applicationSettings
572          *            the applicationSettings to set
573          */
574         public final void setApplicationSettings(
575                         final ApplicationSettings applicationSettings) {
576                 _applicationSettings = applicationSettings;
577         }
578 }