KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: IfNotEmpty.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 IfNotEmpty extends MethodOperation {
41     
42     public static final String JavaDoc module = IfNotEmpty.class.getName();
43
44     List subOps = new LinkedList();
45     List elseSubOps = null;
46
47     ContextAccessor mapAcsr;
48     ContextAccessor fieldAcsr;
49
50     public IfNotEmpty(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
59         if (elseElement != null) {
60             elseSubOps = new LinkedList();
61             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
62         }
63     }
64
65     public boolean exec(MethodContext methodContext) {
66         // if conditions fails, always return true; if a sub-op returns false
67
// return false and stop, otherwise return true
68
// return true;
69

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