Salome HOME
Copyrights update 2015.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / ProjectElement.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012-2015
7  */
8
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.Vector;
17
18 import org.splat.dal.bo.kernel.Entity;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.kernel.MissedPropertyException;
22 import org.splat.kernel.MultiplyDefinedException;
23 import org.splat.kernel.ObjectProperties;
24 import org.splat.som.Step;
25
26 /**
27  * Persistent project element class. It can be study or scenario.
28  */
29 public abstract class ProjectElement extends Entity {
30
31         // Persistent fields
32         /**
33          * Persistent field: project element title.
34          */
35         protected String title;
36         /**
37          * Persistent field: project element author/manager.
38          */
39         protected User manager;
40         /**
41          * Persistent field: object creation date.
42          */
43         protected Date credate;
44         /**
45          * Persistent field: object last modification date.
46          */
47         protected Date lasdate;
48         /**
49          * Persistent field: simulation contexts structured by the Step transient class.
50          */
51         private final List<SimulationContext> contex = new Vector<SimulationContext>();
52         /**
53          * Persistent field: documents publications structured by the Step transient class.
54          */
55         private final Set<Publication> docums = new LinkedHashSet<Publication>();
56
57         /**
58          * Transient array of steps (folders).
59          */
60         private Step[] folders;
61
62         /**
63          * Set the transient array of steps (folders).
64          * 
65          * @param folders
66          *            the steps to set
67          */
68         public void setFolders(final Step[] folders) {
69                 this.folders = folders;
70         }
71
72         /**
73          * Get the transient array of steps (folders).
74          * 
75          * @return the array of steps
76          */
77         public Step[] getFolders() {
78                 return folders;
79         }
80
81         // ==============================================================================================================================
82         // Constructors
83         // ==============================================================================================================================
84
85         /**
86          * Database fetch constructor.
87          */
88         protected ProjectElement() {
89                 super();
90                 folders = null;
91         }
92
93         /**
94          * Initialization constructor.
95          * 
96          * @param oprop
97          * @throws MissedPropertyException
98          * @throws InvalidPropertyException
99          * @throws MultiplyDefinedException
100          */
101         protected ProjectElement(final ObjectProperties oprop)
102                         throws MissedPropertyException, InvalidPropertyException,
103                         MultiplyDefinedException {
104                 super(oprop); // Throws one of the above exception if not valid
105                 title = null; // Initialized by subclasses
106                 credate = null; // Initialized by subclasses
107                 lasdate = null; // Initialized by subclasses
108                 // RKV docums = new LinkedHashSet<Publication>();
109                 // RKV contex = new Vector<SimulationContext>();
110
111                 folders = null;
112         }
113
114         // ==============================================================================================================================
115         // Public member functions
116         // ==============================================================================================================================
117
118         public User getAuthor() {
119                 return manager;
120         }
121
122         /**
123          * Returns the creation date of this Project Element.
124          */
125         public Date getDate() {
126                 return credate;
127         }
128
129         public String getDescription() {
130                 String summary = null;
131                 DescriptionAttribute field = (DescriptionAttribute) this
132                                 .getAttribute(DescriptionAttribute.class);
133                 if (field != null) {
134                         summary = field.getValue();
135                 }
136                 return summary; // May be null
137         }
138
139         public Date getLastModificationDate() {
140                 return lasdate;
141         }
142
143         public void setLastModificationDate(final Date aDate) {
144                 lasdate = aDate;
145         }
146
147         /**
148          * Returns the publication into this Project Element of the given document version, if exists. If exists, a document publication id
149          * unique in a given ProjectElement.
150          * 
151          * @param doc
152          *            a document version published into this Project Element
153          * @return the publication of the document version, or null if the given document version is not published into this Project Element
154          */
155         public Publication getPublication(final Document doc) {
156                 long index = doc.getIndex();
157                 for (Iterator<Publication> i = docums.iterator(); i.hasNext();) {
158                         Publication found = i.next();
159                         if (found.value().getIndex() == index) {
160                                 return found; // A document publication is unique in a given ProjectElement
161                         }
162                 }
163                 return null;
164         }
165
166         public String getTitle() {
167                 return title;
168         }
169
170         public void setTitle(final String aTitle) {
171                 title = aTitle;
172         }
173
174         /**
175          * Check if the given document is published in the scenario.
176          * 
177          * @param doc
178          *            the document
179          * @return true if the document is published in the scenario
180          */
181         public boolean publishes(final Document doc) {
182                 boolean res = false;
183                 long index = doc.getIndex();
184                 for (Publication pub : docums) {
185                         Document found = pub.value();
186                         res = (found.getIndex() == index);
187                         if (res) {
188                                 break;
189                         }
190                 }
191                 return res;
192         }
193
194         public Iterator<Publication> PublicationIterator() {
195                 return Collections.unmodifiableSet(docums).iterator();
196         }
197
198         public Iterator<SimulationContext> SimulationContextIterator() {
199                 return Collections.unmodifiableList(contex).iterator();
200         }
201
202         // ==============================================================================================================================
203         // Protected member functions
204         // ==============================================================================================================================
205
206         /**
207          * Add a document publication to the project element.
208          * 
209          * @param newdoc
210          *            the publication to add
211          * @return true if added successfully, otherwise false
212          */
213         public boolean add(final Publication newdoc) {
214                 return docums.add(newdoc);
215         }
216
217         /**
218          * Add a simulation context to the project element.
219          * 
220          * @param newdoc
221          *            the simulation context to add
222          * @return true if added successfully, otherwise false
223          */
224         public boolean add(final SimulationContext newdoc) {
225                 return contex.add(newdoc);
226         }
227
228         /**
229          * Remove the document publication from the project element.
230          * 
231          * @param oldoc
232          *            the publication to remove
233          * @return true if removed successfully, otherwise false
234          */
235         public boolean remove(final Publication oldoc) {
236                 return docums.remove(oldoc); // The removed tag becoming orphan, it is supposed automatically deleted from the data store
237         }
238
239         /**
240          * Remove the simulation context from the project element.
241          * 
242          * @param oldoc
243          *            the simulation context to remove
244          * @return true if removed successfully, otherwise false
245          */
246         public boolean remove(final SimulationContext oldoc) {
247                 return contex.remove(oldoc);
248         }
249
250         /**
251          * Get the docums.
252          * 
253          * @return the docums
254          */
255         public Set<Publication> getDocums() {
256                 return docums;
257         }
258
259         /**
260          * Return the study of the project element.
261          * 
262          * @return the project element study
263          */
264         public abstract Study getOwnerStudy();
265
266         /**
267          * {@inheritDoc}
268          * 
269          * @see org.splat.dal.bo.kernel.Entity#evict()
270          */
271         @Override
272         public void evict() {
273                 super.evict();
274                 // Evict all attributes of the persistent object
275                 Set<Publication> tmpSet = new HashSet<Publication>();
276                 tmpSet.addAll(docums);
277                 docums.clear();
278                 // Evict publications
279                 for (Publication rel : tmpSet) {
280                         if (rel.isSaved()) { // to avoid recursive evict
281                                 rel.evict();
282                         }
283                         docums.add(rel);
284                 }
285         }
286 }