From d3f198711234c356c67dc15a64ae22e71b958156 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Wed, 30 Sep 2009 13:06:23 +0000
Subject: [PATCH] added validation of sample group compared to children group
 on parent sample update

SVN: 12815
---
 .../bo/AbstractSampleBusinessObject.java      |  1 +
 .../bo/SampleGenericBusinessRules.java        | 38 +++++++++++++++++++
 2 files changed, 39 insertions(+)

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 793245da1de..bef4018e356 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
@@ -285,6 +285,7 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi
                     "Sample group must be the same as experiment group. Shared samples cannot be attached to experiments.");
         }
         SampleGenericBusinessRules.assertValidParents(sample);
+        SampleGenericBusinessRules.assertValidChildren(sample);
     }
 
     protected boolean hasDatasets(IExternalDataDAO externalDataDAO, SamplePE sample)
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleGenericBusinessRules.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleGenericBusinessRules.java
index 275ebb46dc7..7a55771dd9d 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleGenericBusinessRules.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleGenericBusinessRules.java
@@ -16,6 +16,8 @@
 
 package ch.systemsx.cisd.openbis.generic.server.business.bo;
 
+import java.util.List;
+
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper;
@@ -59,6 +61,34 @@ public class SampleGenericBusinessRules
         }
     }
 
+    static private void assertValidChildrenRelation(final List<SamplePE> children,
+            final SamplePE parent) throws UserFailureException
+    {
+        if (children == null || children.size() == 0 || parent == null)
+            return;
+
+        // new identifier of a parent is needed for comparison
+        SampleIdentifier parentId = IdentifierHelper.createSampleIdentifier(parent);
+
+        if (parentId.isGroupLevel())
+        {
+            for (SamplePE child : children)
+            {
+                SampleIdentifier childId = child.getSampleIdentifier();
+                if (childId.isDatabaseInstanceLevel())
+                {
+                    throwUserFailureException("Sample '%s' can not be a group sample because of "
+                            + "a derived database instance sample '%s'.", parent, child);
+                }
+                if (parentId.getGroupLevel().equals(childId.getGroupLevel()) == false)
+                {
+                    throwUserFailureException("Sample '%s' can not have different group "
+                            + "from its derived sample '%s'.", child, parent);
+                }
+            }
+        }
+    }
+
     static public void assertValidParents(SamplePE sample)
     {
         if (sample == null)
@@ -67,6 +97,14 @@ public class SampleGenericBusinessRules
         assertValidParentRelation(sample.getGeneratedFrom(), sample);
     }
 
+    static public void assertValidChildren(SamplePE sample)
+    {
+        if (sample == null)
+            return;
+        assertValidChildrenRelation(sample.getContained(), sample);
+        assertValidChildrenRelation(sample.getGenerated(), sample);
+    }
+
     static private void throwUserFailureException(String messageTemplate, SamplePE sample1,
             SamplePE sample2)
     {
-- 
GitLab