01: /*
02: * $Id: SetNonpkFields.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 non-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 SetNonpkFields extends MethodOperation {
44:
45: public static final String module = SetNonpkFields.class.getName();
46:
47: ContextAccessor valueAcsr;
48: ContextAccessor mapAcsr;
49: String setIfNullStr;
50:
51: public SetNonpkFields(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-nonpk-fields a value was not found with the specified valueAcsr: "
68: + valueAcsr + ", not setting fields";
69: Debug.logWarning(errMsg, module);
70: if (methodContext.getMethodType() == MethodContext.EVENT) {
71: methodContext.putEnv(simpleMethod
72: .getEventErrorMessageName(), errMsg);
73: methodContext.putEnv(simpleMethod
74: .getEventResponseCodeName(), simpleMethod
75: .getDefaultErrorCode());
76: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
77: methodContext.putEnv(simpleMethod
78: .getServiceErrorMessageName(), errMsg);
79: methodContext.putEnv(simpleMethod
80: .getServiceResponseMessageName(), simpleMethod
81: .getDefaultErrorCode());
82: }
83: return false;
84: }
85:
86: Map theMap = (Map) mapAcsr.get(methodContext);
87: if (theMap == null) {
88: Debug.logWarning(
89: "In set-nonpk-fields could not find map with name "
90: + mapAcsr + ", not setting any fields",
91: module);
92: } else {
93: value.setNonPKFields(theMap, setIfNull);
94: }
95: return true;
96: }
97: }
|