Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[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 /**
112  * Returns either the Study Scenario or the Study itself to which this publication belongs, depending on the Study Step into
113  * which the referenced document has been published.<br/>
114  * If this publication belongs to a Study, the Project Element returned is the Study returned by getOwnerStudy().
115  * 
116  * @return the Study Scenario or the Study to which this publication belongs to
117  * @see    #getOwnerStudy()
118  */
119     public ProjectElement getOwner () {
120 //  ---------------------------------
121       return owner;
122     }
123
124     /**
125          * Set the owner.
126          * @param owner the owner to set
127          */
128         public void setOwner(ProjectElement owner) {
129                 this.owner = owner;
130         }
131
132         public Study getOwnerStudy () {
133 //  -----------------------------
134       if (owner instanceof Study) return  (Study)owner;
135       else                        return ((Scenario)owner).getOwnerStudy();
136     }
137
138 /**
139  * Returns the state of this published document.
140  * It is the same than the state of the referenced document, unless this publication is out-of-date, in which case it is
141  * In-Work state.
142  * 
143  * @see #outdate()
144  * @see #isOutdated()
145  */
146     public ProgressState getProgressState () {
147 //  ----------------------------------------
148       if (this.isOutdated()) return ProgressState.inWORK;   // Overrides the document state
149       else                   return mydoc.getProgressState();
150     }
151
152     public List<Publication> getRelations (Class<? extends Relation> type) {
153 //  ----------------------------------------------------------------------
154       if (type == null) return null;
155
156       List<Publication> result = new ArrayList<Publication>();
157       List<Relation>    relist = mydoc.getRelations(type);
158       for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
159         Relation     relation  = i.next();
160         Document     relatedoc = (Document)relation.getTo();
161         Publication  related   = owner.getPublication(relatedoc);
162         if (related != null) {
163           result.add(related);
164         } else
165         if (owner instanceof Scenario) {   // The relation may cross steps belonging to a scenario and its owner study
166           related = ((Scenario)owner).getOwnerStudy().getPublication(relatedoc);
167           if (related != null) result.add(related);
168         }
169       }
170       return result;
171     }
172
173     public File getSourceFile () {
174 //  ----------------------------
175       return mydoc.getSourceFile();
176     }
177
178     public boolean isNewForOwner () {
179 //  -------------------------------
180       return (isnew == 'Y');
181     }
182
183     public boolean isOutdated () {
184 //  ----------------------------
185       return (isnew == 'O');
186     }
187
188     /**
189          * Get the isnew.
190          * @return the isnew
191          */
192         public char getIsnew() {
193                 return isnew;
194         }
195         /**
196          * Set the isnew.
197          * @param isnew the isnew to set
198          */
199         public void setIsnew(char isnew) {
200                 this.isnew = isnew;
201         }
202 /**
203  * Out-dates this publication and recursively all publications using this one.
204  * Typically, a publication is out-dated when modifying a document to which it depends.
205  * 
206  * @see #isOutdated()
207  * @see #getProgressState()
208  * @see #actualize()
209  */
210     public void outdate () {
211 //  ----------------------
212       if (this.isOutdated()) return;
213
214       List<Publication> relist = this.getRelations(UsedByRelation.class);
215       for (Iterator<Publication> i = relist.iterator(); i.hasNext(); ) {
216         i.next().outdate();
217       }
218       isnew = 'O';
219       Database.getSession().update(this);
220     }
221
222 /**
223  * Returns the document version referenced by this Publication.
224  */
225     public Document value () {
226 //  ------------------------
227       return mydoc;
228     }
229         /**
230          * Set the mydoc.
231          * @param mydoc the mydoc to set
232          */
233         public void setValue (Document aDoc) {
234                 this.mydoc = aDoc;
235         }
236 }