From a855bec9b99fa3a83bdfab57535f39dad50921d8 Mon Sep 17 00:00:00 2001 From: eegeek Date: Fri, 14 Feb 2014 18:48:48 +0000 Subject: [PATCH] 0001583: Create new value map column transform --- .../src/docbook/configuration.xml | 11 ++- .../transform/ValueMapColumnTransform.java | 78 +++++++++++++++++++ .../io/data/writer/TransformWriter.java | 2 + 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/transform/ValueMapColumnTransform.java diff --git a/symmetric-assemble/src/docbook/configuration.xml b/symmetric-assemble/src/docbook/configuration.xml index e2febba5df..00b5361ac1 100644 --- a/symmetric-assemble/src/docbook/configuration.xml +++ b/symmetric-assemble/src/docbook/configuration.xml @@ -1929,7 +1929,7 @@ a new identity, not copying the actual identity value from the source. -Mathimatical Transform ('math'): This transformation allows you to +Mathematical Transform ('math'): This transformation allows you to perform mathematical equations in the transform expression. Some variables are provided to the script: #{COLUMN_NAME} @@ -1941,6 +1941,15 @@ is the value of the current source column; is the old value of the source column for an updated row. + + +Value Map Transform ('valueMap'): This transformation allows for simple value substitutions through use of the transform expression. +The transform expresion should consist of a space separated list of value pairs of the format sourceValue=TargetValue. The column value is used to +locate the correct sourceValue, and the transform will change the value into the corresponding targetValue. A sourceValue of * can be used to +represent a default target value in the event that the sourceValue is not found. Otherwise, if no default value is found, +the result will be null. For example, consider the following transform expression: s1=t1 s2=t2 s3=t3 *=t4. A source value of +s1 will be transformed to t1, s2 to t2, s3 to t3, s4 to t4, s5 to t4, null to t4, etc. + diff --git a/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/transform/ValueMapColumnTransform.java b/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/transform/ValueMapColumnTransform.java new file mode 100644 index 0000000000..20e54f9d63 --- /dev/null +++ b/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/transform/ValueMapColumnTransform.java @@ -0,0 +1,78 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * 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 org.jumpmind.symmetric.io.data.transform; + +import java.util.Map; +import java.util.StringTokenizer; + +import org.jumpmind.db.platform.IDatabasePlatform; +import org.jumpmind.extension.IBuiltInExtensionPoint; +import org.jumpmind.symmetric.io.data.DataContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ValueMapColumnTransform implements ISingleValueColumnTransform, IBuiltInExtensionPoint { + + protected final Logger log = LoggerFactory.getLogger(getClass()); + + public static final String NAME = "valueMap"; + + public String getName() { + return NAME; + } + + public boolean isExtractColumnTransform() { + return true; + } + + public boolean isLoadColumnTransform() { + return true; + } + + private static String getValue(String value,String expression) { + if (expression==null) { + return null; + } + + StringTokenizer tokens = new StringTokenizer(expression); + String defaultValue = null; + + while (tokens.hasMoreElements()) { + String keyValue = (String) tokens.nextElement(); + int equalIndex = keyValue.indexOf("="); + if (equalIndex != -1) { + if (keyValue.substring(0, equalIndex).equals(value)) { + return keyValue.substring(equalIndex); + } else if (keyValue.substring(0, equalIndex).equals("*")) { + defaultValue = keyValue.substring(equalIndex); + } + } + } + return defaultValue; + } + + public String transform(IDatabasePlatform platform, DataContext context, TransformColumn column, + TransformedData data, Map sourceValues, String newValue, String oldValue) + throws IgnoreColumnException, IgnoreRowException { + return getValue(newValue,column.getTransformExpression()); + + } +} diff --git a/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/writer/TransformWriter.java b/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/writer/TransformWriter.java index e5b6d6d55a..1cda308961 100644 --- a/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/writer/TransformWriter.java +++ b/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/writer/TransformWriter.java @@ -50,6 +50,7 @@ import org.jumpmind.symmetric.io.data.transform.RemoveColumnTransform; import org.jumpmind.symmetric.io.data.transform.SubstrColumnTransform; import org.jumpmind.symmetric.io.data.transform.TransformColumn; +import org.jumpmind.symmetric.io.data.transform.ValueMapColumnTransform; import org.jumpmind.symmetric.io.data.transform.VariableColumnTransform; import org.jumpmind.symmetric.io.data.transform.TransformColumn.IncludeOnType; import org.jumpmind.symmetric.io.data.transform.TransformColumnException; @@ -95,6 +96,7 @@ public static Map> buildDefaultColumnTransforms() { columnTransforms.put(LookupColumnTransform.NAME, new LookupColumnTransform()); columnTransforms.put(RemoveColumnTransform.NAME, new RemoveColumnTransform()); columnTransforms.put(MathColumnTransform.NAME, new MathColumnTransform()); + columnTransforms.put(ValueMapColumnTransform.NAME, new ValueMapColumnTransform()); return columnTransforms; }