]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/som/Document.java
Salome HOME
The checkin from SALOME operation is updated (ScenarioService.checkin()). Versioning...
[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         /**
306          * Database fetch constructor.
307          */
308         protected Document() {
309                 super();
310         }
311
312         /**
313          * Initialization constructor.
314          * @param dprop
315          * @throws MissedPropertyException
316          * @throws InvalidPropertyException
317          * @throws MultiplyDefinedException
318          */
319         public Document(final Properties dprop) throws MissedPropertyException,
320                         InvalidPropertyException, MultiplyDefinedException {
321                 super(dprop); // Throws one of the above exception if not valid
322                 myfile = new File(null, dprop.format, dprop.date); // The path is initialized below
323                 type = dprop.type;
324                 step = dprop.step.getNumber();
325                 name = dprop.name;
326                 version = dprop.version;
327                 author = dprop.author;
328                 countag = 0;
329                 history = 0;
330                 lasdate = myfile.getDate(); // Today if not defined in the properties
331
332                 state = dprop.state;
333                 if (state == null) {
334                         state = ProgressState.inWORK; // Promoted when saving this document
335                         version = new Revision().toString();
336                 }
337                 if (name == null) { // Newed document
338                         this.name = "%n"; // Named later at publication
339                         this.history = -1; // Marks the document as undefined for future assignment
340                 }
341         }
342
343         // ==============================================================================================================================
344         // Public member functions
345         // ==============================================================================================================================
346
347         public File getAttachedFile(final String format) {
348                 List<Relation> exports = getRelations(ConvertsRelation.class);
349
350                 for (Iterator<Relation> i = exports.iterator(); i.hasNext();) {
351                         File export = (File) i.next().getTo();
352                         if (export.getFormat().equals(format)) {
353                                 return export;
354                         }
355                 }
356                 return null;
357         }
358
359         public User getAuthor() {
360                 return author;
361         }
362
363         public Date getCreationDate() {
364                 return myfile.getDate();
365         }
366
367         public Date getLastModificationDate() {
368                 return lasdate;
369         }
370
371         public void setLastModificationDate(final Date aDate) {
372                 lasdate = aDate;
373         }
374
375         public String getFormat() {
376                 return myfile.getFormat();
377         }
378
379         public Document getPreviousVersion() {
380                 Relation previous = getFirstRelation(VersionsRelation.class);
381                 if (previous == null) {
382                         return null;
383                 } else {
384                         return (Document) previous.getTo();
385                 }
386         }
387
388         public ProgressState getProgressState() {
389                 return state;
390         }
391
392         /**
393          * Returns the path where all physical files attached to this document are saved. This path is relative to the vault of the repository
394          * and include the file name, without extension, common to all physical files attached to this document.
395          * 
396          * @return the path of the document
397          */
398         public String getRelativePath() {
399                 String[] table = myfile.getRelativePath().split("\\x2E");
400                 StringBuffer path = new StringBuffer(table[0]);
401                 for (int i = 1; i < table.length - 1; i++) {
402                         path.append('.').append(table[i]);
403                 }
404                 return path.toString();
405         }
406
407         /**
408          * Returns the global unique reference of this document lineage. The document reference is common to all versions of the document
409          * (versioning a document does not change its reference). It is made of the owner study reference suffixed by a document identifier
410          * unique in the scope of the study.
411          * 
412          * @return the document reference
413          */
414         public String getReference() {
415                 return did;
416         }
417
418         public File getSourceFile() {
419                 return myfile;
420         }
421
422         /**
423          * Returns the stamps such as review and approval attached to this document, if exist. If several stamps exist, they are returned in
424          * ascending order of dates.
425          * 
426          * @return the stamps of the document in ascending order of dates, or an empty array if no stamp exist.
427          */
428         public Timestamp[] getStamps() {
429                 Vector<Timestamp> stamps = new Vector<Timestamp>();
430
431                 for (Iterator<Relation> i = this.getAllRelations().iterator(); i
432                                 .hasNext();) {
433                         Relation link = i.next();
434                         if (link instanceof StampRelation) {
435                                 stamps.add(((StampRelation) link).getTo());
436                         }
437                 }
438                 Timestamp[] result = stamps.toArray(new Timestamp[stamps.size()]);
439                 ComparatorByDate bydate = new Timestamp.ComparatorByDate();
440
441                 Arrays.sort(result, bydate);
442                 return result;
443         }
444
445         /**
446          * Returns the title of this document.
447          * 
448          * @return the document title, or an empty string is this document is undefined.
449          * @see #isUndefined()
450          */
451         public String getTitle() {
452                 // -------------------------
453                 if (this.isUndefined()) {
454                         return "";
455                 } else {
456                         return name;
457                 }
458         }
459
460         /**
461          * Set document title.
462          * 
463          * @param aTitle
464          *            document title to set
465          */
466         public void setTitle(final String aTitle) {
467                 this.name = aTitle;
468         }
469
470         public DocumentType getType() {
471                 return type;
472         }
473
474         /**
475          * Returns the version number of this document. The version number, when exists, is either of the internal form (m.n.s) usable for
476          * building a Revision object, or any string in case of external document (document with EXTERN state).<br/> <br/> Note: document slots
477          * have a version number equal to "0.0.0".
478          * 
479          * @return the version number of this document, or null if this is EXTERN.
480          * @see #isUndefined()
481          */
482         public String getVersion() {
483                 return version;
484         }
485
486         /**
487          * Returns true if this document is undefined. An undefined document is a meta-document created for reserving the persistent reference
488          * of a new document before saving (or importing) this later into the repository. The working copy of a such document may include this
489          * reference.
490          * 
491          * @see #getTitle()
492          * @see #getVersion()
493          * @see #initialize(Properties)
494          */
495         public boolean isUndefined() {
496                 return (history == -1);
497         }
498
499         public boolean isInto(final Step container) {
500                 return (step == container.getNumber());
501         }
502
503         public boolean isPublished() {
504                 return (countag > 0);
505         }
506
507         public boolean isShared() {
508                 return (countag + history > 1);
509         }
510
511         public boolean isVersioned() {
512                 return (history > 0);
513         }
514
515         /**
516          * Get the step.
517          * 
518          * @return the step
519          */
520         public int getStep() {
521                 return step;
522         }
523
524         /**
525          * Set the step.
526          * 
527          * @param step
528          *            the step to set
529          */
530         public void setStep(final int step) {
531                 this.step = step;
532         }
533
534         /**
535          * Get the did.
536          * 
537          * @return the did
538          */
539         public String getDid() {
540                 return did;
541         }
542
543         /**
544          * Set the did.
545          * 
546          * @param did
547          *            the did to set
548          */
549         public void setDid(final String did) {
550                 this.did = did;
551         }
552
553         /**
554          * Get the myfile.
555          * 
556          * @return the myfile
557          */
558         public File getFile() {
559                 return myfile;
560         }
561
562         /**
563          * Set the myfile.
564          * 
565          * @param myfile
566          *            the myfile to set
567          */
568         public void setFile(final File myfile) {
569                 this.myfile = myfile;
570         }
571
572         /**
573          * Get the history.
574          * 
575          * @return the history
576          */
577         public int getHistory() {
578                 return history;
579         }
580
581         /**
582          * Set the history.
583          * 
584          * @param history
585          *            the history to set
586          */
587         public void setHistory(final int history) {
588                 this.history = history;
589         }
590
591         /**
592          * Set the version.
593          * 
594          * @param version
595          *            the version to set
596          */
597         public void setVersion(final String version) {
598                 this.version = version;
599         }
600
601         /**
602          * Set the state.
603          * 
604          * @param state
605          *            the state to set
606          */
607         public void setProgressState(final ProgressState state) {
608                 this.state = state;
609         }
610
611         /**
612          * Get the countag.
613          * 
614          * @return the countag
615          */
616         public int getCountag() {
617                 return countag;
618         }
619
620         /**
621          * Set the countag.
622          * 
623          * @param countag
624          *            the countag to set
625          */
626         public void setCountag(final int countag) {
627                 this.countag = countag;
628         }
629 }