001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.resources.metadata.properties.ranges;
020:
021: import java.util.Date;
022: import java.text.*;
023: import java.util.StringTokenizer;
024: import java.util.logging.*;
025: import java.util.logging.Level;
026:
027: import org.openharmonise.rm.metadata.GeneralPropertyInstance;
028: import org.openharmonise.rm.publishing.*;
029:
030: /**
031: * Class which represents a <code>Property</code> range which is
032: * restricted to <code>Date</code> values.
033: *
034: * @author Michael Bell
035: * @version $Revision: 1.2 $
036: *
037: */
038: public class DateRange extends AbstractRange implements Range,
039: Publishable, Cloneable {
040:
041: /**
042: * Date range tag name
043: */
044: public final static String TAG_DATE_RANGE = "DateRange";
045:
046: /**
047: * Maximum allowed date
048: */
049: private Date m_dtMax = null;
050:
051: /**
052: * Minimum allowed date
053: */
054: private Date m_dtMin = null;
055:
056: /**
057: * <code>boolean</code> which indicates whether the date values should include
058: * time or not
059: */
060: private boolean m_bIncludeTime = true;
061:
062: /**
063: * The separator used to separate values in the string which is
064: * saved to the database representing the restrictions on this range
065: */
066: static final private String SEPARATOR = "|";
067:
068: /**
069: * Date format used to save datetime restrictions to the database
070: */
071: static final public String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss G";
072:
073: /**
074: * Date format used to save date restrictions to the database
075: */
076: static final public String DATE_FORMAT = "yyyy-MM-dd G";
077:
078: /**
079: * Logger for this class
080: */
081: private static final Logger m_logger = Logger
082: .getLogger(DateRange.class.getName());
083:
084: /**
085: * Constructs a new <code>DateRange</code>
086: *
087: */
088: public DateRange() {
089: super (Date.class.getName());
090: }
091:
092: /**
093: * Constructs a <code>DateRange</code> with restrictions.
094: *
095: * @param sDetails the <code>String</code> representation of the date
096: * restrictions for this range
097: */
098: public DateRange(String sDetails) {
099: super (Date.class.getName(), sDetails);
100: }
101:
102: /* (non-Javadoc)
103: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
104: */
105: public boolean isValid(Object obj) {
106: boolean bIsValid = false;
107:
108: if ((obj instanceof Date) || (obj instanceof java.util.Date)) {
109: Date date = (Date) obj;
110: bIsValid = true;
111:
112: if (hasMin() == true) {
113: if (getMin().after(date) == true) {
114: bIsValid = false;
115: }
116: }
117:
118: if (hasMax() == true) {
119: if (getMax().before(date) == true) {
120: bIsValid = false;
121: }
122: }
123: }
124:
125: return bIsValid;
126: }
127:
128: /* (non-Javadoc)
129: * @see java.lang.Object#equals(java.lang.Object)
130: */
131: public boolean equals(Object obj) {
132: boolean bResult = false;
133:
134: if (obj instanceof DateRange) {
135: bResult = super .equals(obj);
136: }
137:
138: return bResult;
139: }
140:
141: /**
142: * Sets manimum date for this range.
143: *
144: * @param min the manimum date for this range
145: */
146: public void setMin(Date min) {
147: if ((m_dtMin == null && min != null)
148: || (m_dtMin != null && m_dtMin.equals(min) == false)) {
149: m_dtMin = min;
150: isChanged(true);
151: }
152:
153: }
154:
155: /**
156: * Sets maximum date for this range.
157: *
158: * @param max the maximum date for this range
159: */
160: public void setMax(Date max) {
161: if ((m_dtMax == null && max != null)
162: || (m_dtMax != null && m_dtMax.equals(max) == false)) {
163: m_dtMax = max;
164: isChanged(true);
165: }
166: }
167:
168: /**
169: * Returns <code>true</code> if this range has a maximum limit.
170: *
171: * @return <code>true</code> if this range has a maximum limit
172: */
173: public boolean hasMax() {
174: return m_dtMax != null;
175: }
176:
177: /**
178: * Returns <code>true</code> if this range has a minimum limit.
179: *
180: * @return <code>true</code> if this range has a minimum limit
181: */
182: public boolean hasMin() {
183: return m_dtMin != null;
184: }
185:
186: /**
187: * Returns the minimum limit for this range.
188: *
189: * @return the minimum limit for this range
190: */
191: public Date getMin() {
192: return m_dtMin;
193: }
194:
195: /**
196: * Returns the maximum limit for this range.
197: *
198: * @return the maximum limit for this range
199: */
200: public Date getMax() {
201: return m_dtMax;
202: }
203:
204: /**
205: * Sets whether the date represented by this range includes time.
206: *
207: * @param bIncludeTime <code>true</code> if dates in this range include time
208: */
209: public void includeTime(boolean bIncludeTime) {
210: if (m_bIncludeTime != bIncludeTime) {
211: m_bIncludeTime = bIncludeTime;
212: isChanged(true);
213: }
214: }
215:
216: /**
217: * Returns <code>true</code> if the dates in this range include time.
218: *
219: * @return <code>true</code> if the dates in this range include time
220: */
221: public boolean includeTime() {
222: return m_bIncludeTime;
223: }
224:
225: /* (non-Javadoc)
226: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
227: */
228: public String getDetails() {
229:
230: if (isChanged()) {
231: StringBuffer strbuf = new StringBuffer();
232:
233: String sFormat = DATETIME_FORMAT;
234:
235: if (m_bIncludeTime == false) {
236: sFormat = DATE_FORMAT;
237: }
238:
239: strbuf.append(m_bIncludeTime);
240: strbuf.append(SEPARATOR);
241: SimpleDateFormat date_formatter = new SimpleDateFormat(
242: sFormat);
243:
244: if (hasMin() == true) {
245: strbuf.append(date_formatter.format(m_dtMin));
246: }
247: strbuf.append(SEPARATOR);
248: if (hasMax() == true) {
249: strbuf.append(date_formatter.format(m_dtMax));
250: }
251: super .setDetails(strbuf.toString());
252: }
253:
254: return super .getDetails();
255: }
256:
257: /* (non-Javadoc)
258: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
259: */
260: public void setDetails(String sDetails) {
261: if (sDetails != null) {
262: StringTokenizer tokenizer = new StringTokenizer(sDetails,
263: SEPARATOR);
264: SimpleDateFormat formatter = null;
265:
266: int i = 0;
267: int nTmp = 0;
268: while (tokenizer.hasMoreTokens()) {
269: String token = tokenizer.nextToken();
270:
271: if (token != null && token.length() > 0) {
272:
273: if (i == 0) {
274: m_bIncludeTime = Boolean.valueOf(token)
275: .booleanValue();
276:
277: String sFormat = DATETIME_FORMAT;
278:
279: if (m_bIncludeTime == false) {
280: sFormat = DATE_FORMAT;
281: }
282:
283: formatter = new SimpleDateFormat(sFormat);
284: } else if (i == 1 && formatter != null) {
285: try {
286: m_dtMin = formatter.parse(token);
287:
288: } catch (ParseException e) {
289: // ignore the limit
290: m_dtMin = null;
291: m_logger.log(Level.WARNING, e
292: .getLocalizedMessage(), e);
293: }
294: } else if (formatter != null) {
295: try {
296: m_dtMax = formatter.parse(token);
297: } catch (ParseException e) {
298: // ignore the limit
299: m_dtMax = null;
300: m_logger.log(Level.WARNING, e
301: .getLocalizedMessage(), e);
302: }
303: }
304:
305: }
306: i++;
307: }
308: }
309:
310: super .setDetails(sDetails);
311: }
312:
313: /* (non-Javadoc)
314: * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
315: */
316: public Class getPropertyInstanceClass()
317: throws ClassNotFoundException {
318: return GeneralPropertyInstance.class;
319: }
320:
321: /* (non-Javadoc)
322: * @see org.openharmonise.rm.publishing.Publishable#getTagName()
323: */
324: public String getTagName() {
325: return TAG_DATE_RANGE;
326: }
327: }
|