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.manox.XMLDocument;
18 import org.splat.dal.bo.kernel.Relation;
19 import org.splat.dal.bo.som.ConvertsRelation;
20 import org.splat.dal.bo.som.Document;
21 import org.splat.som.DocumentRights;
22 import org.splat.dal.bo.som.DocumentType;
23 import org.splat.dal.bo.som.ProgressState;
24 import org.splat.service.PublicationService;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.dal.bo.som.Publication;
27 import org.splat.som.Revision;
28 import org.splat.som.Step;
29 import org.splat.dal.bo.som.Timestamp;
30 import org.splat.dal.bo.som.UsesRelation;
31 import org.splat.dal.bo.som.VersionsRelation;
32 import org.splat.wapp.PopupMenu;
34 public class DocumentFacade implements HistoryFacade {
36 private OpenObject owner;
37 private Publication me;
38 private Document my; // Published document
39 private ProgressState state; // Document state
40 private String version; // My document version in customized format
41 private State display; // Presentation state
42 private String surl; // URL of the source file
43 private String format; // Extension of the source file
44 private String icon; // Corresponding icon
45 private String sharing; // Icon qualifying sharing between scenarios of a same study
46 private String updated; // Icon qualifying versioning from the previous study version
49 private String description;
50 private List<DocumentFacade> uses;
51 private List<FileFacade> exports;
52 private List<HistoryFacade> history;
53 private PopupMenu popup;
54 private ProjectSettingsService _projectSettingsService;
55 private PublicationService _publicationService;
58 closed, open, deepopen
61 // ==============================================================================================================================
63 // ==============================================================================================================================
65 public DocumentFacade(OpenObject opened, Publication represented,
66 ProjectSettingsService projectSettings,
67 PublicationService publicationService) {
68 // ------------------------------------------------------------------
69 setProjectSettings(projectSettings);
70 setPublicationService(publicationService);
74 state = my.getProgressState();
75 display = State.closed;
82 this.refresh(); // Initializes the presentation of my document
86 * Constructs the facade of a document presented in the history folder.
89 * the represented history document
91 private DocumentFacade(OpenObject opened, Document represented,
92 ProjectSettingsService projectSettings,
93 PublicationService publicationService) {
94 // ----------------------------------------------------------------
95 setProjectSettings(projectSettings);
96 setPublicationService(publicationService);
98 me = null; // Marks the history context
100 state = my.getProgressState(); // In reality, HISTORY
101 display = State.open; // Because the given document is a history document
102 description = ResourceBundle.getBundle("som",
103 ApplicationSettings.getCurrentLocale()).getString(
105 + " " + my.getAuthor().toString();
111 this.refresh(); // Initializes the presentation of my document
114 // ==============================================================================================================================
115 // Public member functions
116 // ==============================================================================================================================
118 public void develop() {
119 // -------------------
120 if (display != State.open) { // Opening the document
122 List<Publication> relist = me.getRelations(UsesRelation.class);
124 uses = new ArrayList<DocumentFacade>(relist.size());
125 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
126 Publication used = i.next();
127 long index = used.getIndex();
128 DocumentFacade facade = OpenObject.docpres.get(index);
129 if (facade == null) {
130 facade = new DocumentFacade(owner, used, getProjectSettings(), getPublicationService());
131 OpenObject.docpres.put(index, facade);
136 if (exports == null) {
137 List<Relation> relation = my
138 .getRelations(ConvertsRelation.class);
140 exports = new ArrayList<FileFacade>(relation.size());
141 for (Iterator<Relation> i = relation.iterator(); i.hasNext();) {
142 ConvertsRelation export = (ConvertsRelation) i.next();
143 exports.add(new FileFacade(export));
146 if (history == null) {
147 if (my.getPreviousVersion() != null
148 || state == ProgressState.inCHECK
149 || state == ProgressState.APPROVED)
150 history = new ArrayList<HistoryFacade>();
152 display = State.open;
153 } else { // Opening the history of document, if exist
154 if (history.isEmpty())
156 display = State.deepopen;
160 public void reduce() {
161 // ---------------------
162 if (display == State.deepopen)
163 display = State.open;
166 public void reduceAll() {
167 // ------------------------
168 display = State.closed;
171 // ==============================================================================================================================
173 // ==============================================================================================================================
175 public List<FileFacade> getAttachments() {
176 // ------------------------------------------
180 public String getDate() {
181 // ------------------------
185 public String getDescription() {
186 // -------------------------------
190 public String getEditIcon() {
191 // ----------------------------
192 return "icon.ed" + state + ".png";
195 public String getFileIcon() {
196 // ----------------------------
200 public List<HistoryFacade> getHistory() {
201 // ----------------------------------------
205 public String getIndex() {
206 // -------------------------
207 return String.valueOf(my.getIndex());
210 public PopupMenu getPopup() { // Contextualizes the pop-up
211 // ----------------------------
212 popup.setContext("document", new DocumentRights(owner.getUser(), me));
213 return popup; // callers must "use" the returned pop-up before getting another pop-up
216 public String getPresentationState() {
217 // -------------------------------------
218 return display.toString();
221 public String getProgressState() {
222 // ---------------------------------
223 return state.toString();
226 public String getSharingIcon() {
227 // -------------------------------
231 public String getSize() {
232 // ------------------------
236 public String getStateIcon() {
237 // -----------------------------
238 return "icon." + state + ".png";
241 public String getTitle() {
242 // -------------------------
243 return my.getTitle();
246 public String getURL() {
247 // -----------------------
251 public List<DocumentFacade> getUses() {
252 // ---------------------------------------
256 public String getVersion() {
257 // ---------------------------
261 public String getVersioningIcon() {
262 // ----------------------------------
266 public boolean isFacadeOf(Publication represented) {
267 // ---------------------------------------------------
268 return me.equals(represented);
271 // ==============================================================================================================================
272 // Protected services
273 // ==============================================================================================================================
275 protected void refresh() {
276 // -------------------------
277 ResourceBundle custom = ResourceBundle.getBundle("som",
278 ApplicationSettings.getCurrentLocale());
279 DecimalFormat sizstring = new DecimalFormat(custom
280 .getString("size.format")); // Locale size display format
281 SimpleDateFormat datstring = new SimpleDateFormat(custom
282 .getString("date.format")); // Locale date display format
283 Revision.Format verstring = new Revision.Format(getProjectSettings()
284 .getRevisionPattern());
285 String path = my.getSourceFile().getRelativePath();
286 String[] mapping = ApplicationSettings.getViewersMapping();
288 for (int i = 0; i < mapping.length; i++) {
289 org.splat.dal.bo.som.File export = my.getAttachedFile(mapping[i]);
292 path = export.getRelativePath();
295 surl = ApplicationSettings.getRepositoryURL() + path;
296 surl = surl.replaceAll("'", "\\\\'");
297 format = my.getFormat();
298 if (format.equals("xml"))
299 format = XMLDocument.getActualFormat(my.getSourceFile().asFile());
301 // Document state (overridable by the publication - see below)
302 state = my.getProgressState();
303 version = my.getVersion(); // May be null
306 icon = "icon." + format + ".png";
307 sharing = "image.hold.gif";
308 updated = "image.hold.gif";
309 File image = new File(ApplicationSettings.getApplicationSkinPath()
312 icon = "icon.any.png";
315 if (me.getOwnerStudy().shares(my)) {
316 sharing = "image.share.png";
317 updated = "icon.hold.png";
320 state = ProgressState.inWORK; // Overrides the document state
322 } else { // Facade in the history folder
323 if (!my.isVersioned()) { // History of the last version
324 if (state != ProgressState.inWORK)
325 icon = "icon.inWORK.png";
327 icon = "icon." + state.toString() + ".png";
328 } else if (my.isPublished()) {
329 sharing = "image.share.png"; // Not correct if published in a previous version of the study
330 updated = "icon.hold.png";
333 // Document description
334 VersionsRelation versions = (VersionsRelation) my
335 .getFirstRelation(VersionsRelation.class);
336 if (versions != null) {
337 description = versions.getDescription();
340 if (state != ProgressState.EXTERN)
341 version = verstring.format(version);
342 size = sizstring.format(my.getSourceFile().asFile().length() / 1000);
343 date = datstring.format(my.getLastModificationDate());
345 // Refresh of the history in case of promotion
346 if (display == State.deepopen) {
352 return; // No pop-up (yet) in the history folder
353 if (state == ProgressState.EXTERN)
354 popup = ApplicationSettings.getPopupMenu("extern");
355 else if (state == ProgressState.inWORK)
356 popup = ApplicationSettings.getPopupMenu("editable");
357 else if (state == ProgressState.inDRAFT)
358 popup = ApplicationSettings.getPopupMenu("reviewable");
359 else if (state == ProgressState.APPROVED)
360 popup = ApplicationSettings.getPopupMenu("approved");
361 else { // (state == ProgressState.inCHECK)
362 DocumentType mytype = me.value().getType(); // Only result documents need to be approved
363 Step mystep = getPublicationService().getInvolvedStep(me);
364 if (mytype.isResultOf(mystep.getStep()))
365 popup = ApplicationSettings.getPopupMenu("approvable");
367 popup = ApplicationSettings.getPopupMenu("notresult");
371 protected void setVersioned() {
372 // ------------------------------
373 updated = "image.modified.png";
376 // ==============================================================================================================================
378 // ==============================================================================================================================
380 private void collectHistory(Document given) {
381 // --------------------------------------------
382 VersionsRelation versions = (VersionsRelation) given
383 .getFirstRelation(VersionsRelation.class);
384 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
386 for (int i = stamp.length - 1; i > -1; i--)
387 history.add(new StampFacade(stamp[i]));
388 history.add(new DocumentFacade(owner, given, getProjectSettings(), getPublicationService()));
389 if (versions != null)
390 collectHistory(versions.getTo());
394 * Get project settings.
396 * @return Project settings service
398 private ProjectSettingsService getProjectSettings() {
399 return _projectSettingsService;
403 * Set project settings service.
405 * @param projectSettingsService
406 * project settings service
408 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
409 _projectSettingsService = projectSettingsService;
413 * Get the publicationService.
415 * @return the publicationService
417 public PublicationService getPublicationService() {
418 return _publicationService;
422 * Set the publicationService.
424 * @param publicationService
425 * the publicationService to set
427 public void setPublicationService(PublicationService publicationService) {
428 _publicationService = publicationService;