Salome HOME
Processing instruction is defined now in getScenarioInfo using document types mappings.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / ScenarioServiceImpl.java
index 6adbabaa6b6307520b204ad802b9ec9f483b221d..705b534d17d9ec978a689894cac6c004bbd9137a 100644 (file)
 package org.splat.service;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
 
 import org.apache.log4j.Logger;
+import org.splat.dal.bo.kernel.Relation;
 import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ConvertsRelation;
+import org.splat.dal.bo.som.File;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.KnowledgeElementType;
 import org.splat.dal.bo.som.Publication;
@@ -30,8 +34,12 @@ import org.splat.dal.dao.som.StudyDAO;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
+import org.splat.service.dto.DocumentDTO;
+import org.splat.service.dto.StepDTO;
 import org.splat.service.technical.IndexService;
+import org.splat.service.technical.ProjectSettingsService;
 import org.splat.som.Step;
+import org.splat.util.BeanHelper;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -44,7 +52,7 @@ public class ScenarioServiceImpl implements ScenarioService {
        /**
         * Logger for this class.
         */
-       protected final static Logger logger = Logger
+       protected final static Logger LOG = Logger
                        .getLogger(ScenarioServiceImpl.class);
 
        /**
@@ -106,6 +114,11 @@ public class ScenarioServiceImpl implements ScenarioService {
         */
        private SimulationContextService _simulationContextService;
 
+       /**
+        * Injected project service.
+        */
+       private ProjectSettingsService _projectSettings;
+
        /**
         * Get the projectElementService.
         * 
@@ -141,7 +154,8 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param publicationService
         *            the publicationService to set
         */
-       public void setPublicationService(final PublicationService publicationService) {
+       public void setPublicationService(
+                       final PublicationService publicationService) {
                _publicationService = publicationService;
        }
 
@@ -164,6 +178,74 @@ public class ScenarioServiceImpl implements ScenarioService {
                _stepService = stepService;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.ScenarioService#getScenarioInfo(long)
+        */
+       @Transactional
+       public List<StepDTO> getScenarioInfo(final long scenarioId) {
+               List<StepDTO> res = new ArrayList<StepDTO>();
+               // Get the scenario from the database by id
+               Scenario scen = getScenarioDAO().get(scenarioId);
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("Scenario[" + scenarioId + "]: Number of publications: "
+                                       + scen.getDocums().size());
+               }
+               // Get activities of the scenario
+               Step[] steps = getProjectElementService().getSteps(scen);
+               StepDTO stepDTO;
+               DocumentDTO docDTO;
+               String docType, fileFormat;
+               String processing;
+               boolean doImport;
+               // For each activity create a step DTO and add it to the result list
+               for (Step step : steps) {
+                       stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class);
+                       res.add(stepDTO);
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Step[" + stepDTO.getNumber()
+                                               + "]: Number of documents: "
+                                               + step.getDocuments().size());
+                       }
+                       // For each publication of the activity create a document DTO.
+                       // Each file is considered as a source file.
+                       for (Publication tag : step.getDocuments()) {
+                               docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value()
+                                               .getTitle());
+                               char aState = tag.getIsnew();
+                               docType = tag.value().getType().getName();
+                               // For each file of the document create a file DTO
+                               // Process source file of the document
+                               fileFormat = tag.value().getFile().getFormat();
+                               doImport = getProjectSettings().doImport(docType, fileFormat);
+                               if (doImport && (!tag.isOutdated())) {
+                                       processing = "file-import";
+                               } else {
+                                       processing = "file-download";
+                               }
+                               docDTO.addFile(tag.value().getFile().getRelativePath(), aState,
+                                               processing, false);
+                               // Process all exported files
+                               for (Relation rel : tag.value().getRelations(
+                                               ConvertsRelation.class)) {
+                                       File aFile = ((ConvertsRelation) rel).getTo();
+                                       fileFormat = aFile.getFormat();
+                                       doImport = getProjectSettings().doImport(docType,
+                                                       fileFormat);
+                                       if (doImport && (!tag.isOutdated())) {
+                                               processing = "file-import";
+                                       } else {
+                                               processing = "file-download";
+                                       }
+                                       docDTO.addFile(aFile.getRelativePath(), aState, processing,
+                                                       false);
+                               }
+                       }
+               }
+               return res;
+       }
+
        /**
         * Create a new study with one scenario and "product" simulation context.
         * 
@@ -182,9 +264,11 @@ public class ScenarioServiceImpl implements ScenarioService {
         *             if some property occurs several times
         */
        @Transactional
