Salome HOME
Update copyrights 2014.
[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-2014
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
31  * study/scenario activity.
32  */
33 public class Publication extends Persistent {
34
35     // Persistent fields
36     /**
37      * Persistent field. The published document.
38      */
39     private Document mydoc;
40     /**
41      * Persistent field. Either Study or Scenario, depending on the step
42      * involved by the publication.
43      */
44     private ProjectElement owner;
45     /**
46      * Persistent field. True if this references a document version new for the
47      * owner project element.
48      */
49     private char isnew;
50
51     // Transient fields
52     /**
53      * Transient field. The step where the document is published.
54      */
55     private Step mystep;
56
57     // ==============================================================================================================================
58     // Construction
59     // ==============================================================================================================================
60
61     /**
62      * Get the mystep.
63      * 
64      * @return the mystep
65      */
66     public Step getStep() {
67         return mystep;
68     }
69
70     /**
71      * Set the mystep.
72      * 
73      * @param aStep
74      *            the mystep to set
75      */
76     public void setStep(final Step aStep) {
77         this.mystep = aStep;
78     }
79
80     /**
81      * Database fetch constructor.
82      */
83     public Publication() {
84         super();
85         mystep = null;
86     }
87
88     /**
89      * Internal constructors.
90      * 
91      * @param doc
92      *            the published document
93      * @param publisher
94      *            the project element where the document is published
95      */
96     public Publication(final Document doc, final ProjectElement publisher) {
97         super();
98         mydoc = doc;
99         mystep = null;
100         owner = publisher;
101         isnew = 'Y';
102     }
103
104     // ==============================================================================================================================
105     // Member functions
106     // ==============================================================================================================================
107
108     public Relation addDependency(final Publication to) {
109         return this.addDependency(to.value());
110     }
111
112     public Relation addDependency(final Document to) {
113         Relation res = null;
114         if (to != null) {
115             res = mydoc.addRelation(new UsesRelation(mydoc, to));
116         }
117         return res;
118     }
119
120     /**
121      * Returns either the Study Scenario or the Study itself to which this
122      * publication belongs, depending on the Study Step into which the
123      * referenced document has been published.<br/>
124      * If this publication belongs to a Study, the Project Element returned is
125      * the Study returned by getOwnerStudy().
126      * 
127      * @return the Study Scenario or the Study to which this publication belongs
128      *         to
129      * @see #getOwnerStudy()
130      */
131     public ProjectElement getOwner() {
132         return owner;
133     }
134
135     /**
136      * Set the owner.
137      * 
138      * @param owner
139      *            the owner to set
140      */
141     public void setOwner(final ProjectElement owner) {
142         this.owner = owner;
143     }
144
145     /**
146      * Returns the study where the document is published.
147      * 
148      * @return the owner study
149      */
150     public Study getOwnerStudy() {
151         return owner.getOwnerStudy();
152     }
153
154     /**
155      * Returns the state of this published document. It is the same than the
156      * state of the referenced document, unless this publication is out-of-date,
157      * in which case it is In-Work state.
158      * 
159      * @see #outdate()
160      * @see #isOutdated()
161      * @return the document progress state
162      */
163     public ProgressState getProgressState() {
164         ProgressState res;
165         if (this.isOutdated()) {
166             res = ProgressState.inWORK; // Overrides the document state
167         } else {
168             res = mydoc.getProgressState();
169         }
170         return res;
171     }
172
173     public List<Publication> getRelations(final Class<? extends Relation> type) {
174         List<Publication> result = null;
175         if (type != null) {
176             result = new ArrayList<Publication>();
177             List<Relation> relist = mydoc.getRelations(type);
178             for (Iterator<Relation> i = relist.iterator(); i.hasNext();) {
179                 Relation relation = i.next();
180                 Document relatedoc = (Document) relation.getTo();
181                 Publication related = owner.getPublication(relatedoc);
182
183                                 // Try to find the publication in scenarios
184                 List<Scenario> list = owner.getOwnerStudy().getScenariiList();
185                 if (related == null) {
186                     for (Scenario scen : list) {
187                         related = scen.getPublication(relatedoc);
188                         break;
189                     }
190                 }
191
192                 if (related == null) {
193                                         // If not found try to find the publication in the study
194                     if (owner instanceof Scenario) {
195                         related = owner.getOwnerStudy().getPublication(
196                                 relatedoc);
197                     }
198                 }
199                 
200                 if (related != null) {
201                                         result.add(related);
202                                 }
203             }
204         }
205         return result;
206
207     }
208
209     public File getSourceFile() {
210         return mydoc.getSourceFile();
211     }
212
213     public boolean isNewForOwner() {
214         return (isnew == 'Y');
215     }
216
217     public boolean isOutdated() {
218         return (isnew == 'O');
219     }
220
221     /**
222      * Get the isnew.
223      * 
224      * @return the isnew
225      */
226     public char getIsnew() {
227         return isnew;
228     }
229
230     /**
231      * Set the isnew.
232      * 
233      * @param isnew
234      *            the isnew to set
235      */
236     public void setIsnew(final char isnew) {
237         this.isnew = isnew;
238     }
239
240     /**
241      * Returns the document version referenced by this Publication.
242      */
243     public Document value() {
244         return mydoc;
245     }
246
247     /**
248      * Set the mydoc.
249      * 
250      * @param mydoc
251      *            the mydoc to set
252      */
253     public void setValue(final Document aDoc) {
254         this.mydoc = aDoc;
255     }
256
257     /**
258      * {@inheritDoc}
259      * 
260      * @see org.splat.dal.bo.kernel.Persistent#evict()
261      */
262     @Override
263     public void evict() {
264         super.evict();
265         // Evict the document
266         if (value() != null) {
267             value().evict();
268         }
269     }
270 }