+ /**
+ * Test testUpdateRelations method for updating relations at versioning
+ * document.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create a study and a scenario with documents and try to update
+ * relations for every document in Study or Scenario.<BR>
+ * </i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call the method for the every document in the Study or
+ * Scenario.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>1: Update relations for every document: <BR>
+ * Delete old UsedBy relation from old version and create new UsedBy
+ * relation to new version document. Create new Uses relation from new
+ * version document</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 final void testUpdateRelations() throws
+ BusinessException, IOException, SQLException {
+ LOG.debug(">>>>> BEGIN testUpdateRelations()");
+ 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();
+
+ org.splat.som.Step[] studySteps = _projectElementService
+ .getSteps(aStudy);
+ org.splat.som.Step[] scSteps = _projectElementService.getSteps(scen);
+
+ List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
+ steps.addAll(Arrays.asList(studySteps));
+ steps.addAll(Arrays.asList(scSteps));
+ Publication[] currentArray = new Publication[steps.size() * 2 - 2];
+ Publication[] nextArray = new Publication[steps.size() * 2 - 2];
+ Map<Integer, String[]> docusesMap = new HashMap<Integer, String[]>();
+ Map<Integer, long[]> docusedMap = new HashMap<Integer, long[]>();
+ int pubNumber = 0;
+ for (org.splat.som.Step step : steps) {
+ List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
+ .getStep());
+ if (dtypes.size() > 0) {
+ DocumentType dtype = dtypes.get(0);
+ if (step.getOwner() instanceof Study) {
+ currentArray[pubNumber] = addDoc(aStudy, step, "current"
+ + pubNumber, dtype);
+ currentArray[pubNumber + 1] = addDoc(aStudy, step,
+ "current" + (pubNumber + 1), dtype);
+
+ } else {
+ currentArray[pubNumber] = addDoc(scen, step, "current"
+ + pubNumber, dtype);
+ currentArray[pubNumber + 1] = addDoc(scen, step, "current"
+ + (pubNumber + 1), dtype);
+ }
+
+ pubNumber += 2;
+ }
+ }
+
+ ht.flush();
+ ht.update(aStudy);
+ ht.flush();
+
+ for (Integer numPub = currentArray.length - 1; numPub >= 0; numPub--) {
+ long[] docusedby = new long[1];
+ if (numPub != 0) {
+ currentArray[numPub].addDependency(currentArray[numPub - 1]);
+ }
+ if (numPub != currentArray.length - 1) {
+ docusedby[0] = currentArray[numPub + 1].getIndex();
+ docusedMap.put(new Integer(numPub), docusedby);
+ }
+ }
+
+ for (int numPub = 0; numPub < currentArray.length; numPub++) {
+ String[] docuses = new String[1];
+ nextArray[numPub] = addDoc(aStudy, currentArray[numPub].getStep(),
+ "next" + numPub, currentArray[numPub].value().getType());
+ ht.flush();
+ if (numPub != 0) {
+ docuses[0] = String.valueOf(nextArray[numPub - 1].value()
+ .getIndex());
+ docusesMap.put(numPub, docuses);
+ }
+ _publicationService.updateRelations(currentArray[numPub].getStep(),
+ currentArray[numPub], nextArray[numPub],
+ docusesMap.get(numPub), docusedMap.get(numPub));
+ }
+
+ //Action
+ for (int numPub = 0; numPub < currentArray.length; numPub++) {
+ if (numPub != currentArray.length - 1) {
+
+ Assert.assertEquals(
+ currentArray[numPub].getRelations(UsedByRelation.class),
+ new ArrayList<Publication>());
+ Assert.assertTrue(nextArray[numPub].getRelations(
+ UsedByRelation.class)
+ .contains(currentArray[numPub + 1]));
+ Assert.assertTrue(nextArray[numPub].getRelations(
+ UsedByRelation.class).contains(nextArray[numPub + 1]));
+ }
+
+ if (numPub != 0) {
+ Assert.assertEquals(
+ currentArray[numPub].getRelations(UsesRelation.class)
+ .get(0), nextArray[numPub - 1]);
+ Assert.assertEquals(
+ nextArray[numPub].getRelations(UsesRelation.class).get(
+ 0), nextArray[numPub - 1]);
+ }
+ }
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testUpdateRelations()");
+ }
+
+