Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...c/main/java/org/jumpmind/symmetric/io/data/transform/ColumnsToRowsKeyColumnTransform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...main/java/org/jumpmind/symmetric/io/data/transform/ColumnsToRowsValueColumnTransform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters