From 147fde34954ffed43bd5c84534dd49ddd15e74b9 Mon Sep 17 00:00:00 2001
From: gpawel <gpawel>
Date: Fri, 24 Aug 2012 12:07:51 +0000
Subject: [PATCH] bugfix: exposing method convertStringToElements (needed for
 YestLab)

SVN: 26444
---
 .../api/IStructuredPropertyConverter.java         |  7 +++++++
 .../JsonStructuredPropertyConverter.java          | 13 +++++++++----
 .../XmlOrJsonStructuredPropertyConverter.java     | 15 +++++++++++++++
 .../XmlStructuredPropertyConverter.java           | 14 +++++++++-----
 4 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/api/IStructuredPropertyConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/api/IStructuredPropertyConverter.java
index 04bb8861f6a..76ef98deebb 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/api/IStructuredPropertyConverter.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/api/IStructuredPropertyConverter.java
@@ -36,6 +36,13 @@ public interface IStructuredPropertyConverter
      */
     List<IElement> convertToElements(IManagedProperty property);
 
+    /**
+     * Converts the values of specified String object into a list of elements.
+     * 
+     * @return an empty list if the value is undefined or special.
+     */
+    List<IElement> convertStringToElements(String propertyValue);
+
     /**
      * @return a {@link String} representation of the specified elements that can be persisted in
      *         the database.
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/JsonStructuredPropertyConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/JsonStructuredPropertyConverter.java
index 21ae35c331d..8bc476bccab 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/JsonStructuredPropertyConverter.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/JsonStructuredPropertyConverter.java
@@ -83,8 +83,7 @@ public class JsonStructuredPropertyConverter implements IStructuredPropertyConve
         private int level = 0;
 
         @Override
-        public void writeStartArray(JsonGenerator jg)
-                throws IOException, JsonGenerationException
+        public void writeStartArray(JsonGenerator jg) throws IOException, JsonGenerationException
         {
             if (level++ == 0)
             {
@@ -129,7 +128,12 @@ public class JsonStructuredPropertyConverter implements IStructuredPropertyConve
 
     public boolean canHandle(IManagedProperty property)
     {
-        return property.getValue().startsWith("[");
+        return canHandle(property.getValue());
+    }
+
+    public boolean canHandle(String string)
+    {
+        return string.startsWith("[");
     }
 
     @Override
@@ -138,7 +142,8 @@ public class JsonStructuredPropertyConverter implements IStructuredPropertyConve
         return convertStringToElements(property.getValue());
     }
 
-    private List<IElement> convertStringToElements(String propertyValue)
+    @Override
+    public List<IElement> convertStringToElements(String propertyValue)
     {
         if (ManagedProperty.isSpecialValue(propertyValue) || StringUtils.isBlank(propertyValue))
         {
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlOrJsonStructuredPropertyConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlOrJsonStructuredPropertyConverter.java
index 3b3a26e084e..8b1680908c9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlOrJsonStructuredPropertyConverter.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlOrJsonStructuredPropertyConverter.java
@@ -62,6 +62,21 @@ public class XmlOrJsonStructuredPropertyConverter implements IStructuredProperty
         }
     }
 
+    @Override
+    public List<IElement> convertStringToElements(String string)
+    {
+        if (xmlConverter.canHandle(string))
+        {
+            return xmlConverter.convertStringToElements(string);
+        } else if (jsonConverter.canHandle(string))
+        {
+            return jsonConverter.convertStringToElements(string);
+        } else
+        {
+            throw new UserFailureException("Illegal managed property value '" + string + "'.");
+        }
+    }
+
     @Override
     public String convertToString(List<IElement> elements)
     {
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java
index 86154d1f231..70dd58eb93b 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/managed_property/structured/XmlStructuredPropertyConverter.java
@@ -62,10 +62,13 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver
 
     public boolean canHandle(IManagedProperty property)
     {
-        String propertyValue = property.getValue();
-        return propertyValue.startsWith("<" + ROOT_NAME)
-                || ManagedProperty.isSpecialValue(propertyValue)
-                || StringUtils.isBlank(propertyValue);
+        return canHandle(property.getValue());
+    }
+
+    public boolean canHandle(String string)
+    {
+        return string.startsWith("<" + ROOT_NAME) || ManagedProperty.isSpecialValue(string)
+                || StringUtils.isBlank(string);
     }
 
     @Override
@@ -74,7 +77,8 @@ public class XmlStructuredPropertyConverter implements IStructuredPropertyConver
         return convertStringToElements(property.getValue());
     }
 
-    private List<IElement> convertStringToElements(String propertyValue)
+    @Override
+    public List<IElement> convertStringToElements(String propertyValue)
     {
         if (ManagedProperty.isSpecialValue(propertyValue) || StringUtils.isBlank(propertyValue))
         {
-- 
GitLab