From 2079bff6aa76baecae9622cf3b3a4965eb4d2f31 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 23 Mar 2015 13:05:13 +0000
Subject: [PATCH] SSDM-1621: fixing bugs

SVN: 33708
---
 .../business/bo/AbstractSampleBusinessObject.java  | 14 +++++++++-----
 .../server/business/bo/util/SampleUtils.java       |  2 +-
 2 files changed, 10 insertions(+), 6 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 276320ff2bf..4a0dd2305ba 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
@@ -40,6 +40,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
 import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
+import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.EntityTypePropertyTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
@@ -457,14 +458,17 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi
 
     protected void checkExperimentBusinessRules(SamplePE sample)
     {
-//        checkSampleWithoutDatasets(sample);
+        ExperimentPE experiment = sample.getExperiment();
+        if (experiment == null)
+        {
+            checkSampleWithoutDatasets(sample);
+        }
         if (hasDatasets(sample) && sample.getSpace() == null)
         {
             throw UserFailureException.fromTemplate("Cannot detach the sample '%s' from the space "
                     + "because there are already datasets attached to the sample.",
                     sample.getIdentifier());
         }
-        ExperimentPE experiment = sample.getExperiment();
         if (experiment != null
                 && (sample.getSpace() == null || experiment.getProject().getSpace()
                         .equals(sample.getSpace()) == false))
@@ -558,14 +562,14 @@ abstract class AbstractSampleBusinessObject extends AbstractSampleIdentifierBusi
             return;
         }
         ensureExperimentIsValid(identifierOrNull, newExperiment, sample);
-        ensureSampleAttachableToExperiment(sample);
+        ensureSampleAttachableToExperiment(sample, newExperiment);
 
         assignSampleAndRelatedDataSetsToExperiment(sample, newExperiment);
     }
 
-    private void ensureSampleAttachableToExperiment(SamplePE sample)
+    private void ensureSampleAttachableToExperiment(SamplePE sample, ExperimentPE newExperiment)
     {
-        if (sample.getSpace() == null)
+        if (sample.getSpace() == null && newExperiment != null)
         {
             throw UserFailureException.fromTemplate(
                     "It is not allowed to connect a shared sample '%s' to the experiment.",
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/util/SampleUtils.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/util/SampleUtils.java
index 21703089367..30521c76f70 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/util/SampleUtils.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/util/SampleUtils.java
@@ -44,7 +44,7 @@ public class SampleUtils
     {
         return UserFailureException.fromTemplate(
                 "The dataset '%s' cannot be connected to the sample '%s' because %s.",
-                data.getCode(), sample.getIdentifier(), reason);
+                data.getCode(), sample == null ? "?" : sample.getIdentifier(), reason);
     }
 
 }
-- 
GitLab