From aba75075e4227ff27ac8fa6992da333145548852 Mon Sep 17 00:00:00 2001 From: rkv Date: Wed, 10 Apr 2013 07:45:38 +0000 Subject: [PATCH] Fix for a document version removing. --- .../splat/service/DocumentServiceImpl.java | 2 +- .../org/splat/service/StepServiceImpl.java | 26 +- .../splat/service/TestScenarioService.java | 15 +- .../test/splat/service/TestStepService.java | 252 +++++++++++++++++- .../org/splat/simer/EditDocumentAction.java | 10 +- 5 files changed, 284 insertions(+), 21 deletions(-) diff --git a/Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java index 686e734..98d952f 100644 --- a/Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java @@ -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); } } diff --git a/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java index 4884096..6e8e04a 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java @@ -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 using = new ArrayList(); List files = new ArrayList(); + 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; diff --git a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java index ddb34ac..2cf5931 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java @@ -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; } diff --git a/Workspace/Siman-Common/src/test/splat/service/TestStepService.java b/Workspace/Siman-Common/src/test/splat/service/TestStepService.java index c81589f..a50c23b 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestStepService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestStepService.java @@ -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 stSteps = _projectElementService.getStepsMap(aStudy); - Map scSteps = _projectElementService.getStepsMap(aScen); + Map stSteps = _projectElementService + .getStepsMap(aStudy); + Map 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.
+ * Description :
+ * Create a study and a scenario with versioned documents and try to remove published versions.
+ *

+ * Action :
+ * 1. call the method for a version published in the study step.
+ * 2. call the method for a version published in the scenario step.
+ * Test data :
+ * no input parameters
+ * no input parameters
+ * + * Outcome results:
+ * + *
    + *
  • The published version must be removed, the previous version must be republished in the study.
    + *
  • + *
  • The published version must be removed, the previous version must be republished in the scenario.
    + *
  • + *
+ *
+ * + * @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 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 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. * diff --git a/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java b/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java index ee5b949..d672723 100644 --- a/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java +++ b/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java @@ -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; } -- 2.39.2