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.
181 * the represented history document
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);
192 _me = null; // Marks the history context
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(
199 + " " + _my.getAuthor().toString();
205 this.refresh(); // Initializes the presentation of my document
208 // ==============================================================================================================================
209 // Public member functions
210 // ==============================================================================================================================
212 public void develop() {
213 if (_display == State.open) { // Opening the history of document, if exist
214 if (_history.isEmpty()) {
217 _display = State.deepopen;
218 } else { // Opening the document
220 List<Publication> relist = _me.getRelations(UsesRelation.class);
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);
236 if (_exports == null) {
237 List<Relation> relation = _my
238 .getRelations(ConvertsRelation.class);
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()));
247 if (_history == null) {
248 if (_my.getPreviousVersion() != null
249 || _state == ProgressState.inCHECK
250 || _state == ProgressState.APPROVED) {
251 _history = new ArrayList<HistoryFacade>();
254 _display = State.open;
258 public void reduce() {
259 if (_display == State.deepopen) {
260 _display = State.open;
264 public void reduceAll() {
265 _display = State.closed;
268 // ==============================================================================================================================
270 // ==============================================================================================================================
272 public List<FileFacade> getAttachments() {
276 public String getDate() {
280 public String getDescription() {
284 public String getEditIcon() {
285 return "icon.ed" + _state + ICON_EXT;
288 public String getFileIcon() {
292 public List<HistoryFacade> getHistory() {
296 public String getIndex() {
297 return String.valueOf(_my.getIndex());
300 public PopupMenu getPopup() { // Contextualizes the pop-up
302 .setContext("document", new DocumentRights(_owner.getUser(),
304 return _popup; // callers must "use" the returned pop-up before getting another pop-up
307 public String getPresentationState() {
308 return _display.toString();
311 public String getProgressState() {
312 return _state.toString();
315 public String getSharingIcon() {
319 public String getSize() {
323 public String getStateIcon() {
324 return "icon." + _state + ICON_EXT;
327 public String getTitle() {
328 return _my.getTitle();
331 public String getURL() {
335 public List<DocumentFacade> getUses() {
339 public String getVersion() {
343 public String getVersioningIcon() {
347 public boolean isFacadeOf(final Publication represented) {
348 return _me.equals(represented);
351 // ==============================================================================================================================
352 // Protected services
353 // ==============================================================================================================================
356 * Refresh the document presentation.
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();
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();
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());
384 // Document state (overridable by the publication - see below)
385 _state = _my.getProgressState();
386 _version = _my.getVersion(); // May be null
389 _icon = "icon." + format + ICON_EXT;
390 _sharing = "image.hold.gif";
391 _updated = "image.hold.gif";
392 File image = new File(ApplicationSettings.getApplicationSkinPath()
394 if (!image.exists()) {
395 _icon = "icon.any.png";
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";
405 if (_state == ProgressState.inWORK) {
406 _icon = "icon." + _state.toString() + ICON_EXT;
408 _icon = "icon.inWORK.png";
412 if (_me.getOwnerStudy().shares(_my)) {
413 _sharing = "image.share.png";
414 _updated = "icon.hold.png";
416 if (_me.isOutdated()) {
417 _state = ProgressState.inWORK; // Overrides the document state
420 // Document description
421 VersionsRelation versions = (VersionsRelation) _my
422 .getFirstRelation(VersionsRelation.class);
423 if (versions != null) {
424 _description = versions.getDescription();
427 if (_state != ProgressState.EXTERN) {
428 _version = verstring.format(_version);
430 _size = sizstring.format(_my.getSourceFile().asFile().length() / 1000);
431 _date = datstring.format(_my.getLastModificationDate());
433 // Refresh of the history in case of promotion
434 if (_display == State.deepopen) {
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");
455 _popup = getApplicationSettings().getPopupMenu("notresult");
462 * Turn on the versioning icon.
464 protected void setVersioned() {
465 _updated = "image.modified.png";
468 // ==============================================================================================================================
470 // ==============================================================================================================================
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
477 for (int i = stamp.length - 1; i > -1; i--) {
478 _history.add(new StampFacade(stamp[i], getApplicationSettings()
479 .getCurrentLocale()));
481 _history.add(new DocumentFacade(_owner, given, getProjectSettings(),
482 getPublicationService(), getApplicationSettings()));
483 if (versions != null) {
484 collectHistory(versions.getTo());
489 * Get project settings.
491 * @return Project settings service
493 private ProjectSettingsService getProjectSettings() {
494 return _projectSettings;
498 * Set project settings service.
500 * @param projectSettingsService
501 * project settings service
503 public final void setProjectSettings(
504 final ProjectSettingsService projectSettingsService) {
505 _projectSettings = projectSettingsService;
509 * Get the publicationService.
511 * @return the publicationService
513 public PublicationService getPublicationService() {
514 return _publicationService;
518 * Set the publicationService.
520 * @param publicationService
521 * the publicationService to set
523 public final void setPublicationService(
524 final PublicationService publicationService) {
525 _publicationService = publicationService;
529 * Get the applicationSettings.
531 * @return the applicationSettings
533 public ApplicationSettings getApplicationSettings() {
534 return _applicationSettings;
538 * Set the applicationSettings.
540 * @param applicationSettings
541 * the applicationSettings to set
543 public final void setApplicationSettings(
544 final ApplicationSettings applicationSettings) {
545 _applicationSettings = applicationSettings;