]> SALOME platform Git repositories - tools/siman.git/commitdiff
Salome HOME
Code of checkin method is restructured to satisfy PMD.
authorrkv <rkv@opencascade.com>
Wed, 28 Nov 2012 07:40:36 +0000 (07:40 +0000)
committerrkv <rkv@opencascade.com>
Wed, 28 Nov 2012 07:40:36 +0000 (07:40 +0000)
Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java

index f32c3b40bb9b2deeaecbab54792fe73ab2b1a2cb..1ebc962ef1efc0fb0afc19e521300afc963bb361 100644 (file)
@@ -395,16 +395,16 @@ public class ScenarioServiceImpl implements ScenarioService {
                List<DocumentType> resTypes = getDocumentTypeService()
                                .selectResultTypes();
 
+               // Keep newly created documents to create uses relations to results of a previous step.
                // For each processed existing document keep its new version
                Map<Document, Document> newVersion = new HashMap<Document, Document>();
-               // Keep newly created documents to create uses relations to results of a previous step.
+               // Created publications of new created versions of existing documents
                List<Publication> newVers = new ArrayList<Publication>();
+               // The list of publications of new created documents not existing before the checkin
                List<Publication> newDocs = new ArrayList<Publication>();
                // For each step DTO
                DocumentType resType;
-               java.io.File updir;
-               Document.Properties dprop = new Document.Properties();
-               Date aDate = new Date();
+               Date aDate = new Date(); // The timestamp of the checkin operation
                for (StepDTO stepDTO : scInfo) {
                        if (LOG.isDebugEnabled()) {
                                LOG.debug("Checkin the step:\n" + stepDTO);
@@ -423,96 +423,40 @@ public class ScenarioServiceImpl implements ScenarioService {
                        // Find the appropriate scenario step
                        Step step = findStep(stepDTO, steps);
 
-                       // Process documents of the step
+                       // Process each document of the step
                        for (DocumentDTO doc : stepDTO.getDocs()) {
-                               if (doc.getFiles().size() > 0) {
-                                       // NOTE: Process only the first attached file for each document
-                                       FileDTO file = doc.getFiles().get(0);
-
-                                       // Get document title as the file name
-                                       java.io.File upfile = new java.io.File(file.getPath());
-                                       String fileFormat = upfile.getName().substring(
-                                                       upfile.getName().lastIndexOf('.') + 1);
-                                       String docname = upfile.getName().substring(0,
-                                                       upfile.getName().lastIndexOf('.'));
-
-                                       // Create a new document or a new version of the document
-                                       dprop.clear();
-                                       dprop.setAuthor(aUser).setDate(aDate);
-                                       Publication pub, newPub;
-
-                                       if (doc.getId() > 0) {
-                                               // If the document already exists then create a new version of it
-                                               // Find the document publication
-                                               pub = step.getDocument(doc.getId());
-                                               if (pub == null) {
-                                                       throw new InvalidPropertyException(
-                                                                       MessageKeyEnum.SCN_000002.toString(), doc
-                                                                                       .getId());
-                                               }
-                                               if (pub.value() == null) {
-                                                       throw new MismatchException(
-                                                                       MessageKeyEnum.SCN_000002.toString(), doc
-                                                                                       .getId());
-                                               }
-                                               newPub = getStepService().versionDocument(step, pub,
-                                                               dprop);
-                                               // Remeber the link from the old document to the new document version
-                                               newVersion.put(pub.value(), newPub.value());
-                                               // Remember the new version publication
-                                               newVers.add(newPub);
-                                       } else {
-
-                                               // Otherwise create a new document of the result type
-                                               // If result type is not found try to get type by file extension
-                                               if (resType == null) {
-                                                       dprop.setType(getProjectSettings()
-                                                                       .getDefaultDocumentType(step.getStep(),
-                                                                                       fileFormat));
-                                               } else {
-                                                       dprop.setType(resType);
-                                               }
-                                               dprop.setDescription("Checked in").setName(docname)
-                                                               .setFormat(fileFormat);
-                                               newPub = getStepService().createDocument(step, dprop);
-
-                                               // Remeber the new document
-                                               newDocs.add(newPub);
-                                       }
-
-                                       // Attach the file to the created document
-                                       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.getAbsoluteFile(), scenId);
-                                               } else {
-                                                       throw new IOException(
-                                                                       "Can't delete the existing destination file to move file from "
-                                                                                       + file.getPath() + " to "
-                                                                                       + updir.getAbsolutePath());
-                                               }
-                                       }
-                                       if (upfile.renameTo(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
-                                       } else {
-                                               throw new IOException("Can't move file from "
-                                                               + file.getPath() + " to "
-                                                               + updir.getAbsolutePath());
-                                       }
-                               }
+                               checkinDoc(step, doc, aUser, resType, aDate, newVersion,
+                                               newVers, newDocs);
                        }
                }
 
                // Set uses/used relations
+               updateRelationsAfterCheckin(aScenario, newVersion, newVers, newDocs);
 
+               // Mark the scenario as checked in
+               checkin(aScenario);
+       }
+
+       /**
+        * Updated uses/used relations after checkin operation:<BR>
+        * <ul>
+        * <li>For each new version copy uses relations from the previous version.</li>
+        * <li>Outdate documents which depend from the previous version and were not checked in during this operation.</li>
+        * <li>For each new document create uses relation to the last versions of results of the previous step.</li>
+        * </ul>
+        * 
+        * @param aScenario
+        *            the checked in scenario
+        * @param newVersion
+        *            the mapping of documents existed before the checkin to their new created versions
+        * @param newVers
+        *            the list of publications of new created versions of documents existed before the checkin
+        * @param newDocs
+        *            the list of publications of new created documents not existed before the checkin
+        */
+       private void updateRelationsAfterCheckin(final Scenario aScenario,
+                       final Map<Document, Document> newVersion,
+                       final List<Publication> newVers, final List<Publication> newDocs) {
                // For each new version copy uses relations from the previous version.
                for (Publication newVer : newVers) {
                        // For each Uses relation of the previous version
@@ -562,9 +506,126 @@ public class ScenarioServiceImpl implements ScenarioService {
                                }
                        }
                }
