KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > FieldToField


1 /*
2  * $Id: FieldToField.java 5824 2005-09-25 23:18:16Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

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 /**
36  * Copies a map field to a map field
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5824 $
40  * @since 2.0
41  */

42 public class FieldToField extends MethodOperation {
43     
44     public static final String JavaDoc 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         // set toMapAcsr and toFieldAcsr to their defualt values of mapAcsr and fieldAcsr if empty
59
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 JavaDoc 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             // no map name, try the env
81
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         // note that going to an env field will only work if it came from an env
90
// field because if not specified the to-map-name will be set to the map-name
91
// to go from a map field to an env field, use the field-to-env operation
92
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 our map is a GenericValue, convert the input data to the correct type of the GenericValue fields (See OFBIZ-500)
103
if (toMap instanceof GenericValue) {
104                 GenericValue tmpVal = (GenericValue) toMap;
105                 ModelField modelField = tmpVal.getModelEntity().getField(toFieldAcsr.toString());
106                 if (modelField != null) {
107                     try {
108                         // we need the ModelFieldType because it stores the Java lang type of the field (seems to be only way to do this)
109
ModelFieldType type = tmpVal.getDelegator().getEntityFieldType(tmpVal.getModelEntity(), modelField.getType());
110                         if ((type != null) && !(fieldVal.getClass().getName().equals(type.getJavaType()))) {
111                             // convert with null format and locale for now (should add as attributes of field-to-field)
112
// the last argument tells simpleTypeConvert not to throw a GeneralException when the conversion fails (fieldVal will be unchanged)
113
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                         // this should not happen if we pass false to simpleTypeConvert, log anyway
120
Debug.logError(e, "field-to-field conversion error: " + e.getMessage(), module);
121                     }
122                 }
123             }
124             toFieldAcsr.put(toMap, fieldVal, methodContext);
125         } else {
126             // no to-map, so put in env
127
toFieldAcsr.put(methodContext, fieldVal);
128         }
129
130         return true;
131     }
132
133     public String JavaDoc 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 JavaDoc expandedString(MethodContext methodContext) {
137         // TODO: something more than a stub/dummy
138
return this.rawString();
139     }
140 }
141
Popular Tags