From e35683b281c164d3591bc69b279ea052a21ed65e Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 15 Oct 2010 13:18:51 +0000
Subject: [PATCH] [LMS-1818] refactoring

SVN: 18322
---
 .../server/calculator/AbstractCalculator.java | 82 +++++++++++++++++++
 .../calculator/DynamicPropertyCalculator.java | 44 ++++++++++
 .../web/server/calculator/RowCalculator.java  | 61 ++------------
 .../property/DynamicPropertyCalculator.java   | 25 ------
 .../calculator/property/IEntityAdaptor.java   |  2 +
 .../calculator/property/SampleAdaptor.java    |  2 +-
 6 files changed, 135 insertions(+), 81 deletions(-)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/AbstractCalculator.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/DynamicPropertyCalculator.java
 delete mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/DynamicPropertyCalculator.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/AbstractCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/AbstractCalculator.java
new file mode 100644
index 00000000000..ccf9390ebad
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/AbstractCalculator.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * 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.systemsx.cisd.openbis.generic.client.web.server.calculator;
+
+import java.math.BigInteger;
+
+import ch.systemsx.cisd.common.evaluator.Evaluator;
+import ch.systemsx.cisd.common.evaluator.EvaluatorException;
+import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
+
+/**
+ * @author Piotr Buczek
+ */
+public class AbstractCalculator
+{
+
+    protected final Evaluator evaluator;
+
+    public AbstractCalculator(Evaluator evaluator)
+    {
+        this.evaluator = evaluator;
+    }
+
+    public PrimitiveValue getTypedResult()
+    {
+        Object value = evaluator.eval();
+        if (value == null)
+        {
+            return PrimitiveValue.NULL;
+        }
+        if (value instanceof Long)
+        {
+            return new PrimitiveValue((Long) value);
+        } else if (value instanceof Double)
+        {
+            return new PrimitiveValue((Double) value);
+        } else
+        {
+            return new PrimitiveValue(value.toString());
+        }
+    }
+
+    public boolean evalToBoolean() throws EvaluatorException
+    {
+        return evaluator.evalToBoolean();
+    }
+
+    public int evalToInt() throws EvaluatorException
+    {
+        return evaluator.evalToInt();
+    }
+
+    public BigInteger evalToBigInt() throws EvaluatorException
+    {
+        return evaluator.evalToBigInt();
+    }
+
+    public double evalToDouble() throws EvaluatorException
+    {
+        return evaluator.evalToDouble();
+    }
+
+    public String evalAsString() throws EvaluatorException
+    {
+        return evaluator.evalAsString();
+    }
+
+}
\ No newline at end of file
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/DynamicPropertyCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/DynamicPropertyCalculator.java
new file mode 100644
index 00000000000..df0fcfcd9cd
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/DynamicPropertyCalculator.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * 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.systemsx.cisd.openbis.generic.client.web.server.calculator;
+
+import ch.systemsx.cisd.common.evaluator.Evaluator;
+import ch.systemsx.cisd.openbis.generic.client.web.server.calculator.property.IEntityAdaptor;
+
+/**
+ * @author Piotr Buczek
+ */
+public class DynamicPropertyCalculator extends AbstractCalculator
+{
+    private static final String INITIAL_SCRIPT = "from "
+            + StandardFunctions.class.getCanonicalName() + " import *\n"
+            + "def int(x):return toInt(x)\n                            "
+            + "def float(x):return toFloat(x)\n                        ";
+
+    private static final String ENTITY_VARIABLE_NAME = "entity";
+
+    public DynamicPropertyCalculator(String expression)
+    {
+        super(new Evaluator(expression, Math.class, INITIAL_SCRIPT));
+    }
+
+    public void setEntity(IEntityAdaptor entity)
+    {
+        evaluator.set(ENTITY_VARIABLE_NAME, entity);
+    }
+
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/RowCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/RowCalculator.java
index a5613538846..587429aadd3 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/RowCalculator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/RowCalculator.java
@@ -16,27 +16,23 @@
 
 package ch.systemsx.cisd.openbis.generic.client.web.server.calculator;
 
-import java.math.BigInteger;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
 import ch.systemsx.cisd.common.evaluator.Evaluator;
-import ch.systemsx.cisd.common.evaluator.EvaluatorException;
 import ch.systemsx.cisd.common.utilities.Template;
-import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ParameterWithValue;
 
 /**
  * @author Franz-Josef Elmer
  */
-class RowCalculator
+class RowCalculator extends AbstractCalculator
 {
     private static final String INITIAL_SCRIPT = "from "
             + StandardFunctions.class.getCanonicalName() + " import *\n"
-            + "def int(x):return toInt(x)\n" + "def float(x):return toFloat(x)\n";
-
-    private final Evaluator evaluator;
+            + "def int(x):return toInt(x)\n                            "
+            + "def float(x):return toFloat(x)\n                        ";
 
     private final Row row;
 
@@ -48,9 +44,8 @@ class RowCalculator
     public RowCalculator(ITableDataProvider provider, String expression,
             Set<ParameterWithValue> parameters)
     {
-        evaluator =
-                new Evaluator(substitudeParameters(expression, parameters), Math.class,
-                        INITIAL_SCRIPT);
+        super(new Evaluator(substitudeParameters(expression, parameters), Math.class,
+                INITIAL_SCRIPT));
         row = new Row(provider);
         evaluator.set("row", row);
     }
@@ -60,51 +55,7 @@ class RowCalculator
         row.setRowData(rowValues);
     }
 
-    public PrimitiveValue getTypedResult()
-    {
-        Object value = evaluator.eval();
-        if (value == null)
-        {
-            return PrimitiveValue.NULL;
-        }
-        if (value instanceof Long)
-        {
-            return new PrimitiveValue((Long) value);
-        } else if (value instanceof Double)
-        {
-            return new PrimitiveValue((Double) value);
-        } else
-        {
-            return new PrimitiveValue(value.toString());
-        }
-    }
-
-    public boolean evalToBoolean() throws EvaluatorException
-    {
-        return evaluator.evalToBoolean();
-    }
-
-    public int evalToInt() throws EvaluatorException
-    {
-        return evaluator.evalToInt();
-    }
-
-    public BigInteger evalToBigInt() throws EvaluatorException
-    {
-        return evaluator.evalToBigInt();
-    }
-
-    public double evalToDouble() throws EvaluatorException
-    {
-        return evaluator.evalToDouble();
-    }
-
-    public String evalAsString() throws EvaluatorException
-    {
-        return evaluator.evalAsString();
-    }
-
-    private String substitudeParameters(String originalExpression,
+    private static String substitudeParameters(String originalExpression,
             Set<ParameterWithValue> parameters)
     {
         Template template = new Template(originalExpression);
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/DynamicPropertyCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/DynamicPropertyCalculator.java
deleted file mode 100644
index 44d4e833139..00000000000
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/DynamicPropertyCalculator.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright 2010 ETH Zuerich, CISD
- *
- * 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.systemsx.cisd.openbis.generic.client.web.server.calculator.property;
-
-/**
- * @author Piotr Buczek
- */
-public class DynamicPropertyCalculator
-{
-    // TODO
-}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/IEntityAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/IEntityAdaptor.java
index 26403cdba77..314646f339f 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/IEntityAdaptor.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/IEntityAdaptor.java
@@ -20,6 +20,8 @@ import java.util.Collection;
 
 /**
  * Interface implemented by all entity adaptors
+ * <p>
+ * All methods of this interface are part of the Dynamic Properties API.
  * 
  * @author Piotr Buczek
  */
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/SampleAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/SampleAdaptor.java
index fbaf4bac7bc..1a8e04bcfc3 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/SampleAdaptor.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/calculator/property/SampleAdaptor.java
@@ -37,7 +37,7 @@ public class SampleAdaptor implements IEntityAdaptor
 
     private final SamplePE samplePE;
 
-    SampleAdaptor(SamplePE samplePE)
+    public SampleAdaptor(SamplePE samplePE)
     {
         this.samplePE = samplePE;
         this.code = samplePE.getCode();
-- 
GitLab