Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / ScenarioServiceImpl.java
index 9340c5c31d35a2e236e859ffeb660b36c0dc1186..794dd9029d8a799238c577c7524ad9a97a886a60 100644 (file)
@@ -66,6 +66,7 @@ import org.splat.service.dto.ScenarioDTO;
 import org.splat.service.dto.StepDTO;
 import org.splat.service.technical.IndexService;
 import org.splat.service.technical.ProjectSettingsService;
+import org.splat.service.technical.RepositoryService;
 import org.splat.service.technical.StepsConfigService;
 import org.splat.som.Step;
 import org.splat.util.BeanHelper;
@@ -178,6 +179,11 @@ public class ScenarioServiceImpl implements ScenarioService {
         */
        private StepsConfigService _stepsConfigService;
 
+       /**
+        * Injected repository service.
+        */
+       private RepositoryService _repositoryService;
+
        /**
         * Get the projectElementService.
         * 
@@ -278,6 +284,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                for (Scenario scen : fromStudy.getScenariiList()) {
                        if (scen.getIndex() == fromScenId) {
                                fromScen = scen;
+                               break;
                        }
                }
 
@@ -296,14 +303,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                }
 
                // Copy validation cycles
-               for (ValidationCycle fromCycle : fromStudy.getValidationCycles()
-                               .values()) {
-                       ValidationCycle cycle = new ValidationCycle(toStudy, fromCycle);
-                       getValidationCycleDAO().create(cycle);
-                       toStudy.addRelation(cycle.getContext());
-                       toStudy.getValidationCycles().put(
-                                       cycle.getDocumentType().getName(), cycle); // Replaces the cycle if exists as default,
-               }
+               copyValidationCycles(fromStudy, toStudy);
 
                // Copy content of the study up to the given step
                Map<Publication, Publication> oldToNewPub = new HashMap<Publication, Publication>();
@@ -318,6 +318,27 @@ public class ScenarioServiceImpl implements ScenarioService {
                }
        }
 
+       /**
+        * Copy validation cycles from study to study.
+        * 
+        * @param fromStudy
+        *            the source study
+        * @param toStudy
+        *            the destination study
+        */
+       private void copyValidationCycles(final Study fromStudy, final Study toStudy) {
+               for (ValidationCycle fromCycle : fromStudy.getValidationCycles()
+                               .values()) {
+                       if (fromCycle.isAssigned()) {
+                               ValidationCycle cycle = fromCycle.clone(toStudy);
+                               getValidationCycleDAO().create(cycle);
+                               toStudy.addRelation(cycle.getContext());
+                               toStudy.getValidationCycles().put(
+                                               cycle.getDocumentType().getName(), cycle); // Replaces the cycle if exists as default,
+                       }
+               }
+       }
+
        /**
         * Copy dependencies between documents from the given project element up to <BR>
         * the given step according to the given map of old publications to new publications.
@@ -408,22 +429,38 @@ public class ScenarioServiceImpl implements ScenarioService {
                        throws MissedPropertyException, InvalidPropertyException,
                        MultiplyDefinedException, IOException, NotApplicableException {
 
-               java.io.File upfile = fromDoc.getSourceFile().asFile();
+               java.io.File srcFile = fromDoc.getSourceFile().asFile();
                // Creation of the document
                Document.Properties dprop = new Document.Properties().setName(
                                fromDoc.getTitle()).setType(fromDoc.getType()).setFormat(
                                fromDoc.getFormat()).setAuthor(fromDoc.getAuthor());
+
+               java.io.File tmpDir = getRepositoryService().getDownloadDirectory(
+                               step.getOwnerStudy().getAuthor());
+
+               // Remove local file index prefix to get original filename.
+               java.io.File upfile = new java.io.File(tmpDir.getPath()
+                               + "/"
+                               + srcFile.getName().substring(
+                                               srcFile.getName().indexOf('_') + 1));
+               // Copy the source file into the temporary folder with original filename.
+               copyFile(srcFile, upfile);
+
                dprop.setLocalPath(upfile.getPath());
                Publication addoc = getStepService().createDocument(step, dprop);
-               copyFile(upfile, addoc.getSourceFile());
+
+               // Move the temporary file into the repository
+               moveFile(upfile, addoc.getSourceFile().asFile());
+
                getPublicationService().saveAs(addoc, fromDoc.getProgressState());
 
                // Copy attached files
-               for (Relation rel : addoc.value().getRelations(ConvertsRelation.class)) {
+               for (Relation rel : fromDoc.getRelations(ConvertsRelation.class)) {
                        File attach = ((ConvertsRelation) rel).getTo();
                        ConvertsRelation export = getPublicationService().attach(addoc,
                                        attach.getFormat());
-                       copyFile(attach.asFile(), export.getTo());
+                       // Copy the source document attachment file to the new study vault
+                       copyFile(attach.asFile(), export.getTo().asFile());
                }
                return addoc;
        }
@@ -433,18 +470,33 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @param upfile
         *            the source file.
-        * @param targetFile
+        * @param file
         *            the target file
         * @throws IOException
         *             if failed
         */
-       private void copyFile(final java.io.File upfile, final File targetFile)
+       private void copyFile(final java.io.File upfile, final java.io.File file)
                        throws IOException {
                if (LOG.isInfoEnabled()) {
-                       LOG.info("Copy " + upfile.getAbsolutePath() + TO
-                                       + targetFile.asFile().getPath());
+                       LOG.info("Copy " + upfile.getAbsolutePath() + TO + file.getPath());
                }
-               IOUtils.copy(upfile, targetFile.asFile());
+               IOUtils.copy(upfile, file);
+       }
+
+       /**
+        * Copy a file. Print info message.
+        * 
+        * @param upfile
+        *            the source file.
+        * @param file
+        *            the target file
+        * @return true if renamed otherwise return false
+        */
+       private boolean moveFile(final java.io.File upfile, final java.io.File file) {
+               if (LOG.isInfoEnabled()) {
+                       LOG.info("Move " + upfile.getAbsolutePath() + TO + file.getPath());
+               }
+               return upfile.renameTo(file);
        }
 
        /**
@@ -993,15 +1045,11 @@ public class ScenarioServiceImpl implements ScenarioService {
                                // If there is no attachment with this extension then attach the new one
                                ConvertsRelation export = getPublicationService().attach(pub,
                                                fileFormat);
-                               if (LOG.isDebugEnabled()) {
-                                       LOG.debug("Moving " + upfile.getName() + TO
-                                                       + export.getTo().asFile().getPath());
-                               }
-                               upfile.renameTo(export.getTo().asFile());
+                               moveFile(upfile, export.getTo().asFile());
                        } else {
                                // If an attachment with this extension already exists then
                                // replace it by the new one
-                               upfile.renameTo(attach.asFile());
+                               moveFile(upfile,attach.asFile());
                                // Update attached file modification date
                                attach.setDate(new Date());
                        }
@@ -1027,10 +1075,6 @@ public class ScenarioServiceImpl implements ScenarioService {
                        NotApplicableException {
                // Attach the file to the created document
                java.io.File updir = newPub.getSourceFile().asFile();
-               if (LOG.isDebugEnabled()) {
-                       LOG.debug("Moving \"" + upfile.getName() + "\" to \""
-                                       + updir.getPath() + "\".");
-               }
                if (updir.exists()) {
                        if (updir.delete()) {
                                LOG.info(MessageKeyEnum.SCN_000003.toString(), updir
@@ -1042,7 +1086,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                                                                + updir.getAbsolutePath());
                        }
                }
-               if (upfile.renameTo(updir)) {
+               if (moveFile(upfile, updir)) {
                        // Save the new publication in the scenario.
                        // The old publication is removed from the scenario here.
                        getPublicationService().saveAs(newPub, ProgressState.inWORK); // May throw FileNotFound if rename was not done
@@ -1640,4 +1684,23 @@ public class ScenarioServiceImpl implements ScenarioService {
                _stepsConfigService = stepsConfigService;
        }
 
+       /**
+        * Get the repositoryService.
+        * 
+        * @return the repositoryService
+        */
+       public RepositoryService getRepositoryService() {
+               return _repositoryService;
+       }
+
+       /**
+        * Set the repositoryService.
+        * 
+        * @param repositoryService
+        *            the repositoryService to set
+        */
+       public void setRepositoryService(final RepositoryService repositoryService) {
+               _repositoryService = repositoryService;
+       }
+
 }