1 24 package org.ofbiz.minilang.operation; 25 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.GeneralException; 32 import org.ofbiz.base.util.ObjectType; 33 import org.w3c.dom.Element ; 34 35 42 public class Convert extends SimpleMapOperation { 43 public static final String module = Convert.class.getName(); 44 45 String toField; 46 String type; 47 boolean replace = true; 48 boolean setIfNull = true; 49 String format; 50 51 public Convert(Element element, SimpleMapProcess simpleMapProcess) { 52 super(element, simpleMapProcess); 53 this.toField = element.getAttribute("to-field"); 54 if (this.toField == null || this.toField.length() == 0) { 55 this.toField = this.fieldName; 56 } 57 58 type = element.getAttribute("type"); 59 replace = !"false".equals(element.getAttribute("replace")); 61 setIfNull = !"false".equals(element.getAttribute("set-if-null")); 63 64 format = element.getAttribute("format"); 65 } 66 67 public void exec(Map inMap, Map results, List messages, Locale locale, ClassLoader loader) { 68 Object fieldObject = inMap.get(fieldName); 69 70 if (fieldObject == null) { 71 if (setIfNull && (replace || !results.containsKey(toField))) 72 results.put(toField, null); 73 return; 74 } 75 76 if (fieldObject instanceof java.lang.String ) { 79 if (((String ) fieldObject).length() == 0) { 80 if (setIfNull && (replace || !results.containsKey(toField))) 81 results.put(toField, null); 82 return; 83 } 84 } 85 86 Object convertedObject = null; 87 88 try { 89 convertedObject = ObjectType.simpleTypeConvert(fieldObject, type, format, locale); 90 } catch (GeneralException e) { 91 addMessage(messages, loader, locale); 92 Debug.logError(e, "Error in convert simple-map-processor operation: " + e.toString(), module); 93 return; 94 } 95 96 if (convertedObject == null) 97 return; 98 99 if (replace) { 100 results.put(toField, convertedObject); 101 } else { 103 if (results.containsKey(toField)) { } else { 105 results.put(toField, convertedObject); 106 } 108 } 109 } 110 } 111 | Popular Tags |