001: /*
002: * $Id: Convert.java,v 1.1 2003/08/17 06:06:11 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.operation;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030:
031: /**
032: * Convert the current field from the in-map and place it in the out-map
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.1 $
036: * @since 2.0
037: */
038: public class Convert extends SimpleMapOperation {
039:
040: String toField;
041: String type;
042: boolean replace = true;
043: boolean setIfNull = true;
044: String format;
045:
046: public Convert(Element element, SimpleMapProcess simpleMapProcess) {
047: super (element, simpleMapProcess);
048: this .toField = element.getAttribute("to-field");
049: if (this .toField == null || this .toField.length() == 0) {
050: this .toField = this .fieldName;
051: }
052:
053: type = element.getAttribute("type");
054: // if anything but false it will be true
055: replace = !"false".equals(element.getAttribute("replace"));
056: // if anything but false it will be true
057: setIfNull = !"false"
058: .equals(element.getAttribute("set-if-null"));
059:
060: format = element.getAttribute("format");
061: }
062:
063: public void exec(Map inMap, Map results, List messages,
064: Locale locale, ClassLoader loader) {
065: Object fieldObject = inMap.get(fieldName);
066:
067: if (fieldObject == null) {
068: if (setIfNull && (replace || !results.containsKey(toField)))
069: results.put(toField, null);
070: return;
071: }
072:
073: // if an incoming string is empty,
074: // set to null if setIfNull is true, otherwise do nothing, ie treat as if null
075: if (fieldObject instanceof java.lang.String) {
076: if (((String) fieldObject).length() == 0) {
077: if (setIfNull
078: && (replace || !results.containsKey(toField)))
079: results.put(toField, null);
080: return;
081: }
082: }
083:
084: Object convertedObject = null;
085:
086: try {
087: convertedObject = ObjectType.simpleTypeConvert(fieldObject,
088: type, format, locale);
089: } catch (GeneralException e) {
090: messages.add(e.getMessage());
091: return;
092: }
093:
094: if (convertedObject == null)
095: return;
096:
097: if (replace) {
098: results.put(toField, convertedObject);
099: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
100: } else {
101: if (results.containsKey(toField)) {// do nothing
102: } else {
103: results.put(toField, convertedObject);
104: // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
105: }
106: }
107: }
108: }
|