1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.service;
12 import java.io.FileNotFoundException;
13 import java.io.FileWriter;
14 import java.io.IOException;
15 import java.sql.SQLException;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.List;
21 import org.splat.dal.bo.kernel.User;
22 import org.splat.dal.bo.som.ContributorRelation;
23 import org.splat.dal.bo.som.DescriptionAttribute;
24 import org.splat.dal.bo.som.Document;
25 import org.splat.dal.bo.som.DocumentType;
26 import org.splat.dal.bo.som.KnowledgeElement;
27 import org.splat.dal.bo.som.KnowledgeElementType;
28 import org.splat.dal.bo.som.ProgressState;
29 import org.splat.dal.bo.som.ProjectElement;
30 import org.splat.dal.bo.som.Publication;
31 import org.splat.dal.bo.som.Scenario;
32 import org.splat.dal.bo.som.SimulationContext;
33 import org.splat.dal.bo.som.Study;
34 import org.splat.dal.bo.som.ValidationCycle;
35 import org.splat.dal.bo.som.ValidationCycleRelation;
36 import org.splat.dal.bo.som.ValidationStep;
37 import org.splat.dal.bo.som.Document.Properties;
38 import org.splat.dal.dao.kernel.UserDAO;
39 import org.splat.dal.dao.som.Database;
40 import org.splat.dal.dao.som.KnowledgeElementDAO;
41 import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
42 import org.splat.dal.dao.som.ScenarioDAO;
43 import org.splat.dal.dao.som.StudyDAO;
44 import org.splat.dal.dao.som.ValidationCycleDAO;
45 import org.splat.exception.BusinessException;
46 import org.splat.exception.InvalidParameterException;
47 import org.splat.kernel.InvalidPropertyException;
48 import org.splat.kernel.MissedPropertyException;
49 import org.splat.kernel.MultiplyDefinedException;
50 import org.splat.log.AppLogger;
51 import org.splat.service.DocumentTypeService;
52 import org.splat.service.ProjectElementService;
53 import org.splat.service.PublicationService;
54 import org.splat.service.SimulationContextService;
55 import org.splat.service.StepService;
56 import org.splat.service.StudyService;
57 import org.splat.service.dto.FileDTO;
58 import org.splat.service.technical.ProjectSettingsService;
59 import org.splat.service.technical.RepositoryService;
60 import org.splat.service.technical.ProjectSettingsService.Step;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.beans.factory.annotation.Qualifier;
63 import org.springframework.orm.hibernate3.HibernateTemplate;
64 import org.testng.Assert;
65 import org.testng.annotations.Test;
67 import test.splat.common.BaseTest;
68 import test.splat.util.TestEntitiesGenerator;
71 * Test class for StudyService.
73 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
76 public class TestStudyService extends BaseTest {
79 * Logger for the class.
81 private static final AppLogger LOG = AppLogger
82 .getLogger(TestStudyService.class);
85 * The StudyDAO. Later injected by Spring.
88 @Qualifier("studyDAO")
89 private transient StudyDAO _studyDAO;
92 * The PublicationService. Later injected by Spring.
95 @Qualifier("publicationService")
96 private transient PublicationService _publicationService;
99 * The ProjectElementService. Later injected by Spring.
102 @Qualifier("projectElementService")
103 private transient ProjectElementService _projectElementService;
106 * The StepService. Later injected by Spring.
109 @Qualifier("stepService")
110 private transient StepService _stepService;
113 * The ProjectSettingsService. Later injected by Spring.
116 @Qualifier("projectSettings")
117 private transient ProjectSettingsService _projectSettings;
120 * The DocumentTypeService. Later injected by Spring.
123 @Qualifier("documentTypeService")
124 private transient DocumentTypeService _documentTypeService;
127 * The StudyService. Later injected by Spring.
130 @Qualifier("studyService")
131 private transient StudyService _studyService;
134 * The UserDAO. Later injected by Spring.
137 @Qualifier("userDAO")
138 private transient UserDAO _userDAO;
141 * The KnowledgeElementDAO. Later injected by Spring.
144 @Qualifier("knowledgeElementDAO")
145 private transient KnowledgeElementDAO _knowledgeElementDAO;
148 * The KnowledgeElementTypeDAO. Later injected by Spring.
151 @Qualifier("knowledgeElementTypeDAO")
152 private transient KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
155 * The ScenarioDAO. Later injected by Spring.
158 @Qualifier("scenarioDAO")
159 private transient ScenarioDAO _scenarioDAO;
162 * The SimulationContextService. Later injected by Spring.
165 @Qualifier("simulationContextService")
166 private transient SimulationContextService _simulationContextService;
169 * The ValidationCycleDAO. Later injected by Spring.
172 @Qualifier("validationCycleDAO")
173 private transient ValidationCycleDAO _validationCycleDAO;
176 * The RepositoryService. Later injected by Spring.
179 @Qualifier("repositoryService")
180 private transient RepositoryService _repositoryService;
183 * Create a persistent scenario for tests.
185 * @return a persistent scenario
186 * @throws InvalidPropertyException
187 * if an invalid property is used when creating objects
188 * @throws MultiplyDefinedException
189 * when trying to create an object with already existing id
190 * @throws MissedPropertyException
191 * if a mandatory property is not defined for an object to be created
192 * @throws IOException
193 * if document creation is failed
194 * @throws SQLException
195 * if project settings loading is failed
197 private Study createStudy() throws InvalidPropertyException,
198 MissedPropertyException, MultiplyDefinedException, IOException,
200 // Create a scenario for tests
201 HibernateTemplate ht = getHibernateTemplate();
203 Database.getInstance().reset();
204 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
205 // Load workflow customization
207 _projectSettings.configure("classpath:test/som.xml");
208 } catch (FileNotFoundException e) {
209 Assert.fail("Can't find som.xml: ", e);
211 List<Step> steps = _projectSettings.getAllSteps();
212 Assert.assertTrue(steps.size() > 0, "No steps are created.");
214 // Create a test user
215 User.Properties uprop = new User.Properties();
216 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
217 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
218 .addRole("TST_user").setMailAddress(
219 "noreply@salome-platform.org");
220 uprop.disableCheck();
221 User anAuthor = new User(uprop);
222 ht.saveOrUpdate(anAuthor);
224 // Create a test study
225 Study.Properties stprops = new Study.Properties().setReference(
226 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
227 Study aStudy = new Study(stprops);
228 ht.saveOrUpdate(aStudy);
230 // Create a test scenario
231 Scenario.Properties sprops = new Scenario.Properties().setTitle(
232 "TST_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
233 Scenario aScenario = new Scenario(sprops);
234 aStudy.getScenariiList().add(aScenario);
235 ht.saveOrUpdate(anAuthor);
236 ht.saveOrUpdate(aStudy);
237 ht.saveOrUpdate(aScenario);
239 // Create documents for each scenario step
240 Document.Properties dprop = new Document.Properties().setAuthor(
241 anAuthor).setDate(new Date());
243 Publication usedPub = null;
244 Map<Long, Long> usedMap = new HashMap<Long, Long>();
245 for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
246 Step step = _projectSettings.getStep(stepNum);
247 LOG.debug("Create scenario step: " + stepNum);
248 ProjectElement projElem;
250 if (step.appliesTo(Study.class)) {
253 projElem = aScenario;
255 org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
256 List<DocumentType> dtypes = _documentTypeService
257 .selectTypesOf(step);
258 if (dtypes.size() > 0) {
259 DocumentType dtype = dtypes.get(0);
260 // Create a document published in the scenario
261 // document<i>: document type[0] - first type used on the step
262 // <source-file>.brep
263 // <attached-file>.med
265 dprop.setName("document" + stepNum).setType(dtype);
266 if (step.getNumber() > 3) {
267 dprop.setFormat("med");
269 dprop.setFormat("py");
271 Publication pub = createDoc(projElem, aScStep, dprop, "med",
273 if (usedPub != null) {
274 pub.addDependency(usedPub);
275 LOG.debug("Add dependency: " + pub.value().getTitle()
276 + " from " + usedPub.value().getTitle());
277 ht.saveOrUpdate(pub.value());
280 usedMap.put(pub.getIndex(), usedPub.getIndex());
284 if (dtypes.size() <= 0) {
285 LOG.debug("No document types are found for scenario step " + i);
289 // Check that the scenario and its documents have been created correctly.
291 Assert.assertNotNull(ht.find("from Document"),
292 "No documents in the database.");
293 Assert.assertTrue(ht.find("from Document").size() > 0,
294 "No documents in the database.");
296 Assert.assertNotNull(ht.find("from Publication where owner="
297 + aScenario.getIndex()), "No publications in the database.");
299 ht.find("from Publication where owner=" + aScenario.getIndex())
300 .size() > 0, "No publications in the database.");
302 for (Publication p : (List<Publication>) ht
303 .find("from Publication where owner=" + aScenario.getIndex())) {
304 LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
305 + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
307 Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
308 "The publication was not attached to the scenario.");
311 // Remove the scenario from the current hibernate session.
313 // Check that the scenario is created in the database.
314 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
315 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
316 Assert.assertTrue(aScen.getDocums().size() > 0,
317 "No publications in the scenario.");
319 Assert.assertTrue(i > 0,
320 "More then one document must be in the database");
322 // Check created uses relations
324 .assertTrue(usedMap.size() > 0,
325 "Uses relations must be created.");
326 boolean foundAny = false;
327 for (Long usingId : usedMap.keySet()) {
328 for (Publication pub : aScen.getDocums()) {
329 if (pub.getIndex() == usingId) {
330 boolean found = false;
331 for (Publication used : aScen.getDocums()) {
332 found = (used.getIndex() == usedMap.get(usingId));
338 for (Publication used : aStudy.getDocums()) {
339 found = (used.getIndex() == usedMap.get(usingId));
345 Assert.assertTrue(found,
346 "Uses relation was not created in the database.");
347 foundAny = foundAny || found;
351 Assert.assertTrue(foundAny,
352 "No Uses relation was created in the database.");
354 return aScenario.getOwnerStudy();
358 * Create a document published in the scenario. <BR>
360 * document type - type used on the step <BR>
361 * <source-file>.brep <BR>
362 * <attached-file>.med
365 * the scenario to add the document to
367 * scenario step where the document to be published
369 * document properties
370 * @param attachedFileExt
371 * extension of the secon attached (exported) file
373 * outdated document flag
374 * @return the publication of the created document
375 * @throws IOException
376 * @throws MultiplyDefinedException
377 * @throws InvalidPropertyException
378 * @throws MissedPropertyException
380 private Publication createDoc(final ProjectElement aScenario,
381 final org.splat.som.Step aScStep, final Properties dprop,
382 final String attachedFileExt, final boolean isOutdated)
383 throws MissedPropertyException, InvalidPropertyException,
384 MultiplyDefinedException, IOException {
385 // Create a document published in the scenario
386 // document<i>: document type - type used on the step
387 // <source-file>.brep
388 // <attached-file>.med
389 Publication pub = _stepService.createDocument(aScStep, dprop);
390 Assert.assertNotNull(pub.getOwner(),
391 "The publication must be attached to the scenario.");
392 Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
393 "The publication was not attached to the scenario.");
399 HibernateTemplate ht = getHibernateTemplate();
400 ht.saveOrUpdate(pub);
403 ht.save(pub.value());
405 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
411 * Test of generating a study document index.<BR>
412 * <B>Description :</B> <BR>
413 * <i>Create a study and try to generate the next document index.</i><BR>
414 * <B>Action : </B><BR>
415 * <i>1. call DAO's read method for an existing id.</i><BR>
416 * <B>Test data : </B><BR>
417 * <i>no input parameters</i><BR>
419 * <B>Outcome results:</B><BR>
422 * <li>The new index must be equal to the incremented old one and saved into the database<BR>
427 * @throws InvalidPropertyException
428 * if an invalid property is used when creating objects
429 * @throws MultiplyDefinedException
430 * when trying to create an object with already existing id
431 * @throws MissedPropertyException
432 * if a mandatory property is not defined for an object to be created
433 * @throws SQLException
434 * if test study creation is failed
435 * @throws IOException
436 * if test study creation is failed
440 public void testGenerateLocalIndex() throws InvalidPropertyException,
441 MissedPropertyException, MultiplyDefinedException, IOException,
443 LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
444 startNestedTransaction();
446 HibernateTemplate ht = getHibernateTemplate();
447 Study aStudy = createStudy();
448 // Call DAO's create method for a good transient study.
449 Long id = aStudy.getIndex();
450 Assert.assertNotNull(id,
451 "Create method returns null instead of a new id.");
452 Assert.assertTrue(id > 0, "The new id is not a positive number.");
454 // Call DAO's get method for an existing id.
456 getHibernateTemplate().evict(aStudy);
457 getHibernateTemplate().clear();
458 Study aStudyFound = _studyDAO.get(id);
460 int oldInd = aStudyFound.getLastLocalIndex();
461 int ind = _studyService.generateLocalIndex(aStudyFound);
464 Assert.assertEquals(ind, oldInd + 1, "Index must be incremented.");
465 Assert.assertEquals(ind, aStudyFound.getLastLocalIndex(),
466 "Index must be incremented.");
467 Assert.assertEquals(ind, ht.get(Study.class, aStudyFound.getIndex())
468 .getLastLocalIndex(),
469 "Incremented index must be saved in the database.");
470 aStudy = (Study) ht.find(
471 "from Study where rid = " + aStudyFound.getIndex()).get(0);
472 Assert.assertEquals(ind, aStudy.getLastLocalIndex(),
473 "Incremented index must be saved in the database.");
475 rollbackNestedTransaction();
476 LOG.debug(">>>>> END testGenerateLocalIndex()");
480 * Test of retrieval of a study description.
482 * @throws BusinessException
483 * if there is something wrong likely unrelated to the tested method
486 public void testGetDescription() throws BusinessException {
487 LOG.debug(">>>>> BEGIN testGetDescription()");
488 startNestedTransaction();
490 User user = TestEntitiesGenerator.getTestUser("GoodUser");
491 _userDAO.create(user);
492 Study study = TestEntitiesGenerator.getTestStudy(user);
493 _studyDAO.create(study);
495 Long studyId = Long.valueOf(study.getIndex());
497 // Empty description:
498 Assert.assertNull(_studyService.getDescription(studyId),
499 "returned value for study without description must be null");
501 // Not empty description:
502 study.setAttribute(new DescriptionAttribute(study, "description"));
503 _studyDAO.update(study);
505 Assert.assertEquals("description", _studyService
506 .getDescription(studyId));
510 _studyService.getDescription(null);
511 Assert.fail("retrieval with null study id must fail");
512 } catch (InvalidParameterException e) {
513 LOG.debug("Expected exception is thrown: "
514 + e.getClass().getSimpleName() + ": " + e.getMessage());
518 Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
519 _studyDAO.create(tmpStudy);
520 Long nonExistingId = tmpStudy.getIndex();
521 _studyDAO.delete(tmpStudy);
523 _studyService.getDescription(nonExistingId);
524 Assert.fail("retrieval with non-existing study id must fail");
525 } catch (InvalidParameterException e) {
526 LOG.debug("Expected exception is thrown: "
527 + e.getClass().getSimpleName() + ": " + e.getMessage());
530 rollbackNestedTransaction();
531 LOG.debug(">>>>> END testGetDescription()");
535 * Test of setting of a study description.
537 * @throws BusinessException
538 * if there is something wrong likely unrelated to the tested method
541 public void testSetDescription() throws BusinessException {
542 LOG.debug(">>>>> BEGIN testSetDescription()");
543 startNestedTransaction();
545 User user = TestEntitiesGenerator.getTestUser("GoodUser");
546 _userDAO.create(user);
547 Study study = TestEntitiesGenerator.getTestStudy(user);
548 _studyDAO.create(study);
550 Long studyId = Long.valueOf(study.getIndex());
552 // Setting description for study without any
553 _studyService.setDescription(studyId, "description");
555 // Resetting description
556 _studyService.setDescription(studyId, "replaced description");
560 _studyService.setDescription(null, "description");
561 Assert.fail("setting with null study id must fail");
562 } catch (InvalidParameterException e) {
563 LOG.debug("Expected exception is thrown: "
564 + e.getClass().getSimpleName() + ": " + e.getMessage());
568 Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
569 _studyDAO.create(tmpStudy);
570 Long nonExistingId = tmpStudy.getIndex();
571 _studyDAO.delete(tmpStudy);
574 _studyService.setDescription(nonExistingId, "description");
575 Assert.fail("retrieval with non-existing study id must fail");
576 } catch (InvalidParameterException e) {
577 LOG.debug("Expected exception is thrown: "
578 + e.getClass().getSimpleName() + ": " + e.getMessage());
581 rollbackNestedTransaction();
582 LOG.debug(">>>>> END testSetDescription()");
586 * Test of removal of a study description.
588 * @throws BusinessException
589 * if there is something wrong likely unrelated to the tested method
592 public void testRemoveDescription() throws BusinessException {
593 LOG.debug(">>>>> BEGIN testRemoveDescription()");
594 startNestedTransaction();
596 User user = TestEntitiesGenerator.getTestUser("GoodUser");
597 _userDAO.create(user);
598 Study study = TestEntitiesGenerator.getTestStudy(user);
599 _studyDAO.create(study);
601 Long studyId = Long.valueOf(study.getIndex());
603 // Empty description:
604 Assert.assertFalse(_studyService.removeDescription(studyId),
605 "returned value for study without description must be null");
607 // Not empty description:
608 study.setAttribute(new DescriptionAttribute(study, "description"));
609 _studyDAO.update(study);
611 Assert.assertTrue(_studyService.removeDescription(studyId),
612 "existing description removal must return true");
614 Assert.assertNull(_studyService.getDescription(studyId),
615 "description hasn't been successfully removed");
619 _studyService.removeDescription(null);
620 Assert.fail("removal with null study id must fail");
621 } catch (InvalidParameterException e) {
622 LOG.debug("Expected exception is thrown: "
623 + e.getClass().getSimpleName() + ": " + e.getMessage());
627 Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
628 _studyDAO.create(tmpStudy);
629 Long nonExistingId = tmpStudy.getIndex();
630 _studyDAO.delete(tmpStudy);
632 _studyService.removeDescription(nonExistingId);
633 Assert.fail("removal with non-existing study id must fail");
634 } catch (InvalidParameterException e) {
635 LOG.debug("Expected exception is thrown: "
636 + e.getClass().getSimpleName() + ": " + e.getMessage());
639 rollbackNestedTransaction();
640 LOG.debug(">>>>> END testGetDescription()");
644 * Test study removal.<BR>
645 * <B>Description :</B> <BR>
646 * <i>Delete a study.</i><BR>
647 * <B>Action : </B><BR>
648 * <i>1. call the method for a not existing study id expecting an exception.</i><BR>
649 * <i>2. call the method for an existing study id.</i><BR>
650 * <B>Test data : </B><BR>
651 * <i>no input parameters</i><BR>
653 * <B>Outcome results:</B><BR>
656 * <li>1: Exception must be thrown.</li>
657 * <li>2: The study and all its objects must be removed.</li>
661 * @throws BusinessException
662 * if test data creation is failed
663 * @throws IOException
664 * if document creation is failed
667 public void testRemoveStudy() throws BusinessException, IOException {
668 LOG.debug(">>>>> BEGIN testRemoveStudy()");
669 startNestedTransaction();
671 HibernateTemplate ht = getHibernateTemplate();
673 Database.getInstance().reset();
674 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
675 // Load workflow customization
677 _projectSettings.configure("classpath:test/som.xml");
678 } catch (Exception e) {
679 Assert.fail("Can't load som.xml: ", e);
682 User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
683 _userDAO.create(goodUser);
684 User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
685 _userDAO.create(otherUser);
686 User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
687 _userDAO.create(thirdUser);
688 KnowledgeElementType ktype1 = new KnowledgeElementType("testKType1");
689 KnowledgeElementType ktype2 = new KnowledgeElementType("testKType2");
690 ktype2.setState(ProgressState.inWORK);
691 _knowledgeElementTypeDAO.create(ktype1);
692 _knowledgeElementTypeDAO.create(ktype2);
694 // Create private study
695 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
696 aStudy.setTitle("0.This is private study");
697 Long studyId = _studyDAO.create(aStudy);
699 // Add simulation context to the study
700 SimulationContext.Properties cprop = new SimulationContext.Properties();
701 cprop.setType(_simulationContextService.selectType("product"))
702 .setValue("Test Simulation Context: Product");
703 _studyService.addProjectContext(aStudy, cprop);
706 // Add a scenario to the study
707 Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
708 _scenarioDAO.create(scen);
711 // Add knowledge elements to the scenario
712 KnowledgeElement kelm01 = TestEntitiesGenerator
713 .getTestKnowledgeElement(scen, ktype1, "TestKelm01 title");
714 KnowledgeElement kelm02 = TestEntitiesGenerator
715 .getTestKnowledgeElement(scen, ktype2, "TestKelm02 title");
716 _knowledgeElementDAO.create(kelm01);
717 _knowledgeElementDAO.create(kelm02);
720 // Add contributor relation
721 aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
724 // Add a validation cycle with otherUser as a reviewer
725 ValidationCycle.Properties vprop = new ValidationCycle.Properties();
726 DocumentType dtype = _documentTypeService.selectType("minutes");
727 vprop.setDocumentType(dtype);
728 vprop.setActor(ValidationStep.REVIEW, otherUser);
729 ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
730 _validationCycleDAO.create(cycle);
731 ValidationCycleRelation link = cycle.getContext();
732 aStudy.addRelation(link);
735 // Add documents to the first study activity
736 // Add a converts relations
737 Publication pub1 = addDoc(aStudy, "document1", dtype);
738 Publication pub2 = addDoc(aStudy, "document2", dtype);
739 Publication pub3 = addDoc(aStudy, "document3", dtype);
742 LOG.debug("pub1 version doc: " + pub1.value().getTitle() + " ["
743 + pub1.value().getReference() + "]" + " ["
744 + pub1.value().getRid() + "]");
745 LOG.debug("pub2 version doc: " + pub2.value().getTitle() + " ["
746 + pub2.value().getReference() + "]" + " ["
747 + pub2.value().getRid() + "]");
748 LOG.debug("pub3 version doc: " + pub3.value().getTitle() + " ["
749 + pub3.value().getReference() + "]" + " ["
750 + pub3.value().getRid() + "]");
755 LOG.debug("Before versioning:");
756 for (Publication doc : _projectElementService.getFirstStep(aStudy)
757 .getAllDocuments()) {
758 LOG.debug("Study doc: " + doc.value().getTitle() + " ["
759 + doc.value().getReference() + "]" + " ["
760 + doc.value().getRid() + "]");
762 // Add a version relations
763 Publication pub31 = version(pub3);
765 // _publicationService.versionDocument(_projectElementService
766 // .getFirstStep(aStudy), goodUser, pub3
767 // .getSourceFile().getName(), pub3.value().getIndex(), "",
768 // "The new version", pub3.getProgressState(), new Date(), null,
771 // Publication pub31 = null;
772 // for (Publication pub : aStudy.getDocums()) {
773 // if (pub.value().getPreviousVersion() != null) {
778 LOG.debug("pub31 version doc: " + pub31.value().getTitle() + " ["
779 + pub31.value().getReference() + "]" + " ["
780 + pub31.value().getRid() + "]");
781 ht.saveOrUpdate(aStudy);
783 LOG.debug("After versioning:");
784 for (Publication doc : aStudy.getDocums()) {
785 LOG.debug("Study doc: " + doc.value().getTitle() + " ["
786 + doc.value().getReference() + "]" + " ["
787 + doc.value().getRid() + "]");
790 // Add documents to the first scenario activity
791 Publication spub1 = addDoc(scen, "sdocument1", dtype);
792 Publication spub2 = addDoc(scen, "sdocument2", dtype);
793 Publication spub3 = addDoc(scen, "sdocument3", dtype);
794 LOG.debug("spub1 version doc: " + spub1.value().getTitle() + " ["
795 + spub1.value().getReference() + "]" + " ["
796 + spub1.value().getRid() + "]");
797 LOG.debug("spub2 version doc: " + spub2.value().getTitle() + " ["
798 + spub2.value().getReference() + "]" + " ["
799 + spub2.value().getRid() + "]");
800 LOG.debug("spub3 version doc: " + spub3.value().getTitle() + " ["
801 + spub3.value().getReference() + "]" + " ["
802 + spub3.value().getRid() + "]");
805 // Create a scenario document version
806 Publication spub31 = version(spub3);
807 LOG.debug("spub31 version doc: " + spub31.value().getTitle() + " ["
808 + spub31.value().getReference() + "]" + " ["
809 + spub31.value().getRid() + "]");
811 // Add uses relations
812 pub2.addDependency(pub1);
813 ht.saveOrUpdate(pub2.value());
814 pub3.addDependency(pub2);
815 ht.saveOrUpdate(pub3.value());
817 spub2.addDependency(pub1);
818 spub2.addDependency(spub1);
819 spub2.addDependency(pub2);
820 spub2.addDependency(pub3);
821 ht.saveOrUpdate(spub2.value());
822 spub3.addDependency(spub2);
823 ht.saveOrUpdate(spub3.value());
824 spub31.addDependency(spub31);
825 ht.saveOrUpdate(spub31.value());
828 // /////////////////////////////////////////////////////////
830 // Check removal of contributors relation
831 List found1 = ht.find("from ContributorRelation where owner=" + studyId);
832 Assert.assertEquals(found1.size(), 1,
833 "ContributorRelation is not created");
836 _studyService.removeStudy(studyId);
839 // Check removal of the study
840 Study foundStudy = ht.get(Study.class, studyId);
841 Assert.assertNull(foundStudy);
842 // Check removal of relations to validation cycle
843 List found = ht.find("from ValidationCycleRelation where owner="
845 Assert.assertEquals(found.size(), 0,
846 "ValidationCycleRelation is not removed");
847 // Check removal of validation cycles
848 found = ht.find("from ValidationCycle where rid=" + cycle.getIndex());
849 Assert.assertEquals(found.size(), 0, "ValidationCycle is not removed");
850 // Check removal of contributors relation
851 found = ht.find("from ContributorRelation where owner=" + studyId);
852 Assert.assertEquals(found.size(), 0,
853 "ContributorRelation is not removed");
854 // Check that contributors are not removed
855 User foundUser = ht.get(User.class, otherUser.getIndex());
856 Assert.assertNotNull(foundUser);
857 // Check removal of the scenario
858 found = ht.find("from Scenario where owner=" + studyId);
859 Assert.assertEquals(found.size(), 0, "Scenario is not removed");
860 // Check removal of publications
862 .find("from Publication pub left join pub.owner as own where own.rid="
863 + studyId + " or own.rid=" + scen.getIndex());
864 Assert.assertEquals(found.size(), 0, "Publication is not removed");
865 found = ht.find("from Publication");
866 Assert.assertEquals(found.size(), 0, "Publication is not removed");
867 // Check removal of documents
868 found = ht.find("from Document");
869 Assert.assertEquals(found.size(), 0, "Document is not removed");
870 // Check removal of version relations
871 found = ht.find("from VersionsRelation");
872 Assert.assertEquals(found.size(), 0, "VersionsRelation is not removed");
873 // Check removal of converts relations
874 found = ht.find("from ConvertsRelation");
875 Assert.assertEquals(found.size(), 0, "ConvertsRelation is not removed");
876 // Check removal of files
877 found = ht.find("from File");
878 Assert.assertEquals(found.size(), 0, "File is not removed");
879 // Check removal of uses relations
880 found = ht.find("from UsesRelation");
881 Assert.assertEquals(found.size(), 0, "UsesRelation is not removed");
882 // Check removal of usedBy relations
883 found = ht.find("from UsedByRelation");
884 Assert.assertEquals(found.size(), 0, "UsedByRelation is not removed");
886 rollbackNestedTransaction();
887 LOG.debug(">>>>> END testRemoveStudy()");
891 * Create a new version of the document.
894 * the current document publication
895 * @return the new document version publication
896 * @throws IOException
897 * if versioning is failed
898 * @throws BusinessException
899 * if versioning is failed
901 private Publication version(final Publication pub)
902 throws BusinessException, IOException {
903 Document.Properties dprop = new Document.Properties();
904 dprop.setDocument(pub.value(), pub.getStep().getStep());
905 Publication newpub = _stepService.versionDocument(pub.getStep(), pub,
907 pub.getOwner().getDocums().remove(pub);
908 pub.getStep().getDocuments().remove(pub);
909 pub.getOwner().add(newpub);
910 pub.getStep().getDocuments().add(newpub);
915 * Create a document and publish it in the project element.
918 * the project element
924 * @return publication of the created document
925 * @throws BusinessException
926 * if document creation is failed
927 * @throws IOException
928 * if file creation is failed
930 private Publication addDoc(final ProjectElement aStudy,
931 final String docname, final DocumentType dtype)
932 throws BusinessException, IOException {
933 HibernateTemplate ht = getHibernateTemplate();
934 // Add documents to the first study activity
935 org.splat.som.Step aStep = _projectElementService.getFirstStep(aStudy);
936 Document.Properties dprop = new Document.Properties().setAuthor(
937 aStudy.getAuthor()).setDate(new Date()).setName(docname)
938 .setType(dtype).setFormat("py");
939 dprop.setLocalPath(dprop.getName() + "." + dprop.getFormat());
940 dprop.setStep(aStep.getStep());
941 Publication pub = _stepService.createDocument(aStep, dprop);
944 aStep.getDocuments().add(pub);
945 ht.saveOrUpdate(pub);
946 ht.save(pub.value());
947 // Add a converts relation
949 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
950 createDownloadedFile(aStudy.getAuthor().getIndex(), dprop
952 createDownloadedFile(aStudy.getAuthor().getIndex(), dprop
953 .getLocalPath().substring(0,
954 dprop.getLocalPath().lastIndexOf(".") - 1), "med");
959 * Create a file in the user's repository downloads directory.
967 * @return created file DTO
968 * @throws IOException
969 * if file creation failed
971 private FileDTO createDownloadedFile(final long userId, final String name,
972 final String format) throws IOException {
973 // Create a file in the download directory
974 return createDownloadedFile(userId, name + "." + format);
978 * Get path to the user's downloads directory. The directory is created if it is not exist yet.
982 * @return absolute path to downloads directory followed by slash
984 private String getDownloadPath(final long userId) {
985 // Prepare download directory
986 File tmpDir = _repositoryService.getDownloadDirectory(userId);
987 if (!tmpDir.exists()) {
988 Assert.assertTrue(tmpDir.mkdir(),
989 "Can't create temporary directory: "
990 + tmpDir.getAbsolutePath());
993 return tmpDir.getAbsolutePath() + "/";
997 * Create a file in the user's repository downloads directory.
1003 * @return created file DTO
1004 * @throws IOException
1005 * if file creation failed
1007 private FileDTO createDownloadedFile(final long userId, final String fname)
1008 throws IOException {
1009 // Create a file in the download directory
1010 String filePath = getDownloadPath(userId) + fname;
1011 FileWriter fw = new FileWriter(filePath);
1012 fw.write("Simulation of " + fname + " file for checkin at "
1015 return new FileDTO(filePath);