Salome HOME
New checkIn functionality is implemented. Now some files can be checkedIn simultaneously.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / ScenarioServiceImpl.java
index f75f2d7146f71d92c57ff046a23c59b61a4a6bf2..857b4be9bcb21c6c69297ec092cb53081d99a5d5 100644 (file)
@@ -503,6 +503,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                if (LOG.isInfoEnabled()) {
                        LOG.info("Move " + upfile.getAbsolutePath() + TO + file.getPath());
                }
+               file.delete();  // necessary on some platforms if the file exists.
                return upfile.renameTo(file);
        }
 
@@ -783,6 +784,9 @@ public class ScenarioServiceImpl implements ScenarioService {
                        if (LOG.isDebugEnabled()) {
                                LOG.debug("Checkin the step:\n" + stepDTO);
                        }
+                       if (stepDTO.getDocs().size() == 0) {
+                               break;
+                       }
                        // Find a result document type of the step
                        int i = 0;
                        resType = null;
@@ -797,9 +801,75 @@ public class ScenarioServiceImpl implements ScenarioService {
                        // Find the appropriate scenario step
                        Step step = findStep(stepDTO, steps);
 
-                       // Process each document of the step
-                       for (DocumentDTO doc : stepDTO.getDocs()) {
-                               checkinDoc(step, doc, aUser, resType, aDate, newVersion,
+                       List<FileDTO> filesToBeAttached = new ArrayList<FileDTO>();
+                       FileDTO fileToAttachTo = null;  //all the rest documents will be attached to it
+                       
+                       //The id is the same for all DocumentDTOs of a step
+                       Long currentDocId = stepDTO.getDocs().get(0).getId();
+                       if(currentDocId != null && currentDocId > 0) {  //there is a result document in the step
+                               String currentFormat = step.getDocument(currentDocId).value().getFormat();
+
+                               // Process each document of the step
+                               for (DocumentDTO doc : stepDTO.getDocs()) {
+                                       for (FileDTO fileDTO : doc.getFiles()) {
+                                               
+                                               String path = fileDTO.getPath();
+                                               String format = path.substring(path.lastIndexOf('.') + 1);
+               
+                                               // If it has the same format as current doc (there can be just one such file)
+                                               // then this doc will be DocToAttachTo.
+                                               if (format != null && format.equals(currentFormat)) {
+                                                       fileToAttachTo = fileDTO;
+                                               } else {
+                                                       // Else, put it in the list of docs that will be attached to DocToAttachTo
+                                                       filesToBeAttached.add(fileDTO);
+                                               }
+                                       }
+                               }
+                       } else {
+                               // Process each document of the step
+                               for (DocumentDTO doc : stepDTO.getDocs()) {
+                                       for (FileDTO fileDTO : doc.getFiles()) {
+                                               String path = doc.getFiles().get(0).getPath();
+                                               String format = path.substring(path.lastIndexOf('.') + 1);
+                                               
+                                               ProjectSettingsService.Step stepType = step.getStep();
+                                               // If the default type for its format in the current step is the result type,
+                                               // then this doc will be DocToAttachTo.
+                                               DocumentType defaultDocType = _projectSettings.getDefaultDocumentType(stepType, format);
+                                               if (defaultDocType != null && defaultDocType.isResultOf(stepType)) {
+                                                       //It is possible that there is not just one such doc
+                                                       if(fileToAttachTo != null){
+                                                               filesToBeAttached.add(fileToAttachTo);
+                                                       }
+                                                       fileToAttachTo = fileDTO;
+                                               } else {
+                                                       // Else, put it in the list of docs that will be attached to DocToAttachTo
+                                                       filesToBeAttached.add(fileDTO);
+                                               }
+                                       }
+                               }
+                               
+                               //could not find any file with appropriate format
+                               if (fileToAttachTo == null && !filesToBeAttached.isEmpty()) {
+                                       //All the rest documents will be attached to the first in the list
+                                       fileToAttachTo = filesToBeAttached.get(0);
+                                       filesToBeAttached.remove(0);
+                               }
+                       }
+                       
+                       if (fileToAttachTo != null) {   // true if the DocumentDTO list is not empty
+       
+                               //Process docToAttachTo
+                               Publication pub = checkinDoc(step, fileToAttachTo, currentDocId,
+                                               aUser, resType, aDate,
+                                               newVersion, newVers, newDocs);
+                               currentDocId = pub.value().getIndex();
+                       }
+
+                       //Process rest docs
+                       for (FileDTO file : filesToBeAttached) {
+                               checkinDoc(step, file, currentDocId, aUser, resType, aDate, newVersion,
                                                newVers, newDocs);
                        }
                }
@@ -873,7 +943,8 @@ public class ScenarioServiceImpl implements ScenarioService {
                        for (Publication pub : aScenario.getDocums()) {
                                if ((pub.getStep().getNumber() <= newPub.getStep().getNumber())
                                                && (!pub.isOutdated())
-                                               && usedTypes.contains(pub.value().getType())) {
+                                               && usedTypes.contains(pub.value().getType())
+                                               && !newPub.equals(pub)) {
                                        // Create uses relation from the new document
                                        // to the found document in the previous step.
                                        newPub.addDependency(pub);
@@ -889,8 +960,10 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @param step
         *            the destination scenario step
-        * @param doc
-        *            the DTO of the document to checkin
+        * @param file
+        *                        the FilDTO to checkin
+        * @param docId
+        *            target document id
         * @param aUser
         *            the user who performs checkin
         * @param resType
@@ -903,6 +976,9 @@ public class ScenarioServiceImpl implements ScenarioService {
         *            the list of publications of new created versions of existing documents
         * @param newDocs
         *            the list of publications of new created documents not existing before the checkin
+        * @return 
+        *                        the newly created publication, if exists (the document has been created or versioned),
+        *                        null otherwise (the doc has been attached to  the old one)
         * @throws InvalidPropertyException
         *             if the scenario hasn't some of given steps or documents
         * @throws IOException
@@ -916,17 +992,17 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @throws NotApplicableException
         *             if failed saving of a new publication with a given state
         */
-       private void checkinDoc(final Step step, final DocumentDTO doc,
-                       final User aUser, final DocumentType resType, final Date aDate,
-                       final Map<Document, Document> newVersion,
+       private Publication checkinDoc(final Step step, final FileDTO file,
+                       final long docId, final User aUser, final DocumentType resType,
+                       final Date aDate, final Map<Document, Document> newVersion,
                        final List<Publication> newVers, final List<Publication> newDocs)
                        throws InvalidPropertyException, MismatchException,
                        MissedPropertyException, MultiplyDefinedException, IOException,
                        NotApplicableException {
-               if (doc.getFiles().size() > 0) {
+               Publication newPub = null;
+               if (file != null) {
                        Document.Properties dprop = new Document.Properties();
                        // NOTE: Process only the first attached file for each document
-                       FileDTO file = doc.getFiles().get(0);
                        dprop.setLocalPath(file.getPath());
 
                        // Get document title as the file name
@@ -943,8 +1019,8 @@ public class ScenarioServiceImpl implements ScenarioService {
                                        MessageKeyEnum.DCT_000005.toString(), authorName);
                        dprop.setDescription(summary);
 
-                       if (doc.getId() > 0) {
-                               checkinExistingDoc(step, doc, dprop, fileFormat, upfile,
+                       if (docId > 0) {
+                               newPub = checkinExistingDoc(step, docId, dprop, fileFormat, upfile,
                                                newVersion, newVers);
                        } else {
 
@@ -967,7 +1043,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                                docname += "_" + i; // The generated new document title
 
                                dprop.setName(docname);
-                               Publication newPub = getStepService().createDocument(step,
+                               newPub = getStepService().createDocument(step,
                                                dprop);
 
                                // Remember the new document
@@ -975,7 +1051,8 @@ public class ScenarioServiceImpl implements ScenarioService {
 
                                saveFile(newPub, step, upfile);
                        }
-               }
+               }       
+               return newPub;
        }
 
        /**
@@ -983,8 +1060,8 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @param step
         *            study step to check in
-        * @param doc
-        *            document DTO to check in
+        * @param docId
+        *            target document id
         * @param dprop
         *            document properties
         * @param fileFormat
@@ -995,6 +1072,9 @@ public class ScenarioServiceImpl implements ScenarioService {
         *            the map of created versions during this check in
         * @param newVers
         *            the list of new versions created during this check in
+        * @return 
+        *                        the newly created publication, if exists (the document has been versioned),
+        *                        null otherwise (the doc has been attached to  the old one)
         * @throws InvalidPropertyException
         *             if publication of the document is not found in the step
         * @throws MismatchException
@@ -1008,7 +1088,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @throws NotApplicableException
         *             if failed saving of a new publication with a given state
         */
-       private void checkinExistingDoc(final Step step, final DocumentDTO doc,
+       private Publication checkinExistingDoc(final Step step, final long docId,
                        final Properties dprop, final String fileFormat,
                        final java.io.File upfile,
                        final Map<Document, Document> newVersion,
@@ -1021,14 +1101,16 @@ public class ScenarioServiceImpl implements ScenarioService {
                // If file format (i.e. extension) is the same then create a new
                // version of the document.
                // Find the document publication
-               Publication pub = step.getDocument(doc.getId());
+               //Publication pub = step.getDocument(doc.getId());
+               Publication pub = step.getDocument(docId);
+               Publication newPub = null;
                if (pub == null) {
                        throw new InvalidPropertyException(MessageKeyEnum.SCN_000002
-                                       .toString(), doc.getId());
+                                       .toString(), docId);
                }
                if (pub.value() == null) {
                        throw new MismatchException(MessageKeyEnum.SCN_000002.toString(),
-                                       doc.getId());
+                                       docId);
                }
                if (LOG.isDebugEnabled()) {
                        LOG.debug("Old format: " + pub.value().getFormat()
@@ -1037,7 +1119,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                // If formats are same then create a new document version
                if (pub.value().getFormat() != null
                                && pub.value().getFormat().equals(fileFormat)) {
-                       Publication newPub = getStepService().versionDocument(step, pub,
+                       newPub = getStepService().versionDocument(step, pub,
                                        dprop);
                        if (LOG.isDebugEnabled()) {
                                LOG.debug("Created document type: "
@@ -1066,6 +1148,7 @@ public class ScenarioServiceImpl implements ScenarioService {
                                attach.setDate(new Date());
                        }
                }
+               return newPub;
        }
 
        /**