]> SALOME platform Git repositories - tools/siman.git/commitdiff
Salome HOME
Fix for a document version removing.
authorrkv <rkv@opencascade.com>
Wed, 10 Apr 2013 07:45:38 +0000 (07:45 +0000)
committerrkv <rkv@opencascade.com>
Wed, 10 Apr 2013 07:45:38 +0000 (07:45 +0000)
Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java
Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java
Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java
Workspace/Siman-Common/src/test/splat/service/TestStepService.java
Workspace/Siman/src/org/splat/simer/EditDocumentAction.java

index 686e7343c720f69f7952881faaa7661bbca70b34..98d952f487e8d05c354d7f544a488f7bab3fe20e 100644 (file)
@@ -464,7 +464,7 @@ public class DocumentServiceImpl implements DocumentService {
        public void hold(final Document aDoc) {
                aDoc.setCountag(aDoc.getCountag() + 1);
                if (aDoc.isSaved()) {
-                       getDocumentDAO().update(aDoc);
+                       getDocumentDAO().merge(aDoc);
                }
        }
 
index 48840966b32ddc35f44d6ac1756d33664115dd71..6e8e04a718ded14a8d9a5d356fa5c2bf2f0a857b 100644 (file)
@@ -496,11 +496,20 @@ public class StepServiceImpl implements StepService {
                        if (!torem.value().getRelations(UsedByRelation.class).isEmpty()) {
                                throw new DocumentIsUsedException(torem.value().getTitle());
                        }
+                       // Remove the document publication from the step
                        remove(aStep, torem);
+                       // Republish the previous version if any to avoid it becoming an orphan
+                       Document prevVersion = torem.value().getPreviousVersion();
+                       if (prevVersion != null) {
+                               add(aStep, new Publication(prevVersion, aStep.getOwner()));
+                               getProjectElementDAO().merge(aStep.getOwner());
+                       }
+                       // Delete the document if it is no more used
                        Document value = torem.value();
                        if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
                                List<Document> using = new ArrayList<Document>();
                                List<File> files = new ArrayList<File>();
+                               files.add(value.getFile()); // To delete the source file physically at the end
                                for (Relation link : value.getAllRelations()) {
                                        if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
                                                files.add((File) link.getTo());
@@ -532,10 +541,19 @@ public class StepServiceImpl implements StepService {
                                // The corresponding physical file is not removed from the vault
                                getDocumentDAO().delete(getDocumentDAO().merge(torem.value()));
                                // Delete document's files
-                               //TODO: delete files from file system
-//                             for (File file : files) {
-//                                     getFileDAO().delete(getFileDAO().merge(file)); // The corresponding physical file is not removed from the vault
-//                             }
+                               // Delete files from file system
+                               for (File file : files) {
+                                       // getFileDAO().delete(getFileDAO().merge(file)); // The corresponding physical file is not removed from the vault
+                                       LOG.info("Delete the file "
+                                                       + file.asFile().getAbsolutePath());
+                                       if (file.asFile().delete()) {
+                                               LOG.info("File " + file.asFile().getAbsolutePath()
+                                                               + " successfully deleted.");
+                                       } else {
+                                               LOG.info("File " + file.asFile().getAbsolutePath()
+                                                               + " can not be deleted.");
+                                       }
+                               }
                        }
                }
                return res;
index ddb34ac2c23a4361f052b6a02ce766b6c5ec1788..2cf593124dadde1c7112ef80730f9ce792114d97 100644 (file)
@@ -1357,8 +1357,9 @@ public class TestScenarioService extends BaseTest {
                                        + doc.value().getReference() + "]" + " ["
                                        + doc.value().getRid() + "]");
                }
-               // // Add a version relations
-               // Publication pub31 = version(pub3);
+               // Add a version relations
+               Publication pub31 = version(pub3);
+               ht.flush();
                //
                // LOG.debug("pub31 version doc: " + pub31.value().getTitle() + " ["
                // + pub31.value().getReference() + "]" + " ["
@@ -1391,10 +1392,11 @@ public class TestScenarioService extends BaseTest {
                ht.flush();
 
                // Create a scenario document version
-               // Publication spub31 = version(spub3);
+               Publication spub31 = version(spub3);
                // LOG.debug("spub31 version doc: " + spub31.value().getTitle() + " ["
                // + spub31.value().getReference() + "]" + " ["
                // + spub31.value().getRid() + "]");
+               ht.flush();
 
                // Add uses relations
                pub2.addDependency(pub1);
@@ -1409,8 +1411,8 @@ public class TestScenarioService extends BaseTest {
                ht.saveOrUpdate(spub2.value());
                spub3.addDependency(spub2);
                ht.saveOrUpdate(spub3.value());
-               // spub31.addDependency(spub31);
-               // ht.saveOrUpdate(spub31.value());
+               spub31.addDependency(pub31);
+               ht.saveOrUpdate(spub31.value());
                ht.flush();
 
                // Create target study1
@@ -1551,12 +1553,15 @@ public class TestScenarioService extends BaseTest {
                        throws BusinessException, IOException {
                Document.Properties dprop = new Document.Properties();
                dprop.setDocument(pub.value(), pub.getStep().getStep());
+               dprop.setLocalPath(pub.value().getTitle() + "_v1");
                Publication newpub = _stepService.versionDocument(pub.getStep(), pub,
                                dprop);
                pub.getOwner().getDocums().remove(pub);
                pub.getStep().getDocuments().remove(pub);
                pub.getOwner().add(newpub);
                pub.getStep().getDocuments().add(newpub);
+               String filepath = newpub.getSourceFile().asFile().getAbsolutePath();
+               createFile(filepath);
                return newpub;
        }
 
index c81589f2cb358426b98d4c2d0d667615fb6b7131..a50c23bad9a4669fedccf45e2df9e62b8439e0c1 100644 (file)
@@ -9,6 +9,7 @@
 package test.splat.service;
 
 import java.io.FileNotFoundException;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -316,8 +317,10 @@ public class TestStepService extends BaseTest {
                Study aStudy = _studyService.selectStudy(aScen.getOwnerStudy()
                                .getIndex());
                aScen = aStudy.getScenarii()[0];
-               Map<Integer, org.splat.som.Step> stSteps = _projectElementService.getStepsMap(aStudy);
-               Map<Integer, org.splat.som.Step> scSteps = _projectElementService.getStepsMap(aScen);
+               Map<Integer, org.splat.som.Step> stSteps = _projectElementService
+                               .getStepsMap(aStudy);
+               Map<Integer, org.splat.som.Step> scSteps = _projectElementService
+                               .getStepsMap(aScen);
                org.splat.som.Step aScStep;
                for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
                        LOG.debug("Remove documents from the step " + i);
@@ -366,16 +369,18 @@ public class TestStepService extends BaseTest {
                                Assert.assertEquals(ht.find("from Document where rid=" + docId)
                                                .size(), 1, "Nothing to delete.");
 
-                                ht.evict(aScStep.getDocuments().get(0).value()
-                                .getFile());
-                                LOG.debug("Load file#" + aScStep.getDocuments().get(0).value()
-                                .getRelations(ConvertsRelation.class).get(0).getTo().getIndex());
-                               
+                               ht.evict(aScStep.getDocuments().get(0).value().getFile());
+                               LOG.debug("Load file#"
+                                               + aScStep.getDocuments().get(0).value().getRelations(
+                                                               ConvertsRelation.class).get(0).getTo()
+                                                               .getIndex());
+
                                ht.flush();
                                ht.clear();
-                                File f = _fileDAO.get(aScStep.getDocuments().get(0).value()
-                                .getRelations(ConvertsRelation.class).get(0).getTo().getIndex());
-                                ht.evict(aScStep.getDocuments().get(0).value());
+                               File f = _fileDAO.get(aScStep.getDocuments().get(0).value()
+                                               .getRelations(ConvertsRelation.class).get(0).getTo()
+                                               .getIndex());
+                               ht.evict(aScStep.getDocuments().get(0).value());
 
                                ok = _stepService.removeDocument(aScStep, docId);
 
@@ -448,6 +453,233 @@ public class TestStepService extends BaseTest {
                LOG.debug(">>>>> END testRemoveDocument()");
        }
 
+       /**
+        * Test removeDocument method for a published document version.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Create a study and a scenario with versioned documents and try to remove published versions.<BR>
+        * </i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call the method for a version published in the study step.</i><BR>
+        * <i>2. call the method for a version published in the scenario step.</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>no input parameters</i><BR>
+        * <i>no input parameters</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>The published version must be removed, the previous version must be republished in the study.<BR>
+        * </li>
+        * <li>The published version must be removed, the previous version must be republished in the scenario.<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws BusinessException
+        *             if test data creation is failed
+        * @throws IOException
+        *             if application configuration loading or test data creation is failed
+        * @throws SQLException
+        *             if application configuration loading or test data creation is failed
+        */
+       @Test
+       public void testRemoveDocumentVersion() throws BusinessException,
+                       IOException, SQLException {
+               LOG.debug(">>>>> BEGIN testRemoveDocumentVersion()");
+               startNestedTransaction();
+
+               HibernateTemplate ht = getHibernateTemplate();
+
+               Database.getInstance().reset();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               _projectSettings.configure("classpath:test/som.xml");
+
+               User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
+               _userDAO.create(goodUser);
+
+               // Create private study
+               Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+               aStudy.setTitle("0.This is private study");
+               Long studyId = _studyDAO.create(aStudy);
+
+               // Add a scenario to the study
+               Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
+               Long scenId = _scenarioDAO.create(scen);
+               ht.flush();
+
+               DocumentType dtype = _documentTypeService.selectType("minutes");
+
+               // Add documents to the first study activity
+               // Add a converts relations
+               Map<Integer, org.splat.som.Step> stSteps = _projectElementService
+                               .getStepsMap(aStudy);
+               org.splat.som.Step aStep = stSteps.get(1);
+               Publication pub1 = addDoc(aStudy, aStep, "document1", dtype);
+               ht.flush();
+               ht.update(aStudy);
+               ht.flush();
+               // Add a version relations
+               Publication pub11 = version(pub1);
+               ht.flush();
+               
+               // Add documents to the first scenario activity
+               scen = aStudy.getScenariiList().get(0);
+               Map<Integer, org.splat.som.Step> scSteps = _projectElementService
+                               .getStepsMap(scen);
+               aStep = scSteps.get(2);
+               Publication spub1 = addDoc(scen, aStep, "sdocument1", dtype);
+               ht.flush();
+               // Add a version relations
+               Publication spub11 = version(spub1);
+               ht.flush();
+
+               Long id1 = pub1.value().getIndex(), id11 = pub11.value().getIndex(), id2 = spub1
+                               .value().getIndex(), id21 = spub11.value().getIndex();
+
+               ht.evict(scen);
+               ht.evict(scen.getOwnerStudy());
+               ht.clear();
+
+               Assert.assertTrue(ht.find("from VersionsRelation").size() >= 2,
+                               "Uses relations were not created in the database.");
+
+               // TEST CALL: Remove version from study step
+               aStudy = _studyService.selectStudy(studyId);
+               stSteps = _projectElementService.getStepsMap(aStudy);
+               aStep = stSteps.get(1);
+               Assert.assertTrue(_stepService.removeDocument(aStep, id11));
+               ht.flush();
+
+               // TEST CALL: Remove version from scenario
+               scen = aStudy.getScenariiList().get(0);
+               scSteps = _projectElementService.getStepsMap(scen);
+               aStep = scSteps.get(2);
+               Assert.assertTrue(_stepService.removeDocument(aStep, id21));
+               ht.flush();
+
+               Assert.assertEquals(ht.find("from Document where rid=" + id11).size(),
+                               0, "Document#" + id11 + " was not removed from the database.");
+               Assert.assertEquals(ht
+                               .find("from VersionsRelation where owner=" + id11).size(), 0,
+                               "VersionsRelation(s) were not removed from the database.");
+               Assert.assertEquals(ht.find(
+                               "from Publication where owner=" + studyId + " and mydoc="
+                                               + id11).size(), 0,
+                               "Version publication were not removed from the study.");
+               Assert.assertEquals(ht.find("from Document where rid=" + id21).size(),
+                               0, "Document#" + id21 + " was not removed from the database.");
+               Assert.assertEquals(ht
+                               .find("from VersionsRelation where owner=" + id21).size(), 0,
+                               "VersionsRelation(s) were not removed from the database.");
+               Assert.assertEquals(ht.find(
+                               "from Publication where owner=" + studyId + " and mydoc="
+                                               + id11).size(), 0,
+                               "Version publication were not removed from the study.");
+               Assert.assertEquals(ht
+                               .find(
+                                               "from Publication where owner=" + studyId
+                                                               + " and mydoc=" + id1).size(), 1,
+                               "First version were not republished in the study.");
+               Assert.assertEquals(ht
+                               .find(
+                                               "from Publication where owner=" + scenId
+                                                               + " and mydoc=" + id21).size(), 0,
+                               "Version publication were not removed from the scenario.");
+               Assert.assertEquals(ht.find(
+                               "from Publication where owner=" + scenId + " and mydoc=" + id2)
+                               .size(), 1,
+                               "First version were not republished in the scenario.");
+
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testRemoveDocumentVersion()");
+       }
+
+       /**
+        * Create a document and publish it in the project element.
+        * 
+        * @param aProjElem
+        *            the project element
+        * @param aStep
+        *            the project element step
+        * @param docname
+        *            document name
+        * @param dtype
+        *            document type
+        * @return publication of the created document
+        * @throws BusinessException
+        *             if document creation is failed
+        * @throws IOException
+        *             if file creation is failed
+        */
+       private Publication addDoc(final ProjectElement aProjElem,
+                       final org.splat.som.Step aStep, final String docname,
+                       final DocumentType dtype) throws BusinessException, IOException {
+               HibernateTemplate ht = getHibernateTemplate();
+               // Add documents to the study activity
+               Document.Properties dprop = new Document.Properties().setAuthor(
+                               aProjElem.getAuthor()).setDate(new Date()).setName(docname)
+                               .setType(dtype).setFormat("py");
+               dprop.setLocalPath(dprop.getName() + "." + dprop.getFormat());
+               dprop.setStep(aStep.getStep());
+               Publication pub = _stepService.createDocument(aStep, dprop);
+               pub.setStep(aStep);
+               aProjElem.add(pub);
+               aStep.getDocuments().add(pub);
+               ht.saveOrUpdate(pub);
+               ht.save(pub.value());
+               // Add a converts relation
+               // Attach a med file
+               ht.saveOrUpdate(_publicationService.attach(pub, "med"));
+               String filepath = pub.getSourceFile().asFile().getAbsolutePath();
+               createFile(filepath);
+               createFile(filepath.substring(0, filepath.lastIndexOf(".")) + ".med");
+               return pub;
+       }
+
+       /**
+        * Create a file.
+        * 
+        * @param fname
+        *            file name
+        * @throws IOException
+        *             if file creation failed
+        */
+       private void createFile(final String fname) throws IOException {
+               // Create a file in the download directory
+               LOG.debug("Create file: " + fname);
+               String filePath = fname;
+               FileWriter fw = new FileWriter(filePath);
+               fw.write("Simulation of " + fname + " data file at " + new Date());
+               fw.close();
+       }
+
+       /**
+        * Create a new version of the document.
+        * 
+        * @param pub
+        *            the current document publication
+        * @return the new document version publication
+        * @throws IOException
+        *             if versioning is failed
+        * @throws BusinessException
+        *             if versioning is failed
+        */
+       private Publication version(final Publication pub)
+                       throws BusinessException, IOException {
+               Document.Properties dprop = new Document.Properties();
+               dprop.setDocument(pub.value(), pub.getStep().getStep());
+               dprop.setLocalPath(pub.value().getTitle() + "_v1");
+               Publication newpub = _stepService.versionDocument(pub.getStep(), pub,
+                               dprop);
+               pub.getOwner().getDocums().remove(pub);
+               pub.getStep().getDocuments().remove(pub);
+               pub.getOwner().add(newpub);
+               pub.getStep().getDocuments().add(newpub);
+               String filepath = newpub.getSourceFile().asFile().getAbsolutePath();
+               createFile(filepath);
+               return newpub;
+       }
+
        /**
         * Create a persistent scenario for tests.
         * 
index ee5b9492e46ebe76626640ffa6f47b2c1b095f47..d6727231d824d2b6a61fe991ce22ceb9ee7875e1 100644 (file)
@@ -154,10 +154,18 @@ public class EditDocumentAction extends DisplayStudyStepAction {
                setMenu();
                _openStudy = getOpenStudy();
                Step step = _openStudy.getSelectedStep();
-               Publication doctag = step.getDocument(Integer.valueOf(_index));
+               Publication doctag = step.getDocument(Long.valueOf(_index));
+               Long prevVersion = 0L;
+               if (doctag.value().getPreviousVersion() != null) {
+                       prevVersion = doctag.value().getPreviousVersion().getIndex();
+               }
 
                if (getStepService().removeDocument(step, Long.valueOf(_index))) { // Updates the data structure
                        _openStudy.remove(doctag); // Updates the presentation
+                       // The previous version must be republished if any
+                       if (prevVersion > 0) {
+                               _openStudy.add(step.getDocument(prevVersion));
+                       }
                }
                return SUCCESS;
        }