001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.conditional;
019:
020: import java.util.*;
021: import org.w3c.dom.*;
022: import org.ofbiz.base.util.*;
023: import org.ofbiz.minilang.*;
024: import org.ofbiz.minilang.method.*;
025:
026: /**
027: * Implements compare to a constant condition.
028: */
029: public class EmptyCondition implements Conditional {
030:
031: public static final String module = EmptyCondition.class.getName();
032:
033: SimpleMethod simpleMethod;
034:
035: ContextAccessor mapAcsr;
036: ContextAccessor fieldAcsr;
037:
038: public EmptyCondition(Element element, SimpleMethod simpleMethod) {
039: this .simpleMethod = simpleMethod;
040:
041: this .mapAcsr = new ContextAccessor(element
042: .getAttribute("map-name"));
043: this .fieldAcsr = new ContextAccessor(element
044: .getAttribute("field-name"));
045: }
046:
047: public boolean checkCondition(MethodContext methodContext) {
048: // only run subOps if element is empty/null
049: boolean runSubOps = false;
050: Object fieldVal = getFieldVal(methodContext);
051:
052: if (fieldVal == null) {
053: runSubOps = true;
054: } else {
055: if (fieldVal instanceof String) {
056: String fieldStr = (String) fieldVal;
057:
058: if (fieldStr.length() == 0) {
059: runSubOps = true;
060: }
061: } else if (fieldVal instanceof Collection) {
062: Collection fieldCol = (Collection) fieldVal;
063:
064: if (fieldCol.size() == 0) {
065: runSubOps = true;
066: }
067: } else if (fieldVal instanceof Map) {
068: Map fieldMap = (Map) fieldVal;
069:
070: if (fieldMap.size() == 0) {
071: runSubOps = true;
072: }
073: }
074: }
075:
076: return runSubOps;
077: }
078:
079: protected Object getFieldVal(MethodContext methodContext) {
080: Object fieldVal = null;
081: if (!mapAcsr.isEmpty()) {
082: Map fromMap = (Map) mapAcsr.get(methodContext);
083: if (fromMap == null) {
084: if (Debug.infoOn())
085: Debug.logInfo("Map not found with name " + mapAcsr
086: + ", running operations", module);
087: } else {
088: fieldVal = fieldAcsr.get(fromMap, methodContext);
089: }
090: } else {
091: // no map name, try the env
092: fieldVal = fieldAcsr.get(methodContext);
093: }
094: return fieldVal;
095: }
096:
097: public void prettyPrint(StringBuffer messageBuffer,
098: MethodContext methodContext) {
099: messageBuffer.append("empty[");
100: if (!this .mapAcsr.isEmpty()) {
101: messageBuffer.append(this .mapAcsr);
102: messageBuffer.append(".");
103: }
104: messageBuffer.append(this .fieldAcsr);
105: if (methodContext != null) {
106: messageBuffer.append("=");
107: messageBuffer.append(getFieldVal(methodContext));
108: }
109: messageBuffer.append("]");
110: }
111: }
|