001: /*
002: * $Id: EntityComparisonOperator.java,v 1.1 2003/11/05 12:08:00 jonesde Exp $
003: *
004: * Copyright (c) 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:
025: package org.ofbiz.entity.condition;
026:
027: import java.util.List;
028:
029: import org.apache.oro.text.regex.MalformedPatternException;
030: import org.apache.oro.text.regex.Pattern;
031: import org.apache.oro.text.regex.PatternCompiler;
032: import org.apache.oro.text.regex.PatternMatcher;
033: import org.apache.oro.text.regex.Perl5Compiler;
034: import org.apache.oro.text.regex.Perl5Matcher;
035: import org.apache.oro.text.perl.Perl5Util;
036:
037: /**
038: * Encapsulates operations between entities and entity fields. This is a immutable class.
039: *
040: * @author <a href="mailto:adam@doogie.org">Adam Heath</a>
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @version $Revision: 1.1 $
043: * @since 3.0
044: */
045: public class EntityComparisonOperator extends EntityOperator {
046:
047: protected static PatternMatcher matcher = new Perl5Matcher();
048: protected static Perl5Util perl5Util = new Perl5Util();
049: protected static PatternCompiler compiler = new Perl5Compiler();
050:
051: public static Pattern makeOroPattern(String sqlLike) {
052: sqlLike = perl5Util
053: .substitute("s/([$^.+*?])/\\\\$1/g", sqlLike);
054: sqlLike = perl5Util.substitute("s/%/.*/g", sqlLike);
055: sqlLike = perl5Util.substitute("s/_/./g", sqlLike);
056: try {
057: return compiler.compile(sqlLike);
058: } catch (MalformedPatternException e) {
059: e.printStackTrace();
060: }
061: return null;
062: }
063:
064: public boolean compare(Object lhs, Object rhs) {
065: throw new UnsupportedOperationException(codeString);
066: }
067:
068: public EntityComparisonOperator(int id, String code) {
069: super (id, code);
070: }
071:
072: public static final boolean compareEqual(Object lhs, Object rhs) {
073: if (lhs == null) {
074: if (rhs != null) {
075: return false;
076: }
077: } else if (!lhs.equals(rhs)) {
078: return false;
079: }
080: return true;
081: }
082:
083: public static final boolean compareNotEqual(Object lhs, Object rhs) {
084: if (lhs == null) {
085: if (rhs == null) {
086: return false;
087: }
088: } else if (lhs.equals(rhs)) {
089: return false;
090: }
091: return true;
092: }
093:
094: public static final boolean compareGreaterThan(Object lhs,
095: Object rhs) {
096: if (lhs == null) {
097: if (rhs != null) {
098: return false;
099: }
100: } else if (((Comparable) lhs).compareTo(rhs) <= 0) {
101: return false;
102: }
103: return true;
104: }
105:
106: public static final boolean compareGreaterThanEqualTo(Object lhs,
107: Object rhs) {
108: if (lhs == null) {
109: if (rhs != null) {
110: return false;
111: }
112: } else if (((Comparable) lhs).compareTo(rhs) < 0) {
113: return false;
114: }
115: return true;
116: }
117:
118: public static final boolean compareLessThan(Object lhs, Object rhs) {
119: if (lhs == null) {
120: if (rhs != null) {
121: return false;
122: }
123: } else if (((Comparable) lhs).compareTo(rhs) >= 0) {
124: return false;
125: }
126: return true;
127: }
128:
129: public static final boolean compareLessThanEqualTo(Object lhs,
130: Object rhs) {
131: if (lhs == null) {
132: if (rhs != null) {
133: return false;
134: }
135: } else if (((Comparable) lhs).compareTo(rhs) > 0) {
136: return false;
137: }
138: return true;
139: }
140:
141: public static final boolean compareIn(Object lhs, Object rhs) {
142: if (lhs == null) {
143: if (rhs != null) {
144: return false;
145: }
146: return true;
147: } else if (((List) rhs).contains(lhs)) {
148: return true;
149: }
150: return false;
151: }
152:
153: public static final boolean compareLike(Object lhs, Object rhs) {
154: if (lhs == null) {
155: if (rhs != null) {
156: return false;
157: }
158: } else if (lhs instanceof String && rhs instanceof String) {
159: //see if the lhs value is like the rhs value, rhs will have the pattern characters in it...
160: return matcher.matches((String) lhs,
161: makeOroPattern((String) rhs));
162: }
163: return true;
164: }
165: }
|