package test.splat.service;
import java.io.FileNotFoundException;
+import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
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);
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);
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.
*