4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
9 import java.io.IOException;
10 import java.util.Collections;
11 import java.util.List;
13 import java.util.Vector;
14 import java.util.Iterator;
16 import org.hibernate.Session;
17 import org.splat.dal.bo.kernel.Relation;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.dal.bo.som.ConvertsRelation;
20 import org.splat.dal.bo.som.Document;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.dal.bo.som.KnowledgeElement;
23 import org.splat.dal.bo.som.ProgressState;
24 import org.splat.dal.bo.som.ProjectElement;
25 import org.splat.dal.bo.som.Publication;
26 import org.splat.dal.bo.som.Scenario;
27 import org.splat.dal.bo.som.SimulationContext;
28 import org.splat.dal.bo.som.SimulationContextType;
29 import org.splat.dal.bo.som.Study;
30 import org.splat.dal.bo.som.UsedByRelation;
31 import org.splat.dal.bo.som.UsesRelation;
32 import org.splat.dal.bo.som.VersionsRelation;
33 import org.splat.dal.bo.som.Document.Properties;
34 import org.splat.dal.dao.som.Database;
35 import org.splat.kernel.InvalidPropertyException;
36 import org.splat.kernel.MismatchException;
37 import org.splat.kernel.MissedPropertyException;
38 import org.splat.kernel.MultiplyDefinedException;
39 import org.splat.kernel.NotApplicableException;
40 import org.splat.service.technical.IndexServiceImpl;
41 import org.splat.service.technical.ProjectSettingsService;
46 private ProjectSettingsService.Step step;
47 private ProjectElement owner;
48 private List<SimulationContext> contex;
49 private List<Publication> docums;
50 private User actor; // Actor involved in operations on published documents and requiring a time-stamp
52 // ==============================================================================================================================
54 // ==============================================================================================================================
56 public Step (ProjectSettingsService.Step step, ProjectElement owner) {
57 // ----------------------------------------------------------------
60 this.contex = new Vector<SimulationContext>();
61 this.docums = new Vector<Publication>();
64 // Filtering of Simulation contexts, if exist
65 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
66 SimulationContext adoc = i.next();
67 if (!adoc.isInto(this)) continue;
68 this.contex.add(adoc);
70 // Filtering of Documents, if exist
71 for (Iterator<Publication> i=owner.PublicationIterator(); i.hasNext();) {
72 Publication mydoc = i.next();
73 if (!mydoc.value().isInto(this)) continue;
74 this.docums.add(mydoc);
78 // ==============================================================================================================================
79 // Public member functions
80 // ==============================================================================================================================
82 public User getActor () {
83 // -----------------------
87 public List<Publication> getAllDocuments () {
88 // -------------------------------------------
89 return Collections.unmodifiableList(docums);
92 public List<SimulationContext> getAllSimulationContexts () {
93 // ----------------------------------------------------------
94 return Collections.unmodifiableList(contex);
97 public Publication getDocument (long l) {
98 // ------------------------------------------
99 for (Iterator<Publication> i=docums.iterator(); i.hasNext();) {
100 Publication found = i.next(); // In a given study step,
101 if (found.value().getIndex() == l) return found; // there is only one publication of a given document
106 public int getNumber () {
107 // -----------------------
108 return step.getNumber();
111 public ProjectElement getOwner () {
112 // ---------------------------------
113 return owner; // May be a Study or a Scenario
116 public Study getOwnerStudy () {
117 // -----------------------------
118 if (owner instanceof Study) return (Study)owner;
119 else return ((Scenario)owner).getOwnerStudy();
122 public String getPath () {
123 // ------------------------
124 return step.getPath();
127 public List<Publication> getResultDocuments () {
128 // ----------------------------------------------
129 List<Publication> result = new Vector<Publication>();
131 if (!docums.isEmpty()) for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
132 Publication content = i.next();
133 DocumentType type = content.value().getType();
134 if (!type.isResultOf(this.getStep())) continue;
140 public ProjectSettingsService.Step getStep () {
141 // --------------------------------------
145 public SimulationContext getSimulationContext (long l) {
146 // ---------------------------------------------------------
147 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
148 SimulationContext myctex = i.next();
149 if (myctex.getIndex() == l) return myctex;
154 public List<SimulationContext> getSimulationContext (SimulationContextType type) {
155 // --------------------------------------------------------------------------------
156 Vector<SimulationContext> result = new Vector<SimulationContext>();
158 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
159 SimulationContext myctex = i.next();
160 if (myctex.getType().equals(type)) result.add(myctex);
165 public List<DocumentType> getValidDocumentTypes () {
166 // --------------------------------------------------
167 return Document.selectTypesOf(step);
170 public boolean isStarted () {
171 // ---------------------------
172 if (!step.mayContain(KnowledgeElement.class)) return !docums.isEmpty();
174 List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
175 if (kelm.isEmpty() && docums.isEmpty()) return false;
179 public boolean isFinished () {
180 // ----------------------------
181 if (!step.mayContain(KnowledgeElement.class)) { // Check if all result documents are approved
182 if (docums.isEmpty()) return false;
183 boolean result = false;
184 for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
185 Document content = i.next().value();
186 DocumentType type = content.getType();
187 if (!type.isResultOf(this.getStep())) continue;
188 if (content.getProgressState() == ProgressState.EXTERN) continue;
189 result = true; // There is at least 1 non external result document
190 if (content.getProgressState() != ProgressState.APPROVED) return false;
194 else { // Check if all existing knowledges are approved
195 List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
196 if (kelm.isEmpty()) return false;
197 for (Iterator<KnowledgeElement> i=kelm.iterator(); i.hasNext(); ) {
198 KnowledgeElement content = i.next();
199 if (content.getProgressState() != ProgressState.APPROVED) return false;
205 public boolean mayContain (@SuppressWarnings("rawtypes") Class type) {
206 // --------------------------------------------------------------------
207 return step.mayContain(type);
210 public boolean removeDocument (Publication doctag) {
211 // --------------------------------------------------
212 Document value = doctag.value();
213 Publication torem = getDocument(value.getIndex());
214 Session session = Database.getSession();
216 if (torem == null) return false;
219 session.update(owner);
220 if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
221 Set<Relation> links = value.getAllRelations();
222 List<Document> using = new Vector<Document>();
223 for (Iterator<Relation> i=links.iterator(); i.hasNext(); ) {
224 Relation link = i.next();
225 if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
226 session.delete(link.getTo()); // The corresponding physical file is not removed from the vault
228 if (link.getClass().equals(UsesRelation.class)) { // Document dependency
229 using.add((Document)link.getTo());
232 for (Iterator<Document> i=using.iterator(); i.hasNext(); ) {
233 i.next().removeRelation(UsedByRelation.class, value);
235 session.delete(value); // The corresponding physical file is not removed from the vault
240 public void setActor (User user) {
241 // --------------------------------
244 // ==============================================================================================================================
245 // Protected member functions
246 // ==============================================================================================================================
248 public boolean add (Publication newdoc) {
249 // ------------------------------------------
250 if (!owner.add(newdoc)) return false; // Updates the study in memory
251 docums.add(0, newdoc); // Updates this step
252 newdoc.value().hold(); // Increments the configuration tag count of document
253 // If not yet saved, the Publication MUST NOT be saved here, although this creates a temporary inconsistent state into the
254 // database (it will be saved later by cascading the update of owner scenario).
258 public boolean remove (Publication oldoc) {
259 // --------------------------------------------
260 if (!owner.remove(oldoc)) return false; // Updates the study in memory
261 docums.remove(oldoc); // Updates this step
262 oldoc.value().release(); // Decrements the configuration tag count of document
263 // The publication becoming orphan, it should automatically be removed from the database when updating of owner scenario.
267 public List<SimulationContext> getContex() {