From: rkv Date: Tue, 25 Dec 2012 13:03:16 +0000 (+0000) Subject: Fixed removing of a document from scenario. Deleted related documents and their relat... X-Git-Tag: Root_Delivery2_2013_04_22~193 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=a6cd260a1c8255b8a661433182a317c3b391a026;p=tools%2Fsiman.git Fixed removing of a document from scenario. Deleted related documents and their relations are no more restored during further deleting of used document. --- diff --git a/Workspace/Siman-Common/src/conf/log-messages.properties b/Workspace/Siman-Common/src/conf/log-messages.properties index 8669c7b..343ff9a 100644 --- a/Workspace/Siman-Common/src/conf/log-messages.properties +++ b/Workspace/Siman-Common/src/conf/log-messages.properties @@ -16,3 +16,4 @@ USR-000001=User {0} is not found in the database KNT-000001=Knowledge element type "{0}" already exists SCT-000001=Simulation context type "{0}" already exists DCT-000001=Document type "{0}" already exists +DCT-000002=Can not delete the document "{0}" because it is used by other documents. diff --git a/Workspace/Siman-Common/src/conf/log-messages_en.properties b/Workspace/Siman-Common/src/conf/log-messages_en.properties index 8669c7b..343ff9a 100644 --- a/Workspace/Siman-Common/src/conf/log-messages_en.properties +++ b/Workspace/Siman-Common/src/conf/log-messages_en.properties @@ -16,3 +16,4 @@ USR-000001=User {0} is not found in the database KNT-000001=Knowledge element type "{0}" already exists SCT-000001=Simulation context type "{0}" already exists DCT-000001=Document type "{0}" already exists +DCT-000002=Can not delete the document "{0}" because it is used by other documents. diff --git a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java index ce03ca8..62aa249 100644 --- a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java +++ b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java @@ -84,7 +84,11 @@ public enum MessageKeyEnum { /** * Document type "{0}" already exists. */ - DCT_000001("DCT-000001"); + DCT_000001("DCT-000001"), + /** + * Can not delete the document "{0}" because it is used by other documents. + */ + DCT_000002("DCT-000002"); /** * Value. diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java index bb8f0d4..060f84d 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java +++ b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java @@ -167,9 +167,11 @@ public abstract class Entity extends Any { break; } } - this.getAllRelations().remove(res); - if (res.isBidirectional()) { - ((Entity)res.getTo()).getAllRelations().remove(res.getReverse()); + if (res != null) { + this.getAllRelations().remove(res); + if (res.isBidirectional()) { + ((Entity)res.getTo()).getAllRelations().remove(res.getReverse()); + } } return res; } diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/som/Scenario.hbm.xml b/Workspace/Siman-Common/src/org/splat/dal/bo/som/Scenario.hbm.xml index fb54359..efad6f2 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/bo/som/Scenario.hbm.xml +++ b/Workspace/Siman-Common/src/org/splat/dal/bo/som/Scenario.hbm.xml @@ -11,7 +11,7 @@ - + diff --git a/Workspace/Siman-Common/src/org/splat/exception/DocumentIsUsedException.java b/Workspace/Siman-Common/src/org/splat/exception/DocumentIsUsedException.java new file mode 100644 index 0000000..95fc57b --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/exception/DocumentIsUsedException.java @@ -0,0 +1,53 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 25.12.2012 + * @author $Author$ + * @version $Revision$ + * @copyright OPEN CASCADE 2012 + *****************************************************************************/ + +package org.splat.exception; + +import org.splat.common.properties.MessageKeyEnum; + +/** + * This exception is thrown when trying to delete a document used by other documents. + * + * @author Roman Kozlov (RKV) + */ +public class DocumentIsUsedException extends BusinessException { + + /** + * Serial version ID. + */ + private static final long serialVersionUID = 8835396863040438781L; + + /** + * Error message key. + */ + private static final MessageKeyEnum MESSAGE_KEY = MessageKeyEnum.DCT_000002; + + /** + * The document title. + */ + private transient final String _documentTitle; + /** + * Constructor with document title as a parameter. + * + * @param documentTitle + * the document title + */ + public DocumentIsUsedException(final String documentTitle) { + super(MESSAGE_KEY.toString(), documentTitle); + _documentTitle = documentTitle; + } + /** + * Get the document title. + * @return the document title + */ + public String getDocumentTitle() { + return _documentTitle; + } +} diff --git a/Workspace/Siman-Common/src/org/splat/service/StepService.java b/Workspace/Siman-Common/src/org/splat/service/StepService.java index 95c1ccf..a4071b1 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StepService.java +++ b/Workspace/Siman-Common/src/org/splat/service/StepService.java @@ -16,6 +16,7 @@ import org.splat.dal.bo.som.Document; import org.splat.dal.bo.som.DocumentType; import org.splat.dal.bo.som.Publication; import org.splat.dal.bo.som.SimulationContext; +import org.splat.exception.DocumentIsUsedException; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MismatchException; import org.splat.kernel.MissedPropertyException; @@ -168,8 +169,11 @@ public interface StepService { * @param docId * the document id * @return true if removing of the document succeeded + * @throws DocumentIsUsedException + * if the document is used by other documents */ - boolean removeDocument(Step aStep, long docId); + boolean removeDocument(Step aStep, long docId) + throws DocumentIsUsedException; /** * Get document types which are applicable for the given study step (activity). diff --git a/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java index 5d29e80..0faf6d8 100644 --- a/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/StepServiceImpl.java @@ -34,6 +34,7 @@ import org.splat.dal.dao.som.ProjectElementDAO; import org.splat.dal.dao.som.PublicationDAO; import org.splat.dal.dao.som.SimulationContextDAO; import org.splat.dal.dao.som.VersionsRelationDAO; +import org.splat.exception.DocumentIsUsedException; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MismatchException; import org.splat.kernel.MissedPropertyException; @@ -479,13 +480,18 @@ public class StepServiceImpl implements StepService { * @param docId * the document id * @return true if removing of the document succeeded + * @throws DocumentIsUsedException + * if the document is used by other documents */ @Transactional - public boolean removeDocument(final Step aStep, final long docId) { + public boolean removeDocument(final Step aStep, final long docId) + throws DocumentIsUsedException { Publication torem = aStep.getDocument(docId); boolean res = (torem != null); if (res) { - torem = getPublicationDAO().merge(torem); + if (!torem.value().getRelations(UsedByRelation.class).isEmpty()) { + throw new DocumentIsUsedException(torem.value().getTitle()); + } remove(aStep, torem); Document value = torem.value(); if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used @@ -509,11 +515,12 @@ public class StepServiceImpl implements StepService { + doc.getTitle() + " to " + value.getTitle()); } doc.removeRelation(UsedByRelation.class, value); + getDocumentDAO().merge(doc); } + value = getPublicationDAO().merge(torem).value(); // Synchronize deleted objects with the database to avoid hibernate exception - // related with null value of not-nullable property because back reference from Document to Publication is not mapped: // org.hibernate.PropertyValueException: not-null property references a null or transient value: - // org.splat.dal.bo.som.Publication.mydoc + // org.splat.dal.bo.som.UsedByRelation.refer getDocumentDAO().flush(); // To show to the database that files from ConvertsRelation(s) are deleted already getDocumentDAO().delete(value); // The corresponding physical file is not removed from the vault // Delete document's files diff --git a/Workspace/Siman-Common/src/test/splat/service/TestStepService.java b/Workspace/Siman-Common/src/test/splat/service/TestStepService.java index dd9b728..dc3d615 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestStepService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestStepService.java @@ -11,6 +11,7 @@ package test.splat.service; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -28,22 +29,17 @@ import org.splat.dal.bo.som.UsesRelation; import org.splat.dal.bo.som.Document.Properties; import org.splat.dal.dao.som.Database; import org.splat.dal.dao.som.ScenarioDAO; +import org.splat.exception.BusinessException; +import org.splat.exception.DocumentIsUsedException; import org.splat.kernel.InvalidPropertyException; -import org.splat.kernel.MismatchException; import org.splat.kernel.MissedPropertyException; import org.splat.kernel.MultiplyDefinedException; -import org.splat.kernel.NotApplicableException; import org.splat.log.AppLogger; import org.splat.service.DocumentTypeService; -import org.splat.service.KnowledgeElementTypeService; -import org.splat.service.ProjectElementService; import org.splat.service.PublicationService; -import org.splat.service.ScenarioService; -import org.splat.service.SimulationContextService; import org.splat.service.StepService; import org.splat.service.StudyService; import org.splat.service.technical.ProjectSettingsService; -import org.splat.service.technical.RepositoryService; import org.splat.service.technical.ProjectSettingsService.Step; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -74,27 +70,6 @@ public class TestStepService extends BaseTest { @Qualifier("studyService") private transient StudyService _studyService; - /** - * The ScenarioService. Later injected by Spring. - */ - @Autowired - @Qualifier("scenarioService") - private transient ScenarioService _scenarioService; - - /** - * The ProjectElementService. Later injected by Spring. - */ - @Autowired - @Qualifier("projectElementService") - private transient ProjectElementService _projectElementService; - - /** - * The RepositoryService. Later injected by Spring. - */ - @Autowired - @Qualifier("repositoryService") - private transient RepositoryService _repositoryService; - /** * The Scenario DAO. Later injected by Spring. */ @@ -116,13 +91,6 @@ public class TestStepService extends BaseTest { @Qualifier("stepService") private transient StepService _stepService; - /** - * The SimulationContextService. Later injected by Spring. - */ - @Autowired - @Qualifier("simulationContextService") - private transient SimulationContextService _simulationContextService; - /** * The ProjectSettingsService. Later injected by Spring. */ @@ -138,33 +106,13 @@ public class TestStepService extends BaseTest { private transient DocumentTypeService _documentTypeService; /** - * The KnowledgeElementTypeService. Later injected by Spring. - */ - @Autowired - @Qualifier("knowledgeElementTypeService") - private transient KnowledgeElementTypeService _knowledgeElementTypeService; - - /** - * Test check-in scenario operation to be performed after SALOME session.
+ * Test removeDocument method.
* Description :
- * Create a scenario and try to check-in it with some simulated SALOME results data.
- * After check-in verify following points: - *
    - *
  • scenario is no more marked as checked out
  • - *
  • new document versions are created for checked in documents
  • - *
  • presentation of the previous version is removed
  • - *
  • uses relations are copied correctly
  • - *
  • files are moved correctly
  • - *
  • formats of files are new if they are according to the document's type on the study step
  • - *
  • new documents are created for new data
  • - *
  • new documents have correctly generated names
  • - *
  • uses relations are created correctly
  • - *
  • files are moved correctly
  • - *
+ * Create a scenario and try to remove documents from it.
*

* Action :
- * 1. call the method for an existing scenario id.
- * 2. call the method for a not existing scenario id.
+ * 1. call the method for all documents used by other document(s).
+ * 2. call the method for all documents starting from the last activity.
* Test data :
* no input parameters
* no input parameters
@@ -172,33 +120,23 @@ public class TestStepService extends BaseTest { * Outcome results:
* *
    - *
  • New version of existing documents must be created and new documents must be imported for documents with zero id. Correct - * relations must be created.
    + *
  • Exception DocumentIsUsedException is expected
    *
  • - *
  • Exception is thrown
    + *
  • All documents and their files and relations must be removed
    *
  • *
*
* - * @throws InvalidPropertyException - * if an invalid property is used when creating objects - * @throws MultiplyDefinedException - * when trying to create an object with already existing id - * @throws MissedPropertyException - * if a mandatory property is not defined for an object to be created + * @throws BusinessException + * if scenario creation or checkin is failed * @throws IOException * if scenario creation is failed * @throws SQLException * if scenario creation is failed - * @throws NotApplicableException - * if checkin failed - * @throws MismatchException - * if checkin failed */ @Test - public void testRemoveDocument() throws InvalidPropertyException, - MissedPropertyException, MultiplyDefinedException, IOException, - SQLException, MismatchException, NotApplicableException { + public void testRemoveDocument() throws BusinessException, IOException, + SQLException { LOG.debug(">>>>> BEGIN testRemoveDocument()"); startNestedTransaction(); @@ -212,20 +150,121 @@ public class TestStepService extends BaseTest { _scenarioDAO.flush(); Scenario aScen = _scenarioDAO.get(scenarioId); + ht.evict(aScen); + ht.evict(aScen.getOwnerStudy()); + Assert.assertTrue(ht.find("from UsesRelation").size() > 0, "Uses relations were not created in the database."); Assert.assertTrue(ht.find("from UsedByRelation").size() > 0, "UsedBy relations were not created in the database."); - // //////////////////////////////////////////////////////// - // Call removeDocument method for each document of the - // study/scenario starting from the last document. - ProjectElement projElem; - boolean ok; + boolean ok = false; long docId; Step step; + int nbDoc = ht.find("from Document").size(); + int nbRemovedDoc = 0; + List removedDocs = new ArrayList(); + + // //////////////////////////////////////////////////////// + // Call removeDocument method for each document + // used by other document(s). + // Exception DocumentIsUsedException is expected + for (int i = 1; i <= _projectSettings.getAllSteps().size(); i++) { + LOG.debug("Remove used documents from the step " + i); + step = _projectSettings.getStep(i); + if (step.appliesTo(Study.class)) { + projElem = _studyService.selectStudy(aScen.getOwnerStudy() + .getIndex()); + } else { + projElem = aScen; + } + + org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem); + + if (aScStep.getDocuments().size() > 0) { + docId = aScStep.getDocuments().get(0).value().getIndex(); + ht.flush(); + List uses = ht + .find("from UsesRelation where owner=" + docId); + List usedBy = ht + .find("from UsedByRelation where owner=" + docId); + if (!usedBy.isEmpty()) { + int usesRefer = ht.find( + "from UsesRelation where refer=" + docId).size(); + int usedByRefer = ht.find( + "from UsedByRelation where refer=" + docId).size(); + int convertsNb = ht.find( + "from ConvertsRelation where owner=" + docId) + .size(); + LOG.debug("Remove used document " + + aScStep.getDocument(docId).value().getTitle()); + LOG.debug("From db: It uses following " + uses.size() + + " documents: "); + for (UsesRelation rel : uses) { + LOG.debug(rel.getTo().getTitle()); + } + LOG + .debug("From step: It uses following " + + aScStep.getDocument(docId).value() + .getRelations(UsesRelation.class) + .size() + " documents: "); + for (Publication rel : aScStep.getDocument(docId) + .getRelations(UsesRelation.class)) { + LOG.debug(rel.value().getTitle()); + } + LOG.debug("From db: It is used by following " + + usedBy.size() + " documents: "); + for (UsedByRelation rel : usedBy) { + LOG.debug(rel.getTo().getTitle()); + } + LOG.debug("From step: It is used by following " + + aScStep.getDocument(docId).value().getRelations( + UsedByRelation.class).size() + + " documents: "); + for (Publication rel : aScStep.getDocument(docId) + .getRelations(UsedByRelation.class)) { + LOG.debug(rel.value().getTitle()); + } + try { + ok = _stepService.removeDocument(aScStep, docId); + Assert.fail("DocumentIsUsedException must be thrown."); + } catch (DocumentIsUsedException e) { + LOG.debug("Expected exception is thrown: " + + e.getLocalizedMessage()); + } + _scenarioDAO.flush(); + ht.flush(); + Assert + .assertEquals(ht.find( + "from UsesRelation where owner=" + docId) + .size(), uses.size(), + "UsesRelation(s) must not be changed."); + Assert.assertEquals(ht.find( + "from UsedByRelation where owner=" + docId).size(), + usedBy.size(), + "UsedByRelation(s) must not be changed."); + Assert.assertEquals(ht.find( + "from UsesRelation where refer=" + docId).size(), + usesRefer, + "Referencing UsesRelation(s) must not be changed."); + Assert + .assertEquals(ht.find( + "from UsedByRelation where refer=" + docId) + .size(), usedByRefer, + "Referencing UsedByRelation(s) must not be changed."); + Assert.assertEquals(ht.find( + "from ConvertsRelation where owner=" + docId) + .size(), convertsNb, + "ConvertsRelation(s) must not be changed."); + } + } + } + + // //////////////////////////////////////////////////////// + // Call removeDocument method for each document + // starting from the last activity. for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) { LOG.debug("Remove documents from the step " + i); step = _projectSettings.getStep(i); @@ -240,7 +279,7 @@ public class TestStepService extends BaseTest { if (aScStep.getDocuments().size() > 0) { docId = aScStep.getDocuments().get(0).value().getIndex(); - LOG.debug("Remove document " + LOG.debug("Remove document#" + docId + " " + aScStep.getDocument(docId).value().getTitle()); ht.flush(); List uses = ht @@ -271,11 +310,32 @@ public class TestStepService extends BaseTest { UsedByRelation.class)) { LOG.debug(rel.value().getTitle()); } + Assert.assertEquals(ht.find("from Document where rid=" + docId) + .size(), 1, "Nothing to delete."); + ok = _stepService.removeDocument(aScStep, docId); + nbRemovedDoc++; + removedDocs.add(docId); + Assert.assertTrue(ok, "Removing was failed."); _scenarioDAO.flush(); ht.flush(); + + for (Long id : removedDocs) { + String title = ""; + if (ht.get(Document.class, id) != null) { + title = ht.get(Document.class, id).getTitle(); + } + Assert.assertEquals(ht + .find("from Document where rid=" + id).size(), 0, + "Document#" + id + "(" + title + + ") was not removed from the database."); + } + + Assert.assertEquals(ht.find("from Document").size(), nbDoc + - nbRemovedDoc, + "Documents were not removed from the database."); Assert.assertEquals(ht.find( "from UsesRelation where owner=" + docId).size(), 0, "UsesRelation(s) were not removed from the database."); @@ -284,25 +344,33 @@ public class TestStepService extends BaseTest { "from UsedByRelation where owner=" + docId) .size(), 0, "UsedByRelation(s) were not removed from the database."); - Assert.assertEquals(ht.find( - "from UsesRelation where refer=" + docId).size(), 0, - "Referencing UsesRelation(s) were not removed from the database."); + Assert + .assertEquals(ht.find( + "from UsesRelation where refer=" + docId) + .size(), 0, + "Referencing UsesRelation(s) were not removed from the database."); Assert .assertEquals(ht.find( "from UsedByRelation where refer=" + docId) .size(), 0, "Referencing UsedByRelation(s) were not removed from the database."); Assert - .assertEquals(ht.find( - "from ConvertsRelation where owner=" + docId) - .size(), 0, - "ConvertsRelation(s) were not removed from the database."); + .assertEquals(ht.find( + "from ConvertsRelation where owner=" + docId) + .size(), 0, + "ConvertsRelation(s) were not removed from the database."); } } - Assert.assertEquals(ht.find("from Document").size(), 0, - "Documents were not removed from the database."); + Assert + .assertEquals(ht.find("from Document").size(), nbDoc + - nbRemovedDoc, + "Documents were not removed from the database."); + Assert + .assertEquals(ht.find("from File").size(), 0, + "Files were not removed from the database."); + Assert.assertEquals(ht.find( "from Publication where owner=" + aScen.getIndex()).size(), 0, "Publications were not removed from the database."); diff --git a/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java b/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java index c4260d8..795572d 100644 --- a/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java +++ b/Workspace/Siman/src/org/splat/simer/EditDocumentAction.java @@ -6,6 +6,7 @@ import java.util.Calendar; import org.splat.dal.bo.kernel.User; import org.splat.dal.bo.som.ConvertsRelation; import org.splat.dal.bo.som.Publication; +import org.splat.exception.DocumentIsUsedException; import org.splat.kernel.InvalidPropertyException; import org.splat.service.PublicationService; import org.splat.service.StepService; @@ -137,7 +138,17 @@ public class EditDocumentAction extends DisplayStudyStepAction { } } - public String doDeleteDocument() { + /** + * Remove the selected document from the current activity. + * + * @return SUCCESS + * @throws DocumentIsUsedException + * if the document is used by other document(s) + * @throws NumberFormatException + * if the document id can not be transformed to number + */ + public String doDeleteDocument() throws DocumentIsUsedException, + NumberFormatException { setMenu(); _openStudy = getOpenStudy();