KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > entityops > SetNonpkFields


1 /*
2  * $Id: SetNonpkFields.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.entityops;
25
26 import java.util.Map JavaDoc;
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 JavaDoc;
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 $Rev: 5462 $
41  * @since 2.0
42  */

43 public class SetNonpkFields extends MethodOperation {
44     
45     public static final String JavaDoc module = SetNonpkFields.class.getName();
46     
47     ContextAccessor valueAcsr;
48     ContextAccessor mapAcsr;
49     String JavaDoc setIfNullStr;
50
51     public SetNonpkFields(Element JavaDoc element, SimpleMethod simpleMethod) {
52         super(element, simpleMethod);
53         valueAcsr = new ContextAccessor(element.getAttribute("value-name"));
54         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
55         setIfNullStr = element.getAttribute("set-if-null");
56     }
57
58     public boolean exec(MethodContext methodContext) {
59         // if anything but false it will be true
60
boolean setIfNull = !"false".equals(methodContext.expandString(setIfNullStr));
61         
62         GenericValue value = (GenericValue) valueAcsr.get(methodContext);
63         if (value == null) {
64             String JavaDoc errMsg = "In set-nonpk-fields a value was not found with the specified valueAcsr: " + valueAcsr + ", not setting fields";
65             Debug.logWarning(errMsg, module);
66             if (methodContext.getMethodType() == MethodContext.EVENT) {
67                 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
68                 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
69             } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
70                 methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
71                 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
72             }
73             return false;
74         }
75
76         Map JavaDoc theMap = (Map JavaDoc) mapAcsr.get(methodContext);
77         if (theMap == null) {
78             Debug.logWarning("In set-nonpk-fields could not find map with name " + mapAcsr + ", not setting any fields", module);
79         } else {
80             value.setNonPKFields(theMap, setIfNull);
81         }
82         return true;
83     }
84
85     public String JavaDoc rawString() {
86         // TODO: something more than the empty tag
87
return "<set-nonpk-fields/>";
88     }
89     public String JavaDoc expandedString(MethodContext methodContext) {
90         // TODO: something more than a stub/dummy
91
return this.rawString();
92     }
93 }
94
Popular Tags