1 package org.splat.simer;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012-2015
10 import java.text.DecimalFormat;
11 import java.text.SimpleDateFormat;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.ResourceBundle;
16 import org.splat.dal.bo.kernel.Relation;
17 import org.splat.dal.bo.kernel.User;
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;
35 * Document wrapper class for presentation layer.
37 public class DocumentFacade implements HistoryFacade {
42 static private final String ICON_EXT = ".png";
46 private final transient AbstractOpenObject _owner;
48 * Wrapped document publication.
50 private final transient Publication _me;
54 private final transient Document _mydoc;
58 private transient ProgressState _state;
60 * My document version in customized format.
62 private transient String _version;
64 * Presentation state: open, deep open, closed.
66 * @see DocumentFacade.State
68 private transient State _display;
70 * URL of the source file.
72 private transient String _surl;
76 private transient String _icon;
78 * Icon qualifying sharing between scenarios of a same study.
80 private transient String _sharing;
82 * Icon qualifying versioning from the previous study version.
84 private transient String _updated;
88 private transient String _size;
90 * Document last modification date.
92 private transient String _date;
94 * Document's description.
96 private transient String _description;
98 * List of used documents presentations.
100 private transient List<DocumentFacade> _uses;
102 * List of attached files presentations.
104 private transient List<FileFacade> _exports;
106 * List of the document's history nodes.
108 private transient List<HistoryFacade> _history;
110 * Document's popup menu.
112 private transient PopupMenu _popup;
114 * Injected project settings service.
116 private ProjectSettingsService _projectSettings;
118 * Injected publication service.
120 private PublicationService _publicationService;
122 * Injected application settings.
124 private ApplicationSettings _applicationSettings;
127 * Document presentation state enumeration.
131 * Closed presentation state.
135 * Open presentation state (history and attached files nodes are visible).
139 * Deeply open presentation.
144 // ==============================================================================================================================
146 // ==============================================================================================================================
154 * document presentation
155 * @param projectSettings
156 * project settings service
157 * @param publicationService
158 * publication service
159 * @param applicationSettings
160 * application settings
162 public DocumentFacade(final AbstractOpenObject opened,
163 final Publication represented,
164 final ProjectSettingsService projectSettings,
165 final PublicationService publicationService,
166 final ApplicationSettings applicationSettings) {
167 setProjectSettings(projectSettings);
168 setPublicationService(publicationService);
169 setApplicationSettings(applicationSettings);
172 _mydoc = _me.value();
173 _state = _mydoc.getProgressState();
174 _display = State.closed;
176 refresh(); // Initializes the presentation of my document //NOPMD:RKV: to be reviewed
180 * Constructs the facade of a document presented in the history folder.
185 * document presentation
186 * @param projectSettings
187 * project settings service
188 * @param publicationService
189 * publication service
190 * @param applicationSettings
191 * application settings
193 private DocumentFacade(final AbstractOpenObject opened,
194 final Document represented,
195 final ProjectSettingsService projectSettings,
196 final PublicationService publicationService,
197 final ApplicationSettings applicationSettings) {
198 setProjectSettings(projectSettings);
199 setPublicationService(publicationService);
200 setApplicationSettings(applicationSettings);
202 _me = null; // Marks the history context
203 _mydoc = represented;
204 _state = _mydoc.getProgressState(); // In reality, HISTORY
205 _display = State.open; // Because the given document is a history document
206 _description = ResourceBundle.getBundle("som",
207 applicationSettings.getCurrentLocale()).getString(
209 + " " + _mydoc.getAuthor().toString();
211 this.refresh(); // Initializes the presentation of my document
214 // ==============================================================================================================================
215 // Public member functions
216 // ==============================================================================================================================
219 * Open the presentation subtree.
221 public void develop() {
222 if (_display == State.open) { // Opening the history of document, if exist
223 if (_history.isEmpty()) {
224 collectHistory(_mydoc);
226 _display = State.deepopen;
227 } else { // Opening the document
229 List<Relation> relist = _me.value().getRelations(UsesRelation.class);
231 _uses = new ArrayList<DocumentFacade>(relist.size());
232 for (Relation relation : relist) {
233 Document used = ((UsesRelation)relation).getTo();
235 DocumentFacade facade = null;
237 Publication publication = _me.getOwner().getPublication(used);
238 if(publication == null) {
239 publication = _me.getOwnerStudy().getPublication(used);
241 if(publication != null) {
242 facade = _owner._docpres.get(publication.getIndex());
245 if (facade == null) {
246 facade = new DocumentFacade(_owner, used,
247 getProjectSettings(), getPublicationService(),
248 getApplicationSettings());
253 if (_exports == null) {
256 if (_history == null) {
257 if (_mydoc.getPreviousVersion() != null
258 || _state == ProgressState.inCHECK
259 || _state == ProgressState.APPROVED) {
260 _history = new ArrayList<HistoryFacade>();
263 _display = State.open;
268 * Update the list of export files.
270 private void updateExports() {
271 List<Relation> relations = _mydoc
272 .getRelations(ConvertsRelation.class);
274 _exports = new ArrayList<FileFacade>(relations.size());
275 for (Relation rel : relations) {
276 _exports.add(new FileFacade((ConvertsRelation)rel,
277 getApplicationSettings()));
282 * Close the presentation tree partially.
284 public void reduce() {
285 if (_display == State.deepopen) {
286 _display = State.open;
291 * Close the presentation tree.
293 public void reduceAll() {
294 _display = State.closed;
297 // ==============================================================================================================================
299 // ==============================================================================================================================
302 * Get a list of attached files presentations.
304 * @return list of FileFacades
306 public List<FileFacade> getAttachments() {
313 * @see org.splat.simer.HistoryFacade#getDate()
315 public String getDate() {
322 * @see org.splat.simer.HistoryFacade#getDescription()
324 public String getDescription() {
329 * Get a document edit icon file name according to the document state.
331 * @return the icon file name
333 public String getEditIcon() {
334 return "icon.ed" + _state + ICON_EXT;
340 * @see org.splat.simer.HistoryFacade#getFileIcon()
342 public String getFileIcon() {
347 * Get the document history presentation.
349 * @return the history facade
351 public List<HistoryFacade> getHistory() {
356 * Get the document persistent id.
358 * @return the document persistent id
360 public String getIndex() {
361 return String.valueOf(_mydoc.getIndex());
365 * Get the document contextual popup menu.
367 * @return the document popup menu
369 public PopupMenu getPopup() { // Contextualizes the pop-up
371 .setContext("document", new DocumentRights(_owner.getUser(),
373 return _popup; // callers must "use" the returned pop-up before getting another pop-up
377 * Get the document presentation state: open, deep open, closed.
379 * @return the document presentation state
380 * @see DocumentFacade.State
382 public String getPresentationState() {
383 return _display.toString();
387 * Get document progress state.
389 * @return the document state
392 public String getProgressState() {
393 return _state.toString();
399 * @see org.splat.simer.HistoryFacade#getSharingIcon()
401 public String getSharingIcon() {
408 * @see org.splat.simer.HistoryFacade#getSize()
410 public String getSize() {
415 * Get a document state icon file name according to the document state.
417 * @return the icon file name
419 public String getStateIcon() {
420 return "icon." + _state + ICON_EXT;
424 * Get the document title.
426 * @return the document title
428 public String getTitle() {
429 return _mydoc.getTitle();
433 * Get the URL of the document in the repository.
435 * @return the document URL
437 public String getURL() {
442 * Get the list of used documents presentations.
444 * @return list of DocumentFacades
446 public List<DocumentFacade> getUses() {
453 * @see org.splat.simer.HistoryFacade#getVersion()
455 public String getVersion() {
462 * @see org.splat.simer.HistoryFacade#getVersioningIcon()
464 public String getVersioningIcon() {
469 * Check if this is a representation of the given document publication.
472 * the document publication
473 * @return true if this facades represents the given document publication
475 public boolean isFacadeOf(final Publication represented) {
476 return _me.equals(represented);
479 // ==============================================================================================================================
480 // Protected services
481 // ==============================================================================================================================
484 * Refresh the document presentation.
486 protected final void refresh() {
487 ResourceBundle custom = ResourceBundle.getBundle("som",
488 getApplicationSettings().getCurrentLocale());
489 DecimalFormat sizstring = new DecimalFormat(custom
490 .getString("size.format")); // Locale size display format
491 SimpleDateFormat datstring = new SimpleDateFormat(custom
492 .getString("date.format"), getApplicationSettings()
493 .getCurrentLocale()); // Locale date display format
494 String path = _mydoc.getSourceFile().getRelativePath();
495 String[] mapping = ApplicationSettings.getViewersMapping();
497 for (int i = 0; i < mapping.length; i++) {
498 org.splat.dal.bo.som.File export = _mydoc
499 .getAttachedFile(mapping[i]);
500 if (export != null) {
501 path = export.getRelativePath();
505 _surl = getApplicationSettings().getRepositoryURL() + path;
506 _surl = _surl.replaceAll("'", "\\\\'");
508 // Document state (overridable by the publication - see below)
509 _state = _mydoc.getProgressState();
510 _version = _mydoc.getVersion(); // May be null
514 // Document description
515 VersionsRelation versions = (VersionsRelation) _mydoc
516 .getFirstRelation(VersionsRelation.class);
517 if (versions != null) {
518 _description = versions.getDescription();
521 Revision.Format verstring = new Revision.Format(
522 getProjectSettings().getRevisionPattern());
523 _version = verstring.format(_version);
524 _size = sizstring.format(_mydoc.getSourceFile().asFile().length() / 1000);
525 _date = datstring.format(_mydoc.getLastModificationDate());
527 // Refresh of the history in case of promotion
528 if (_display == State.deepopen) {
530 this.collectHistory(_mydoc);
536 * Initialize document presentation icons.
538 private final void initIcons() {
540 String format = _mydoc.getFormat();
541 if ("xml".equals(format)) {
542 format = XMLDocument.getActualFormat(_mydoc.getSourceFile()
544 if ("schema".equals(_mydoc.getType().getName())) {
549 _icon = "icon." + format + ICON_EXT;
550 _sharing = "image.hold.gif";
551 _updated = "image.hold.gif";
552 File image = new File(ApplicationSettings.getApplicationSkinPath()
554 if (!image.exists()) {
555 _icon = "icon.any.png";
558 if (_me == null) { // Facade in the history folder
559 if (_mydoc.isVersioned()) { // History of the last version
560 if (_mydoc.isPublished()) {
561 _sharing = "image.share.png"; // Not correct if published in a previous version of the study
562 _updated = "icon.hold.png";
565 if (_state == ProgressState.inWORK) {
566 _icon = "icon." + _state.toString() + ICON_EXT;
568 _icon = "icon.inWORK.png";
572 if (_me.getOwnerStudy().shares(_mydoc)) {
573 _sharing = "image.share.png";
574 _updated = "icon.hold.png";
576 //if (_me.isOutdated()) {
577 // _state = ProgressState.inWORK; // Overrides the document state
583 * Initialize document's popup menu.
585 private final void initPopupMenu() {
587 if (_me != null) { // There is a pop-up
588 if (_state == ProgressState.EXTERN) {
589 _popup = getApplicationSettings().getPopupMenu("extern");
590 } else if (_state == ProgressState.inWORK) {
591 _popup = getApplicationSettings().getPopupMenu("editable");
592 } else if (_state == ProgressState.inDRAFT) {
593 if(_owner.getUser().equals(_me.value().getAuthor())) { //connected usr is the author of the document
594 _popup = getApplicationSettings().getPopupMenu("reviewable");
596 _popup = getApplicationSettings().getPopupMenu("reviewableNonAuthor");
598 } else if (_state == ProgressState.APPROVED) {
599 _popup = getApplicationSettings().getPopupMenu("approved");
600 } else { // (state == ProgressState.inCHECK)
601 DocumentType aType = _me.value().getType(); // Only result documents need to be approved
602 Step aStep = getPublicationService().getInvolvedStep(_me);
603 if (aType.isResultOf(aStep.getStep())) {
604 if(_owner.getUser().equals(_me.value().getAuthor())) {
605 _popup = getApplicationSettings().getPopupMenu("approvable");
607 _popup = getApplicationSettings().getPopupMenu("approvableNonAuthor");
610 if(_owner.getUser().equals(_me.value().getAuthor())) { //connected usr is the author of the document
611 _popup = getApplicationSettings().getPopupMenu("notresult");
613 _popup = getApplicationSettings().getPopupMenu("notresultNonAuthor");
621 * Turn on the versioning icon.
623 protected void setVersioned() {
624 _updated = "image.modified.png";
627 // ==============================================================================================================================
629 // ==============================================================================================================================
632 * Collect the document's previous versions presentation.
635 * the document to explore
637 private final void collectHistory(final Document given) {
638 VersionsRelation versions = (VersionsRelation) given
639 .getFirstRelation(VersionsRelation.class);
640 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
642 for (int i = stamp.length - 1; i > -1; i--) {
643 _history.add(new StampFacade(stamp[i], getApplicationSettings()
644 .getCurrentLocale()));
646 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
647 getPublicationService(), getApplicationSettings()));
648 if (versions != null) {
649 this.collectHistory(versions.getTo());
654 * Get project settings.
656 * @return Project settings service
658 private ProjectSettingsService getProjectSettings() {
659 return _projectSettings;
663 * Set project settings service.
665 * @param projectSettingsService
666 * project settings service
668 public final void setProjectSettings(
669 final ProjectSettingsService projectSettingsService) {
670 _projectSettings = projectSettingsService;
674 * Get the publicationService.
676 * @return the publicationService
678 public PublicationService getPublicationService() {
679 return _publicationService;
683 * Set the publicationService.
685 * @param publicationService
686 * the publicationService to set
688 public final void setPublicationService(
689 final PublicationService publicationService) {
690 _publicationService = publicationService;
694 * Get the applicationSettings.
696 * @return the applicationSettings
698 public ApplicationSettings getApplicationSettings() {
699 return _applicationSettings;
703 * Set the applicationSettings.
705 * @param applicationSettings
706 * the applicationSettings to set
708 public final void setApplicationSettings(
709 final ApplicationSettings applicationSettings) {
710 _applicationSettings = applicationSettings;