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