01: /*
02: * $Id: SetPkFields.java,v 1.1 2003/08/17 06:06:11 ajzeneski Exp $
03: *
04: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * 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.entityops;
25:
26: import java.util.Map;
27:
28: import org.ofbiz.base.util.Debug;
29: import org.ofbiz.entity.GenericValue;
30: import org.ofbiz.minilang.SimpleMethod;
31: import org.ofbiz.minilang.method.ContextAccessor;
32: import org.ofbiz.minilang.method.MethodContext;
33: import org.ofbiz.minilang.method.MethodOperation;
34: import org.w3c.dom.Element;
35:
36: /**
37: * Looks for each PK field in the named map and if it exists there it will copy it into the named value object.
38: *
39: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
40: * @version $Revision: 1.1 $
41: * @since 2.0
42: */
43: public class SetPkFields extends MethodOperation {
44:
45: public static final String module = SetPkFields.class.getName();
46:
47: ContextAccessor valueAcsr;
48: ContextAccessor mapAcsr;
49: String setIfNullStr;
50:
51: public SetPkFields(Element element, SimpleMethod simpleMethod) {
52: super (element, simpleMethod);
53: valueAcsr = new ContextAccessor(element
54: .getAttribute("value-name"));
55: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
56: setIfNullStr = element.getAttribute("set-if-null");
57: }
58:
59: public boolean exec(MethodContext methodContext) {
60: // if anything but false it will be true
61: boolean setIfNull = !"false".equals(methodContext
62: .expandString(setIfNullStr));
63:
64: GenericValue value = (GenericValue) valueAcsr
65: .get(methodContext);
66: if (value == null) {
67: String errMsg = "In set-pk-fields a value was not found with the specified valueAcsr: "
68: + valueAcsr + ", not setting fields";
69:
70: Debug.logWarning(errMsg, module);
71: if (methodContext.getMethodType() == MethodContext.EVENT) {
72: methodContext.putEnv(simpleMethod
73: .getEventErrorMessageName(), errMsg);
74: methodContext.putEnv(simpleMethod
75: .getEventResponseCodeName(), simpleMethod
76: .getDefaultErrorCode());
77: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
78: methodContext.putEnv(simpleMethod
79: .getServiceErrorMessageName(), errMsg);
80: methodContext.putEnv(simpleMethod
81: .getServiceResponseMessageName(), simpleMethod
82: .getDefaultErrorCode());
83: }
84: return false;
85: }
86:
87: Map theMap = (Map) mapAcsr.get(methodContext);
88: if (theMap == null) {
89: Debug.logWarning(
90: "In set-pk-fields could not find map with name "
91: + mapAcsr + ", not setting any fields",
92: module);
93: } else {
94: value.setPKFields(theMap, setIfNull);
95: }
96: return true;
97: }
98: }
|