01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.method.entityops;
19:
20: import java.util.Map;
21:
22: import org.ofbiz.base.util.Debug;
23: import org.ofbiz.entity.GenericValue;
24: import org.ofbiz.minilang.SimpleMethod;
25: import org.ofbiz.minilang.method.ContextAccessor;
26: import org.ofbiz.minilang.method.MethodContext;
27: import org.ofbiz.minilang.method.MethodOperation;
28: import org.w3c.dom.Element;
29:
30: /**
31: * Looks for each PK field in the named map and if it exists there it will copy it into the named value object.
32: */
33: public class SetPkFields extends MethodOperation {
34:
35: public static final String module = SetPkFields.class.getName();
36:
37: ContextAccessor valueAcsr;
38: ContextAccessor mapAcsr;
39: String setIfNullStr;
40:
41: public SetPkFields(Element element, SimpleMethod simpleMethod) {
42: super (element, simpleMethod);
43: valueAcsr = new ContextAccessor(element
44: .getAttribute("value-name"));
45: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
46: setIfNullStr = element.getAttribute("set-if-null");
47: }
48:
49: public boolean exec(MethodContext methodContext) {
50: // if anything but false it will be true
51: boolean setIfNull = !"false".equals(methodContext
52: .expandString(setIfNullStr));
53:
54: GenericValue value = (GenericValue) valueAcsr
55: .get(methodContext);
56: if (value == null) {
57: String errMsg = "In set-pk-fields a value was not found with the specified valueAcsr: "
58: + valueAcsr + ", not setting fields";
59:
60: Debug.logWarning(errMsg, module);
61: if (methodContext.getMethodType() == MethodContext.EVENT) {
62: methodContext.putEnv(simpleMethod
63: .getEventErrorMessageName(), errMsg);
64: methodContext.putEnv(simpleMethod
65: .getEventResponseCodeName(), simpleMethod
66: .getDefaultErrorCode());
67: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
68: methodContext.putEnv(simpleMethod
69: .getServiceErrorMessageName(), errMsg);
70: methodContext.putEnv(simpleMethod
71: .getServiceResponseMessageName(), simpleMethod
72: .getDefaultErrorCode());
73: }
74: return false;
75: }
76:
77: Map theMap = (Map) mapAcsr.get(methodContext);
78: if (theMap == null) {
79: Debug.logWarning(
80: "In set-pk-fields could not find map with name "
81: + mapAcsr + ", not setting any fields",
82: module);
83: } else {
84: value.setPKFields(theMap, setIfNull);
85: }
86: return true;
87: }
88:
89: public String rawString() {
90: // TODO: something more than the empty tag
91: return "<set-pk-fields/>";
92: }
93:
94: public String expandedString(MethodContext methodContext) {
95: // TODO: something more than a stub/dummy
96: return this.rawString();
97: }
98: }
|