]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/som/Document.java
Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / Document.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.util.Arrays;
10 import java.util.Date;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Vector;
14
15 import org.splat.dal.bo.kernel.Persistent;
16 import org.splat.dal.bo.kernel.Relation;
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.dal.bo.som.Timestamp.ComparatorByDate;
19 import org.splat.kernel.InvalidPropertyException;
20 import org.splat.kernel.MissedPropertyException;
21 import org.splat.kernel.MultiplyDefinedException;
22 import org.splat.service.technical.ProjectSettingsService;
23 import org.splat.service.technical.ProjectSettingsServiceImpl;
24 import org.splat.som.Revision;
25 import org.splat.som.Step;
26
27 /**
28  * Document persistent class. 
29  */
30 public class Document extends Entity {
31
32         // Persistent fields
33         private DocumentType type; // User expendable types
34         private File myfile;
35         private String did;
36         private int step;
37         private ProgressState state;
38         private String name;
39         private String version;
40         private int countag;
41         private int history;
42         private User author;
43         private Date lasdate;
44
45         // Transient fields
46         public static String suformat = "00"; // Format of the suffix number of document did and file name
47
48         // ==============================================================================================================================
49         // Construction
50         // ==============================================================================================================================
51
52         /**
53          * Fields initialization class. 
54          */
55         public static class Properties extends Persistent.Properties {
56                 // ------------------------------------------------------------
57                 private DocumentType type = null;
58                 private String did = null; // Only for searching from a given reference
59                 private ProjectElement owner = null; // Only for constructing a document
60                 private ProjectSettingsService.Step step = null;
61                 private ProgressState state = null;
62                 private String name = null;
63                 protected String format = null;
64                 private String version = null;
65                 private User author = null;
66                 protected Date date = null;
67                 private String summary = null; // Only for versioning a document
68                 private String path = null; // Only for searching from a given path
69
70                 // - Public services
71
72                 public void clear() {
73                         super.clear();
74                         type = null;
75                         did = null;
76                         owner = null;
77                         step = null;
78                         state = null;
79                         name = null;
80                         format = null;
81                         version = null;
82                         author = null;
83                         date = null;
84                         summary = null;
85                         path = null;
86                 }
87
88                 public Properties copy() {
89                         Properties copy = new Properties();
90                         copy.type = this.type;
91                         copy.did = this.did;
92                         copy.owner = this.owner;
93                         copy.step = this.step;
94                         copy.state = this.state;
95                         copy.name = this.name;
96                         copy.format = this.format;
97                         copy.version = this.version;
98                         copy.author = this.author;
99                         copy.date = this.date;
100                         copy.summary = this.summary;
101                         copy.path = this.path;
102                         return copy;
103                 }
104
105                 // - Protected services
106
107                 public User getAuthor() {
108                         return author;
109                 }
110
111                 public String getDescription() {
112                         return summary;
113                 }
114
115                 public String getLocalPath() {
116                         return path;
117                 }
118
119                 public String getReference() {
120                         return did;
121                 }
122
123                 public ProjectSettingsService.Step getStep() {
124                         return step;
125                 }
126
127                 public DocumentType getType() {
128                         return type;
129                 }
130
131                 // - Property setters
132
133                 public Properties setAuthor(User user) {
134                         this.author = user;
135                         return this;
136                 }
137
138                 public Properties setDate(Date date) {
139                         this.date = date;
140                         return this;
141                 }
142
143                 /**
144                  * Get the date.
145                  * @return the date
146                  */
147                 public Date getDate() {
148                         return date;
149                 }
150
151                 public Properties setDescription(String summary)
152                                 throws InvalidPropertyException {
153                         if (summary.length() == 0)
154                                 throw new InvalidPropertyException("description");
155                         this.summary = summary;
156                         return this;
157                 }
158
159                 public Properties setDocument(Document base) {
160                         type = base.type;
161                         step = ProjectSettingsServiceImpl.getStep(base.step);
162                         name = base.name;
163                         format = base.getFormat();
164                         state = ProgressState.inWORK; // For incrementing the version number at save time
165                         version = base.version;
166                         return this;
167                 }
168
169                 public Properties setExternReference(String ref)
170                                 throws InvalidPropertyException {
171                         if (ref.length() == 0)
172                                 throw new InvalidPropertyException("reference");
173                         if (ref.equals(new Revision().toString()))
174                                 throw new InvalidPropertyException("reference"); // Internal version number
175                         this.version = ref;
176                         return this;
177                 }
178
179                 public Properties setFormat(String format)
180                                 throws InvalidPropertyException {
181                         if (format.length() == 0)
182                                 throw new InvalidPropertyException("format");
183                         this.format = format;
184                         return this;
185                 }
186
187                 // Required only for passing search arguments
188                 public Properties setLocalPath(String path)
189                                 throws InvalidPropertyException {
190                         if (path.length() == 0)
191                                 throw new InvalidPropertyException("path");
192                         this.path = path;
193                         return this;
194                 }
195
196                 public Properties setName(String name) throws InvalidPropertyException {
197                         if (name.length() == 0)
198                                 throw new InvalidPropertyException("name");
199                         this.name = name;
200                         return this;
201                 }
202                 
203                 public String getName() {
204                         return this.name;
205                 }
206
207                 public Properties setOwner(ProjectElement owner) {
208                         this.owner = owner;
209                         return this;
210                 }
211
212                 public ProjectElement getOwner() {
213                         return this.owner;
214                 }
215                 // Required only for passing search arguments
216                 public Properties setReference(String did)
217                                 throws InvalidPropertyException {
218                         if (did.length() == 0)
219                                 throw new InvalidPropertyException("reference");
220                         this.did = did;
221                         return this;
222                 }
223
224                 public Properties setState(ProgressState state)
225                                 throws InvalidPropertyException {
226                         if (state == ProgressState.inPROGRESS
227                                         || state == ProgressState.TEMPLATE)
228                                 throw new InvalidPropertyException("state"); // Non document states
229                         this.state = state;
230                         return this;
231                 }
232
233                 public Properties setStep(ProjectSettingsService.Step step) {
234                         this.step = step;
235                         return this;
236                 }
237
238                 public Properties setType(DocumentType type) {
239                         this.type = type;
240                         return this;
241                 }
242
243                 // - Global validity check
244
245                 public void checkValidity() throws MissedPropertyException,
246                                 InvalidPropertyException, MultiplyDefinedException {
247                         if (type == null)
248                                 throw new MissedPropertyException("type");
249                         if (owner == null)
250                                 throw new MissedPropertyException("owner");
251                         if (step == null)
252                                 throw new MissedPropertyException("step");
253                         if (author == null)
254                                 throw new MissedPropertyException("author");
255                         if (format == null)
256                                 throw new MissedPropertyException("format");
257                         if (owner instanceof Study && !step.appliesTo(Study.class))
258                                 throw new InvalidPropertyException("step");
259                         if (!type.isContentInto(step))
260                                 throw new InvalidPropertyException("step");
261                         if (state != null && state != ProgressState.EXTERN) {
262                                 // inDRAFT, inCHECK or APPROVED + version = imposed version (future use)
263                                 // inWORK + version = base version incremented at save time (used for versioning)
264                                 if (version == null)
265                                         throw new InvalidPropertyException("state");
266                         }
267                         if (version != null) {
268                                 if (state == null)
269                                         state = ProgressState.EXTERN;
270                         }
271                 }
272         }
273
274         // Database fetch constructor
275         protected Document() {
276         }
277
278         // Internal constructor
279         public Document(Properties dprop) throws MissedPropertyException,
280                         InvalidPropertyException, MultiplyDefinedException {
281                 // -------------------------------------
282                 super(dprop); // Throws one of the above exception if not valid
283                 myfile = new File(null, dprop.format, dprop.date); // The path is initialized below
284                 type = dprop.type;
285                 step = dprop.step.getNumber();
286                 name = dprop.name;
287                 version = dprop.version;
288                 author = dprop.author;
289                 countag = 0;
290                 history = 0;
291                 lasdate = myfile.getDate(); // Today if not defined in the properties
292
293                 state = dprop.state;
294                 if (state == null) {
295                         state = ProgressState.inWORK; // Promoted when saving this document
296                         version = new Revision().toString();
297                 }
298                 if (name == null) { // Newed document
299                         this.name = "%n"; // Named later at publication
300                         this.history = -1; // Marks the document as undefined for future assignment
301                 }
302         }
303
304         // ==============================================================================================================================
305         // Public member functions
306         // ==============================================================================================================================
307
308         public File getAttachedFile(String format) {
309                 // -------------------------------------------
310                 List<Relation> exports = getRelations(ConvertsRelation.class);
311
312                 for (Iterator<Relation> i = exports.iterator(); i.hasNext();) {
313                         File export = (File) i.next().getTo();
314                         if (export.getFormat().equals(format))
315                                 return export;
316                 }
317                 return null;
318         }
319
320         public User getAuthor() {
321                 // ------------------------
322                 return author;
323         }
324
325         public Date getCreationDate() {
326                 // ------------------------------
327                 return myfile.getDate();
328         }
329
330         public Date getLastModificationDate() {
331                 // --------------------------------------
332                 return lasdate;
333         }
334         
335         public void setLastModificationDate(Date aDate) {
336                 lasdate = aDate;
337         }
338
339         public String getFormat() {
340                 // --------------------------
341                 return myfile.getFormat();
342         }
343
344         public Document getPreviousVersion() {
345                 // -------------------------------------
346                 Relation previous = getFirstRelation(VersionsRelation.class);
347                 if (previous != null)
348                         return (Document) previous.getTo();
349                 else
350                         return null;
351         }
352
353         public ProgressState getProgressState() {
354                 // ----------------------------------------
355                 return state;
356         }
357
358         /**
359          * Returns the path where all physical files attached to this document are saved. This path is relative to the vault of the repository
360          * and include the file name, without extension, common to all physical files attached to this document.
361          * 
362          * @return the path of the document
363          */
364         public String getRelativePath() {
365                 // --------------------------------
366                 String[] table = myfile.getRelativePath().split("\\x2E");
367                 StringBuffer path = new StringBuffer(table[0]);
368                 for (int i = 1; i < table.length - 1; i++)
369                         path.append('.').append(table[i]);
370                 return path.toString();
371         }
372
373         /**
374          * Returns the global unique reference of this document lineage. The document reference is common to all versions of the document
375          * (versioning a document does not change its reference). It is made of the owner study reference suffixed by a document identifier
376          * unique in the scope of the study.
377          * 
378          * @return the document reference
379          */
380         public String getReference() {
381                 // -----------------------------
382                 return did;
383         }
384
385         public File getSourceFile() {
386                 // ----------------------------
387                 return myfile;
388         }
389
390         /**
391          * Returns the stamps such as review and approval attached to this document, if exist. If several stamps exist, they are returned in
392          * ascending order of dates.
393          * 
394          * @return the stamps of the document in ascending order of dates, or an empty array if no stamp exist.
395          */
396         public Timestamp[] getStamps() {
397                 // -------------------------------
398                 Vector<Timestamp> stamps = new Vector<Timestamp>();
399
400                 for (Iterator<Relation> i = this.getAllRelations().iterator(); i
401                                 .hasNext();) {
402                         Relation link = i.next();
403                         if (link instanceof StampRelation)
404                                 stamps.add(((StampRelation) link).getTo());
405                 }
406                 Timestamp[] result = stamps.toArray(new Timestamp[stamps.size()]);
407                 ComparatorByDate bydate = new Timestamp.ComparatorByDate();
408
409                 Arrays.sort(result, bydate);
410                 return result;
411         }
412
413         /**
414          * Returns the title of this document.
415          * 
416          * @return the document title, or an empty string is this document is undefined.
417          * @see #isUndefined()
418          */
419         public String getTitle() {
420                 // -------------------------
421                 if (this.isUndefined())
422                         return "";
423                 else
424                         return name;
425         }
426         
427         /**
428          * Set document title.
429          * @param aTitle document title to set
430          */
431         public void setTitle(String aTitle) {
432                 this.name = aTitle;
433         }
434
435         public DocumentType getType() {
436                 // ------------------------------
437                 return type;
438         }
439
440         /**
441          * Returns the version number of this document. The version number, when exists, is either of the internal form (m.n.s) usable for
442          * building a Revision object, or any string in case of external document (document with EXTERN state).<br/> <br/> Note: document slots
443          * have a version number equal to "0.0.0".
444          * 
445          * @return the version number of this document, or null if this is EXTERN.
446          * @see #isUndefined()
447          */
448         public String getVersion() {
449                 // ---------------------------
450                 return version;
451         }
452
453         /**
454          * Returns true if this document is undefined. An undefined document is a meta-document created for reserving the persistent reference
455          * of a new document before saving (or importing) this later into the repository. The working copy of a such document may include this
456          * reference.
457          * 
458          * @see #getTitle()
459          * @see #getVersion()
460          * @see #initialize(Properties)
461          */
462         public boolean isUndefined() {
463                 // -----------------------------
464                 return (history == -1);
465         }
466
467         public boolean isInto(Step container) {
468                 // --------------------------------------
469                 return (step == container.getNumber());
470         }
471
472         public boolean isPublished() {
473                 // -----------------------------
474                 return (countag > 0);
475         }
476
477         public boolean isShared() {
478                 // --------------------------
479                 return (countag + history > 1);
480         }
481
482         public boolean isVersioned() {
483                 // -----------------------------
484                 return (history > 0);
485         }
486
487         /**
488          * Get the step.
489          * @return the step
490          */
491         public int getStep() {
492                 return step;
493         }
494
495         /**
496          * Set the step.
497          * @param step the step to set
498          */
499         public void setStep(int step) {
500                 this.step = step;
501         }
502
503         /**
504          * Get the did.
505          * @return the did
506          */
507         public String getDid() {
508                 return did;
509         }
510
511         /**
512          * Set the did.
513          * @param did the did to set
514          */
515         public void setDid(String did) {
516                 this.did = did;
517         }
518
519         /**
520          * Get the myfile.
521          * @return the myfile
522          */
523         public File getFile() {
524                 return myfile;
525         }
526
527         /**
528          * Set the myfile.
529          * @param myfile the myfile to set
530          */
531         public void setFile(File myfile) {
532                 this.myfile = myfile;
533         }
534
535         /**
536          * Get the history.
537          * @return the history
538          */
539         public int getHistory() {
540                 return history;
541         }
542
543         /**
544          * Set the history.
545          * @param history the history to set
546          */
547         public void setHistory(int history) {
548                 this.history = history;
549         }
550
551         /**
552          * Set the version.
553          * @param version the version to set
554          */
555         public void setVersion(String version) {
556                 this.version = version;
557         }
558
559         /**
560          * Set the state.
561          * @param state the state to set
562          */
563         public void setProgressState(ProgressState state) {
564                 this.state = state;
565         }
566
567         /**
568          * Get the countag.
569          * @return the countag
570          */
571         public int getCountag() {
572                 return countag;
573         }
574
575         /**
576          * Set the countag.
577          * @param countag the countag to set
578          */
579         public void setCountag(int countag) {
580                 this.countag = countag;
581         }
582 }