001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.operation;
019:
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.base.util.GeneralException;
026: import org.ofbiz.base.util.ObjectType;
027: import org.w3c.dom.Element;
028:
029: /**
030: * Convert the current field from the in-map and place it in the out-map
031: */
032: public class Convert extends SimpleMapOperation {
033: public static final String module = Convert.class.getName();
034:
035: String toField;
036: String type;
037: boolean replace = true;
038: boolean setIfNull = true;
039: String format;
040:
041: public Convert(Element element, SimpleMapProcess simpleMapProcess) {
042: super (element, simpleMapProcess);
043: this .toField = element.getAttribute("to-field");
044: if (this .toField == null || this .toField.length() == 0) {
045: this .toField = this .fieldName;
046: }
047:
048: type = element.getAttribute("type");
049: // if anything but false it will be true
050: replace = !"false".equals(element.getAttribute("replace"));
051: // if anything but false it will be true
052: setIfNull = !"false"
053: .equals(element.getAttribute("set-if-null"));
054:
055: format = element.getAttribute("format");
056: }
057:
058: public void exec(Map inMap, Map results, List messages,
059: Locale locale, ClassLoader loader) {
060: Object fieldObject = inMap.get(fieldName);
061:
062: if (fieldObject == null) {
063: if (setIfNull && (replace || !results.containsKey(toField)))
064: results.put(toField, null);
065: return;
066: }
067:
068: // if an incoming string is empty,
069: // set to null if setIfNull is true, otherwise do nothing, ie treat as if null
070: if (fieldObject instanceof java.lang.String) {
071: if (((String) fieldObject).length() == 0) {
072: if (setIfNull
073: && (replace || !results.containsKey(toField)))
074: results.put(toField, null);
075: return;
076: }
077: }
078:
079: Object convertedObject = null;
080:
081: try {
082: convertedObject = ObjectType.simpleTypeConvert(fieldObject,
083: type, format, locale);
084: } catch (GeneralException e) {
085: addMessage(messages, loader, locale);
086: Debug.logError(e,
087: "Error in convert simple-map-processor operation: "
088: + e.toString(), module);
089: return;
090: }
091:
092: if (convertedObject == null)
093: return;
094:
095: if (replace) {
096: results.put(toField, convertedObject);
097: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
098: } else {
099: if (results.containsKey(toField)) {// do nothing
100: } else {
101: results.put(toField, convertedObject);
102: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
103: }
104: }
105: }
106: }
|