]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/som/Publication.java
Salome HOME
0ce5c59706bd9b83ca18b9a92118228985d0ecdf
[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.io.FileNotFoundException;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.hibernate.Session;
27 import org.splat.dal.bo.kernel.Persistent;
28 import org.splat.dal.bo.kernel.Relation;
29 import org.splat.dal.bo.kernel.User;
30 import org.splat.dal.dao.som.Database;
31 import org.splat.kernel.InvalidPropertyException;
32 import org.splat.kernel.NotApplicableException;
33 import org.splat.manox.Reader;
34 import org.splat.manox.Toolbox;
35 import org.splat.som.DocumentRights;
36 import org.splat.som.Revision;
37 import org.splat.som.Step;
38
39
40 public class Publication extends Persistent {
41
42 //  Persistent fields
43     private  Document        mydoc;
44     private  ProjectElement  owner;     // Either Study or Scenario, depending on the step involved by the publication
45     private  char            isnew;     // True if this references a document version new for the owner project element
46
47 //  Transient fields
48     private  Step            mystep;
49
50 //  ==============================================================================================================================
51 //  Construction
52 //  ==============================================================================================================================
53
54 /**
55          * Get the mystep.
56          * @return the mystep
57          */
58         public Step getStep() {
59                 return mystep;
60         }
61         /**
62          * Set the mystep.
63          * @param aStep the mystep to set
64          */
65         public void setStep(Step aStep) {
66                 this.mystep = aStep;
67         }
68         //  Database fetch constructor
69     public Publication () {
70 //  ------------------------
71       mystep = null;
72     }
73 //  Internal constructors
74     public Publication (Document doc, ProjectElement publisher) {
75 //  --------------------------------------------------------------
76       mydoc  = doc;
77       mystep = null;
78       owner  = publisher;
79       isnew  = 'Y';
80     }
81 //  ==============================================================================================================================
82 //  Member functions
83 //  ==============================================================================================================================
84
85     public Relation addDependency (Publication to) {
86 //  ----------------------------------------------
87       return  this.addDependency(to.value());
88     }
89
90     public Relation addDependency (Document to) {
91 //  -------------------------------------------
92       if (to == null) return null;
93       else            return mydoc.addRelation( new UsesRelation(mydoc, to) );
94     }
95
96 /**
97  * Undo the out-date operation.
98  * 
99  * @return true if the acceptance succeeds
100  * @see    #outdate()
101  * @see    DocumentRights#canAccept()
102  */
103     public boolean actualize () {
104 //  ---------------------------
105       if (!this.isOutdated()) return false;
106       isnew = 'Y';
107       Database.getSession().update(this);
108       return true;
109     }
110
111     public ConvertsRelation attach (String format) {
112 //  ----------------------------------------------
113       return mydoc.attach(format);
114     }
115
116     public ConvertsRelation attach (String format, String description) {
117 //  ------------------------------------------------------------------
118       return mydoc.attach(format, description);
119     }
120
121 /**
122  * Returns either the Study Scenario or the Study itself to which this publication belongs, depending on the Study Step into
123  * which the referenced document has been published.<br/>
124  * If this publication belongs to a Study, the Project Element returned is the Study returned by getOwnerStudy().
125  * 
126  * @return the Study Scenario or the Study to which this publication belongs to
127  * @see    #getOwnerStudy()
128  */
129     public ProjectElement getOwner () {
130 //  ---------------------------------
131       return owner;
132     }
133
134     /**
135          * Set the owner.
136          * @param owner the owner to set
137          */
138         public void setOwner(ProjectElement owner) {
139                 this.owner = owner;
140         }
141
142         public Study getOwnerStudy () {
143 //  -----------------------------
144       if (owner instanceof Study) return  (Study)owner;
145       else                        return ((Scenario)owner).getOwnerStudy();
146     }
147
148 /**
149  * Returns the state of this published document.
150  * It is the same than the state of the referenced document, unless this publication is out-of-date, in which case it is
151  * In-Work state.
152  * 
153  * @see #outdate()
154  * @see #isOutdated()
155  */
156     public ProgressState getProgressState () {
157 //  ----------------------------------------
158       if (this.isOutdated()) return ProgressState.inWORK;   // Overrides the document state
159       else                   return mydoc.getProgressState();
160     }
161
162     public List<Publication> getRelations (Class<? extends Relation> type) {
163 //  ----------------------------------------------------------------------
164       if (type == null) return null;
165
166       List<Publication> result = new ArrayList<Publication>();
167       List<Relation>    relist = mydoc.getRelations(type);
168       for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
169         Relation     relation  = i.next();
170         Document     relatedoc = (Document)relation.getTo();
171         Publication  related   = owner.getPublication(relatedoc);
172         if (related != null) {
173           result.add(related);
174         } else
175         if (owner instanceof Scenario) {   // The relation may cross steps belonging to a scenario and its owner study
176           related = ((Scenario)owner).getOwnerStudy().getPublication(relatedoc);
177           if (related != null) result.add(related);
178         }
179       }
180       return result;
181     }
182
183     public File getSourceFile () {
184 //  ----------------------------
185       return mydoc.getSourceFile();
186     }
187
188     public boolean isNewForOwner () {
189 //  -------------------------------
190       return (isnew == 'Y');
191     }
192
193     public boolean isOutdated () {
194 //  ----------------------------
195       return (isnew == 'O');
196     }
197
198     /**
199          * Get the isnew.
200          * @return the isnew
201          */
202         public char getIsnew() {
203                 return isnew;
204         }
205         /**
206          * Set the isnew.
207          * @param isnew the isnew to set
208          */
209         public void setIsnew(char isnew) {
210                 this.isnew = isnew;
211         }
212         public void rename (String title) throws InvalidPropertyException {
213 //  ---------------------------------
214       mydoc.rename(title);
215     }
216
217 /**
218  * Out-dates this publication and recursively all publications using this one.
219  * Typically, a publication is out-dated when modifying a document to which it depends.
220  * 
221  * @see #isOutdated()
222  * @see #getProgressState()
223  * @see #actualize()
224  */
225     public void outdate () {
226 //  ----------------------
227       if (this.isOutdated()) return;
228
229       List<Publication> relist = this.getRelations(UsedByRelation.class);
230       for (Iterator<Publication> i = relist.iterator(); i.hasNext(); ) {
231         i.next().outdate();
232       }
233       isnew = 'O';
234       Database.getSession().update(this);
235     }
236
237 /**
238  * Returns the document version referenced by this Publication.
239  */
240     public Document value () {
241 //  ------------------------
242       return mydoc;
243     }
244         /**
245          * Set the mydoc.
246          * @param mydoc the mydoc to set
247          */
248         public void setValue (Document aDoc) {
249                 this.mydoc = aDoc;
250         }
251 }