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