KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > IfEmpty


1 /*
2  * $Id: IfEmpty.java 5462 2005-08-05 18:35:48Z 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.ifops;
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
33 /**
34  * Iff the specified field is not empty process sub-operations
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.0
39  */

40 public class IfEmpty extends MethodOperation {
41     
42     public static final String JavaDoc module = IfEmpty.class.getName();
43
44     List subOps = new LinkedList();
45     List elseSubOps = null;
46
47     ContextAccessor mapAcsr;
48     ContextAccessor fieldAcsr;
49
50     public IfEmpty(Element element, SimpleMethod simpleMethod) {
51         super(element, simpleMethod);
52         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
53         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
54
55         SimpleMethod.readOperations(element, subOps, simpleMethod);
56
57         Element elseElement = UtilXml.firstChildElement(element, "else");
58         if (elseElement != null) {
59             elseSubOps = new LinkedList();
60             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
61         }
62     }
63
64     public boolean exec(MethodContext methodContext) {
65         // if conditions fails, always return true; if a sub-op returns false
66
// return false and stop, otherwise return true
67
// return true;
68

69         // only run subOps if element is empty/null
70
boolean runSubOps = false;
71         Object JavaDoc fieldVal = null;
72
73         if (!mapAcsr.isEmpty()) {
74             Map fromMap = (Map) mapAcsr.get(methodContext);
75             if (fromMap == null) {
76                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module);
77             } else {
78                 fieldVal = fieldAcsr.get(fromMap, methodContext);
79             }
80         } else {
81             // no map name, try the env
82
fieldVal = fieldAcsr.get(methodContext);
83         }
84         
85         runSubOps = ObjectType.isEmpty(fieldVal);
86
87         if (runSubOps) {
88             return SimpleMethod.runSubOps(subOps, methodContext);
89         } else {
90             if (elseSubOps != null) {
91                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
92             } else {
93                 return true;
94             }
95         }
96     }
97
98     public String JavaDoc rawString() {
99         return "<if-empty field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
100     }
101     public String JavaDoc expandedString(MethodContext methodContext) {
102         // TODO: something more than a stub/dummy
103
return this.rawString();
104     }
105 }
106
Popular Tags