001: /*
002: * $Id: EmptyCondition.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.conditional;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028: import org.ofbiz.base.util.*;
029: import org.ofbiz.minilang.*;
030: import org.ofbiz.minilang.method.*;
031:
032: /**
033: * Implements compare to a constant condition.
034: *
035: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: * @version $Revision: 1.1 $
037: * @since 2.1
038: */
039: public class EmptyCondition implements Conditional {
040:
041: public static final String module = EmptyCondition.class.getName();
042:
043: SimpleMethod simpleMethod;
044:
045: ContextAccessor mapAcsr;
046: ContextAccessor fieldAcsr;
047:
048: public EmptyCondition(Element element, SimpleMethod simpleMethod) {
049: this .simpleMethod = simpleMethod;
050:
051: this .mapAcsr = new ContextAccessor(element
052: .getAttribute("map-name"));
053: this .fieldAcsr = new ContextAccessor(element
054: .getAttribute("field-name"));
055: }
056:
057: public boolean checkCondition(MethodContext methodContext) {
058: // only run subOps if element is empty/null
059: boolean runSubOps = false;
060: Object fieldVal = null;
061:
062: if (!mapAcsr.isEmpty()) {
063: Map fromMap = (Map) mapAcsr.get(methodContext);
064: if (fromMap == null) {
065: if (Debug.infoOn())
066: Debug.logInfo("Map not found with name " + mapAcsr
067: + ", running operations", module);
068: } else {
069: fieldVal = fieldAcsr.get(fromMap, methodContext);
070: }
071: } else {
072: // no map name, try the env
073: fieldVal = fieldAcsr.get(methodContext);
074: }
075:
076: if (fieldVal == null) {
077: runSubOps = true;
078: } else {
079: if (fieldVal instanceof String) {
080: String fieldStr = (String) fieldVal;
081:
082: if (fieldStr.length() == 0) {
083: runSubOps = true;
084: }
085: } else if (fieldVal instanceof Collection) {
086: Collection fieldCol = (Collection) fieldVal;
087:
088: if (fieldCol.size() == 0) {
089: runSubOps = true;
090: }
091: } else if (fieldVal instanceof Map) {
092: Map fieldMap = (Map) fieldVal;
093:
094: if (fieldMap.size() == 0) {
095: runSubOps = true;
096: }
097: }
098: }
099:
100: return runSubOps;
101: }
102: }
|