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 {
39 private final AbstractOpenObject owner;
40 private final Publication me;
41 private final Document my; // Published document
42 private ProgressState state; // Document state
43 private String version; // My document version in customized format
44 private State display; // Presentation state
45 private String surl; // URL of the source file
46 private String format; // Extension of the source file
47 private String icon; // Corresponding icon
48 private String sharing; // Icon qualifying sharing between scenarios of a same study
49 private String updated; // Icon qualifying versioning from the previous study version
52 private String description;
53 private List<DocumentFacade> uses;
54 private List<FileFacade> exports;
55 private List<HistoryFacade> history;
56 private PopupMenu popup;
58 * Injected project settings service.
60 private ProjectSettingsService _projectSettingsService;
62 * Injected publication service.
64 private PublicationService _publicationService;
66 * Injected application settings.
68 private ApplicationSettings _applicationSettings;
71 closed, open, deepopen
74 // ==============================================================================================================================
76 // ==============================================================================================================================
78 public DocumentFacade(final AbstractOpenObject opened,
79 final Publication represented,
80 final ProjectSettingsService projectSettings,
81 final PublicationService publicationService,
82 final ApplicationSettings applicationSettings) {
83 setProjectSettings(projectSettings);
84 setPublicationService(publicationService);
85 setApplicationSettings(applicationSettings);
89 state = my.getProgressState();
90 display = State.closed;
97 this.refresh(); // Initializes the presentation of my document
101 * Constructs the facade of a document presented in the history folder.
104 * the represented history document
106 private DocumentFacade(final AbstractOpenObject opened,
107 final Document represented,
108 final ProjectSettingsService projectSettings,
109 final PublicationService publicationService,
110 final ApplicationSettings applicationSettings) {
111 setProjectSettings(projectSettings);
112 setPublicationService(publicationService);
113 setApplicationSettings(applicationSettings);
115 me = null; // Marks the history context
117 state = my.getProgressState(); // In reality, HISTORY
118 display = State.open; // Because the given document is a history document
119 description = ResourceBundle.getBundle("som",
120 applicationSettings.getCurrentLocale()).getString(
122 + " " + my.getAuthor().toString();
128 this.refresh(); // Initializes the presentation of my document
131 // ==============================================================================================================================
132 // Public member functions
133 // ==============================================================================================================================
135 public void develop() {
136 if (display != State.open) { // Opening the document
138 List<Publication> relist = me.getRelations(UsesRelation.class);
140 uses = new ArrayList<DocumentFacade>(relist.size());
141 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
142 Publication used = i.next();
143 long index = used.getIndex();
144 DocumentFacade facade = owner.docpres
146 if (facade == null) {
147 facade = new DocumentFacade(owner, used,
148 getProjectSettings(), getPublicationService(),
149 getApplicationSettings());
150 owner.docpres.put(index, facade);
155 if (exports == null) {
156 List<Relation> relation = my
157 .getRelations(ConvertsRelation.class);
159 exports = new ArrayList<FileFacade>(relation.size());
160 for (Iterator<Relation> i = relation.iterator(); i.hasNext();) {
161 ConvertsRelation export = (ConvertsRelation) i.next();
163 .add(new FileFacade(export,
164 getApplicationSettings()));
167 if (history == null) {
168 if (my.getPreviousVersion() != null
169 || state == ProgressState.inCHECK
170 || state == ProgressState.APPROVED) {
171 history = new ArrayList<HistoryFacade>();
174 display = State.open;
175 } else { // Opening the history of document, if exist
176 if (history.isEmpty()) {
179 display = State.deepopen;
183 public void reduce() {
184 if (display == State.deepopen) {
185 display = State.open;
189 public void reduceAll() {
190 display = State.closed;
193 // ==============================================================================================================================
195 // ==============================================================================================================================
197 public List<FileFacade> getAttachments() {
201 public String getDate() {
205 public String getDescription() {
209 public String getEditIcon() {
210 return "icon.ed" + state + ".png";
213 public String getFileIcon() {
217 public List<HistoryFacade> getHistory() {
221 public String getIndex() {
222 return String.valueOf(my.getIndex());
225 public PopupMenu getPopup() { // Contextualizes the pop-up
226 popup.setContext("document", new DocumentRights(owner.getUser(), me));
227 return popup; // callers must "use" the returned pop-up before getting another pop-up
230 public String getPresentationState() {
231 return display.toString();
234 public String getProgressState() {
235 return state.toString();
238 public String getSharingIcon() {
242 public String getSize() {
246 public String getStateIcon() {
247 return "icon." + state + ".png";
250 public String getTitle() {
251 return my.getTitle();
254 public String getURL() {
258 public List<DocumentFacade> getUses() {
262 public String getVersion() {
266 public String getVersioningIcon() {
270 public boolean isFacadeOf(final Publication represented) {
271 return me.equals(represented);
274 // ==============================================================================================================================
275 // Protected services
276 // ==============================================================================================================================
278 protected void refresh() {
279 ResourceBundle custom = ResourceBundle.getBundle("som",
280 getApplicationSettings().getCurrentLocale());
281 DecimalFormat sizstring = new DecimalFormat(custom
282 .getString("size.format")); // Locale size display format
283 SimpleDateFormat datstring = new SimpleDateFormat(custom
284 .getString("date.format")); // Locale date display format
285 Revision.Format verstring = new Revision.Format(getProjectSettings()
286 .getRevisionPattern());
287 String path = my.getSourceFile().getRelativePath();
288 String[] mapping = ApplicationSettings.getViewersMapping();
290 for (int i = 0; i < mapping.length; i++) {
291 org.splat.dal.bo.som.File export = my.getAttachedFile(mapping[i]);
292 if (export == null) {
295 path = export.getRelativePath();
298 surl = getApplicationSettings().getRepositoryURL() + path;
299 surl = surl.replaceAll("'", "\\\\'");
300 format = my.getFormat();
301 if (format.equals("xml")) {
302 format = XMLDocument.getActualFormat(my.getSourceFile().asFile());
305 // Document state (overridable by the publication - see below)
306 state = my.getProgressState();
307 version = my.getVersion(); // May be null
310 icon = "icon." + format + ".png";
311 sharing = "image.hold.gif";
312 updated = "image.hold.gif";
313 File image = new File(ApplicationSettings.getApplicationSkinPath()
315 if (!image.exists()) {
316 icon = "icon.any.png";
320 if (me.getOwnerStudy().shares(my)) {
321 sharing = "image.share.png";
322 updated = "icon.hold.png";
324 if (me.isOutdated()) {
325 state = ProgressState.inWORK; // Overrides the document state
328 } else { // Facade in the history folder
329 if (!my.isVersioned()) { // History of the last version
330 if (state != ProgressState.inWORK) {
331 icon = "icon.inWORK.png";
333 icon = "icon." + state.toString() + ".png";
335 } else if (my.isPublished()) {
336 sharing = "image.share.png"; // Not correct if published in a previous version of the study
337 updated = "icon.hold.png";
340 // Document description
341 VersionsRelation versions = (VersionsRelation) my
342 .getFirstRelation(VersionsRelation.class);
343 if (versions != null) {
344 description = versions.getDescription();
347 if (state != ProgressState.EXTERN) {
348 version = verstring.format(version);
350 size = sizstring.format(my.getSourceFile().asFile().length() / 1000);
351 date = datstring.format(my.getLastModificationDate());
353 // Refresh of the history in case of promotion
354 if (display == State.deepopen) {
360 return; // No pop-up (yet) in the history folder
362 if (state == ProgressState.EXTERN) {
363 popup = getApplicationSettings().getPopupMenu("extern");
364 } else if (state == ProgressState.inWORK) {
365 popup = getApplicationSettings().getPopupMenu("editable");
366 } else if (state == ProgressState.inDRAFT) {
367 popup = getApplicationSettings().getPopupMenu("reviewable");
368 } else if (state == ProgressState.APPROVED) {
369 popup = getApplicationSettings().getPopupMenu("approved");
370 } else { // (state == ProgressState.inCHECK)
371 DocumentType mytype = me.value().getType(); // Only result documents need to be approved
372 Step mystep = getPublicationService().getInvolvedStep(me);
373 if (mytype.isResultOf(mystep.getStep())) {
374 popup = getApplicationSettings().getPopupMenu("approvable");
376 popup = getApplicationSettings().getPopupMenu("notresult");
381 protected void setVersioned() {
382 updated = "image.modified.png";
385 // ==============================================================================================================================
387 // ==============================================================================================================================
389 private void collectHistory(final Document given) {
390 VersionsRelation versions = (VersionsRelation) given
391 .getFirstRelation(VersionsRelation.class);
392 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
394 for (int i = stamp.length - 1; i > -1; i--) {
395 history.add(new StampFacade(stamp[i], getApplicationSettings()
396 .getCurrentLocale()));
398 history.add(new DocumentFacade(owner, given, getProjectSettings(),
399 getPublicationService(), getApplicationSettings()));
400 if (versions != null) {
401 collectHistory(versions.getTo());
406 * Get project settings.
408 * @return Project settings service
410 private ProjectSettingsService getProjectSettings() {
411 return _projectSettingsService;
415 * Set project settings service.
417 * @param projectSettingsService
418 * project settings service
420 public void setProjectSettings(
421 final ProjectSettingsService projectSettingsService) {
422 _projectSettingsService = projectSettingsService;
426 * Get the publicationService.
428 * @return the publicationService
430 public PublicationService getPublicationService() {
431 return _publicationService;
435 * Set the publicationService.
437 * @param publicationService
438 * the publicationService to set
440 public void setPublicationService(
441 final PublicationService publicationService) {
442 _publicationService = publicationService;
446 * Get the applicationSettings.
448 * @return the applicationSettings
450 public ApplicationSettings getApplicationSettings() {
451 return _applicationSettings;
455 * Set the applicationSettings.
457 * @param applicationSettings
458 * the applicationSettings to set
460 public void setApplicationSettings(
461 final ApplicationSettings applicationSettings) {
462 _applicationSettings = applicationSettings;