KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > ranges > DateRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.metadata.properties.ranges;
20
21 import java.util.Date JavaDoc;
22 import java.text.*;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.logging.*;
25 import java.util.logging.Level JavaDoc;
26
27 import org.openharmonise.rm.metadata.GeneralPropertyInstance;
28 import org.openharmonise.rm.publishing.*;
29
30
31 /**
32  * Class which represents a <code>Property</code> range which is
33  * restricted to <code>Date</code> values.
34  *
35  * @author Michael Bell
36  * @version $Revision: 1.2 $
37  *
38  */

39 public class DateRange
40     extends AbstractRange
41     implements Range, Publishable, Cloneable JavaDoc {
42
43     /**
44      * Date range tag name
45      */

46     public final static String JavaDoc TAG_DATE_RANGE = "DateRange";
47
48
49     /**
50      * Maximum allowed date
51      */

52     private Date JavaDoc m_dtMax = null;
53     
54     /**
55      * Minimum allowed date
56      */

57     private Date JavaDoc m_dtMin = null;
58     
59     /**
60      * <code>boolean</code> which indicates whether the date values should include
61      * time or not
62      */

63     private boolean m_bIncludeTime = true;
64     
65     /**
66      * The separator used to separate values in the string which is
67      * saved to the database representing the restrictions on this range
68      */

69     static final private String JavaDoc SEPARATOR = "|";
70     
71     /**
72      * Date format used to save datetime restrictions to the database
73      */

74     static final public String JavaDoc DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss G";
75     
76     /**
77      * Date format used to save date restrictions to the database
78      */

79     static final public String JavaDoc DATE_FORMAT = "yyyy-MM-dd G";
80     
81     /**
82      * Logger for this class
83      */

84     private static final Logger m_logger = Logger.getLogger(DateRange.class.getName());
85
86     /**
87      * Constructs a new <code>DateRange</code>
88      *
89      */

90     public DateRange() {
91         super(Date JavaDoc.class.getName());
92     }
93
94     /**
95      * Constructs a <code>DateRange</code> with restrictions.
96      *
97      * @param sDetails the <code>String</code> representation of the date
98      * restrictions for this range
99      */

100     public DateRange(String JavaDoc sDetails) {
101         super(Date JavaDoc.class.getName(), sDetails);
102     }
103
104     /* (non-Javadoc)
105      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
106      */

107     public boolean isValid(Object JavaDoc obj) {
108         boolean bIsValid = false;
109         
110         if((obj instanceof Date JavaDoc) || (obj instanceof java.util.Date JavaDoc)) {
111             Date JavaDoc date = (Date JavaDoc) obj;
112             bIsValid = true;
113         
114             if(hasMin() == true) {
115                 if(getMin().after(date) == true) {
116                     bIsValid = false;
117                 }
118             }
119             
120             if(hasMax() == true) {
121                 if(getMax().before(date) == true) {
122                     bIsValid = false;
123                 }
124             }
125         }
126         
127         return bIsValid;
128     }
129
130     /* (non-Javadoc)
131      * @see java.lang.Object#equals(java.lang.Object)
132      */

133     public boolean equals(Object JavaDoc obj) {
134         boolean bResult = false;
135
136         if (obj instanceof DateRange) {
137             bResult = super.equals(obj);
138         }
139
140         return bResult;
141     }
142
143     /**
144      * Sets manimum date for this range.
145      *
146      * @param min the manimum date for this range
147      */

148     public void setMin(Date JavaDoc min) {
149         if((m_dtMin == null && min != null)
150             || (m_dtMin != null && m_dtMin.equals(min) == false)) {
151             m_dtMin = min;
152             isChanged(true);
153         }
154         
155     }
156
157     /**
158      * Sets maximum date for this range.
159      *
160      * @param max the maximum date for this range
161      */

162     public void setMax(Date JavaDoc max) {
163         if((m_dtMax == null && max != null)
164         || (m_dtMax != null && m_dtMax.equals(max) == false)) {
165             m_dtMax = max;
166             isChanged(true);
167         }
168     }
169
170     /**
171      * Returns <code>true</code> if this range has a maximum limit.
172      *
173      * @return <code>true</code> if this range has a maximum limit
174      */

175     public boolean hasMax() {
176         return m_dtMax != null;
177     }
178
179     /**
180     * Returns <code>true</code> if this range has a minimum limit.
181     *
182     * @return <code>true</code> if this range has a minimum limit
183     */

184     public boolean hasMin() {
185         return m_dtMin != null;
186     }
187     
188     /**
189      * Returns the minimum limit for this range.
190      *
191      * @return the minimum limit for this range
192      */

193     public Date JavaDoc getMin() {
194         return m_dtMin;
195     }
196     
197     /**
198      * Returns the maximum limit for this range.
199      *
200      * @return the maximum limit for this range
201      */

202     public Date JavaDoc getMax() {
203         return m_dtMax;
204     }
205     
206     /**
207      * Sets whether the date represented by this range includes time.
208      *
209      * @param bIncludeTime <code>true</code> if dates in this range include time
210      */

211     public void includeTime(boolean bIncludeTime) {
212         if(m_bIncludeTime != bIncludeTime) {
213             m_bIncludeTime = bIncludeTime;
214             isChanged(true);
215         }
216     }
217     
218     /**
219      * Returns <code>true</code> if the dates in this range include time.
220      *
221      * @return <code>true</code> if the dates in this range include time
222      */

223     public boolean includeTime() {
224         return m_bIncludeTime;
225     }
226
227     /* (non-Javadoc)
228      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
229      */

230     public String JavaDoc getDetails() {
231
232         if (isChanged()) {
233             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
234             
235             String JavaDoc sFormat = DATETIME_FORMAT;
236             
237             if(m_bIncludeTime == false) {
238                 sFormat = DATE_FORMAT;
239             }
240             
241             strbuf.append(m_bIncludeTime);
242             strbuf.append(SEPARATOR);
243             SimpleDateFormat date_formatter =
244                 new SimpleDateFormat(sFormat);
245
246             if (hasMin() == true) {
247                 strbuf.append(date_formatter.format(m_dtMin));
248             }
249             strbuf.append(SEPARATOR);
250             if (hasMax() == true) {
251                 strbuf.append(date_formatter.format(m_dtMax));
252             }
253             super.setDetails(strbuf.toString());
254         }
255
256         return super.getDetails();
257     }
258
259     /* (non-Javadoc)
260      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
261      */

262     public void setDetails(String JavaDoc sDetails) {
263         if (sDetails != null) {
264             StringTokenizer JavaDoc tokenizer =
265                 new StringTokenizer JavaDoc(sDetails, SEPARATOR);
266             SimpleDateFormat formatter = null;
267
268             int i = 0;
269             int nTmp = 0;
270             while (tokenizer.hasMoreTokens()) {
271                 String JavaDoc token = tokenizer.nextToken();
272
273                 if (token != null && token.length() > 0) {
274                     
275                     if(i == 0) {
276                         m_bIncludeTime = Boolean.valueOf(token).booleanValue();
277                         
278                         String JavaDoc sFormat = DATETIME_FORMAT;
279                         
280                         if(m_bIncludeTime == false) {
281                             sFormat = DATE_FORMAT;
282                         }
283                         
284                         formatter = new SimpleDateFormat(sFormat);
285                     } else if (i == 1 && formatter != null) {
286                         try {
287                             m_dtMin = formatter.parse(token);
288                             
289                             
290                         } catch (ParseException e) {
291                             // ignore the limit
292
m_dtMin = null;
293                             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
294                         }
295                     } else if(formatter != null) {
296                         try {
297                             m_dtMax = formatter.parse(token);
298                         } catch (ParseException e) {
299                             // ignore the limit
300
m_dtMax = null;
301                             m_logger.log(Level.WARNING, e.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 JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc {
317         return GeneralPropertyInstance.class;
318     }
319     
320     /* (non-Javadoc)
321      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
322      */

323     public String JavaDoc getTagName() {
324         return TAG_DATE_RANGE;
325     }
326 }
327
Popular Tags