4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.util.Collections;
10 import java.util.Iterator;
11 import java.util.LinkedHashSet;
12 import java.util.List;
14 import java.util.Vector;
16 import org.apache.log4j.Logger;
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;
25 public abstract class ProjectElement extends Entity {
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
36 private Step[] folders;
38 protected final static Logger logger = Logger.getLogger(ProjectElement.class);
40 // ==============================================================================================================================
42 // ==============================================================================================================================
44 // Database fetch constructor
45 protected ProjectElement () {
46 // ---------------------------
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>();
62 // ==============================================================================================================================
63 // Public member functions
64 // ==============================================================================================================================
66 public User getAuthor () {
67 // ------------------------
72 * Returns the creation date of this Project Element.
74 public Date getDate () {
75 // ----------------------
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
87 public Step getFirstStep () {
88 // ---------------------------
89 return this.getSteps()[0];
92 public Date getLastModificationDate () {
93 // --------------------------------------
97 public Step getLastStep () {
98 // --------------------------
99 Step[] mystep = this.getSteps(); // For getting the folders length, if null
100 return mystep[mystep.length-1];
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.
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
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
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();
126 folders = new Step[steps.size()];
127 for (int i=0; i<folders.length; i++) {
128 folders[i] = new Step(nstep.next(), this);
131 return folders; // No protection against this object corruption as it would not corrupt the database
134 public String getTitle () {
135 // -------------------------
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;
149 public Iterator<Publication> PublicationIterator () {
150 // ---------------------------------------------------
151 return Collections.unmodifiableSet(docums).iterator();
154 public Iterator<SimulationContext> SimulationContextIterator () {
155 // ---------------------------------------------------------------
156 return Collections.unmodifiableList(contex).iterator();
159 // ==============================================================================================================================
160 // Protected member functions
161 // ==============================================================================================================================
163 protected boolean add (Publication newdoc) {
164 // ------------------------------------------
165 return docums.add(newdoc);
168 protected boolean add (SimulationContext newdoc) {
169 // ------------------------------------------------
170 return contex.add(newdoc);
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
178 protected boolean remove (SimulationContext oldoc) {
179 // --------------------------------------------------
180 return contex.remove(oldoc);
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.
189 protected void refresh () {
190 // -------------------------
191 Publication[] curdoc = docums.toArray(new Publication[docums.size()]);
193 folders = null; // Just in case
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);