From 156fa724e95aaa59a5f335c4264b8f543326a814 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 5 Jan 2017 07:18:47 +0000
Subject: [PATCH] SSDM-3768: Introducing EntityUtils.render methods to add perm
 ID to verification error messages because the original identifier might be
 changed.

SVN: 37552
---
 ...ifyDataSetSampleAndExperimentExecutor.java |  3 +-
 .../sample/VerifySampleContainerExecutor.java |  5 ++-
 .../sample/VerifySampleDataSetsExecutor.java  |  3 +-
 .../VerifySampleExperimentExecutor.java       | 11 ++---
 .../sample/VerifySampleProjectExecutor.java   |  5 ++-
 .../server/asapi/v3/utils/EntityUtils.java    | 45 +++++++++++++++++++
 6 files changed, 61 insertions(+), 11 deletions(-)
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/utils/EntityUtils.java

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/VerifyDataSetSampleAndExperimentExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/VerifyDataSetSampleAndExperimentExecutor.java
index d9951c5749e..1de443ab088 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/VerifyDataSetSampleAndExperimentExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/VerifyDataSetSampleAndExperimentExecutor.java
@@ -28,6 +28,7 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatch;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatchProcessor;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.VerifyProgress;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.utils.EntityUtils;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.util.DataSetTypeWithoutExperimentChecker;
@@ -102,7 +103,7 @@ public class VerifyDataSetSampleAndExperimentExecutor implements IVerifyDataSetS
         if (experiment != null && experiment.getDeletion() != null)
         {
             throw new UserFailureException("Data set can not be registered because experiment '"
-                    + experiment.getIdentifier() + "' is in trash.");
+                    + EntityUtils.render(experiment) + "' is in trash.");
         }
     }
 
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleContainerExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleContainerExecutor.java
index 4bbff53601f..3aa68dac212 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleContainerExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleContainerExecutor.java
@@ -23,6 +23,7 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatch;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatchProcessor;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.VerifyProgress;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.utils.EntityUtils;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.SampleGenericBusinessRules;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
@@ -48,8 +49,8 @@ public class VerifySampleContainerExecutor implements IVerifySampleContainerExec
                     {
                         if (sample.equals(containerCandidate))
                         {
-                            throw UserFailureException.fromTemplate("'%s' cannot be it's own container.",
-                                    sample.getIdentifier());
+                            throw UserFailureException.fromTemplate("Sample %s cannot be it's own container.",
+                                    EntityUtils.render(sample));
                         }
                         containerCandidate = containerCandidate.getContainer();
                     }
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleDataSetsExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleDataSetsExecutor.java
index 267178bdd4c..063530c0c97 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleDataSetsExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleDataSetsExecutor.java
@@ -26,6 +26,7 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.stereotype.Component;
 
 import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.utils.EntityUtils;
 import ch.systemsx.cisd.common.collection.CollectionUtils;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer;
@@ -73,7 +74,7 @@ public class VerifySampleDataSetsExecutor implements IVerifySampleDataSetsExecut
         if (dataSetsNeedingExperiment.isEmpty() == false)
         {
             throw new UserFailureException("Operation cannot be performed, because the sample "
-                    + sample.getIdentifier() + " has the following datasets which need an experiment: "
+                    + EntityUtils.render(sample) + " has the following datasets which need an experiment: "
                     + CollectionUtils.abbreviate(dataSetsNeedingExperiment, 10));
         }
 
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleExperimentExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleExperimentExecutor.java
index 98b7e1f5ca1..5dcccd19af6 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleExperimentExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleExperimentExecutor.java
@@ -26,6 +26,7 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatch;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatchProcessor;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.VerifyProgress;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.utils.EntityUtils;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataDAO;
@@ -79,26 +80,26 @@ public class VerifySampleExperimentExecutor implements IVerifySampleExperimentEx
                     {
                         throw UserFailureException.fromTemplate("Cannot detach the sample '%s' from the space "
                                 + "because there are already datasets attached to the sample.",
-                                sample.getIdentifier());
+                                EntityUtils.render(sample));
                     }
 
                     if (experiment != null && sample.getSpace() == null)
                     {
                         throw new UserFailureException("Shared samples cannot be attached to experiments. Sample: "
-                                + sample.getIdentifier() + ", Experiment: " + experiment.getIdentifier());
+                                + EntityUtils.render(sample) + ", Experiment: " + EntityUtils.render(experiment));
                     }
 
                     if (experiment != null && experiment.getProject().getSpace().equals(sample.getSpace()) == false)
                     {
                         throw new UserFailureException("Sample space must be the same as experiment space. "
-                                + "Sample: " + sample.getIdentifier() + ", Experiment: " + experiment.getIdentifier());
+                                + "Sample: " + EntityUtils.render(sample) + ", Experiment: " + EntityUtils.render(experiment));
                     }
                     if (experiment != null && sample.getProject() != null
                             && experiment.getProject().equals(sample.getProject()) == false)
                     {
                         throw new UserFailureException("Sample project must be the same as experiment project. "
-                                + "Sample: " + sample.getIdentifier() + ", Project: " + sample.getProject().getIdentifier()
-                                + ", Experiment: " + experiment.getIdentifier());
+                                + "Sample: " + EntityUtils.render(sample) + ", Project: " + EntityUtils.render(sample.getProject())
+                                + ", Experiment: " + EntityUtils.render(experiment));
                     }
                 }
 
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleProjectExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleProjectExecutor.java
index 1dc4e1470d7..df258036942 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleProjectExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/sample/VerifySampleProjectExecutor.java
@@ -23,6 +23,7 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatch;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.CollectionBatchProcessor;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.VerifyProgress;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.utils.EntityUtils;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ProjectPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
@@ -46,12 +47,12 @@ public class VerifySampleProjectExecutor implements IVerifySampleProjectExecutor
                     if (project != null && sample.getSpace() == null)
                     {
                         throw new UserFailureException("Shared samples cannot be attached to projects. Sample: "
-                                + sample.getIdentifier() + ", Project: " + project.getIdentifier());
+                                + EntityUtils.render(sample) + ", Project: " + EntityUtils.render(project));
                     }
                     if (project != null && project.getSpace().equals(sample.getSpace()) == false)
                     {
                         throw new UserFailureException("Sample space must be the same as project space. Sample: "
-                                + sample.getIdentifier() + ", Project: " + project.getIdentifier());
+                                + EntityUtils.render(sample) + ", Project: " + EntityUtils.render(project));
                     }
                 }
 
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/utils/EntityUtils.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/utils/EntityUtils.java
new file mode 100644
index 00000000000..0fbcf51b1ae
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/utils/EntityUtils.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017 ETH Zuerich, SIS
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.server.asapi.v3.utils;
+
+import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationHolderDTO;
+import ch.systemsx.cisd.openbis.generic.shared.dto.ProjectPE;
+
+/**
+ * Entity utility functions. 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class EntityUtils
+{
+    public static String render(IEntityInformationHolderDTO entity)
+    {
+        String identifier = entity.getIdentifier();
+        String permId = entity.getPermId();
+        return render(identifier, permId);
+    }
+    
+    public static String render(ProjectPE project)
+    {
+        return render(project.getIdentifier(), project.getPermId());
+    }
+
+    private static String render(String identifier, String permId)
+    {
+        return identifier + " (perm id: " + permId + ")";
+    }
+}
-- 
GitLab