I personally think that manually creating 30+ copy conditions is time wasting and heavy on future code maintenance. Copy conditions cannot be seen at a glance in WM Developer so you won’t really know what the code does unless you click on each and every map and check the contents of every copy condition. So with the risk of starting a religious war, I believe that copy conditions should be generally avoided if possible.
With that being said, I created a quick (and really dirty) Java service that will remove or replace all null or whitespace values in a document or document arrray, optionally replacing them with a string instead of just dropping the values.
So I’m suggesting the following approach:
- Do your map from document x to document y
- Call a service to remove all empty or null values in y
The code does not support subdocuments though, so you’ll have to call the java service for each subdocument.
Input:
replaceEmptyValuesIn/document (optional document)
replaceEmptyValuesIn/inputDocArray (optional document array)
replaceEmptyValuesIn/replaceWith (optional replacement string, rather than removing empty fields)
replaceEmptyValuesIn/checkEmpty (true/false string, empty string check)
replaceEmptyValuesIn/checkEoln (true/false string, line end check)
Either document or document array, but not both, are allowed at the same time.
Output:
replaceEmptyValuesOut/document (corrected document)
replaceEmptyValuesOut/inputDocArray (corrected input doc array)
Java code (should really be rewritten, it was created in something like half an hour but seems to work):
IDataCursor baseCursor = pipeline.getCursor();
IData inputScopeDoc = IDataUtil.getIData(baseCursor, "replaceEmptyValuesIn");
IDataCursor pipelineCursor = inputScopeDoc.getCursor();
IData inputDoc = IDataUtil.getIData(pipelineCursor, "document");
IData[] inputDocArray = IDataUtil.getIDataArray(pipelineCursor, "inputDocArray");
String eolnCheck = IDataUtil.getString(pipelineCursor, "checkEoln");
String checkEmptyStr = IDataUtil.getString(pipelineCursor, "checkEmpty");
String replaceWith = IDataUtil.getString(pipelineCursor, "replaceWith");
boolean checkEoln = false;
boolean checkEmpty = false;
if( eolnCheck != null && eolnCheck.equals( "true" ) ) {
checkEoln = true;
}
if( checkEmptyStr != null && checkEmptyStr.equals("true") ) {
checkEmpty = true;
}
try {
int arrayIndex = 0;
do {
if (inputDocArray != null && inputDocArray.length > arrayIndex ) {
inputDoc = inputDocArray[arrayIndex];
}
IDataCursor curs = inputDoc.getCursor();
if (curs != null) {
boolean moreElements = curs.first();
while( moreElements ) {
Object value = curs.getValue();
String currentKeyName = curs.getKey();
IData[] currentArray = IDataUtil.getIDataArray(curs, currentKeyName);
if (value == null || ( checkEmpty && value.equals("") ) ||
( checkEoln && (value.equals("\r\n") || value.equals("\n") )) ) {
if (replaceWith == null) {
moreElements = curs.delete();
} else {
curs.setValue(replaceWith);
}
} else {
moreElements = curs.next();
}
}
} //while
arrayIndex++;
} while ( inputDocArray != null && arrayIndex < inputDocArray.length);
IData outputDocument = IDataFactory.create();
IDataCursor outputDocCursor = outputDocument.getCursor();
if(inputDocArray == null) {
if(inputDoc != null) {
IDataUtil.put(outputDocCursor, "document", inputDoc);
}
} else {
IDataUtil.put(outputDocCursor, "document", inputDocArray);
}
IDataUtil.put(baseCursor, "replaceEmptyValuesOut", outputDocument);
outputDocCursor.destroy();
} finally {
pipelineCursor.destroy();
baseCursor.destroy();
}