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.kernel.Relation;
18 import org.splat.som.ConvertsRelation;
19 import org.splat.som.Document;
20 import org.splat.som.DocumentRights;
21 import org.splat.som.DocumentType;
22 import org.splat.som.ProgressState;
23 import org.splat.som.ProjectSettings;
24 import org.splat.som.Publication;
25 import org.splat.som.Revision;
26 import org.splat.som.Step;
27 import org.splat.som.Timestamp;
28 import org.splat.som.UsesRelation;
29 import org.splat.som.VersionsRelation;
30 import org.splat.wapp.PopupMenu;
33 public class DocumentFacade implements HistoryFacade {
35 private OpenObject owner;
36 private Publication me;
37 private Document my; // Published document
38 private ProgressState state; // Document state
39 private String version; // My document version in customized format
40 private State display; // Presentation state
41 private String surl; // URL of the source file
42 private String format; // Extension of the source file
43 private String icon; // Corresponding icon
44 private String sharing; // Icon qualifying sharing between scenarios of a same study
45 private String updated; // Icon qualifying versioning from the previous study version
48 private String description;
49 private List<DocumentFacade> uses;
50 private List<FileFacade> exports;
51 private List<HistoryFacade> history;
52 private PopupMenu popup;
54 private enum State { closed, open, deepopen }
56 // ==============================================================================================================================
58 // ==============================================================================================================================
60 public DocumentFacade (OpenObject opened, Publication represented) {
61 // ------------------------------------------------------------------
65 state = my.getProgressState();
66 display = State.closed;
73 this.refresh(); // Initializes the presentation of my document
76 * Constructs the facade of a document presented in the history folder.
78 * @param represented the represented history document
80 private DocumentFacade (OpenObject opened, Document represented) {
81 // ----------------------------------------------------------------
83 me = null; // Marks the history context
85 state = my.getProgressState(); // In reality, HISTORY
86 display = State.open; // Because the given document is a history document
87 description = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale()).getString("history.creation") + " " + my.getAuthor().toString();
93 this.refresh(); // Initializes the presentation of my document
96 // ==============================================================================================================================
97 // Public member functions
98 // ==============================================================================================================================
100 public void develop () {
101 // -------------------
102 if (display != State.open) { // Opening the document
104 List<Publication> relist = me.getRelations(UsesRelation.class);
106 uses = new ArrayList<DocumentFacade>(relist.size());
107 for (Iterator<Publication> i=relist.iterator(); i.hasNext();) {
108 Publication used = i.next();
109 Integer index = used.getIndex();
110 DocumentFacade facade = OpenObject.docpres.get(index);
111 if (facade == null) {
112 facade = new DocumentFacade(owner, used);
113 OpenObject.docpres.put(index, facade);
118 if (exports == null) {
119 List<Relation> relation = my.getRelations(ConvertsRelation.class);
121 exports = new ArrayList<FileFacade>(relation.size());
122 for (Iterator<Relation> i=relation.iterator(); i.hasNext();) {
123 ConvertsRelation export = (ConvertsRelation)i.next();
124 exports.add( new FileFacade(export) );
127 if (history == null) {
128 if (my.getPreviousVersion() != null || state == ProgressState.inCHECK || state == ProgressState.APPROVED) history = new ArrayList<HistoryFacade>();
130 display = State.open;
131 } else { // Opening the history of document, if exist
132 if (history.isEmpty()) collectHistory(my);
133 display = State.deepopen;
137 public void reduce () {
138 // ---------------------
139 if (display == State.deepopen) display = State.open;
142 public void reduceAll () {
143 // ------------------------
144 display = State.closed;
147 // ==============================================================================================================================
149 // ==============================================================================================================================
151 public List<FileFacade> getAttachments () {
152 // ------------------------------------------
155 public String getDate () {
156 // ------------------------
159 public String getDescription () {
160 // -------------------------------
163 public String getEditIcon () {
164 // ----------------------------
165 return "icon.ed" + state + ".png";
167 public String getFileIcon () {
168 // ----------------------------
171 public List<HistoryFacade> getHistory () {
172 // ----------------------------------------
175 public String getIndex () {
176 // -------------------------
177 return String.valueOf(my.getIndex());
179 public PopupMenu getPopup () { // Contextualizes the pop-up
180 // ----------------------------
181 popup.setContext("document", new DocumentRights(owner.getUser(), me));
182 return popup; // callers must "use" the returned pop-up before getting another pop-up
184 public String getPresentationState () {
185 // -------------------------------------
186 return display.toString();
188 public String getProgressState () {
189 // ---------------------------------
190 return state.toString();
192 public String getSharingIcon () {
193 // -------------------------------
196 public String getSize () {
197 // ------------------------
200 public String getStateIcon () {
201 // -----------------------------
202 return "icon." + state + ".png";
204 public String getTitle () {
205 // -------------------------
206 return my.getTitle();
208 public String getURL () {
209 // -----------------------
212 public List<DocumentFacade> getUses () {
213 // ---------------------------------------
216 public String getVersion () {
217 // ---------------------------
220 public String getVersioningIcon () {
221 // ----------------------------------
224 public boolean isFacadeOf (Publication represented) {
225 // ---------------------------------------------------
226 return me.equals(represented);
228 // ==============================================================================================================================
229 // Protected services
230 // ==============================================================================================================================
232 protected void refresh () {
233 // -------------------------
234 ResourceBundle custom = ResourceBundle.getBundle("som", ApplicationSettings.getCurrentLocale());
235 DecimalFormat sizstring = new DecimalFormat(custom.getString("size.format")); // Locale size display format
236 SimpleDateFormat datstring = new SimpleDateFormat(custom.getString("date.format")); // Locale date display format
237 Revision.Format verstring = new Revision.Format(ProjectSettings.getRevisionPattern());
238 String path = my.getSourceFile().getRelativePath();
239 String[] mapping = ApplicationSettings.getViewersMapping();
241 for (int i=0; i<mapping.length; i++) {
242 org.splat.som.File export = my.getAttachedFile(mapping[i]);
243 if (export == null) continue;
244 path = export.getRelativePath();
247 surl = ApplicationSettings.getRepositoryURL() + path;
248 surl = surl.replaceAll("'", "\\\\'");
249 format = my.getFormat();
250 if (format.equals("xml")) format = XMLDocument.getActualFormat(my.getSourceFile().asFile());
252 // Document state (overridable by the publication - see below)
253 state = my.getProgressState();
254 version = my.getVersion(); // May be null
257 icon = "icon." + format + ".png";
258 sharing = "image.hold.gif";
259 updated = "image.hold.gif";
260 File image = new File(ApplicationSettings.getApplicationSkinPath() + icon);
261 if (!image.exists()) icon = "icon.any.png";
264 if (me.getOwnerStudy().shares(my)) {
265 sharing = "image.share.png";
266 updated = "icon.hold.png";
268 if (me.isOutdated()) state = ProgressState.inWORK; // Overrides the document state
270 } else { // Facade in the history folder
271 if (!my.isVersioned()) { // History of the last version
272 if (state != ProgressState.inWORK) icon = "icon.inWORK.png";
273 else icon = "icon." + state.toString() + ".png";
275 if ( my.isPublished()) {
276 sharing = "image.share.png"; // Not correct if published in a previous version of the study
277 updated = "icon.hold.png";
280 // Document description
281 VersionsRelation versions = (VersionsRelation)my.getFirstRelation(VersionsRelation.class);
282 if (versions != null) {
283 description = versions.getDescription();
286 if (state != ProgressState.EXTERN) version = verstring.format(version);
287 size = sizstring.format(my.getSourceFile().asFile().length()/1000);
288 date = datstring.format(my.getLastModificationDate());
290 // Refresh of the history in case of promotion
291 if (display == State.deepopen) {
296 if (me == null) return; // No pop-up (yet) in the history folder
297 if (state == ProgressState.EXTERN) popup = ApplicationSettings.getPopupMenu("extern");
298 else if (state == ProgressState.inWORK) popup = ApplicationSettings.getPopupMenu("editable");
299 else if (state == ProgressState.inDRAFT) popup = ApplicationSettings.getPopupMenu("reviewable");
300 else if (state == ProgressState.APPROVED) popup = ApplicationSettings.getPopupMenu("approved");
301 else { //(state == ProgressState.inCHECK)
302 DocumentType mytype = me.value().getType(); // Only result documents need to be approved
303 Step mystep = me.getInvolvedStep();
304 if (mytype.isResultOf(mystep.getStep())) popup = ApplicationSettings.getPopupMenu("approvable");
305 else popup = ApplicationSettings.getPopupMenu("notresult");
309 protected void setVersioned () {
310 // ------------------------------
311 updated = "image.modified.png";
314 // ==============================================================================================================================
316 // ==============================================================================================================================
318 private void collectHistory (Document given) {
319 // --------------------------------------------
320 VersionsRelation versions = (VersionsRelation)given.getFirstRelation(VersionsRelation.class);
321 Timestamp[] stamp = given.getStamps(); // Stamps in ascending order of date
323 for (int i=stamp.length-1; i>-1; i--) history.add( new StampFacade(stamp[i]) );
324 history.add( new DocumentFacade(owner, given) );
325 if (versions != null) collectHistory(versions.getTo());