Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / ProjectElement.java
1 package org.splat.dal.bo.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.util.Collections;
9 import java.util.Date;
10 import java.util.Iterator;
11 import java.util.LinkedHashSet;
12 import java.util.List;
13 import java.util.Set;
14 import java.util.Vector;
15
16 import org.apache.log4j.Logger;
17
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.dal.dao.som.Database;
20 import org.splat.kernel.ObjectProperties;
21 import org.splat.kernel.InvalidPropertyException;
22 import org.splat.kernel.MissedPropertyException;
23 import org.splat.kernel.MultiplyDefinedException;
24 import org.splat.som.Step;
25
26
27 public abstract class ProjectElement extends Entity {
28
29 //  Persistent fields
30     protected  String                   title;
31     protected  User                     manager;
32     protected  Date                     credate;    // Object creation date
33     protected  Date                     lasdate;    // Object Last modification date
34     private    List<SimulationContext>  contex;     // Structured by the Step transient class
35     private    Set<Publication>         docums;     // Structured by the Step transient class
36
37 //  Transient field
38     private    Step[]                   folders;
39
40     /**
41          * Set the folders.
42          * @param folders the folders to set
43          */
44         public void setFolders(Step[] folders) {
45                 this.folders = folders;
46         }
47         /**
48          * Get the folders.
49          * @return the folders
50          */
51         public Step[] getFolders() {
52                 return folders;
53         }
54
55         protected final static Logger       logger = Logger.getLogger(ProjectElement.class);
56
57 //  ==============================================================================================================================
58 //  Constructors
59 //  ==============================================================================================================================
60
61 //  Database fetch constructor
62     protected ProjectElement () {
63 //  ---------------------------
64       folders  = null;
65     }
66 //  Initialization constructor
67     protected ProjectElement (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
68 //  -------------------------------------------------           
69       super(oprop);       // Throws one of the above exception if not valid
70       title    = null;     // Initialized by subclasses
71       credate  = null;     // Initialized by subclasses
72       lasdate  = null;     // Initialized by subclasses
73       docums   = new LinkedHashSet<Publication>();
74       contex   = new Vector<SimulationContext>();
75       
76       folders  = null;
77     }
78
79 //  ==============================================================================================================================
80 //  Public member functions
81 //  ==============================================================================================================================
82
83     public User getAuthor () {
84 //  ------------------------
85       return manager;
86     }
87
88 /**
89  * Returns the creation date of this Project Element.
90  */
91     public Date getDate () {
92 //  ----------------------
93       return credate;
94     }
95
96     public String getDescription () {
97 //  -------------------------------
98       String               summary = null;
99       DescriptionAttribute field   = (DescriptionAttribute)this.getAttribute(DescriptionAttribute.class);
100       if (field != null)   summary = field.getValue();
101       return summary;               // May be null
102     }
103
104     public Date getLastModificationDate () {
105       return lasdate;
106     }
107
108     public void setLastModificationDate (Date aDate) {
109       lasdate = aDate;
110     }
111
112     /**
113  * Returns the publication into this Project Element of the given document version, if exists.
114  * If exists, a document publication id unique in a given ProjectElement.
115  * 
116  * @param doc a document version published into this Project Element
117  * @return the publication of the document version, or null if the given document version is not published into this Project Element
118  */
119     public Publication getPublication (Document doc) {
120 //  ------------------------------------------------
121       int  index = doc.getIndex();
122       for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
123         Publication  found = i.next();
124         if (found.value().getIndex() == index) return found;   // A document publication is unique in a given ProjectElement
125       }
126       return null;
127     }
128
129     public String getTitle () {
130 //  -------------------------
131       return title;
132     }
133
134     public void setTitle (String aTitle) {
135       title = aTitle;
136     }
137
138     public boolean publishes (Document doc) {
139 //  ---------------------------------------
140       int  index = doc.getIndex();
141       for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
142         Document  found = i.next().value();
143         if (found.getIndex() == index) return true;
144       }
145       return false;
146     }
147
148     public Iterator<Publication> PublicationIterator () {
149 //  ---------------------------------------------------
150       return  Collections.unmodifiableSet(docums).iterator();
151     }
152
153     public Iterator<SimulationContext> SimulationContextIterator () {
154 //  ---------------------------------------------------------------
155       return  Collections.unmodifiableList(contex).iterator();
156     }
157
158 //  ==============================================================================================================================
159 //  Protected member functions
160 //  ==============================================================================================================================
161
162     public boolean add (Publication newdoc) {
163 //  ------------------------------------------
164       return  docums.add(newdoc);
165     }
166
167     public boolean add (SimulationContext newdoc) {
168 //  ------------------------------------------------
169       return  contex.add(newdoc);
170     }
171
172     public boolean remove (Publication oldoc) {
173 //  --------------------------------------------
174       return  docums.remove(oldoc);   // The removed tag becoming orphan, it is supposed automatically deleted from the data store
175     }
176
177     public boolean remove (SimulationContext oldoc) {
178 //  --------------------------------------------------
179       return  contex.remove(oldoc);
180     }
181
182 /**
183  * Refreshes the internal data potentially out-of-date.
184  * This function needs to be called when Publication objects are added to this Project Element before being saved. The reason is,
185  * as saving a persistent object changes its hashcode, hashed data need to be rebuilt after saving for making functions based
186  * on this hashcode such as remove(), working.
187  */
188     public void refresh () {
189 //  -------------------------
190       Publication[] curdoc = docums.toArray(new Publication[docums.size()]);
191
192       folders = null;                 // Just in case
193       docums.clear();
194       for (int i=0; i<curdoc.length; i++) docums.add(curdoc[i]);
195 //    No need to rebuild the list of SimulationContext as it does not use hashcodes
196       Database.getSession().update(this);
197     }
198 }