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