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