1 24 package org.ofbiz.minilang.method.envops; 25 26 import java.util.*; 27 28 import org.w3c.dom.*; 29 import org.ofbiz.base.util.*; 30 import org.ofbiz.minilang.*; 31 import org.ofbiz.minilang.method.*; 32 import org.ofbiz.entity.*; 33 import org.ofbiz.entity.model.*; 34 35 42 public class FieldToField extends MethodOperation { 43 44 public static final String module = FieldToField.class.getName(); 45 46 ContextAccessor mapAcsr; 47 ContextAccessor fieldAcsr; 48 ContextAccessor toMapAcsr; 49 ContextAccessor toFieldAcsr; 50 51 public FieldToField(Element element, SimpleMethod simpleMethod) { 52 super(element, simpleMethod); 53 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 54 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 55 toMapAcsr = new ContextAccessor(element.getAttribute("to-map-name")); 56 toFieldAcsr = new ContextAccessor(element.getAttribute("to-field-name")); 57 58 if (toMapAcsr.isEmpty()) { 60 toMapAcsr = mapAcsr; 61 } 62 if (toFieldAcsr.isEmpty()) { 63 toFieldAcsr = fieldAcsr; 64 } 65 } 66 67 public boolean exec(MethodContext methodContext) { 68 Object fieldVal = null; 69 70 if (!mapAcsr.isEmpty()) { 71 Map fromMap = (Map) mapAcsr.get(methodContext); 72 73 if (fromMap == null) { 74 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", not copying from this map", module); 75 return true; 76 } 77 78 fieldVal = fieldAcsr.get(fromMap, methodContext); 79 } else { 80 fieldVal = fieldAcsr.get(methodContext); 82 } 83 84 if (fieldVal == null) { 85 if (Debug.verboseOn()) Debug.logVerbose("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not copying field", module); 86 return true; 87 } 88 89 Map toMap = null; 93 94 if (!toMapAcsr.isEmpty()) { 95 toMap = (Map) toMapAcsr.get(methodContext); 96 if (toMap == null) { 97 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + toMapAcsr + ", creating new map", module); 98 toMap = new HashMap(); 99 toMapAcsr.put(methodContext, toMap); 100 } 101 102 if (toMap instanceof GenericValue) { 104 GenericValue tmpVal = (GenericValue) toMap; 105 ModelField modelField = tmpVal.getModelEntity().getField(toFieldAcsr.toString()); 106 if (modelField != null) { 107 try { 108 ModelFieldType type = tmpVal.getDelegator().getEntityFieldType(tmpVal.getModelEntity(), modelField.getType()); 110 if ((type != null) && !(fieldVal.getClass().getName().equals(type.getJavaType()))) { 111 fieldVal = ObjectType.simpleTypeConvert(fieldVal, type.getJavaType(), null, null, false); 114 } 115 } catch (GenericEntityException e) { 116 Debug.logError(e, "Failed to convert " + fieldVal.getClass().getName() + " of from-field input into " 117 + modelField.getType() + " needed by " + tmpVal.getEntityName() + "." + modelField.getName(), module); 118 } catch (GeneralException e) { 119 Debug.logError(e, "field-to-field conversion error: " + e.getMessage(), module); 121 } 122 } 123 } 124 toFieldAcsr.put(toMap, fieldVal, methodContext); 125 } else { 126 toFieldAcsr.put(methodContext, fieldVal); 128 } 129 130 return true; 131 } 132 133 public String rawString() { 134 return "<field-to-field field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\" to-field-name=\"" + this.toFieldAcsr + "\" to-map-name=\"" + this.toMapAcsr + "\"/>"; 135 } 136 public String expandedString(MethodContext methodContext) { 137 return this.rawString(); 139 } 140 } 141 | Popular Tags |