001: /*
002: * $Id: IfNotEmpty.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.ifops;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * Iff the specified field is not empty process sub-operations
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.1 $
038: * @since 2.0
039: */
040: public class IfNotEmpty extends MethodOperation {
041:
042: public static final String module = IfNotEmpty.class.getName();
043:
044: List subOps = new LinkedList();
045: List elseSubOps = null;
046:
047: ContextAccessor mapAcsr;
048: ContextAccessor fieldAcsr;
049:
050: public IfNotEmpty(Element element, SimpleMethod simpleMethod) {
051: super (element, simpleMethod);
052: this .mapAcsr = new ContextAccessor(element
053: .getAttribute("map-name"));
054: this .fieldAcsr = new ContextAccessor(element
055: .getAttribute("field-name"));
056:
057: SimpleMethod.readOperations(element, subOps, simpleMethod);
058:
059: Element elseElement = UtilXml
060: .firstChildElement(element, "else");
061:
062: if (elseElement != null) {
063: elseSubOps = new LinkedList();
064: SimpleMethod.readOperations(elseElement, elseSubOps,
065: simpleMethod);
066: }
067: }
068:
069: public boolean exec(MethodContext methodContext) {
070: // if conditions fails, always return true; if a sub-op returns false
071: // return false and stop, otherwise return true
072: // return true;
073:
074: Object fieldVal = null;
075:
076: if (!mapAcsr.isEmpty()) {
077: Map fromMap = (Map) mapAcsr.get(methodContext);
078: if (fromMap == null) {
079: if (Debug.verboseOn())
080: Debug.logVerbose("Map not found with name "
081: + mapAcsr + ", not running operations",
082: module);
083: } else {
084: fieldVal = fieldAcsr.get(fromMap, methodContext);
085: }
086: } else {
087: // no map name, try the env
088: fieldVal = fieldAcsr.get(methodContext);
089: }
090:
091: if (fieldVal == null) {
092: if (Debug.verboseOn())
093: Debug.logVerbose("Field value not found with name "
094: + fieldAcsr + " in Map with name " + mapAcsr
095: + ", not running operations", module);
096: }
097:
098: // only run subOps if element is not empty/null
099: boolean runSubOps = false;
100: if (fieldVal != null) {
101: if (fieldVal instanceof String) {
102: String fieldStr = (String) fieldVal;
103:
104: if (fieldStr.length() > 0) {
105: runSubOps = true;
106: }
107: } else if (fieldVal instanceof Collection) {
108: Collection fieldCol = (Collection) fieldVal;
109:
110: if (fieldCol.size() > 0) {
111: runSubOps = true;
112: }
113: } else if (fieldVal instanceof Map) {
114: Map fieldMap = (Map) fieldVal;
115:
116: if (fieldMap.size() > 0) {
117: runSubOps = true;
118: }
119: } else {
120: runSubOps = true;
121: }
122: }
123:
124: if (runSubOps) {
125: // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Running if operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
126: return SimpleMethod.runSubOps(subOps, methodContext);
127: } else {
128: if (elseSubOps != null) {
129: // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Running else operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
130: return SimpleMethod
131: .runSubOps(elseSubOps, methodContext);
132: } else {
133: // if (Debug.verboseOn()) Debug.logVerbose("IfNotEmpty: Not Running any operations mapAcsr=" + mapAcsr + " fieldAcsr=" + fieldAcsr, module);
134: return true;
135: }
136: }
137: }
138: }
|