Skip to content

Commit

Permalink
0001583: Create new value map column transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanes committed Feb 14, 2014
1 parent 635398e commit a855bec
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
11 changes: 10 additions & 1 deletion symmetric-assemble/src/docbook/configuration.xml
Expand Up @@ -1929,7 +1929,7 @@ a new identity, not copying the actual identity value from the source.
</listitem>

<listitem>
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:
<code>#{COLUMN_NAME}</code>
Expand All @@ -1941,6 +1941,15 @@ is the value of the current source column;
is the old value of the source column for an updated row.
</listitem>


<listitem>
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.
</listitem>
</itemizedlist>
</para>
</section>
Expand Down
@@ -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
* <http://www.gnu.org/licenses/>.
*
* 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<String, String> sourceValues, String newValue, String oldValue)
throws IgnoreColumnException, IgnoreRowException {
return getValue(newValue,column.getTransformExpression());

}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,7 @@ public static Map<String, IColumnTransform<?>> 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;
}

Expand Down

0 comments on commit a855bec

Please sign in to comment.