From 5b8b08d318a0f21eab4a4da5325d42f276a2a507 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Wed, 11 Apr 2012 13:37:43 +0000
Subject: [PATCH] LMS-2789 Consistent handling of properties that were not
 fetched

SVN: 24867
---
 .../generic/shared/api/v1/dto/Sample.java     | 41 ++++++++++++++++++-
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java
index c4f6ce86e5e..dedf21c0c17 100644
--- a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/Sample.java
@@ -29,6 +29,7 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 import org.codehaus.jackson.annotate.JsonIgnore;
+import org.codehaus.jackson.annotate.JsonProperty;
 
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet.Connections;
 import ch.systemsx.cisd.openbis.generic.shared.basic.IIdentifierHolder;
@@ -342,9 +343,17 @@ public final class Sample implements Serializable, IIdentifierHolder
         return sampleTypeCode;
     }
 
-    public Map<String, String> getProperties()
+    @JsonIgnore
+    public Map<String, String> getProperties() throws IllegalArgumentException
     {
-        return Collections.unmodifiableMap(properties);
+        if (getRetrievedFetchOptions().contains(SampleFetchOption.PROPERTIES))
+        {
+            return Collections.unmodifiableMap(properties);
+        } else
+        {
+            throw new IllegalArgumentException("Properties were not retrieved for sample "
+                    + getIdentifier() + ".");
+        }
     }
 
     /**
@@ -508,6 +517,12 @@ public final class Sample implements Serializable, IIdentifierHolder
         this.sampleTypeCode = sampleTypeCode;
     }
 
+    @JsonProperty(value = "properties")
+    public Map<String, String> getPropertiesJson()
+    {
+        return retrievedFetchOptions.contains(SampleFetchOption.PROPERTIES) ? properties : null;
+    }
+
     private void setProperties(HashMap<String, String> properties)
     {
         this.properties = properties;
@@ -523,4 +538,26 @@ public final class Sample implements Serializable, IIdentifierHolder
         this.retrievedFetchOptions = fetchOptions;
     }
 
+    @JsonProperty(value = "children")
+    public List<Sample> getChildrenJson()
+    {
+        return retrievedFetchOptions.contains(SampleFetchOption.CHILDREN) ? children : null;
+    }
+
+    @JsonProperty(value = "parents")
+    public List<Sample> getParentsJson()
+    {
+        return retrievedFetchOptions.contains(SampleFetchOption.PARENTS) ? parents : null;
+    }
+
+    private void setParents(List<Sample> parents)
+    {
+        this.parents = parents;
+    }
+
+    private void setChildren(List<Sample> children)
+    {
+        this.children = children;
+    }
+
 }
-- 
GitLab