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.entity.condition;
019:
020: import java.sql.Timestamp;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.UtilDateTime;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericModelException;
027: import org.ofbiz.entity.config.DatasourceInfo;
028: import org.ofbiz.entity.model.ModelEntity;
029:
030: public class EntityDateFilterCondition extends EntityCondition {
031:
032: protected String fromDateName;
033: protected String thruDateName;
034:
035: public EntityDateFilterCondition(String fromDateName,
036: String thruDateName) {
037: this .fromDateName = fromDateName;
038: this .thruDateName = thruDateName;
039: }
040:
041: public String makeWhereString(ModelEntity modelEntity,
042: List entityConditionParams, DatasourceInfo datasourceInfo) {
043: EntityCondition condition = makeCondition();
044: return condition.makeWhereString(modelEntity,
045: entityConditionParams, datasourceInfo);
046: }
047:
048: public void checkCondition(ModelEntity modelEntity)
049: throws GenericModelException {
050: EntityCondition condition = makeCondition();
051: condition.checkCondition(modelEntity);
052: }
053:
054: public boolean mapMatches(GenericDelegator delegator, Map map) {
055: EntityCondition condition = makeCondition();
056: return condition.mapMatches(delegator, map);
057: }
058:
059: public boolean equals(Object obj) {
060: if (!(obj instanceof EntityDateFilterCondition))
061: return false;
062: EntityDateFilterCondition other = (EntityDateFilterCondition) obj;
063: return equals(fromDateName, other.fromDateName)
064: && equals(thruDateName, other.thruDateName);
065: }
066:
067: public int hashCode() {
068: return hashCode(fromDateName) ^ hashCode(thruDateName);
069: }
070:
071: public void visit(EntityConditionVisitor visitor) {
072: visitor.acceptEntityDateFilterCondition(this );
073: }
074:
075: public void accept(EntityConditionVisitor visitor) {
076: visitor.acceptEntityDateFilterCondition(this );
077: }
078:
079: public EntityCondition freeze() {
080: return this ;
081: }
082:
083: public void encryptConditionFields(ModelEntity modelEntity,
084: GenericDelegator delegator) {
085: // nothing to do here...
086: }
087:
088: protected EntityCondition makeCondition() {
089: return makeCondition(UtilDateTime.nowTimestamp(), fromDateName,
090: thruDateName);
091: }
092:
093: public static EntityExpr makeCondition(Timestamp moment,
094: String fromDateName, String thruDateName) {
095: return new EntityExpr(new EntityExpr(new EntityExpr(
096: thruDateName, EntityOperator.EQUALS, null),
097: EntityOperator.OR, new EntityExpr(thruDateName,
098: EntityOperator.GREATER_THAN, moment)),
099: EntityOperator.AND, new EntityExpr(new EntityExpr(
100: fromDateName, EntityOperator.EQUALS, null),
101: EntityOperator.OR, new EntityExpr(fromDateName,
102: EntityOperator.LESS_THAN_EQUAL_TO,
103: moment)));
104: }
105: }
|