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.dao.som.Database;
34 import org.splat.kernel.InvalidPropertyException;
35 import org.splat.kernel.MismatchException;
36 import org.splat.kernel.MissedPropertyException;
37 import org.splat.kernel.MultiplyDefinedException;
38 import org.splat.kernel.NotApplicableException;
39 import org.splat.service.technical.IndexServiceImpl;
40 import org.splat.service.technical.ProjectSettingsService;
45 private ProjectSettingsService.Step step;
46 private ProjectElement owner;
47 private List<SimulationContext> contex;
48 private List<Publication> docums;
49 private User actor; // Actor involved in operations on published documents and requiring a time-stamp
51 // ==============================================================================================================================
53 // ==============================================================================================================================
55 public Step (ProjectSettingsService.Step step, ProjectElement owner) {
56 // ----------------------------------------------------------------
59 this.contex = new Vector<SimulationContext>();
60 this.docums = new Vector<Publication>();
63 // Filtering of Simulation contexts, if exist
64 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
65 SimulationContext adoc = i.next();
66 if (!adoc.isInto(this)) continue;
67 this.contex.add(adoc);
69 // Filtering of Documents, if exist
70 for (Iterator<Publication> i=owner.PublicationIterator(); i.hasNext();) {
71 Publication mydoc = i.next();
72 if (!mydoc.value().isInto(this)) continue;
73 this.docums.add(mydoc);
77 // ==============================================================================================================================
78 // Public member functions
79 // ==============================================================================================================================
81 public Publication createDocument (Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException {
82 // -------------------------------------------------------------
83 Document newdoc = new Document(dprop.setOwner(owner).setStep(step));
85 // Creation of the save directory
86 File wdir = newdoc.getSaveDirectory();
87 if (!wdir.exists()) if (!wdir.mkdirs()) throw new IOException("Cannot create the repository vault directory");
89 // Identification and save
90 newdoc.buildReferenceFrom(getOwnerStudy());
91 Database.getSession().save(newdoc);
93 return new Publication(newdoc, owner);
96 public Publication assignDocument (Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, NotApplicableException {
97 // -------------------------------------------------------------
98 String refid = dprop.getReference();
99 if (refid == null) return null;
101 Document slot = Database.selectDocument(refid, new Revision().toString());
102 if ( slot == null ) return null;
103 if (!slot.isUndefined()) return null; // Should not happen
105 slot.initialize(dprop.setOwner(getOwnerStudy()));
106 return new Publication(slot, owner);
109 public Publication versionDocument (Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
110 // -----------------------------------------------------
111 return versionDocument(base, new Document.Properties());
114 public Publication versionDocument (Publication base, String reason) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
115 // --------------------------------------------------------------------
116 return versionDocument(base, new Document.Properties().setDescription(reason));
119 public Publication versionDocument (Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
120 // --------------------------------------------------------------------------------
121 Document previous = base.value();
123 dprop.setDocument(previous); // Initializes the Step property
124 if (dprop.getStep().getNumber() != this.step.getNumber()) throw new MismatchException();
126 if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
127 String summary = dprop.getDescription();
129 // Creation of the document
130 Document newdoc = new Document(dprop.setOwner(owner).setStep(step));
131 newdoc.buildReferenceFrom(getOwner(), previous);
132 Database.getSession().save(newdoc);
135 if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
136 else newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
138 // Update of usedby relations, if exist
139 List<Relation> relist = previous.getRelations(UsedByRelation.class);
140 Study scope = getOwnerStudy();
141 for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
142 UsedByRelation relation = (UsedByRelation)i.next();
143 Document relatedoc = relation.getTo();
144 if (scope.shares(relatedoc)) relatedoc.addRelation( new UsesRelation(relatedoc, newdoc) );
145 else relation.moveTo(newdoc);
147 return new Publication(newdoc, owner);
150 public User getActor () {
151 // -----------------------
155 public List<Publication> getAllDocuments () {
156 // -------------------------------------------
157 return Collections.unmodifiableList(docums);
160 public List<SimulationContext> getAllSimulationContexts () {
161 // ----------------------------------------------------------
162 return Collections.unmodifiableList(contex);
165 public Publication getDocument (int index) {
166 // ------------------------------------------
167 for (Iterator<Publication> i=docums.iterator(); i.hasNext();) {
168 Publication found = i.next(); // In a given study step,
169 if (found.value().getIndex() == index) return found; // there is only one publication of a given document
174 public int getNumber () {
175 // -----------------------
176 return step.getNumber();
179 public ProjectElement getOwner () {
180 // ---------------------------------
181 return owner; // May be a Study or a Scenario
184 public Study getOwnerStudy () {
185 // -----------------------------
186 if (owner instanceof Study) return (Study)owner;
187 else return ((Scenario)owner).getOwnerStudy();
190 public String getPath () {
191 // ------------------------
192 return step.getPath();
195 public List<Publication> getResultDocuments () {
196 // ----------------------------------------------
197 List<Publication> result = new Vector<Publication>();
199 if (!docums.isEmpty()) for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
200 Publication content = i.next();
201 DocumentType type = content.value().getType();
202 if (!type.isResultOf(this.getStep())) continue;
208 public ProjectSettingsService.Step getStep () {
209 // --------------------------------------
213 public SimulationContext getSimulationContext (int index) {
214 // ---------------------------------------------------------
215 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
216 SimulationContext myctex = i.next();
217 if (myctex.getIndex() == index) return myctex;
222 public List<SimulationContext> getSimulationContext (SimulationContextType type) {
223 // --------------------------------------------------------------------------------
224 Vector<SimulationContext> result = new Vector<SimulationContext>();
226 for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
227 SimulationContext myctex = i.next();
228 if (myctex.getType().equals(type)) result.add(myctex);
233 public List<DocumentType> getValidDocumentTypes () {
234 // --------------------------------------------------
235 return Document.selectTypesOf(step);
238 public boolean isStarted () {
239 // ---------------------------
240 if (!step.mayContain(KnowledgeElement.class)) return !docums.isEmpty();
242 List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
243 if (kelm.isEmpty() && docums.isEmpty()) return false;
247 public boolean isFinished () {
248 // ----------------------------
249 if (!step.mayContain(KnowledgeElement.class)) { // Check if all result documents are approved
250 if (docums.isEmpty()) return false;
251 boolean result = false;
252 for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
253 Document content = i.next().value();
254 DocumentType type = content.getType();
255 if (!type.isResultOf(this.getStep())) continue;
256 if (content.getProgressState() == ProgressState.EXTERN) continue;
257 result = true; // There is at least 1 non external result document
258 if (content.getProgressState() != ProgressState.APPROVED) return false;
262 else { // Check if all existing knowledges are approved
263 List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
264 if (kelm.isEmpty()) return false;
265 for (Iterator<KnowledgeElement> i=kelm.iterator(); i.hasNext(); ) {
266 KnowledgeElement content = i.next();
267 if (content.getProgressState() != ProgressState.APPROVED) return false;
273 public boolean mayContain (@SuppressWarnings("rawtypes") Class type) {
274 // --------------------------------------------------------------------
275 return step.mayContain(type);
278 public boolean removeDocument (Publication doctag) {
279 // --------------------------------------------------
280 Document value = doctag.value();
281 Publication torem = getDocument(value.getIndex());
282 Session session = Database.getSession();
284 if (torem == null) return false;
287 session.update(owner);
288 if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
289 Set<Relation> links = value.getAllRelations();
290 List<Document> using = new Vector<Document>();
291 for (Iterator<Relation> i=links.iterator(); i.hasNext(); ) {
292 Relation link = i.next();
293 if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
294 session.delete(link.getTo()); // The corresponding physical file is not removed from the vault
296 if (link.getClass().equals(UsesRelation.class)) { // Document dependency
297 using.add((Document)link.getTo());
300 for (Iterator<Document> i=using.iterator(); i.hasNext(); ) {
301 i.next().removeRelation(UsedByRelation.class, value);
303 session.delete(value); // The corresponding physical file is not removed from the vault
308 public void setActor (User user) {
309 // --------------------------------
312 // ==============================================================================================================================
313 // Protected member functions
314 // ==============================================================================================================================
316 public boolean add (Publication newdoc) {
317 // ------------------------------------------
318 if (!owner.add(newdoc)) return false; // Updates the study in memory
319 docums.add(0, newdoc); // Updates this step
320 newdoc.value().hold(); // Increments the configuration tag count of document
321 // If not yet saved, the Publication MUST NOT be saved here, although this creates a temporary inconsistent state into the
322 // database (it will be saved later by cascading the update of owner scenario).
326 public boolean remove (Publication oldoc) {
327 // --------------------------------------------
328 if (!owner.remove(oldoc)) return false; // Updates the study in memory
329 docums.remove(oldoc); // Updates this step
330 oldoc.value().release(); // Decrements the configuration tag count of document
331 // The publication becoming orphan, it should automatically be removed from the database when updating of owner scenario.
335 public List<SimulationContext> getContex() {