1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Calendar;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
21 import org.apache.log4j.Logger;
22 import org.splat.common.properties.MessageKeyEnum;
23 import org.splat.dal.bo.kernel.Relation;
24 import org.splat.dal.bo.kernel.User;
25 import org.splat.dal.bo.som.ConvertsRelation;
26 import org.splat.dal.bo.som.Document;
27 import org.splat.dal.bo.som.DocumentType;
28 import org.splat.dal.bo.som.File;
29 import org.splat.dal.bo.som.KnowledgeElement;
30 import org.splat.dal.bo.som.KnowledgeElementType;
31 import org.splat.dal.bo.som.ProgressState;
32 import org.splat.dal.bo.som.Publication;
33 import org.splat.dal.bo.som.Scenario;
34 import org.splat.dal.bo.som.SimulationContext;
35 import org.splat.dal.bo.som.Study;
36 import org.splat.dal.bo.som.UsesRelation;
37 import org.splat.dal.dao.kernel.UserDAO;
38 import org.splat.dal.dao.som.KnowledgeElementDAO;
39 import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
40 import org.splat.dal.dao.som.ScenarioDAO;
41 import org.splat.dal.dao.som.StudyDAO;
42 import org.splat.kernel.InvalidPropertyException;
43 import org.splat.kernel.MismatchException;
44 import org.splat.kernel.MissedPropertyException;
45 import org.splat.kernel.MultiplyDefinedException;
46 import org.splat.kernel.NotApplicableException;
47 import org.splat.service.dto.DocumentDTO;
48 import org.splat.service.dto.FileDTO;
49 import org.splat.service.dto.StepDTO;
50 import org.splat.service.technical.IndexService;
51 import org.splat.service.technical.ProjectSettingsService;
52 import org.splat.som.Step;
53 import org.splat.util.BeanHelper;
54 import org.springframework.transaction.annotation.Transactional;
57 * Scenario service implementation.
59 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
61 public class ScenarioServiceImpl implements ScenarioService {
64 * Logger for this class.
66 protected final static Logger LOG = Logger
67 .getLogger(ScenarioServiceImpl.class);
70 * Injected index service.
72 private IndexService _indexService;
74 * Injected step service.
76 private StepService _stepService;
78 * Injected study service.
80 private StudyService _studyService;
82 * Injected publication service.
84 private PublicationService _publicationService;
86 * Injected project element service.
88 private ProjectElementService _projectElementService;
90 * Injected knowledge element DAO.
92 private KnowledgeElementDAO _knowledgeElementDAO;
94 * Injected scenario DAO.
96 private ScenarioDAO _scenarioDAO;
101 private StudyDAO _studyDAO;
104 * Injected knowledge element service.
106 private KnowledgeElementTypeService _knowledgeElementTypeService;
109 * Injected user service.
111 private UserService _userService;
116 private UserDAO _userDAO;
119 * Injected knowledge element type DAO.
121 private KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
124 * Injected simulation context service.
126 private SimulationContextService _simulationContextService;
129 * Injected project service.
131 private ProjectSettingsService _projectSettings;
134 * Injected document type service.
136 private DocumentTypeService _documentTypeService;
139 * Get the projectElementService.
141 * @return the projectElementService
143 public ProjectElementService getProjectElementService() {
144 return _projectElementService;
148 * Set the projectElementService.
150 * @param projectElementService
151 * the projectElementService to set
153 public void setProjectElementService(
154 final ProjectElementService projectElementService) {
155 _projectElementService = projectElementService;
159 * Get the publicationService.
161 * @return the publicationService
163 public PublicationService getPublicationService() {
164 return _publicationService;
168 * Set the publicationService.
170 * @param publicationService
171 * the publicationService to set
173 public void setPublicationService(
174 final PublicationService publicationService) {
175 _publicationService = publicationService;
179 * Get the stepService.
181 * @return the stepService
183 public StepService getStepService() {
188 * Set the stepService.
191 * the stepService to set
193 public void setStepService(final StepService stepService) {
194 _stepService = stepService;
200 * @see org.splat.service.ScenarioService#getScenarioInfo(long)
202 @Transactional(readOnly = true)
203 public List<StepDTO> getScenarioInfo(final long scenarioId) {
204 List<StepDTO> res = new ArrayList<StepDTO>();
205 // Get the scenario from the database by id
206 Scenario scen = getScenarioDAO().get(scenarioId);
207 if (LOG.isDebugEnabled()) {
208 LOG.debug("Scenario[" + scenarioId + "]: Number of publications: "
209 + scen.getDocums().size());
211 // Get activities of the scenario
212 Step[] steps = getProjectElementService().getSteps(scen);
215 String docType, fileFormat;
218 // For each activity create a step DTO and add it to the result list
219 for (Step step : steps) {
220 stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class);
222 if (LOG.isDebugEnabled()) {
223 LOG.debug("Step[" + stepDTO.getNumber()
224 + "]: Number of documents: "
225 + step.getDocuments().size());
227 // For each publication of the activity create a document DTO.
228 // Each file is considered as a source file.
229 for (Publication tag : step.getDocuments()) {
230 docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value()
232 char aState = tag.getIsnew();
233 docType = tag.value().getType().getName();
234 // For each file of the document create a file DTO
235 // Process source file of the document
236 fileFormat = tag.value().getFile().getFormat();
237 doImport = getProjectSettings().doImport(docType, fileFormat);
238 if (doImport && (!tag.isOutdated())) {
239 processing = "file-import";
241 processing = "file-download";
243 docDTO.addFile(tag.value().getFile().getRelativePath(), aState,
245 // Process all exported files
246 for (Relation rel : tag.value().getRelations(
247 ConvertsRelation.class)) {
248 File aFile = ((ConvertsRelation) rel).getTo();
249 fileFormat = aFile.getFormat();
250 doImport = getProjectSettings().doImport(docType,
252 if (doImport && (!tag.isOutdated())) {
253 processing = "file-import";
255 processing = "file-download";
257 docDTO.addFile(aFile.getRelativePath(), aState, processing,
266 * Create a new study with one scenario and "product" simulation context.
269 * the study properties
271 * the scenario properties
273 * the "product" simulation context properties
274 * @return the created study
275 * @throws MissedPropertyException
276 * if a mandatory property is missed
277 * @throws InvalidPropertyException
278 * if a property is invalid
279 * @throws MultiplyDefinedException
280 * if some property occurs several times
283 public Study createStudy(final Study.Properties sprop,
284 final Scenario.Properties oprop,
285 final SimulationContext.Properties cprop)
286 throws MissedPropertyException, InvalidPropertyException,
287 MultiplyDefinedException {
288 Study study = getStudyService().createStudy(sprop);
289 addScenario(study, oprop);
290 if (cprop.getIndex() == 0) { // Input of new project context
291 cprop.setType(getSimulationContextService().selectType("product"))
292 .setValue(cprop.getValue());
293 getStudyService().addProjectContext(study, cprop);
294 } else { // Selection of existing project context
295 SimulationContext context = getSimulationContextService()
296 .selectSimulationContext(cprop.getIndex());
297 getStudyService().addProjectContext(study, context);
305 * @see org.splat.service.ScenarioService#addKnowledgeElement(org.splat.dal.bo.som.Scenario,
306 * org.splat.dal.bo.som.KnowledgeElement.Properties)
309 public KnowledgeElement addKnowledgeElement(final Scenario aScenarioDTO,
310 final KnowledgeElement.Properties kprop)
311 throws MissedPropertyException, InvalidPropertyException,
312 MultiplyDefinedException {
313 KnowledgeElement kelm = null;
315 long aScenarioId = aScenarioDTO.getIndex();
316 if (LOG.isDebugEnabled()) {
317 LOG.debug("Add a knowledge element to the scenario #"
320 // Get the persistent scenario.
321 Scenario aScenario = getScenarioDAO().get(aScenarioId);
322 // Get persistent objects for creating a new knowledge.
323 // TODO: Actions must use DTO instead of persistent objects.
324 getUserDAO().merge(kprop.getAuthor());
325 getKnowledgeElementTypeDAO().merge(kprop.getType());
326 // Create a transient knowledge element related to the given scenario.
327 kelm = new KnowledgeElement(kprop.setOwnerScenario(aScenario));
328 // Save the new knowledge in the database.
329 getKnowledgeElementDAO().create(kelm);
330 // Update scenario transient data.
331 if (kelm.getType().equals("usecase")) {
332 aScenarioDTO.setUcase(kelm);
333 } else if (aScenarioDTO.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
334 aScenarioDTO.getKnowledgeElementsList().add(kelm);
337 // Load the workflow for the parent study to take into account
338 // all study actors durng reindexing.
339 getStudyService().loadWorkflow(aScenario.getOwnerStudy());
341 // Update the lucene index of knowledge elements.
342 getIndexService().add(kelm);
343 if (LOG.isDebugEnabled()) {
344 LOG.debug("A knowledge element #" + kelm.getIndex()
345 + " is added to the scenario #" + aScenario.getIndex());
347 } catch (IOException error) {
348 LOG.error("Unable to index the knowedge element '"
349 + kelm.getIndex() + "', reason:", error);
357 * Update the scenario in the database.
360 * the scenario to update
361 * @return true if updating succeeded
364 private boolean update(final Scenario aScenario) {
365 boolean isOk = false;
367 getScenarioDAO().update(aScenario); // Update of relational base
369 } catch (Exception error) {
370 LOG.error("Unable to re-index the knowledge element '"
371 + aScenario.getIndex() + "', reason:", error);
379 * @see org.splat.service.ScenarioService#checkin(long, long, java.util.List)
382 public void checkin(final long scenId, final long userId,
383 final List<StepDTO> scInfo) throws InvalidPropertyException,
384 MissedPropertyException, MultiplyDefinedException,
385 MismatchException, IOException, NotApplicableException {
386 // Get the scenario from the database by id
387 Scenario aScenario = getScenarioDAO().get(scenId);
388 // Get the user who perform this check-in operation
389 User aUser = getUserService().selectUser(userId);
390 // Get activities of the scenario
391 Step[] steps = getProjectElementService().getSteps(aScenario);
392 // Find result document types
393 List<DocumentType> resTypes = getDocumentTypeService()
394 .selectResultTypes();
396 // For each processed existing document keep its new version
397 Map<Document, Document> newVersion = new HashMap<Document, Document>();
398 // Keep newly created documents to create uses relations to results of a previous step.
399 List<Publication> newVers = new ArrayList<Publication>();
400 List<Publication> newDocs = new ArrayList<Publication>();
402 DocumentType resType;
404 Document.Properties dprop = new Document.Properties();
405 Date aDate = new Date();
406 for (StepDTO stepDTO : scInfo) {
407 if (LOG.isDebugEnabled()) {
408 LOG.debug("Checkin the step:\n" + stepDTO);
410 // Find a result document type of the step
414 if (resTypes.get(i).isResultOf(
415 getProjectSettings().getStep(stepDTO.getNumber()))) {
416 resType = resTypes.get(i);
419 } while ((resType == null) && (i < resTypes.size()));
421 // Find the appropriate scenario step
422 Step step = findStep(stepDTO, steps);
424 // Process documents of the step
425 for (DocumentDTO doc : stepDTO.getDocs()) {
426 if (doc.getFiles().size() > 0) {
427 // NOTE: Process only the first attached file for each document
428 FileDTO file = doc.getFiles().get(0);
430 // Get document title as the file name
431 java.io.File upfile = new java.io.File(file.getPath());
432 String fileFormat = upfile.getName().substring(
433 upfile.getName().lastIndexOf('.') + 1);
434 String docname = upfile.getName().substring(0,
435 upfile.getName().lastIndexOf('.'));
437 // Create a new document or a new version of the document
439 dprop.setAuthor(aUser).setDate(aDate);
440 Publication pub, newPub;
442 if (doc.getId() > 0) {
443 // If the document already exists then create a new version of it
444 // Find the document publication
445 pub = step.getDocument(doc.getId());
447 throw new InvalidPropertyException(
448 MessageKeyEnum.SCN_000002.toString(), doc
451 if (pub.value() == null) {
452 throw new MismatchException(
453 MessageKeyEnum.SCN_000002.toString(), doc
456 newPub = getStepService().versionDocument(step, pub,
458 // Remeber the link from the old document to the new document version
459 newVersion.put(pub.value(), newPub.value());
460 // Remember the new version publication
464 // Otherwise create a new document of the result type
465 // If result type is not found try to get type by file extension
466 if (resType == null) {
467 dprop.setType(getProjectSettings()
468 .getDefaultDocumentType(step.getStep(),
471 dprop.setType(resType);
473 dprop.setDescription("Checked in").setName(docname)
474 .setFormat(fileFormat);
475 newPub = getStepService().createDocument(step, dprop);
477 // Remeber the new document
481 // Attach the file to the created document
482 updir = newPub.getSourceFile().asFile();
483 if (LOG.isInfoEnabled()) {
484 LOG.info("Moving \"" + upfile.getName() + "\" to \""
485 + updir.getPath() + "\".");
487 if (upfile.renameTo(updir)) {
488 // Save the new publication in the scenario.
489 // The old publication is removed from the scenario here.
490 getPublicationService().saveAs(newPub,
491 ProgressState.inWORK); // May throw FileNotFound if rename was not done
497 // Set uses/used relations
499 // For each new version copy uses relations from the previous version.
500 for (Publication newVer : newVers) {
501 // For each Uses relation of the previous version
502 Document prevDoc = newVer.value().getPreviousVersion();//prevVersion.get(newVer);
503 if (LOG.isDebugEnabled()) {
504 LOG.debug("Previous version for publication #"
505 + newVer.getIndex() + " is found: " + prevDoc);
507 List<Relation> usesRelations = prevDoc
508 .getRelations(UsesRelation.class);
509 for (Relation rel : usesRelations) {
510 // If used document has been also versioned then refer to its new version.
511 Document usedDoc = ((UsesRelation) rel).getTo();
512 if (newVersion.containsKey(usedDoc)) {
513 usedDoc = newVersion.get(usedDoc);
515 // Build the appropriate relation for the new version.
516 newVer.addDependency(usedDoc);
518 // TODO: Outdate documents which depend from the previous version and were not checked in during this operation.
522 // For each new document create uses relation to the last versions of
523 // results of the previous step.
524 for (Publication newDoc : newDocs) {
525 // Find used document type according to the configuration.
526 // Find documents of used type in the previous study step.
528 // Create uses relation from the new document
529 // to the found document in the previous step.
530 // newDoc.addDependency(to);
533 // Mark the scenario as checked in
538 * Find appropriate step in the array of scenario steps according to the given step DTO.
544 * @return appropriate scenario step
545 * @throws InvalidPropertyException
546 * if appropriate step is not found
548 private Step findStep(final StepDTO stepDTO, final Step[] steps)
549 throws InvalidPropertyException {
553 if (steps[i].getNumber() == stepDTO.getNumber()) {
557 } while ((step == null) && (i < steps.length));
560 throw new InvalidPropertyException(MessageKeyEnum.SCN_000001
561 .toString(), stepDTO.getNumber());
569 * @see org.splat.service.ScenarioService#checkin(org.splat.dal.bo.som.Scenario)
571 public void checkin(final Scenario aScenario) {
572 aScenario.setUser(null);
573 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
574 getScenarioDAO().update(aScenario);
580 * @see org.splat.service.ScenarioService#checkout(org.splat.dal.bo.som.Scenario, org.splat.dal.bo.kernel.User)
582 public boolean checkout(final Scenario aScenario, final User user) {
583 boolean res = getStudyService().isStaffedBy(aScenario.getOwnerStudy(),
586 aScenario.setUser(user);
587 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
588 getScenarioDAO().update(aScenario);
596 * @see org.splat.service.ScenarioService#copyContentsUpTo(org.splat.dal.bo.som.Scenario, org.splat.som.Step)
598 public void copyContentsUpTo(final Scenario scenario, final Step lastep) {
599 Scenario base = (Scenario) lastep.getOwner();
600 Step[] from = getProjectElementService().getSteps(base);
601 Step[] to = getProjectElementService().getSteps(scenario);
602 for (int i = 0; i < from.length; i++) {
604 if (step.getNumber() > lastep.getNumber()) {
608 List<Publication> docs = step.getAllDocuments();
609 for (Iterator<Publication> j = docs.iterator(); j.hasNext();) {
610 Publication doc = getPublicationService().copy(j.next(),
611 scenario); // Creation of a new reference to the document
612 // Database.getSession().save(doc); Publications MUST be saved later through cascading when saving the scenario
613 getStepService().add(to[i], doc);
615 List<SimulationContext> ctex = step.getAllSimulationContexts();
616 for (Iterator<SimulationContext> j = ctex.iterator(); j.hasNext();) {
617 getStepService().addSimulationContext(to[i], j.next());
625 * @see org.splat.service.ScenarioService#isEmpty(org.splat.dal.bo.som.Scenario)
627 public boolean isEmpty(final Scenario scenario) {
628 Step[] mystep = getProjectElementService().getSteps(scenario);
629 boolean isEmp = true;
630 for (int i = 0; i < mystep.length; i++) {
631 if (mystep[i].isStarted()) {
643 public boolean isFinished(final Scenario scenario) {
644 Step[] mystep = getProjectElementService().getSteps(scenario);
645 boolean notempty = false; // If this is empty, this is not finished
646 for (int i = 0; i < mystep.length; i++) {
647 if (!mystep[i].isStarted()) {
650 if (!mystep[i].isFinished()) {
661 * @see org.splat.service.StudyService#addScenario(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Scenario.Properties)
664 public Scenario addScenario(final Study aStudy,
665 final Scenario.Properties sprop) throws MissedPropertyException,
666 InvalidPropertyException, MultiplyDefinedException {
667 if (sprop.getManager() == null) {
668 sprop.setManager(aStudy.getAuthor());
671 Scenario scenario = new Scenario(sprop.setOwnerStudy(aStudy));
672 if (sprop.getBaseStep() != null) {
673 copyContentsUpTo(scenario, sprop.getBaseStep());
675 Scenario previous = sprop.getInsertAfter();
677 if (previous == null) {
678 aStudy.getScenariiList().add(scenario);
680 aStudy.getScenariiList().add(
681 aStudy.getScenariiList().indexOf(previous) + 1, scenario);
683 getStudyDAO().update(aStudy); // No need to update the Lucene index
684 getScenarioDAO().create(scenario); // Must be done after updating this study because of the back reference to the study
685 if (sprop.getBaseStep() != null) {
686 // No need to update the Knowledge Element index as Knowledge Elements are not copied
687 getProjectElementService().refresh(scenario); // Because saving the scenario changes the hashcode of copied Publications
689 KnowledgeElementType ucase = getKnowledgeElementTypeService()
690 .selectType("usecase");
691 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
692 User admin = getUserService().selectUser(1); // First user created when creating the database
693 kprop.setType(ucase).setTitle(aStudy.getTitle()).setValue(
694 scenario.getTitle()).setAuthor(admin); // Internal Knowledge Element required by the validation process of
696 addKnowledgeElement(scenario, kprop);
701 * Remove a knowledge element from a scenario.
706 * the knowledge element to remove
707 * @return true if removal succeeded
709 public boolean removeKnowledgeElement(final Scenario scenario,
710 final KnowledgeElement kelm) {
711 KnowledgeElement torem = scenario.getKnowledgeElement(kelm.getIndex());
715 boolean done = scenario.getKnowledgeElements().remove(torem);
717 // Update of my transient data
718 // RKV: These transient data are not used indeed.
719 // RKV: List<KnowledgeElement> kelms = scenario.getKnowledgeByType().get(
720 // RKV: kelm.getType().getIndex());
721 // RKV: kelms.remove(torem);
722 if (scenario.getKnowledgeElementsList() != null) {
723 scenario.getKnowledgeElementsList().remove(torem);
725 getScenarioDAO().update(scenario);
726 // TODO: If the owner study is not private, remove the knowledge from the Lucene index
734 * Get the knowledgeElementDAO.
736 * @return the knowledgeElementDAO
738 public KnowledgeElementDAO getKnowledgeElementDAO() {
739 return _knowledgeElementDAO;
743 * Set the knowledgeElementDAO.
745 * @param knowledgeElementDAO
746 * the knowledgeElementDAO to set
748 public void setKnowledgeElementDAO(
749 final KnowledgeElementDAO knowledgeElementDAO) {
750 _knowledgeElementDAO = knowledgeElementDAO;
754 * Get the indexService.
756 * @return the indexService
758 public IndexService getIndexService() {
759 return _indexService;
763 * Set the indexService.
765 * @param indexService
766 * the indexService to set
768 public void setIndexService(final IndexService indexService) {
769 _indexService = indexService;
773 * Get the scenarioDAO.
775 * @return the scenarioDAO
777 public ScenarioDAO getScenarioDAO() {
782 * Set the scenarioDAO.
785 * the scenarioDAO to set
787 public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
788 _scenarioDAO = scenarioDAO;
794 * @return the studyDAO
796 public StudyDAO getStudyDAO() {
804 * the studyDAO to set
806 public void setStudyDAO(final StudyDAO studyDAO) {
807 _studyDAO = studyDAO;
811 * Get the knowledgeElementTypeService.
813 * @return the knowledgeElementTypeService
815 public KnowledgeElementTypeService getKnowledgeElementTypeService() {
816 return _knowledgeElementTypeService;
820 * Set the knowledgeElementTypeService.
822 * @param knowledgeElementTypeService
823 * the knowledgeElementTypeService to set
825 public void setKnowledgeElementTypeService(
826 final KnowledgeElementTypeService knowledgeElementTypeService) {
827 _knowledgeElementTypeService = knowledgeElementTypeService;
831 * Get the studyService.
833 * @return the studyService
835 public StudyService getStudyService() {
836 return _studyService;
840 * Set the studyService.
842 * @param studyService
843 * the studyService to set
845 public void setStudyService(final StudyService studyService) {
846 _studyService = studyService;
850 * Get the userService.
852 * @return the userService
854 public UserService getUserService() {
859 * Set the userService.
862 * the userService to set
864 public void setUserService(final UserService userService) {
865 _userService = userService;
871 * @return the userDAO
873 public UserDAO getUserDAO() {
883 public void setUserDAO(final UserDAO userDAO) {
888 * Get the knowledgeElementTypeDAO.
890 * @return the knowledgeElementTypeDAO
892 public KnowledgeElementTypeDAO getKnowledgeElementTypeDAO() {
893 return _knowledgeElementTypeDAO;
897 * Set the knowledgeElementTypeDAO.
899 * @param knowledgeElementTypeDAO
900 * the knowledgeElementTypeDAO to set
902 public void setKnowledgeElementTypeDAO(
903 final KnowledgeElementTypeDAO knowledgeElementTypeDAO) {
904 _knowledgeElementTypeDAO = knowledgeElementTypeDAO;
908 * Get the simulationContextService.
910 * @return the simulationContextService
912 public SimulationContextService getSimulationContextService() {
913 return _simulationContextService;
917 * Set the simulationContextService.
919 * @param simulationContextService
920 * the simulationContextService to set
922 public void setSimulationContextService(
923 final SimulationContextService simulationContextService) {
924 _simulationContextService = simulationContextService;
928 * Get project settings.
930 * @return Project settings service
932 private ProjectSettingsService getProjectSettings() {
933 return _projectSettings;
937 * Set project settings service.
939 * @param projectSettingsService
940 * project settings service
942 public void setProjectSettings(
943 final ProjectSettingsService projectSettingsService) {
944 _projectSettings = projectSettingsService;
948 * Get the documentTypeService.
950 * @return the documentTypeService
952 public DocumentTypeService getDocumentTypeService() {
953 return _documentTypeService;
957 * Set the documentTypeService.
959 * @param documentTypeService
960 * the documentTypeService to set
962 public void setDocumentTypeService(
963 final DocumentTypeService documentTypeService) {
964 _documentTypeService = documentTypeService;