From fc1d8f12f76fdd2ee8ea2008d6aac5ea9fd34407 Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Mon, 14 Jan 2013 16:33:56 +0000
Subject: [PATCH] SP-207 BIS-124 add check that sample cannot contain itself

SVN: 28089
---
 .../bo/AbstractSampleBusinessObject.java      | 21 ++++++++++++++++++-
 .../UpdateSampleContainmentTest.java          |  5 ++---
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java
index 54ed321c013..482f492349f 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractSampleBusinessObject.java
@@ -17,7 +17,6 @@
 package ch.systemsx.cisd.openbis.generic.server.business.bo;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
@@ -237,6 +236,8 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi
             return;
         }
 
+        checkIfCanBeContainer(samplePE, containerPE);
+
         if (containerPE == null)
         {
             relationshipService.removeSampleFromContainer(session, samplePE);
@@ -270,6 +271,24 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi
         replaceParents(childPE, parentPEs);
     }
 
+    /**
+     * check if the given candidate for the container is not already contained in the sample.
+     */
+    private void checkIfCanBeContainer(final SamplePE sample, final SamplePE container)
+    {
+        SamplePE containerCandidate = container;
+
+        while (containerCandidate != null)
+        {
+            if (sample.equals(containerCandidate))
+            {
+                throw UserFailureException.fromTemplate("'%s' cannot be it's own container.",
+                        sample.getIdentifier());
+            }
+            containerCandidate = containerCandidate.getContainer();
+        }
+    }
+
     /**
      * depth-first search through all parents of parent candidate in search of a childPE. If it's
      * found - the exception is being thrown
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java
index 45766938eb8..47bb54a2e7a 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/UpdateSampleContainmentTest.java
@@ -119,14 +119,13 @@ public class UpdateSampleContainmentTest extends BaseTest
         perform(anUpdateOf(subComponent).toHaveContainer(component));
     }
 
-    @Test
+    @Test(expectedExceptions =
+        { UserFailureException.class })
     public void sampleCanContainItself() throws Exception
     {
         Sample sample = create(aSample().inSpace(space));
 
         perform(anUpdateOf(sample).toHaveContainer(sample));
-
-        assertThat(sample, hasContainer(sample));
     }
 
     Space containerSpace;
-- 
GitLab