+       }
 
-               // Mark the scenario as checked in
-               checkin(aScenario);
+       /**
+        * Pure checkin of the document without creation of uses/usedBy relations. For an existing document a new version is created. New
+        * documents become published in the given step of the appropriate scenario. The appropriate uploaded file is attached to the created
+        * document and the document is published in the scenario. The publication of the old version is removed from the scenario.
+        * 
+        * @param step
+        *            the destination scenario step
+        * @param doc
+        *            the DTO of the document to checkin
+        * @param aUser
+        *            the user who performs checkin
+        * @param resType
+        *            the result document type of the given step
+        * @param aDate
+        *            timestamp of the checkin operation
+        * @param newVersion
+        *            the mapping of existing documents to their new created versions
+        * @param newVers
+        *            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
+        * @throws InvalidPropertyException
+        *             if the scenario hasn't some of given steps or documents
+        * @throws IOException
+        *             if a file can't be moved into the vault
+        * @throws MismatchException
+        *             if version creation in some of steps is failed
+        * @throws MissedPropertyException
+        *             if some mandatory property is missed when new document or new document version is created
+        * @throws MultiplyDefinedException
+        *             if some property is defined several times when new document or new document version is created
+        * @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,
+                       final List<Publication> newVers, final List<Publication> newDocs)
+                       throws InvalidPropertyException, MismatchException,
+                       MissedPropertyException, MultiplyDefinedException, IOException,
+                       NotApplicableException {
+               if (doc.getFiles().size() > 0) {
+                       Document.Properties dprop = new Document.Properties();
+                       // NOTE: Process only the first attached file for each document
+                       FileDTO file = doc.getFiles().get(0);
+
+                       // Get document title as the file name
+                       java.io.File upfile = new java.io.File(file.getPath());
+                       String fileFormat = upfile.getName().substring(
+                                       upfile.getName().lastIndexOf('.') + 1);
+                       String docname = upfile.getName().substring(0,
+                                       upfile.getName().lastIndexOf('.'));
+
+                       // Create a new document or a new version of the document
+                       dprop.setAuthor(aUser).setDate(aDate);
+                       Publication pub, newPub;
+
+                       if (doc.getId() > 0) {
+                               // If the document already exists then create a new version of it
+                               // Find the document publication
+                               pub = step.getDocument(doc.getId());
+                               if (pub == null) {
+                                       throw new InvalidPropertyException(
+                                                       MessageKeyEnum.SCN_000002.toString(), doc.getId());
+                               }
+                               if (pub.value() == null) {
+                                       throw new MismatchException(MessageKeyEnum.SCN_000002
+                                                       .toString(), doc.getId());
+                               }
+                               newPub = getStepService().versionDocument(step, pub, dprop);
+                               // Remeber the link from the old document to the new document version
+                               newVersion.put(pub.value(), newPub.value());
+                               // Remember the new version publication
+                               newVers.add(newPub);
+                       } else {
+
+                               // Otherwise create a new document of the result type
+                               // If result type is not found try to get type by file extension
+                               if (resType == null) {
+                                       dprop.setType(getProjectSettings().getDefaultDocumentType(
+                                                       step.getStep(), fileFormat));
+                               } else {
+                                       dprop.setType(resType);
+                               }
+                               dprop.setDescription("Checked in").setName(docname).setFormat(
+                                               fileFormat);
+                               newPub = getStepService().createDocument(step, dprop);
+
+                               // Remeber the new document
+                               newDocs.add(newPub);
+                       }
+
+                       // 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
+                                                       .getAbsoluteFile(), step.getOwner().getIndex());
+                               } else {
+                                       throw new IOException(
+                                                       "Can't delete the existing destination file to move file from "
+                                                                       + file.getPath() + " to "
+                                                                       + updir.getAbsolutePath());
+                               }
+                       }
+                       if (upfile.renameTo(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
+                       } else {
+                               throw new IOException("Can't move file from " + file.getPath()
+                                               + " to " + updir.getAbsolutePath());
+                       }
+               }
        }
 
        /**