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.Iterator;
14 import java.util.List;
15 import java.util.ResourceBundle;
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;
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 _my;
58 private transient ProgressState _state;
60 * My document version in customized format.
62 private transient String _version;
66 private transient State _display;
68 * URL of the source file.
70 private transient String _surl;
74 private transient String _icon;
76 * Icon qualifying sharing between scenarios of a same study.
78 private transient String _sharing;
80 * Icon qualifying versioning from the previous study version.
82 private transient String _updated;
86 private transient String _size;
88 * Document last modification date.
90 private transient String _date;
92 * Document's description.
94 private transient String _description;
96 * List of used documents presentations.
98 private transient List<DocumentFacade> _uses;
100 * List of attached files presentations.
102 private transient List<FileFacade> _exports;
104 * List of the document's history nodes.
106 private transient List<HistoryFacade> _history;
108 * Document's popup menu.
110 private transient PopupMenu _popup;
112 * Injected project settings service.
114 private ProjectSettingsService _projectSettings;
116 * Injected publication service.
118 private PublicationService _publicationService;
120 * Injected application settings.
122 private ApplicationSettings _applicationSettings;
125 * Document presentation state enumeration.
129 * Closed presentation state.
133 * Open presentation state (history and attached files nodes are visible).
137 * Deeply open presentation.
142 // ==============================================================================================================================
144 // ==============================================================================================================================
152 * document presentation
153 * @param projectSettings
154 * project settings service
155 * @param publicationService
156 * publication service
157 * @param applicationSettings
158 * application settings
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);
171 _state = _my.getProgressState();
172 _display = State.closed;
174 refresh(); // Initializes the presentation of my document
178 * Constructs the facade of a document presented in the history folder.
183 * document presentation
184 * @param projectSettings
185 * project settings service
186 * @param publicationService
187 * publication service
188 * @param applicationSettings
189 * application settings
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);
200 _me = null; // Marks the history context
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(
207 + " " + _my.getAuthor().toString();
213 this.refresh(); // Initializes the presentation of my document
216 // ==============================================================================================================================
217 // Public member functions
218 // ==============================================================================================================================
221 * Open the presentation subtree.
223 public void develop() {
224 if (_display == State.open) { // Opening the history of document, if exist
225 if (_history.isEmpty()) {
228 _display = State.deepopen;
229 } else { // Opening the document
231 List<Publication> relist = _me.getRelations(UsesRelation.class);
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);
247 if (_exports == null) {
248 List<Relation> relation = _my
249 .getRelations(ConvertsRelation.class);
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()));
258 if (_history == null) {
259 if (_my.getPreviousVersion() != null
260 || _state == ProgressState.inCHECK
261 || _state == ProgressState.APPROVED) {
262 _history = new ArrayList<HistoryFacade>();
265 _display = State.open;
269 public void reduce() {
270 if (_display == State.deepopen) {
271 _display = State.open;
275 public void reduceAll() {
276 _display = State.closed;
279 // ==============================================================================================================================
281 // ==============================================================================================================================
283 public List<FileFacade> getAttachments() {
287 public String getDate() {
291 public String getDescription() {
295 public String getEditIcon() {
296 return "icon.ed" + _state + ICON_EXT;
299 public String getFileIcon() {
303 public List<HistoryFacade> getHistory() {
307 public String getIndex() {
308 return String.valueOf(_my.getIndex());
311 public PopupMenu getPopup() { // Contextualizes the pop-up
313 .setContext("document", new DocumentRights(_owner.getUser(),
315 return _popup; // callers must "use" the returned pop-up before getting another pop-up
318 public String getPresentationState() {
319 return _display.toString();
322 public String getProgressState() {
323 return _state.toString();
326 public String getSharingIcon() {
330 public String getSize() {
334 public String getStateIcon() {
335 return "icon." + _state + ICON_EXT;
338 public String getTitle() {
339 return _my.getTitle();
342 public String getURL() {
346 public List<DocumentFacade> getUses() {
350 public String getVersion() {
354 public String getVersioningIcon() {
358 public boolean isFacadeOf(final Publication represented) {
359 return _me.equals(represented);
362 // ==============================================================================================================================
363 // Protected services
364 // ==============================================================================================================================
367 * Refresh the document presentation.
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();
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();
389 _surl = getApplicationSettings().getRepositoryURL() + path;
390 _surl = _surl.replaceAll("'", "\\\\'");
392 // Document state (overridable by the publication - see below)
393 _state = _my.getProgressState();
394 _version = _my.getVersion(); // May be null
398 // Document description
399 VersionsRelation versions = (VersionsRelation) _my
400 .getFirstRelation(VersionsRelation.class);
401 if (versions != null) {
402 _description = versions.getDescription();
405 if (_state != ProgressState.EXTERN) {
406 _version = verstring.format(_version);
408 _size = sizstring.format(_my.getSourceFile().asFile().length() / 1000);
409 _date = datstring.format(_my.getLastModificationDate());
411 // Refresh of the history in case of promotion
412 if (_display == State.deepopen) {
414 this.collectHistory(_my);
420 * Initialize document presentation icons.
422 private final void initIcons() {
424 String format = _my.getFormat();
425 if ("xml".equals(format)) {
426 format = XMLDocument.getActualFormat(_my.getSourceFile().asFile());
429 _icon = "icon." + format + ICON_EXT;
430 _sharing = "image.hold.gif";
431 _updated = "image.hold.gif";
432 File image = new File(ApplicationSettings.getApplicationSkinPath()
434 if (!image.exists()) {
435 _icon = "icon.any.png";
438 if (_me == null) { // Facade in the history folder
439 if (_my.isVersioned()) { // History of the last version
440 if (_my.isPublished()) {
441 _sharing = "image.share.png"; // Not correct if published in a previous version of the study
442 _updated = "icon.hold.png";
445 if (_state == ProgressState.inWORK) {
446 _icon = "icon." + _state.toString() + ICON_EXT;
448 _icon = "icon.inWORK.png";
452 if (_me.getOwnerStudy().shares(_my)) {
453 _sharing = "image.share.png";
454 _updated = "icon.hold.png";
456 if (_me.isOutdated()) {
457 _state = ProgressState.inWORK; // Overrides the document state
463 * Initialize document's popup menu.
465 private final void initPopupMenu() {
467 if (_me != null) { // There is a pop-up
468 if (_state == ProgressState.EXTERN) {
469 _popup = getApplicationSettings().getPopupMenu("extern");
470 } else if (_state == ProgressState.inWORK) {
471 _popup = getApplicationSettings().getPopupMenu("editable");
472 } else if (_state == ProgressState.inDRAFT) {
473 _popup = getApplicationSettings().getPopupMenu("reviewable");
474 } else if (_state == ProgressState.APPROVED) {
475 _popup = getApplicationSettings().getPopupMenu("approved");
476 } else { // (state == ProgressState.inCHECK)
477 DocumentType aType = _me.value().getType(); // Only result documents need to be approved
478 Step aStep = getPublicationService().getInvolvedStep(_me);
479 if (aType.isResultOf(aStep.getStep())) {
480 _popup = getApplicationSettings()
481 .getPopupMenu("approvable");
483 _popup = getApplicationSettings().getPopupMenu("notresult");
490 * Turn on the versioning icon.
492 protected void setVersioned() {
493 _updated = "image.modified.png";
496 // ==============================================================================================================================
498 // ==============================================================================================================================
500 private final void collectHistory(final Document given) {
501 VersionsRelation versions = (VersionsRelation) given
502 .getFirstRelation(VersionsRelation.class);
503 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
505 for (int i = stamp.length - 1; i > -1; i--) {
506 _history.add(new StampFacade(stamp[i], getApplicationSettings()
507 .getCurrentLocale()));
509 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
510 getPublicationService(), getApplicationSettings()));
511 if (versions != null) {
512 this.collectHistory(versions.getTo());
517 * Get project settings.
519 * @return Project settings service
521 private ProjectSettingsService getProjectSettings() {
522 return _projectSettings;
526 * Set project settings service.
528 * @param projectSettingsService
529 * project settings service
531 public final void setProjectSettings(
532 final ProjectSettingsService projectSettingsService) {
533 _projectSettings = projectSettingsService;
537 * Get the publicationService.
539 * @return the publicationService
541 public PublicationService getPublicationService() {
542 return _publicationService;
546 * Set the publicationService.
548 * @param publicationService
549 * the publicationService to set
551 public final void setPublicationService(
552 final PublicationService publicationService) {
553 _publicationService = publicationService;
557 * Get the applicationSettings.
559 * @return the applicationSettings
561 public ApplicationSettings getApplicationSettings() {
562 return _applicationSettings;
566 * Set the applicationSettings.
568 * @param applicationSettings
569 * the applicationSettings to set
571 public final void setApplicationSettings(
572 final ApplicationSettings applicationSettings) {
573 _applicationSettings = applicationSettings;