Salome HOME
Document id generation is fixed. Document index is updated in the database now.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / Publication.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * Publication objects are the way to reference document versions from a Project Element.
5  * As such, a Document version is added (or published) to a Project Element through a Publication object.
6  * This publication is done by saving the Publication object produced when creating and versioning a Document from a given
7  * Project Element Step (call of the saveAs() function).<br/>
8  * <br/>
9  * A Publication object is homogeneous to a reference to a Document version and belongs to one Project Element, this latter
10  * being either a Study Scenario or a Study itself, depending on the Study Step to which the document is published.<br/>
11  * <br/>
12  * The document version referenced by a Publication object is the Value of the publication.
13  * 
14  * @see Document
15  * @see ProjectElement
16  * @see Step
17  * @author    Daniel Brunier-Coulin
18  * @copyright OPEN CASCADE 2012
19  */
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.splat.dal.bo.kernel.Persistent;
26 import org.splat.dal.bo.kernel.Relation;
27 import org.splat.som.Step;
28
29 /**
30  * Persistent class representing a tag of a document published in a study/scenario activity.
31  */
32 public class Publication extends Persistent {
33
34         // Persistent fields
35         /**
36          * Persistent field. The published document.
37          */
38         private Document mydoc;
39         /**
40          * Persistent field. Either Study or Scenario, depending on the step involved by the publication.
41          */
42         private ProjectElement owner;
43         /**
44          * Persistent field. True if this references a document version new for the owner project element.
45          */
46         private char isnew;
47
48         // Transient fields
49         /**
50          * Transient field. The step where the document is published.
51          */
52         private Step mystep;
53
54         // ==============================================================================================================================
55         // Construction
56         // ==============================================================================================================================
57
58         /**
59          * Get the mystep.
60          * 
61          * @return the mystep
62          */
63         public Step getStep() {
64                 return mystep;
65         }
66
67         /**
68          * Set the mystep.
69          * 
70          * @param aStep
71          *            the mystep to set
72          */
73         public void setStep(final Step aStep) {
74                 this.mystep = aStep;
75         }
76
77         /**
78          * Database fetch constructor.
79          */
80         public Publication() {
81                 super();
82                 mystep = null;
83         }
84
85         /**
86          * Internal constructors.
87          * 
88          * @param doc
89          *            the published document
90          * @param publisher
91          *            the project element where the document is published
92          */
93         public Publication(final Document doc, final ProjectElement publisher) {
94                 super();
95                 mydoc = doc;
96                 mystep = null;
97                 owner = publisher;
98                 isnew = 'Y';
99         }
100
101         // ==============================================================================================================================
102         // Member functions
103         // ==============================================================================================================================
104
105         public Relation addDependency(final Publication to) {
106                 return this.addDependency(to.value());
107         }
108
109         public Relation addDependency(final Document to) {
110                 Relation res = null;
111                 if (to != null) {
112                         res = mydoc.addRelation(new UsesRelation(mydoc, to));
113                 }
114                 return res;
115         }
116
117         /**
118          * Returns either the Study Scenario or the Study itself to which this publication belongs, depending on the Study Step into which the
119          * referenced document has been published.<br/> If this publication belongs to a Study, the Project Element returned is the Study
120          * returned by getOwnerStudy().
121          * 
122          * @return the Study Scenario or the Study to which this publication belongs to
123          * @see #getOwnerStudy()
124          */
125         public ProjectElement getOwner() {
126                 return owner;
127         }
128
129         /**
130          * Set the owner.
131          * 
132          * @param owner
133          *            the owner to set
134          */
135         public void setOwner(final ProjectElement owner) {
136                 this.owner = owner;
137         }
138
139         /**
140          * Returns the study where the document is published.
141          * 
142          * @return the owner study
143          */
144         public Study getOwnerStudy() {
145                 return owner.getOwnerStudy();
146         }
147
148         /**
149          * Returns the state of this published document. It is the same than the state of the referenced document, unless this publication is
150          * out-of-date, in which case it is In-Work state.
151          * 
152          * @see #outdate()
153          * @see #isOutdated()
154          * @return the document progress state
155          */
156         public ProgressState getProgressState() {
157                 ProgressState res;
158                 if (this.isOutdated()) {
159                         res = ProgressState.inWORK; // Overrides the document state
160                 } else {
161                         res = mydoc.getProgressState();
162                 }
163                 return res;
164         }
165
166         public List<Publication> getRelations(final Class<? extends Relation> type) {
167                 if (type == null) {
168                         return null;
169                 }
170
171                 List<Publication> result = new ArrayList<Publication>();
172                 List<Relation> relist = mydoc.getRelations(type);
173                 for (Iterator<Relation> i = relist.iterator(); i.hasNext();) {
174                         Relation relation = i.next();
175                         Document relatedoc = (Document) relation.getTo();
176                         Publication related = owner.getPublication(relatedoc);
177                         if (related != null) {
178                                 result.add(related);
179                         } else if (owner instanceof Scenario) { // The relation may cross steps belonging to a scenario and its owner study
180                                 related = owner.getOwnerStudy().getPublication(relatedoc);
181                                 if (related != null) {
182                                         result.add(related);
183                                 }
184                         }
185                 }
186                 return result;
187         }
188
189         public File getSourceFile() {
190                 return mydoc.getSourceFile();
191         }
192
193         public boolean isNewForOwner() {
194                 return (isnew == 'Y');
195         }
196
197         public boolean isOutdated() {
198                 return (isnew == 'O');
199         }
200
201         /**
202          * Get the isnew.
203          * 
204          * @return the isnew
205          */
206         public char getIsnew() {
207                 return isnew;
208         }
209
210         /**
211          * Set the isnew.
212          * 
213          * @param isnew
214          *            the isnew to set
215          */
216         public void setIsnew(final char isnew) {
217                 this.isnew = isnew;
218         }
219
220         /**
221          * Returns the document version referenced by this Publication.
222          */
223         public Document value() {
224                 return mydoc;
225         }
226
227         /**
228          * Set the mydoc.
229          * 
230          * @param mydoc
231          *            the mydoc to set
232          */
233         public void setValue(final Document aDoc) {
234                 this.mydoc = aDoc;
235         }
236 }