001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.filter.text.cql2;
017:
018: import java.security.InvalidParameterException;
019: import java.util.Date;
020: import org.opengis.filter.FilterFactory;
021: import org.opengis.filter.expression.Literal;
022:
023: /**
024: * Period is constructed in the parsing process. this has convenient method to
025: * deliver begin and end date of period. a period can be created from
026: * date-time/date-time or date-time/duration or duration/date-time
027: *
028: * @since 2.4
029: * @author Mauricio Pazos - Axios Engineering
030: * @author Gabriel Roldan - Axios Engineering
031: * @version $Id: PeriodNode.java 28745 2008-01-14 12:22:32Z vmpazos $
032: * @source $URL:
033: * http://svn.geotools.org/geotools/trunk/gt/modules/library/cql/src/main/java/org/geotools/text/filter/PeriodNode.java $
034: *
035: */
036: class PeriodNode {
037: private Literal begin = null;
038: private Literal end = null;
039:
040: /**
041: * @see create
042: *
043: * @param begin
044: * @param end
045: */
046: private PeriodNode(final Literal begin, final Literal end) {
047: if (!(begin.getValue() instanceof Date)) {
048: throw new InvalidParameterException(
049: "begin parameter must be Literal with Date");
050: }
051:
052: if (!(begin.getValue() instanceof Date)) {
053: throw new InvalidParameterException(
054: "end paremeter must be Literal with Date");
055: }
056:
057: this .begin = begin;
058: this .end = end;
059: }
060:
061: public static PeriodNode createPeriodDateAndDate(
062: final Literal beginDate, final Literal endDate) {
063: PeriodNode period = new PeriodNode(beginDate, endDate);
064:
065: return period;
066: }
067:
068: public static PeriodNode createPeriodDateAndDuration(
069: final Literal date, final Literal duration,
070: FilterFactory filterFactory) {
071: // compute last date from duration
072: // Y M D and H M S
073: Date firstDate = (Date) date.getValue();
074: String strDuration = (String) duration.getValue();
075:
076: Date lastDate = Util.addDurationToDate(firstDate, strDuration);
077:
078: Literal literalLastDate = filterFactory.literal(lastDate);
079:
080: PeriodNode period = new PeriodNode(date, literalLastDate);
081:
082: return period;
083: }
084:
085: public static PeriodNode createPeriodDurationAndDate(
086: final Literal duration, final Literal date,
087: FilterFactory filterFactory) {
088: // compute first date from duration Y M D and H M S
089: Date lastDate = (Date) date.getValue();
090: String strDuration = (String) duration.getValue();
091:
092: Date firstDate = Util.subtractDurationToDate(lastDate,
093: strDuration);
094:
095: Literal literalFirstDate = filterFactory.literal(firstDate);
096:
097: PeriodNode period = new PeriodNode(literalFirstDate, date);
098:
099: return period;
100: }
101:
102: /**
103: * @return Literal with begining date of period
104: */
105: public Literal getBeginning() {
106: return this .begin;
107: }
108:
109: /**
110: * @return with ending date of period
111: */
112: public Literal getEnding() {
113: return this.end;
114: }
115: }
|