]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/DocumentFacade.java
Salome HOME
Fixed case when translation is not found. Now default translation parameter is used...
[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
175         }
176
177         /**
178          * Constructs the facade of a document presented in the history folder.
179          * 
180          * @param represented
181          *            the represented history document
182          */
183         private DocumentFacade(final AbstractOpenObject opened,
184                         final Document represented,
185                         final ProjectSettingsService projectSettings,
186                         final PublicationService publicationService,
187                         final ApplicationSettings applicationSettings) {
188                 setProjectSettings(projectSettings);
189                 setPublicationService(publicationService);
190                 setApplicationSettings(applicationSettings);
191                 _owner = opened;
192                 _me = null; // Marks the history context
193                 _my = represented;
194                 _state = _my.getProgressState(); // In reality, HISTORY
195                 _display = State.open; // Because the given document is a history document
196                 _description = ResourceBundle.getBundle("som",
197                                 applicationSettings.getCurrentLocale()).getString(
198                                 "history.creation")
199                                 + " " + _my.getAuthor().toString();
200                 _uses = null;
201                 _exports = null;
202                 _history = null;
203                 _popup = null;
204
205                 this.refresh(); // Initializes the presentation of my document
206         }
207
208         // ==============================================================================================================================
209         // Public member functions
210         // ==============================================================================================================================
211
212         public void develop() {
213                 if (_display == State.open) { // Opening the history of document, if exist
214                         if (_history.isEmpty()) {
215                                 collectHistory(_my);
216                         }
217                         _display = State.deepopen;
218                 } else { // Opening the document
219                         if (_uses == null) {
220                                 List<Publication> relist = _me.getRelations(UsesRelation.class);
221
222                                 _uses = new ArrayList<DocumentFacade>(relist.size());
223                                 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
224                                         Publication used = i.next();
225                                         long index = used.getIndex();
226                                         DocumentFacade facade = _owner.docpres.get(index);
227                                         if (facade == null) {
228                                                 facade = new DocumentFacade(_owner, used,
229                                                                 getProjectSettings(), getPublicationService(),
230                                                                 getApplicationSettings());
231                                                 _owner.docpres.put(index, facade);
232                                         }
233                                         _uses.add(facade);
234                                 }
235                         }
236                         if (_exports == null) {
237                                 List<Relation> relation = _my
238                                                 .getRelations(ConvertsRelation.class);
239
240                                 _exports = new ArrayList<FileFacade>(relation.size());
241                                 for (Iterator<Relation> i = relation.iterator(); i.hasNext();) {
242                                         ConvertsRelation export = (ConvertsRelation) i.next();
243                                         _exports.add(new FileFacade(export,
244                                                         getApplicationSettings()));
245                                 }
246                         }
247                         if (_history == null) {
248                                 if (_my.getPreviousVersion() != null
249                                                 || _state == ProgressState.inCHECK
250                                                 || _state == ProgressState.APPROVED) {
251                                         _history = new ArrayList<HistoryFacade>();
252                                 }
253                         }
254                         _display = State.open;
255                 }
256         }
257
258         public void reduce() {
259                 if (_display == State.deepopen) {
260                         _display = State.open;
261                 }
262         }
263
264         public void reduceAll() {
265                 _display = State.closed;
266         }
267
268         // ==============================================================================================================================
269         // Getters
270         // ==============================================================================================================================
271
272         public List<FileFacade> getAttachments() {
273                 return _exports;
274         }
275
276         public String getDate() {
277                 return _date;
278         }
279
280         public String getDescription() {
281                 return _description;
282         }
283
284         public String getEditIcon() {
285                 return "icon.ed" + _state + ICON_EXT;
286         }
287
288         public String getFileIcon() {
289                 return _icon;
290         }
291
292         public List<HistoryFacade> getHistory() {
293                 return _history;
294         }
295
296         public String getIndex() {
297                 return String.valueOf(_my.getIndex());
298         }
299
300         public PopupMenu getPopup() { // Contextualizes the pop-up
301                 _popup
302                                 .setContext("document", new DocumentRights(_owner.getUser(),
303                                                 _me));
304                 return _popup; // callers must "use" the returned pop-up before getting another pop-up
305         }
306
307         public String getPresentationState() {
308                 return _display.toString();
309         }
310
311         public String getProgressState() {
312                 return _state.toString();
313         }
314
315         public String getSharingIcon() {
316                 return _sharing;
317         }
318
319         public String getSize() {
320                 return _size;
321         }
322
323         public String getStateIcon() {
324                 return "icon." + _state + ICON_EXT;
325         }
326
327         public String getTitle() {
328                 return _my.getTitle();
329         }
330
331         public String getURL() {
332                 return _surl;
333         }
334
335         public List<DocumentFacade> getUses() {
336                 return _uses;
337         }
338
339         public String getVersion() {
340                 return _version;
341         }
342
343         public String getVersioningIcon() {
344                 return _updated;
345         }
346
347         public boolean isFacadeOf(final Publication represented) {
348                 return _me.equals(represented);
349         }
350
351         // ==============================================================================================================================
352         // Protected services
353         // ==============================================================================================================================
354
355         /**
356          * Refresh the document presentation.
357          */
358         protected final void refresh() {
359                 ResourceBundle custom = ResourceBundle.getBundle("som",
360                                 getApplicationSettings().getCurrentLocale());
361                 DecimalFormat sizstring = new DecimalFormat(custom
362                                 .getString("size.format")); // Locale size display format
363                 SimpleDateFormat datstring = new SimpleDateFormat(custom
364                                 .getString("date.format")); // Locale date display format
365                 Revision.Format verstring = new Revision.Format(getProjectSettings()
366                                 .getRevisionPattern());
367                 String path = _my.getSourceFile().getRelativePath();
368                 String[] mapping = ApplicationSettings.getViewersMapping();
369
370                 for (int i = 0; i < mapping.length; i++) {
371                         org.splat.dal.bo.som.File export = _my.getAttachedFile(mapping[i]);
372                         if (export != null) {
373                                 path = export.getRelativePath();
374                                 break;
375                         }
376                 }
377                 _surl = getApplicationSettings().getRepositoryURL() + path;
378                 _surl = _surl.replaceAll("'", "\\\\'");
379                 String format = _my.getFormat();
380                 if ("xml".equals(format)) {
381                         format = XMLDocument.getActualFormat(_my.getSourceFile().asFile());
382                 }
383
384                 // Document state (overridable by the publication - see below)
385                 _state = _my.getProgressState();
386                 _version = _my.getVersion(); // May be null
387
388                 // Icons definition
389                 _icon = "icon." + format + ICON_EXT;
390                 _sharing = "image.hold.gif";
391                 _updated = "image.hold.gif";
392                 File image = new File(ApplicationSettings.getApplicationSkinPath()
393                                 + _icon);
394                 if (!image.exists()) {
395                         _icon = "icon.any.png";
396                 }
397
398                 if (_me == null) { // Facade in the history folder
399                         if (_my.isVersioned()) { // History of the last version
400                                 if (_my.isPublished()) {
401                                         _sharing = "image.share.png"; // Not correct if published in a previous version of the study
402                                         _updated = "icon.hold.png";
403                                 }
404                         } else {
405                                 if (_state == ProgressState.inWORK) {
406                                         _icon = "icon." + _state.toString() + ICON_EXT;
407                                 } else {
408                                         _icon = "icon.inWORK.png";
409                                 }
410                         }
411                 } else {
412                         if (_me.getOwnerStudy().shares(_my)) {
413                                 _sharing = "image.share.png";
414                                 _updated = "icon.hold.png";
415                         }
416                         if (_me.isOutdated()) {
417                                 _state = ProgressState.inWORK; // Overrides the document state
418                         }
419                 }
420                 // Document description
421                 VersionsRelation versions = (VersionsRelation) _my
422                                 .getFirstRelation(VersionsRelation.class);
423                 if (versions != null) {
424                         _description = versions.getDescription();
425                 }
426                 // File details
427                 if (_state != ProgressState.EXTERN) {
428                         _version = verstring.format(_version);
429                 }
430                 _size = sizstring.format(_my.getSourceFile().asFile().length() / 1000);
431                 _date = datstring.format(_my.getLastModificationDate());
432
433                 // Refresh of the history in case of promotion
434                 if (_display == State.deepopen) {
435                         _history.clear();
436                         collectHistory(_my);
437                 }
438                 // Popup menus
439                 if (_me != null) { // There is a pop-up
440                         if (_state == ProgressState.EXTERN) {
441                                 _popup = getApplicationSettings().getPopupMenu("extern");
442                         } else if (_state == ProgressState.inWORK) {
443                                 _popup = getApplicationSettings().getPopupMenu("editable");
444                         } else if (_state == ProgressState.inDRAFT) {
445                                 _popup = getApplicationSettings().getPopupMenu("reviewable");
446                         } else if (_state == ProgressState.APPROVED) {
447                                 _popup = getApplicationSettings().getPopupMenu("approved");
448                         } else { // (state == ProgressState.inCHECK)
449                                 DocumentType mytype = _me.value().getType(); // Only result documents need to be approved
450                                 Step mystep = getPublicationService().getInvolvedStep(_me);
451                                 if (mytype.isResultOf(mystep.getStep())) {
452                                         _popup = getApplicationSettings()
453                                                         .getPopupMenu("approvable");
454                                 } else {
455                                         _popup = getApplicationSettings().getPopupMenu("notresult");
456                                 }
457                         }
458                 }
459         }
460
461         /**
462          * Turn on the versioning icon.
463          */
464         protected void setVersioned() {
465                 _updated = "image.modified.png";
466         }
467
468         // ==============================================================================================================================
469         // Private services
470         // ==============================================================================================================================
471
472         private final void collectHistory(final Document given) {
473                 VersionsRelation versions = (VersionsRelation) given
474                                 .getFirstRelation(VersionsRelation.class);
475                 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
476
477                 for (int i = stamp.length - 1; i > -1; i--) {
478                         _history.add(new StampFacade(stamp[i], getApplicationSettings()
479                                         .getCurrentLocale()));
480                 }
481                 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
482                                 getPublicationService(), getApplicationSettings()));
483                 if (versions != null) {
484                         collectHistory(versions.getTo());
485                 }
486         }
487
488         /**
489          * Get project settings.
490          * 
491          * @return Project settings service
492          */
493         private ProjectSettingsService getProjectSettings() {
494                 return _projectSettings;
495         }
496
497         /**
498          * Set project settings service.
499          * 
500          * @param projectSettingsService
501          *            project settings service
502          */
503         public final void setProjectSettings(
504                         final ProjectSettingsService projectSettingsService) {
505                 _projectSettings = projectSettingsService;
506         }
507
508         /**
509          * Get the publicationService.
510          * 
511          * @return the publicationService
512          */
513         public PublicationService getPublicationService() {
514                 return _publicationService;
515         }
516
517         /**
518          * Set the publicationService.
519          * 
520          * @param publicationService
521          *            the publicationService to set
522          */
523         public final void setPublicationService(
524                         final PublicationService publicationService) {
525                 _publicationService = publicationService;
526         }
527
528         /**
529          * Get the applicationSettings.
530          * 
531          * @return the applicationSettings
532          */
533         public ApplicationSettings getApplicationSettings() {
534                 return _applicationSettings;
535         }
536
537         /**
538          * Set the applicationSettings.
539          * 
540          * @param applicationSettings
541          *            the applicationSettings to set
542          */
543         public final void setApplicationSettings(
544                         final ApplicationSettings applicationSettings) {
545                 _applicationSettings = applicationSettings;
546         }
547 }