1 package org.splat.simer;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
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.som.ConvertsRelation;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.DocumentType;
20 import org.splat.dal.bo.som.ProgressState;
21 import org.splat.dal.bo.som.Publication;
22 import org.splat.dal.bo.som.Timestamp;
23 import org.splat.dal.bo.som.UsesRelation;
24 import org.splat.dal.bo.som.VersionsRelation;
25 import org.splat.manox.XMLDocument;
26 import org.splat.service.PublicationService;
27 import org.splat.service.technical.ProjectSettingsService;
28 import org.splat.som.DocumentRights;
29 import org.splat.som.Revision;
30 import org.splat.som.Step;
31 import org.splat.wapp.PopupMenu;
34 * Document wrapper class for presentation layer.
36 public class DocumentFacade implements HistoryFacade {
41 static private final String ICON_EXT = ".png";
45 private final transient AbstractOpenObject _owner;
47 * Wrapped document publication.
49 private final transient Publication _me;
53 private final transient Document _mydoc;
57 private transient ProgressState _state;
59 * My document version in customized format.
61 private transient String _version;
63 * Presentation state: open, deep open, closed.
65 * @see DocumentFacade.State
67 private transient State _display;
69 * URL of the source file.
71 private transient String _surl;
75 private transient String _icon;
77 * Icon qualifying sharing between scenarios of a same study.
79 private transient String _sharing;
81 * Icon qualifying versioning from the previous study version.
83 private transient String _updated;
87 private transient String _size;
89 * Document last modification date.
91 private transient String _date;
93 * Document's description.
95 private transient String _description;
97 * List of used documents presentations.
99 private transient List<DocumentFacade> _uses;
101 * List of attached files presentations.
103 private transient List<FileFacade> _exports;
105 * List of the document's history nodes.
107 private transient List<HistoryFacade> _history;
109 * Document's popup menu.
111 private transient PopupMenu _popup;
113 * Injected project settings service.
115 private ProjectSettingsService _projectSettings;
117 * Injected publication service.
119 private PublicationService _publicationService;
121 * Injected application settings.
123 private ApplicationSettings _applicationSettings;
126 * Document presentation state enumeration.
130 * Closed presentation state.
134 * Open presentation state (history and attached files nodes are visible).
138 * Deeply open presentation.
143 // ==============================================================================================================================
145 // ==============================================================================================================================
153 * document presentation
154 * @param projectSettings
155 * project settings service
156 * @param publicationService
157 * publication service
158 * @param applicationSettings
159 * application settings
161 public DocumentFacade(final AbstractOpenObject opened,
162 final Publication represented,
163 final ProjectSettingsService projectSettings,
164 final PublicationService publicationService,
165 final ApplicationSettings applicationSettings) {
166 setProjectSettings(projectSettings);
167 setPublicationService(publicationService);
168 setApplicationSettings(applicationSettings);
171 _mydoc = _me.value();
172 _state = _mydoc.getProgressState();
173 _display = State.closed;
175 refresh(); // Initializes the presentation of my document //NOPMD:RKV: to be reviewed
179 * Constructs the facade of a document presented in the history folder.
184 * document presentation
185 * @param projectSettings
186 * project settings service
187 * @param publicationService
188 * publication service
189 * @param applicationSettings
190 * application settings
192 private DocumentFacade(final AbstractOpenObject opened,
193 final Document represented,
194 final ProjectSettingsService projectSettings,
195 final PublicationService publicationService,
196 final ApplicationSettings applicationSettings) {
197 setProjectSettings(projectSettings);
198 setPublicationService(publicationService);
199 setApplicationSettings(applicationSettings);
201 _me = null; // Marks the history context
202 _mydoc = represented;
203 _state = _mydoc.getProgressState(); // In reality, HISTORY
204 _display = State.open; // Because the given document is a history document
205 _description = ResourceBundle.getBundle("som",
206 applicationSettings.getCurrentLocale()).getString(
208 + " " + _mydoc.getAuthor().toString();
210 this.refresh(); // Initializes the presentation of my document
213 // ==============================================================================================================================
214 // Public member functions
215 // ==============================================================================================================================
218 * Open the presentation subtree.
220 public void develop() {
221 if (_display == State.open) { // Opening the history of document, if exist
222 if (_history.isEmpty()) {
223 collectHistory(_mydoc);
225 _display = State.deepopen;
226 } else { // Opening the document
228 List<Relation> relist = _me.value().getRelations(UsesRelation.class);
230 _uses = new ArrayList<DocumentFacade>(relist.size());
231 for (Relation relation : relist) {
232 Document used = ((UsesRelation)relation).getTo();
234 DocumentFacade facade = null;
236 Publication publication = _me.getOwner().getPublication(used);
237 if(publication == null) {
238 publication = _me.getOwnerStudy().getPublication(used);
240 if(publication != null) {
241 facade = _owner._docpres.get(publication.getIndex());
244 if (facade == null) {
245 facade = new DocumentFacade(_owner, used,
246 getProjectSettings(), getPublicationService(),
247 getApplicationSettings());
252 if (_exports == null) {
255 if (_history == null) {
256 if (_mydoc.getPreviousVersion() != null
257 || _state == ProgressState.inCHECK
258 || _state == ProgressState.APPROVED) {
259 _history = new ArrayList<HistoryFacade>();
262 _display = State.open;
267 * Update the list of export files.
269 private void updateExports() {
270 List<Relation> relations = _mydoc
271 .getRelations(ConvertsRelation.class);
273 _exports = new ArrayList<FileFacade>(relations.size());
274 for (Relation rel : relations) {
275 _exports.add(new FileFacade((ConvertsRelation)rel,
276 getApplicationSettings()));
281 * Close the presentation tree partially.
283 public void reduce() {
284 if (_display == State.deepopen) {
285 _display = State.open;
290 * Close the presentation tree.
292 public void reduceAll() {
293 _display = State.closed;
296 // ==============================================================================================================================
298 // ==============================================================================================================================
301 * Get a list of attached files presentations.
303 * @return list of FileFacades
305 public List<FileFacade> getAttachments() {
312 * @see org.splat.simer.HistoryFacade#getDate()
314 public String getDate() {
321 * @see org.splat.simer.HistoryFacade#getDescription()
323 public String getDescription() {
328 * Get a document edit icon file name according to the document state.
330 * @return the icon file name
332 public String getEditIcon() {
333 return "icon.ed" + _state + ICON_EXT;
339 * @see org.splat.simer.HistoryFacade#getFileIcon()
341 public String getFileIcon() {
346 * Get the document history presentation.
348 * @return the history facade
350 public List<HistoryFacade> getHistory() {
355 * Get the document persistent id.
357 * @return the document persistent id
359 public String getIndex() {
360 return String.valueOf(_mydoc.getIndex());
364 * Get the document contextual popup menu.
366 * @return the document popup menu
368 public PopupMenu getPopup() { // Contextualizes the pop-up
370 .setContext("document", new DocumentRights(_owner.getUser(),
372 return _popup; // callers must "use" the returned pop-up before getting another pop-up
376 * Get the document presentation state: open, deep open, closed.
378 * @return the document presentation state
379 * @see DocumentFacade.State
381 public String getPresentationState() {
382 return _display.toString();
386 * Get document progress state.
388 * @return the document state
391 public String getProgressState() {
392 return _state.toString();
398 * @see org.splat.simer.HistoryFacade#getSharingIcon()
400 public String getSharingIcon() {
407 * @see org.splat.simer.HistoryFacade#getSize()
409 public String getSize() {
414 * Get a document state icon file name according to the document state.
416 * @return the icon file name
418 public String getStateIcon() {
419 return "icon." + _state + ICON_EXT;
423 * Get the document title.
425 * @return the document title
427 public String getTitle() {
428 return _mydoc.getTitle();
432 * Get the URL of the document in the repository.
434 * @return the document URL
436 public String getURL() {
441 * Get the list of used documents presentations.
443 * @return list of DocumentFacades
445 public List<DocumentFacade> getUses() {
452 * @see org.splat.simer.HistoryFacade#getVersion()
454 public String getVersion() {
461 * @see org.splat.simer.HistoryFacade#getVersioningIcon()
463 public String getVersioningIcon() {
468 * Check if this is a representation of the given document publication.
471 * the document publication
472 * @return true if this facades represents the given document publication
474 public boolean isFacadeOf(final Publication represented) {
475 return _me.equals(represented);
478 // ==============================================================================================================================
479 // Protected services
480 // ==============================================================================================================================
483 * Refresh the document presentation.
485 protected final void refresh() {
486 ResourceBundle custom = ResourceBundle.getBundle("som",
487 getApplicationSettings().getCurrentLocale());
488 DecimalFormat sizstring = new DecimalFormat(custom
489 .getString("size.format")); // Locale size display format
490 SimpleDateFormat datstring = new SimpleDateFormat(custom
491 .getString("date.format"), getApplicationSettings()
492 .getCurrentLocale()); // Locale date display format
493 String path = _mydoc.getSourceFile().getRelativePath();
494 String[] mapping = ApplicationSettings.getViewersMapping();
496 for (int i = 0; i < mapping.length; i++) {
497 org.splat.dal.bo.som.File export = _mydoc
498 .getAttachedFile(mapping[i]);
499 if (export != null) {
500 path = export.getRelativePath();
504 _surl = getApplicationSettings().getRepositoryURL() + path;
505 _surl = _surl.replaceAll("'", "\\\\'");
507 // Document state (overridable by the publication - see below)
508 _state = _mydoc.getProgressState();
509 _version = _mydoc.getVersion(); // May be null
513 // Document description
514 VersionsRelation versions = (VersionsRelation) _mydoc
515 .getFirstRelation(VersionsRelation.class);
516 if (versions != null) {
517 _description = versions.getDescription();
520 Revision.Format verstring = new Revision.Format(
521 getProjectSettings().getRevisionPattern());
522 _version = verstring.format(_version);
523 _size = sizstring.format(_mydoc.getSourceFile().asFile().length() / 1000);
524 _date = datstring.format(_mydoc.getLastModificationDate());
526 // Refresh of the history in case of promotion
527 if (_display == State.deepopen) {
529 this.collectHistory(_mydoc);
535 * Initialize document presentation icons.
537 private final void initIcons() {
539 String format = _mydoc.getFormat();
540 if ("xml".equals(format)) {
541 format = XMLDocument.getActualFormat(_mydoc.getSourceFile()
543 if ("schema".equals(_mydoc.getType().getName())) {
548 _icon = "icon." + format + ICON_EXT;
549 _sharing = "image.hold.gif";
550 _updated = "image.hold.gif";
551 File image = new File(ApplicationSettings.getApplicationSkinPath()
553 if (!image.exists()) {
554 _icon = "icon.any.png";
557 if (_me == null) { // Facade in the history folder
558 if (_mydoc.isVersioned()) { // History of the last version
559 if (_mydoc.isPublished()) {
560 _sharing = "image.share.png"; // Not correct if published in a previous version of the study
561 _updated = "icon.hold.png";
564 if (_state == ProgressState.inWORK) {
565 _icon = "icon." + _state.toString() + ICON_EXT;
567 _icon = "icon.inWORK.png";
571 if (_me.getOwnerStudy().shares(_mydoc)) {
572 _sharing = "image.share.png";
573 _updated = "icon.hold.png";
575 //if (_me.isOutdated()) {
576 // _state = ProgressState.inWORK; // Overrides the document state
582 * Initialize document's popup menu.
584 private final void initPopupMenu() {
586 if (_me != null) { // There is a pop-up
587 if (_state == ProgressState.EXTERN) {
588 _popup = getApplicationSettings().getPopupMenu("extern");
589 } else if (_state == ProgressState.inWORK) {
590 _popup = getApplicationSettings().getPopupMenu("editable");
591 } else if (_state == ProgressState.inDRAFT) {
592 _popup = getApplicationSettings().getPopupMenu("reviewable");
593 } else if (_state == ProgressState.APPROVED) {
594 _popup = getApplicationSettings().getPopupMenu("approved");
595 } else { // (state == ProgressState.inCHECK)
596 DocumentType aType = _me.value().getType(); // Only result documents need to be approved
597 Step aStep = getPublicationService().getInvolvedStep(_me);
598 if (aType.isResultOf(aStep.getStep())) {
599 _popup = getApplicationSettings()
600 .getPopupMenu("approvable");
602 _popup = getApplicationSettings().getPopupMenu("notresult");
609 * Turn on the versioning icon.
611 protected void setVersioned() {
612 _updated = "image.modified.png";
615 // ==============================================================================================================================
617 // ==============================================================================================================================
620 * Collect the document's previous versions presentation.
623 * the document to explore
625 private final void collectHistory(final Document given) {
626 VersionsRelation versions = (VersionsRelation) given
627 .getFirstRelation(VersionsRelation.class);
628 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
630 for (int i = stamp.length - 1; i > -1; i--) {
631 _history.add(new StampFacade(stamp[i], getApplicationSettings()
632 .getCurrentLocale()));
634 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
635 getPublicationService(), getApplicationSettings()));
636 if (versions != null) {
637 this.collectHistory(versions.getTo());
642 * Get project settings.
644 * @return Project settings service
646 private ProjectSettingsService getProjectSettings() {
647 return _projectSettings;
651 * Set project settings service.
653 * @param projectSettingsService
654 * project settings service
656 public final void setProjectSettings(
657 final ProjectSettingsService projectSettingsService) {
658 _projectSettings = projectSettingsService;
662 * Get the publicationService.
664 * @return the publicationService
666 public PublicationService getPublicationService() {
667 return _publicationService;
671 * Set the publicationService.
673 * @param publicationService
674 * the publicationService to set
676 public final void setPublicationService(
677 final PublicationService publicationService) {
678 _publicationService = publicationService;
682 * Get the applicationSettings.
684 * @return the applicationSettings
686 public ApplicationSettings getApplicationSettings() {
687 return _applicationSettings;
691 * Set the applicationSettings.
693 * @param applicationSettings
694 * the applicationSettings to set
696 public final void setApplicationSettings(
697 final ApplicationSettings applicationSettings) {
698 _applicationSettings = applicationSettings;