001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.list.structure.filter.field;
016:
017: import java.util.Calendar;
018: import java.util.Comparator;
019: import java.util.Date;
020: import java.util.Map;
021: import org.araneaframework.backend.list.memorybased.Expression;
022: import org.araneaframework.uilib.form.Control;
023: import org.araneaframework.uilib.form.FormElement;
024: import org.araneaframework.uilib.form.constraint.RangeConstraint;
025: import org.araneaframework.uilib.list.structure.filter.FilterContext;
026: import org.araneaframework.uilib.list.util.ExpressionUtil;
027: import org.araneaframework.uilib.list.util.FilterFormUtil;
028: import org.araneaframework.uilib.list.util.FormUtil;
029: import org.araneaframework.uilib.util.Event;
030:
031: public abstract class RangeFilter extends BaseRangeFilter {
032:
033: private static final long serialVersionUID = 1L;
034:
035: private Comparator comparator;
036:
037: public static RangeFilter getInstance(final FilterContext ctx,
038: final String fieldId, final String lowValueId,
039: final String highValueId) {
040: final RangeFilter filter;
041:
042: Class type = ctx.getFieldType(fieldId);
043: if (java.util.Date.class.equals(type)
044: || java.sql.Date.class.isAssignableFrom(type)) {
045: if (ctx.isStrict()) {
046: filter = new DateStrict();
047: } else {
048: filter = new DateNonStrict();
049: }
050: } else {
051: if (ctx.isStrict()) {
052: filter = new Strict();
053: } else {
054: filter = new NonStrict();
055: }
056: }
057:
058: filter.setFieldId(fieldId);
059: filter.setLowValueId(lowValueId);
060: filter.setHighValueId(highValueId);
061:
062: ctx.addInitEvent(new Event() {
063: public void run() {
064: filter.setComparator(ctx.getFieldComparator(fieldId));
065: }
066: });
067:
068: return filter;
069: }
070:
071: public static void addToForm(FilterContext ctx, String lowId,
072: String highId, FormElement lowElement,
073: FormElement highElement) {
074: ctx.getForm().addElement(lowId, lowElement);
075: ctx.getForm().addElement(highId, highElement);
076: FormUtil.addConstraint(ctx.getForm(), new RangeConstraint(
077: lowElement, highElement, true));
078: }
079:
080: public static void addToForm(FilterContext ctx, String lowId,
081: String highId, Control lowControl, Control highControl) {
082: addToForm(ctx, lowId, highId, FilterFormUtil.createElement(ctx,
083: lowId, lowControl), FilterFormUtil.createElement(ctx,
084: highId, highControl));
085: }
086:
087: public static void addToForm(FilterContext ctx, String lowId,
088: String highId) {
089: addToForm(ctx, lowId, highId, FilterFormUtil.createElement(ctx,
090: lowId), FilterFormUtil.createElement(ctx, highId));
091: }
092:
093: public Comparator getComparator() {
094: return comparator;
095: }
096:
097: public void setComparator(Comparator comparator) {
098: this .comparator = comparator;
099: }
100:
101: // General
102:
103: static class Strict extends RangeFilter {
104: public Expression buildExpression(Map filterInfo) {
105: if (!isActive(filterInfo)) {
106: return null;
107: }
108: Expression var = buildVariableExpression();
109: return ExpressionUtil.and(ExpressionUtil.gt(var,
110: buildLowValueExpression(filterInfo),
111: getComparator()), ExpressionUtil.lt(var,
112: buildHighValueExpression(filterInfo),
113: getComparator()));
114: }
115: }
116:
117: static class NonStrict extends RangeFilter {
118: public Expression buildExpression(Map filterInfo) {
119: if (!isActive(filterInfo)) {
120: return null;
121: }
122: Expression var = buildVariableExpression();
123: return ExpressionUtil.and(ExpressionUtil.ge(var,
124: buildLowValueExpression(filterInfo),
125: getComparator()), ExpressionUtil.le(var,
126: buildHighValueExpression(filterInfo),
127: getComparator()));
128: }
129: }
130:
131: // java.util.Date / java.sql.Date
132:
133: static class RightStrict extends RangeFilter {
134: public Expression buildExpression(Map filterInfo) {
135: if (!isActive(filterInfo)) {
136: return null;
137: }
138: Expression var = buildVariableExpression();
139: // Left non-strict and right strict
140: return ExpressionUtil.and(ExpressionUtil.ge(var,
141: buildLowValueExpression(filterInfo),
142: getComparator()), ExpressionUtil.lt(var,
143: buildHighValueExpression(filterInfo),
144: getComparator()));
145: }
146: }
147:
148: static class DateStrict extends RightStrict {
149: protected Object convertLowValue(Object value) {
150: return nextDay((Date) value);
151: }
152: }
153:
154: static class DateNonStrict extends RightStrict {
155: protected Object convertHighValue(Object value) {
156: return nextDay((Date) value);
157: }
158: }
159:
160: static Date nextDay(Date date) {
161: Calendar cal = Calendar.getInstance();
162: cal.setTime(date);
163: cal.add(Calendar.DAY_OF_MONTH, 1);
164: return cal.getTime();
165: }
166: }
|