From 819755e5d78fa9d36448fb10c2f4e6634c47c8eb Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Mon, 22 Nov 2010 09:17:10 +0000
Subject: [PATCH] [LMS-1858] added DynamicPropertyFunctions with material
 function

SVN: 18823
---
 .../web/server/calculator/RowCalculator.java  |  2 +-
 .../calculator/DynamicPropertyCalculator.java |  5 ++-
 .../calculator/DynamicPropertyFunctions.java  | 38 +++++++++++++++++++
 .../shared/calculator/AbstractCalculator.java | 15 ++++++--
 4 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyFunctions.java

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 163ca478243..f3d7af2e314 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
@@ -41,7 +41,7 @@ class RowCalculator extends AbstractCalculator
             Set<ParameterWithValue> parameters)
     {
         super(new Evaluator(substitudeParameters(expression, parameters), Math.class,
-                BASIC_INITIAL_SCRIPT));
+                getBasicInitialScript()));
         row = new Row(provider);
         evaluator.set("row", row);
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyCalculator.java
index 71868c54439..a1d18ffb1a3 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyCalculator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyCalculator.java
@@ -46,10 +46,11 @@ public class DynamicPropertyCalculator extends AbstractCalculator
     public static DynamicPropertyCalculator create(String expression)
     {
         String calculatedExpression = expression;
-        String initialScript = BASIC_INITIAL_SCRIPT;
+        String initialScript = getBasicInitialScript();
+        initialScript += importFunctions(DynamicPropertyFunctions.class) + NEWLINE;
         if (Evaluator.isMultiline(expression))
         {
-            initialScript += "\n" + expression;
+            initialScript += expression;
             calculatedExpression = INVOKE_CALCULATE_EXPR;
         }
         return new DynamicPropertyCalculator(new Evaluator(calculatedExpression, Math.class,
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyFunctions.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyFunctions.java
new file mode 100644
index 00000000000..2603c0d917e
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/dynamic_property/calculator/DynamicPropertyFunctions.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2009 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.server.dataaccess.db.dynamic_property.calculator;
+
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialIdentifier;
+
+/**
+ * Set of specific dynamic property related functions for functions used in jython expressions.
+ * <p>
+ * All public methods of this class are part of the Dynamic Property Evaluation API.
+ * 
+ * @author Piotr Buczek
+ */
+final class DynamicPropertyFunctions
+{
+    /**
+     * @return String representation of material identifier for given material code and material
+     *         type code.
+     */
+    public static String material(String code, String typeCode)
+    {
+        return MaterialIdentifier.print(code, typeCode);
+    }
+}
\ No newline at end of file
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/calculator/AbstractCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/calculator/AbstractCalculator.java
index 879a602c44d..8fb658111e4 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/calculator/AbstractCalculator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/calculator/AbstractCalculator.java
@@ -27,9 +27,18 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
  */
 public class AbstractCalculator
 {
-    protected static final String BASIC_INITIAL_SCRIPT = "from "
-            + StandardFunctions.class.getCanonicalName() + " import *\n"
-            + "def int(x):return toInt(x)\n" + "def float(x):return toFloat(x)\n";
+    protected static String NEWLINE = "\n";
+
+    protected static String importFunctions(Class<?> clazz)
+    {
+        return "from " + clazz.getCanonicalName() + " import *";
+    }
+
+    protected final static String getBasicInitialScript()
+    {
+        return importFunctions(StandardFunctions.class) + NEWLINE + "def int(x):return toInt(x)"
+                + NEWLINE + "def float(x):return toFloat(x)" + NEWLINE;
+    }
 
     protected final Evaluator evaluator;
 
-- 
GitLab