001: /*
002: * $Id: IfEmpty.java,v 1.1 2003/08/17 06:06:13 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 IfEmpty extends MethodOperation {
041:
042: public static final String module = IfEmpty.class.getName();
043:
044: List subOps = new LinkedList();
045: List elseSubOps = null;
046:
047: ContextAccessor mapAcsr;
048: ContextAccessor fieldAcsr;
049:
050: public IfEmpty(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: if (elseElement != null) {
062: elseSubOps = new LinkedList();
063: SimpleMethod.readOperations(elseElement, elseSubOps,
064: simpleMethod);
065: }
066: }
067:
068: public boolean exec(MethodContext methodContext) {
069: // if conditions fails, always return true; if a sub-op returns false
070: // return false and stop, otherwise return true
071: // return true;
072:
073: // only run subOps if element is empty/null
074: boolean runSubOps = false;
075: Object fieldVal = null;
076:
077: if (!mapAcsr.isEmpty()) {
078: Map fromMap = (Map) mapAcsr.get(methodContext);
079: if (fromMap == null) {
080: if (Debug.infoOn())
081: Debug.logInfo("Map not found with name " + mapAcsr
082: + ", running operations", 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: runSubOps = true;
093: } else {
094: if (fieldVal instanceof String) {
095: String fieldStr = (String) fieldVal;
096:
097: if (fieldStr.length() == 0) {
098: runSubOps = true;
099: }
100: } else if (fieldVal instanceof Collection) {
101: Collection fieldCol = (Collection) fieldVal;
102:
103: if (fieldCol.size() == 0) {
104: runSubOps = true;
105: }
106: } else if (fieldVal instanceof Map) {
107: Map fieldMap = (Map) fieldVal;
108:
109: if (fieldMap.size() == 0) {
110: runSubOps = true;
111: }
112: }
113: }
114:
115: if (runSubOps) {
116: return SimpleMethod.runSubOps(subOps, methodContext);
117: } else {
118: if (elseSubOps != null) {
119: return SimpleMethod
120: .runSubOps(elseSubOps, methodContext);
121: } else {
122: return true;
123: }
124: }
125: }
126: }
|