1 package org.splat.simer;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
9 import java.text.DecimalFormat;
10 import java.text.SimpleDateFormat;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.ResourceBundle;
16 import org.splat.manox.XMLDocument;
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.som.DocumentRights;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.dal.bo.som.ProgressState;
23 import org.splat.service.PublicationService;
24 import org.splat.service.technical.ProjectSettingsService;
25 import org.splat.dal.bo.som.Publication;
26 import org.splat.som.Revision;
27 import org.splat.som.Step;
28 import org.splat.dal.bo.som.Timestamp;
29 import org.splat.dal.bo.som.UsesRelation;
30 import org.splat.dal.bo.som.VersionsRelation;
31 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;
57 private enum State { closed, open, deepopen }
59 // ==============================================================================================================================
61 // ==============================================================================================================================
63 public DocumentFacade (OpenObject opened, Publication represented) {
64 // ------------------------------------------------------------------
68 state = my.getProgressState();
69 display = State.closed;
76 this.refresh(); // Initializes the presentation of my document
79 * Constructs the facade of a document presented in the history folder.
81 * @param represented the represented history document
83 private DocumentFacade (OpenObject opened, Document represented) {
84 // ----------------------------------------------------------------
86 me = null; // Marks the history context
88 state = my.getProgressState(); // In reality, HISTORY
89 display = State.open; // Because the given document is a history document
90 description = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale()).getString("history.creation") + " " + my.getAuthor().toString();
96 this.refresh(); // Initializes the presentation of my document
99 // ==============================================================================================================================
100 // Public member functions
101 // ==============================================================================================================================
103 public void develop () {
104 // -------------------
105 if (display != State.open) { // Opening the document
107 List<Publication> relist = me.getRelations(UsesRelation.class);
109 uses = new ArrayList<DocumentFacade>(relist.size());
110 for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
111 Publication used = i.next();
112 Integer index = used.getIndex();
113 DocumentFacade facade = OpenObject.docpres.get(index);
114 if (facade == null) {
115 facade = new DocumentFacade(owner, used);
116 OpenObject.docpres.put(index, facade);
121 if (exports == null) {
122 List<Relation> relation = my.getRelations(ConvertsRelation.class);
124 exports = new ArrayList<FileFacade>(relation.size());
125 for (Iterator<Relation> i=relation.iterator(); i.hasNext();) {
126 ConvertsRelation export = (ConvertsRelation)i.next();
127 exports.add( new FileFacade(export) );
130 if (history == null) {
131 if (my.getPreviousVersion() != null || state == ProgressState.inCHECK || state == ProgressState.APPROVED) history = new ArrayList<HistoryFacade>();
133 display = State.open;
134 } else { // Opening the history of document, if exist
135 if (history.isEmpty()) collectHistory(my);
136 display = State.deepopen;
140 public void reduce () {
141 // ---------------------
142 if (display == State.deepopen) display = State.open;
145 public void reduceAll () {
146 // ------------------------
147 display = State.closed;
150 // ==============================================================================================================================
152 // ==============================================================================================================================
154 public List<FileFacade> getAttachments () {
155 // ------------------------------------------
158 public String getDate () {
159 // ------------------------
162 public String getDescription () {
163 // -------------------------------
166 public String getEditIcon () {
167 // ----------------------------
168 return "icon.ed" + state + ".png";
170 public String getFileIcon () {
171 // ----------------------------
174 public List<HistoryFacade> getHistory () {
175 // ----------------------------------------
178 public String getIndex () {
179 // -------------------------
180 return String.valueOf(my.getIndex());
182 public PopupMenu getPopup () { // Contextualizes the pop-up
183 // ----------------------------
184 popup.setContext("document", new DocumentRights(owner.getUser(), me));
185 return popup; // callers must "use" the returned pop-up before getting another pop-up
187 public String getPresentationState () {
188 // -------------------------------------
189 return display.toString();
191 public String getProgressState () {
192 // ---------------------------------
193 return state.toString();
195 public String getSharingIcon () {
196 // -------------------------------
199 public String getSize () {
200 // ------------------------
203 public String getStateIcon () {
204 // -----------------------------
205 return "icon." + state + ".png";
207 public String getTitle () {
208 // -------------------------
209 return my.getTitle();
211 public String getURL () {
212 // -----------------------
215 public List<DocumentFacade> getUses () {
216 // ---------------------------------------
219 public String getVersion () {
220 // ---------------------------
223 public String getVersioningIcon () {
224 // ----------------------------------
227 public boolean isFacadeOf (Publication represented) {
228 // ---------------------------------------------------
229 return me.equals(represented);
231 // ==============================================================================================================================
232 // Protected services
233 // ==============================================================================================================================
235 protected void refresh () {
236 // -------------------------
237 ResourceBundle custom = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
238 DecimalFormat sizstring = new DecimalFormat(custom.getString("size.format")); // Locale size display format
239 SimpleDateFormat datstring = new SimpleDateFormat(custom.getString("date.format")); // Locale date display format
240 Revision.Format verstring = new Revision.Format(getProjectSettings().getRevisionPattern());
241 String path = my.getSourceFile().getRelativePath();
242 String[] mapping = ApplicationSettings.getViewersMapping();
244 for (int i=0; i<mapping.length; i++) {
245 org.splat.dal.bo.som.File export = my.getAttachedFile(mapping[i]);
246 if (export == null) continue;
247 path = export.getRelativePath();
250 surl = ApplicationSettings.getRepositoryURL() + path;
251 surl = surl.replaceAll("'", "\\\\'");
252 format = my.getFormat();
253 if (format.equals("xml")) format = XMLDocument.getActualFormat(my.getSourceFile().asFile());
255 // Document state (overridable by the publication - see below)
256 state = my.getProgressState();
257 version = my.getVersion(); // May be null
260 icon = "icon." + format + ".png";
261 sharing = "image.hold.gif";
262 updated = "image.hold.gif";
263 File image = new File(ApplicationSettings.getApplicationSkinPath() + icon);
264 if (!image.exists()) icon = "icon.any.png";
267 if (me.getOwnerStudy().shares(my)) {
268 sharing = "image.share.png";
269 updated = "icon.hold.png";
271 if (me.isOutdated()) state = ProgressState.inWORK; // Overrides the document state
273 } else { // Facade in the history folder
274 if (!my.isVersioned()) { // History of the last version
275 if (state != ProgressState.inWORK) icon = "icon.inWORK.png";
276 else icon = "icon." + state.toString() + ".png";
278 if ( my.isPublished()) {
279 sharing = "image.share.png"; // Not correct if published in a previous version of the study
280 updated = "icon.hold.png";
283 // Document description
284 VersionsRelation versions = (VersionsRelation)my.getFirstRelation(VersionsRelation.class);
285 if (versions != null) {
286 description = versions.getDescription();
289 if (state != ProgressState.EXTERN) version = verstring.format(version);
290 size = sizstring.format(my.getSourceFile().asFile().length()/1000);
291 date = datstring.format(my.getLastModificationDate());
293 // Refresh of the history in case of promotion
294 if (display == State.deepopen) {
299 if (me == null) return; // No pop-up (yet) in the history folder
300 if (state == ProgressState.EXTERN) popup = ApplicationSettings.getPopupMenu("extern");
301 else if (state == ProgressState.inWORK) popup = ApplicationSettings.getPopupMenu("editable");
302 else if (state == ProgressState.inDRAFT) popup = ApplicationSettings.getPopupMenu("reviewable");
303 else if (state == ProgressState.APPROVED) popup = ApplicationSettings.getPopupMenu("approved");
304 else { //(state == ProgressState.inCHECK)
305 DocumentType mytype = me.value().getType(); // Only result documents need to be approved
306 Step mystep = getPublicationService().getInvolvedStep(me);
307 if (mytype.isResultOf(mystep.getStep())) popup = ApplicationSettings.getPopupMenu("approvable");
308 else popup = ApplicationSettings.getPopupMenu("notresult");
312 protected void setVersioned () {
313 // ------------------------------
314 updated = "image.modified.png";
317 // ==============================================================================================================================
319 // ==============================================================================================================================
321 private void collectHistory (Document given) {
322 // --------------------------------------------
323 VersionsRelation versions = (VersionsRelation)given.getFirstRelation(VersionsRelation.class);
324 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
326 for (int i=stamp.length-1; i>-1; i--) history.add( new StampFacade(stamp[i]) );
327 history.add( new DocumentFacade(owner, given) );
328 if (versions != null) collectHistory(versions.getTo());
332 * Get project settings.
333 * @return Project settings service
335 private ProjectSettingsService getProjectSettings() {
336 return _projectSettingsService;
340 * Set project settings service.
341 * @param projectSettingsService project settings service
343 public void setProjectSettings(
344 ProjectSettingsService projectSettingsService) {
345 _projectSettingsService = projectSettingsService;
348 * Get the publicationService.
350 * @return the publicationService
352 public PublicationService getPublicationService() {
353 return _publicationService;
357 * Set the publicationService.
359 * @param publicationService
360 * the publicationService to set
362 public void setPublicationService(PublicationService publicationService) {
363 _publicationService = publicationService;