-       public Study createStudy(final Study.Properties sprop, final Scenario.Properties oprop,
-                       final SimulationContext.Properties cprop) throws MissedPropertyException,
-                       InvalidPropertyException, MultiplyDefinedException {
+       public Study createStudy(final Study.Properties sprop,
+                       final Scenario.Properties oprop,
+                       final SimulationContext.Properties cprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       MultiplyDefinedException {
                Study study = getStudyService().createStudy(sprop);
                addScenario(study, oprop);
                if (cprop.getIndex() == 0) { // Input of new project context
@@ -207,13 +291,14 @@ public class ScenarioServiceImpl implements ScenarioService {
         */
        @Transactional
        public KnowledgeElement addKnowledgeElement(final Scenario aScenarioDTO,
-                       final KnowledgeElement.Properties kprop) throws MissedPropertyException,
-                       InvalidPropertyException, MultiplyDefinedException {
+                       final KnowledgeElement.Properties kprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       MultiplyDefinedException {
                KnowledgeElement kelm = null;
                try {
                        long aScenarioId = aScenarioDTO.getIndex();
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("Add a knowledge element to the scenario #"
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Add a knowledge element to the scenario #"
                                                + aScenarioId);
                        }
                        // Get the persistent scenario.
@@ -232,19 +317,19 @@ public class ScenarioServiceImpl implements ScenarioService {
                        } else if (aScenarioDTO.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
                                aScenarioDTO.getKnowledgeElementsList().add(kelm);
                        }
-                       
-                       // Load the workflow for the parent study to take into account 
+
+                       // Load the workflow for the parent study to take into account
                        // all study actors durng reindexing.
                        getStudyService().loadWorkflow(aScenario.getOwnerStudy());
-                       
+
                        // Update the lucene index of knowledge elements.
                        getIndexService().add(kelm);
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("A knowledge element #" + kelm.getIndex()
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("A knowledge element #" + kelm.getIndex()
                                                + " is added to the scenario #" + aScenario.getIndex());
                        }
                } catch (IOException error) {
-                       logger.error("Unable to index the knowedge element '"
+                       LOG.error("Unable to index the knowedge element '"
                                        + kelm.getIndex() + "', reason:", error);
                        kelm = null;
                }
@@ -266,7 +351,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                        getScenarioDAO().update(aScenario); // Update of relational base
                        isOk = true;
                } catch (Exception error) {
-                       logger.error("Unable to re-index the knowledge element '"
+                       LOG.error("Unable to re-index the knowledge element '"
                                        + aScenario.getIndex() + "', reason:", error);
                }
                return isOk;
@@ -370,9 +455,9 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @see org.splat.service.StudyService#addScenario(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Scenario.Properties)
         */
        @Transactional
-       public Scenario addScenario(final Study aStudy, final Scenario.Properties sprop)
-                       throws MissedPropertyException, InvalidPropertyException,
-                       MultiplyDefinedException {
+       public Scenario addScenario(final Study aStudy,
+                       final Scenario.Properties sprop) throws MissedPropertyException,
+                       InvalidPropertyException, MultiplyDefinedException {
                if (sprop.getManager() == null) {
                        sprop.setManager(aStudy.getAuthor());
                }
@@ -454,7 +539,8 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param knowledgeElementDAO
         *            the knowledgeElementDAO to set
         */
-       public void setKnowledgeElementDAO(final KnowledgeElementDAO knowledgeElementDAO) {
+       public void setKnowledgeElementDAO(
+                       final KnowledgeElementDAO knowledgeElementDAO) {
                _knowledgeElementDAO = knowledgeElementDAO;
        }
 
@@ -632,4 +718,24 @@ public class ScenarioServiceImpl implements ScenarioService {
                _simulationContextService = simulationContextService;
        }
 
+       /**
+        * Get project settings.
+        * 
+        * @return Project settings service
+        */
+       private ProjectSettingsService getProjectSettings() {
+               return _projectSettings;
+       }
+
+       /**
+        * Set project settings service.
+        * 
+        * @param projectSettingsService
+        *            project settings service
+        */
+       public void setProjectSettings(
+                       final ProjectSettingsService projectSettingsService) {
+               _projectSettings = projectSettingsService;
+       }
+
 }