Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
0001604: Create a set of two transforms that can map a series of colu…
…mns to a series of rows based on a Map
  • Loading branch information
mhanes committed Feb 25, 2014
1 parent 7243089 commit 5042aff
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 4 deletions.
@@ -0,0 +1,93 @@
/**
* 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.symmetric.io.data.DataContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ColumnsToRowsKeyColumnTransform implements IMultipleValueColumnTransform {

public final static String NAME = "columnsToRowsKeyColumnTransform";

public final static String CONTEXT_MAP="Map";
public final static String CONTEXT_PK_COLUMN="PKColumn";

private static final Logger logger = LoggerFactory.getLogger(ColumnsToRowsKeyColumnTransform.class);

public String getName() {
return NAME;
}

public static String getContextBase(String transformId) {
return NAME + ":" + transformId + ":";
}

public boolean isExtractColumnTransform() {
return true;
}

public boolean isLoadColumnTransform() {
return true;
}

public List<String> transform(IDatabasePlatform platform, DataContext context, TransformColumn column,
TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue)
throws IgnoreRowException {

if (StringUtils.trimToNull(column.getTransformExpression()) == null) {
throw new RuntimeException("Transform configured incorrectly. A map reprenting PK and column names must be defined as part of the transform expression");
}
String mapAsString = StringUtils.trimToEmpty(column.getTransformExpression());

// Build reverse map, while also building up array to return

List<String> result = new ArrayList<String>();
Map<String,String> reverseMap = new HashMap<String,String>();

StringTokenizer tokens = new StringTokenizer(mapAsString);

while (tokens.hasMoreElements()) {
String keyValue = (String) tokens.nextElement();
int equalIndex = keyValue.indexOf("=");
if (equalIndex != -1) {
reverseMap.put(keyValue.substring(equalIndex+1),keyValue.substring(0, equalIndex));
result.add(keyValue.substring(equalIndex+1));
} else {
throw new RuntimeException("Map string for "+column.getTransformExpression()+" is invalid format: "+mapAsString);
}

}

context.put(getContextBase(column.getTransformId())+CONTEXT_MAP, reverseMap);
context.put(getContextBase(column.getTransformId())+CONTEXT_PK_COLUMN, column.getTargetColumnName());

return result;
}
}
@@ -0,0 +1,81 @@
/**
* 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 org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.symmetric.io.data.DataContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ColumnsToRowsValueColumnTransform implements ISingleValueColumnTransform {

private static final Logger logger = LoggerFactory.getLogger(ColumnsToRowsValueColumnTransform.class);

public final static String NAME = "columnsToRowsValueColumnTransform";


public String getName() {
return NAME;
}

public boolean isExtractColumnTransform() {
return true;
}

public boolean isLoadColumnTransform() {
return true;
}

public String transform(IDatabasePlatform platform, DataContext context, TransformColumn column,
TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue)
throws IgnoreRowException {

String contextBase = ColumnsToRowsKeyColumnTransform.getContextBase(column.getTransformId());


@SuppressWarnings("unchecked")
Map<String, String> reverseMap = (Map<String, String>) context.get(contextBase+ColumnsToRowsKeyColumnTransform.CONTEXT_MAP);
String pkColumnName = (String) context.get(contextBase+ColumnsToRowsKeyColumnTransform.CONTEXT_PK_COLUMN);

if (reverseMap == null) {
throw new RuntimeException("Reverse map not found in context as key " + contextBase+ColumnsToRowsKeyColumnTransform.CONTEXT_MAP+ " Unable to transform.");
}
if (pkColumnName == null) {
throw new RuntimeException("Primary key column name not found in context as key " + contextBase+ColumnsToRowsKeyColumnTransform.CONTEXT_PK_COLUMN+" Unable to transform.");
}

String pkValue = data.getTargetKeyValues().get(pkColumnName);
String value = null;

if (pkValue!=null) {
value = reverseMap.get(pkValue);
if (value!=null) {
return data.getSourceValues().get(value);
} else {
throw new RuntimeException("Unable to locate column name for pk value "+pkValue);
}
} else {
throw new RuntimeException("Unable to locate column with pk name "+pkColumnName+" in target values.");
}
}
}
Expand Up @@ -29,8 +29,7 @@
/**
* An extension point that can be implemented to provide custom transformation
* logic. Column transforms are stateless and so should not keep references to
* objects as attributes (Don't use ISymmetricEngineAware in a multi-homed
* environment because you might end up with a handle to the wrong engine.)
* objects as attributes.
*/
public interface IColumnTransform<T> extends IExtensionPoint {

Expand Down
Expand Up @@ -36,6 +36,8 @@
import org.jumpmind.symmetric.io.data.IDataWriter;
import org.jumpmind.symmetric.io.data.transform.AdditiveColumnTransform;
import org.jumpmind.symmetric.io.data.transform.BshColumnTransform;
import org.jumpmind.symmetric.io.data.transform.ColumnsToRowsKeyColumnTransform;
import org.jumpmind.symmetric.io.data.transform.ColumnsToRowsValueColumnTransform;
import org.jumpmind.symmetric.io.data.transform.ConstantColumnTransform;
import org.jumpmind.symmetric.io.data.transform.CopyColumnTransform;
import org.jumpmind.symmetric.io.data.transform.DeleteAction;
Expand All @@ -50,13 +52,13 @@
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;
import org.jumpmind.symmetric.io.data.transform.TransformPoint;
import org.jumpmind.symmetric.io.data.transform.TransformTable;
import org.jumpmind.symmetric.io.data.transform.TransformedData;
import org.jumpmind.symmetric.io.data.transform.ValueMapColumnTransform;
import org.jumpmind.symmetric.io.data.transform.VariableColumnTransform;
import org.jumpmind.util.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -97,6 +99,8 @@ public static Map<String, IColumnTransform<?>> buildDefaultColumnTransforms() {
columnTransforms.put(RemoveColumnTransform.NAME, new RemoveColumnTransform());
columnTransforms.put(MathColumnTransform.NAME, new MathColumnTransform());
columnTransforms.put(ValueMapColumnTransform.NAME, new ValueMapColumnTransform());
columnTransforms.put(ColumnsToRowsKeyColumnTransform.NAME, new ColumnsToRowsKeyColumnTransform());
columnTransforms.put(ColumnsToRowsValueColumnTransform.NAME, new ColumnsToRowsValueColumnTransform());
return columnTransforms;
}

Expand Down

0 comments on commit 5042aff

Please sign in to comment.