1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.io.FileNotFoundException;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.hibernate.Session;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.dal.bo.som.Document;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.dal.bo.som.ProgressState;
23 import org.splat.dal.bo.som.ProjectElement;
24 import org.splat.dal.bo.som.Publication;
25 import org.splat.dal.bo.som.SimulationContext;
26 import org.splat.dal.bo.som.SimulationContextType;
27 import org.splat.dal.bo.som.Study;
28 import org.splat.dal.bo.som.Timestamp;
29 import org.splat.dal.bo.som.ValidationCycle;
30 import org.splat.dal.bo.som.ValidationStep;
31 import org.splat.dal.dao.som.Database;
32 import org.splat.kernel.NotApplicableException;
33 import org.splat.manox.Reader;
34 import org.splat.manox.Toolbox;
35 import org.splat.som.Revision;
36 import org.splat.som.Step;
39 * Publication service implementation.
41 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
43 public class PublicationServiceImpl implements PublicationService {
46 * Injected study service.
48 private StudyService _studyService;
50 * Injected study service.
52 private StepService _stepService;
54 * Injected study service.
56 private DocumentTypeService _documentTypeService;
58 * Injected study service.
60 private ProjectElementService _projectElementService;
62 * Injected simulation context service.
64 private SimulationContextService _simulationContextService;
69 * @see org.splat.service.PublicationService#copy(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProjectElement)
71 public Publication copy(Publication aPublication, ProjectElement publisher) {
72 Publication copy = new Publication();
73 copy.setValue(aPublication.value());
74 copy.setStep(aPublication.getStep()); // May not be initialized yet
75 copy.setOwner(publisher);
76 copy.setIsnew(aPublication.getIsnew());
77 if (!copy.getOwnerStudy().equals(aPublication.getOwnerStudy())) {
78 copy.setIsnew('N'); // The referenced document is not new for the given study
86 * @see org.splat.service.PublicationService#approve(org.splat.dal.bo.som.Publication, java.util.Date)
88 public Timestamp approve(Publication aPublication, Date adate) {
89 // -------------------------------------
90 if (aPublication.isOutdated())
92 else if (aPublication.value().getProgressState() != ProgressState.inCHECK)
93 return null; // This statement must conform to the corresponding right
95 DocumentType type = aPublication.value().getType();
96 Study owner = aPublication.getOwnerStudy();
97 ValidationCycle cycle = owner.getValidationCycleOf(type);
98 User approver = cycle.getActor(ValidationStep.APPROVAL);
99 Timestamp stamp = new Timestamp(ValidationStep.APPROVAL, aPublication
100 .value(), approver, adate);
101 if (!aPublication.value().promote(stamp))
103 if (getDocumentTypeService().isStudyResult(type)
104 && owner.getProgressState() == ProgressState.inCHECK)
105 getStudyService().promote(owner);
106 return stamp; // Hoping that promotion of the study succeeded
112 * @see org.splat.service.PublicationService#demote(org.splat.dal.bo.som.Publication)
114 public boolean demote(Publication aPublication) {
115 // ------------------------
116 DocumentType type = aPublication.value().getType();
117 Study owner = aPublication.getOwnerStudy();
119 if (aPublication.value().getProgressState() == ProgressState.inCHECK) {
120 ValidationCycle cycle = owner.getValidationCycleOf(type);
121 if (cycle.enables(ValidationStep.REVIEW)) {
122 if (!aPublication.value().demote())
125 if (!aPublication.value().demote())
127 aPublication.value().demote();
129 } else if (aPublication.value().getProgressState() == ProgressState.inDRAFT) {
130 if (!aPublication.value().demote())
135 if (getDocumentTypeService().isStudyResult(type)
136 && owner.getProgressState() != ProgressState.inWORK)
137 getStudyService().demote(owner);
144 * @see org.splat.service.PublicationService#invalidate(org.splat.dal.bo.som.Publication)
146 public boolean invalidate(Publication aPublication) {
147 // ----------------------------
148 if (aPublication.value().getProgressState() != ProgressState.inCHECK)
150 if (!aPublication.value().demote()) // Removes the reviewer if this document is In-Check
152 DocumentType type = aPublication.value().getType();
153 Study owner = aPublication.getOwnerStudy();
154 if (getDocumentTypeService().isStudyResult(type)
155 && owner.getProgressState() == ProgressState.inCHECK)
156 getStudyService().demote(owner);
163 * @see org.splat.service.PublicationService#promote(org.splat.dal.bo.som.Publication, java.util.Date)
165 public Timestamp promote(Publication aPublication, Date pdate) {
166 if (aPublication.isOutdated())
168 else if (aPublication.value().getProgressState() != ProgressState.inWORK)
169 return null; // This statement must conform to the corresponding right
171 DocumentType type = aPublication.value().getType();
172 Study owner = aPublication.getOwnerStudy();
173 ValidationCycle cycle = owner.getValidationCycleOf(type);
174 User promoter = cycle.getActor(ValidationStep.PROMOTION);
175 if (promoter == null)
176 promoter = getInvolvedStep(aPublication).getActor();
177 if (promoter == null)
178 promoter = owner.getAuthor();
179 Timestamp stamp = new Timestamp(ValidationStep.PROMOTION,
180 aPublication.value(), promoter, pdate);
182 if (!aPublication.value().promote(stamp)) // Promotion to being reviewed
184 if (!cycle.enables(ValidationStep.REVIEW)) {
185 aPublication.value().promote(null);
187 if (getDocumentTypeService().isStudyResult(type)
188 && owner.getProgressState() == ProgressState.inWORK)
189 getStudyService().promote(owner);
190 return stamp; // Hoping that promotion of the study succeeded
197 * @see org.splat.service.PublicationService#review(org.splat.dal.bo.som.Publication, java.util.Date)
199 public Timestamp review(Publication aPublication, Date rdate) {
200 if (aPublication.isOutdated())
202 else if (aPublication.value().getProgressState() != ProgressState.inDRAFT)
203 return null; // This statement must conform to the corresponding right
205 DocumentType type = aPublication.value().getType();
206 Study owner = aPublication.getOwnerStudy();
207 ValidationCycle cycle = owner.getValidationCycleOf(type);
208 User reviewer = cycle.getActor(ValidationStep.REVIEW);
209 Timestamp stamp = new Timestamp(ValidationStep.REVIEW, aPublication
210 .value(), reviewer, rdate);
211 if (!aPublication.value().promote(stamp))
213 if (getDocumentTypeService().isStudyResult(type)
214 && owner.getProgressState() == ProgressState.inDRAFT)
215 getStudyService().promote(owner);
216 return stamp; // Hoping that promotion of the study succeeded
222 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.som.Revision)
225 public void saveAs(Publication aPublication, Revision newvers)
226 throws FileNotFoundException, NotApplicableException {
227 // -------------------------------------
228 if (aPublication.value().isUndefined())
229 throw new NotApplicableException(
230 "Cannot save a Publication object refering an undefined Document");
231 if (!aPublication.value().getSourceFile().exists())
232 throw new FileNotFoundException();
234 Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based) hascode
235 aPublication.value().updateAs(newvers); // May change the branch name of given revision
236 updateOwner(aPublication);
242 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProgressState)
244 public void saveAs(Publication aPublication, ProgressState state)
245 throws FileNotFoundException, NotApplicableException {
246 // ----------------------------------------
247 if (aPublication.value().isUndefined())
248 throw new NotApplicableException(
249 "Cannot save a Publication object refering an undefined Document");
250 if (!aPublication.value().getSourceFile().exists())
251 throw new FileNotFoundException();
253 if (state == ProgressState.inWORK || state == ProgressState.EXTERN) {
254 Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
256 aPublication.value().updateAs(state);
258 DocumentType mytype = aPublication.value().getType();
259 Study owner = aPublication.getOwnerStudy();
260 ValidationCycle cycle = owner.getValidationCycleOf(mytype);
261 boolean review = cycle.enables(ValidationStep.REVIEW);
262 if (!(state == ProgressState.inDRAFT && review)
263 && !(state == ProgressState.inCHECK && !review)) {
264 throw new NotApplicableException(
265 "Cannot save a result document in " + state.toString()
268 Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
270 aPublication.value().updateAs(ProgressState.inWORK);
272 promote(aPublication, aPublication.value()
273 .getLastModificationDate()); // Promotes to the appropriate state in accordance to the validation cycle
275 updateOwner(aPublication);
279 * Update an owner of the publication.
281 * @param aPublication
282 * the document publication
284 private void updateOwner(Publication aPublication) {
285 Session session = Database.getSession();
286 Step step = getInvolvedStep(aPublication);
288 // Update of involved step
289 Document previous = aPublication.value().getPreviousVersion();
290 if (previous != null) {
291 Publication oldoc = step.getDocument(previous.getIndex());
292 boolean done = step.remove(oldoc); // Decrements the configuration tag count of document
294 session.delete(oldoc); // WARNING: Potential problem because it's not automatically done as orphan object
296 step.add(aPublication); // Increments the configuration tag count of document
298 // Import the document properties and update of the study
299 forwardProperties(aPublication, aPublication.value().getSourceFile()
301 session.update(aPublication.getOwner());
305 * Propagate simulation contexts from the given config file to the publication's owner (study or step).
307 * @param aPublication
308 * the document publication
314 private void forwardProperties(Publication aPublication, java.io.File from,
316 // -----------------------------------------------------------
317 Reader tool = Toolbox.getReader(from);
319 return; // No properties extractor available for this type of document
321 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
322 .setStep(to.getStep()).setState(ProgressState.APPROVED);
323 List<SimulationContextType> contype = getSimulationContextService()
324 .selectTypesWhere(sprop);
325 if (contype.isEmpty())
326 return; // No approved property type configured at this step
328 SimulationContext.Properties cprop = new SimulationContext.Properties();
329 List<SimulationContext> context = to.getAllSimulationContexts();
331 context = new ArrayList<SimulationContext>(context.size());
332 context.addAll(to.getAllSimulationContexts());
333 cprop.disableCheck();
334 for (Iterator<SimulationContextType> i = contype.iterator(); i
336 SimulationContextType property = i.next();
337 for (Iterator<SimulationContext> j = context.iterator(); j
339 SimulationContext existing = j.next();
340 if (!existing.getType().equals(property))
342 property = null; // Forget this property as it is already set
345 if (property != null)
347 String value = tool.extractProperty(property.getName());
349 continue; // Property not defined into the document
351 cprop.setType(property).setValue(value);
352 if (aPublication.getOwner() instanceof Study)
353 getStudyService().addProjectContext(
354 (Study) aPublication.getOwner(), cprop); // Re-indexes knowledges and the study
356 getStepService().addSimulationContext(to, cprop); // Re-indexes knowledges only
357 } catch (Exception e) {
364 * Returns the study Step into which the document version referenced by this publication has been published.
366 * @param aPublication
367 * the document publication
368 * @return the study step where the document is published
370 public Step getInvolvedStep(Publication aPublication) {
371 if (aPublication.getStep() == null) {
372 Step[] step = getProjectElementService().getSteps(
373 aPublication.getOwner());
374 for (int i = 0; i < step.length; i++) {
375 aPublication.setStep(step[i]); // The involved step necessarily exists
376 if (aPublication.value().isInto(aPublication.getStep()))
380 return aPublication.getStep();
384 * Get the projectElementService.
386 * @return the projectElementService
388 public ProjectElementService getProjectElementService() {
389 return _projectElementService;
393 * Set the projectElementService.
395 * @param projectElementService
396 * the projectElementService to set
398 public void setProjectElementService(
399 ProjectElementService projectElementService) {
400 _projectElementService = projectElementService;
404 * Get the simulationContextService.
406 * @return the simulationContextService
408 public SimulationContextService getSimulationContextService() {
409 return _simulationContextService;
413 * Set the simulationContextService.
415 * @param simulationContextService
416 * the simulationContextService to set
418 public void setSimulationContextService(
419 SimulationContextService simulationContextService) {
420 _simulationContextService = simulationContextService;
424 * Get the studyService.
425 * @return the studyService
427 public StudyService getStudyService() {
428 return _studyService;
432 * Set the studyService.
433 * @param studyService the studyService to set
435 public void setStudyService(StudyService studyService) {
436 _studyService = studyService;
440 * Get the stepService.
441 * @return the stepService
443 public StepService getStepService() {
448 * Set the stepService.
449 * @param stepService the stepService to set
451 public void setStepService(StepService stepService) {
452 _stepService = stepService;
456 * Get the documentTypeService.
457 * @return the documentTypeService
459 public DocumentTypeService getDocumentTypeService() {
460 return _documentTypeService;
464 * Set the documentTypeService.
465 * @param documentTypeService the documentTypeService to set
467 public void setDocumentTypeService(DocumentTypeService documentTypeService) {
468 _documentTypeService = documentTypeService;