001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.metadata;
051:
052: import org.jaffa.datatypes.*;
053: import org.jaffa.util.LocaleHelper;
054: import java.util.StringTokenizer;
055:
056: /**
057: * An instance of this class will hold meta information for a DateTime field.
058: */
059: public class DateTimeFieldMetaData extends FieldMetaData {
060:
061: /** Default width.*/
062: public static final int DEFAULT_WIDTH = 20;
063:
064: /** Default Layout To Use for Formatting a DateOnly object into a String.*/
065: //public static final String DEFAULT_FORMAT_LAYOUT = "MM/dd/yyyy HH:mm:ss";
066: /** An array of Default Layouts To Use for Parsing a String into a DateOnly object.
067: * The String will parsed based on each layout, until a successful parse occurs.
068: * This is to support entry of just the date, or date + hours, or date + hours + minutes....
069: */
070: //public static final String[] DEFAULT_PARSE_LAYOUT = new String[] {"MM/dd/yy HH:mm:ss", "MM/dd/yy HH:mm", "MM/dd/yy HH", "MM/dd/yy"};
071:
072: // NOTE: keep the equals(), clone(), compareTo(), hashCode() methods in sync
073: private String m_layout = null;
074: private DateTime m_minValue = null;
075: private DateTime m_maxValue = null;
076:
077: /** Creates an instance.
078: */
079: public DateTimeFieldMetaData() {
080: super (null, Defaults.DATETIME, null, null);
081: }
082:
083: /** Creates an instance.
084: * @param name The field name.
085: * @param labelToken The token used for displaying labels.
086: * @param isMandatory Indicates if the field is mandatory.
087: * @param layout The layout.
088: * @param minValue The minimum value.
089: * @param maxValue The maximum value.
090: */
091: public DateTimeFieldMetaData(String name, String labelToken,
092: Boolean isMandatory, String layout, DateTime minValue,
093: DateTime maxValue) {
094: super (name, Defaults.DATETIME, labelToken, isMandatory);
095: m_layout = layout;
096: m_minValue = minValue;
097: m_maxValue = maxValue;
098: }
099:
100: /** Getter for property layout.
101: * @return Value of property layout.
102: */
103: public String getLayout() {
104: return m_layout;
105: }
106:
107: /** Getter for property minValue.
108: * @return Value of property minValue.
109: */
110: public DateTime getMinValue() {
111: return m_minValue;
112: }
113:
114: /** Getter for property maxValue.
115: * @return Value of property maxValue.
116: */
117: public DateTime getMaxValue() {
118: return m_maxValue;
119: }
120:
121: /** Returns a clone of the object.
122: * @return a clone of the object.
123: */
124: public Object clone() {
125: // no more processing required since the fields are immutable
126: return super .clone();
127: }
128:
129: /** Returns the hash code.
130: * @return the hash code.
131: */
132: public int hashCode() {
133: int i = 0;
134: i = super .hashCode();
135: if (m_layout != null)
136: i += m_layout.hashCode();
137: if (m_minValue != null)
138: i += m_minValue.hashCode();
139: if (m_minValue != null)
140: i += m_maxValue.hashCode();
141: return i;
142: }
143:
144: /** Compares this object with another DateTimeFieldMetaData object.
145: * Returns a true if both the objects have the same properties.
146: * @param obj the other DateTimeFieldMetaData object.
147: * @return a true if both the objects have the same properties.
148: */
149: public boolean equals(Object obj) {
150: boolean isEqual = false;
151: if (obj instanceof DateTimeFieldMetaData) {
152: DateTimeFieldMetaData field2 = (DateTimeFieldMetaData) obj;
153: if (super .equals(field2)) {
154: if (((m_layout != null && m_layout
155: .equals(field2.m_layout)) || (m_layout == null && field2.m_layout == null))
156: && ((m_minValue != null && m_minValue
157: .equals(field2.m_minValue)) || (m_minValue == null && field2.m_minValue == null))
158: && ((m_maxValue != null && m_maxValue
159: .equals(field2.m_maxValue)) || (m_maxValue == null && field2.m_maxValue == null)))
160: isEqual = true;
161: }
162: }
163: return isEqual;
164: }
165:
166: /** Compares this object with another DateTimeFieldMetaData object.
167: * Note: this class has a natural ordering that is inconsistent with equals
168: * @param obj the other DateTimeFieldMetaData object.
169: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
170: */
171: public int compareTo(Object obj) {
172: // NOTE: this isnt a perfect compare !!!
173: return super .compareTo(obj);
174: }
175:
176: /** Returns the diagnostic information.
177: * @return the diagnostic information.
178: */
179: public String toString() {
180: String comma = ", ";
181: String equals = "=";
182: StringBuffer buffer = new StringBuffer(super .toString());
183:
184: buffer.append(comma);
185: buffer.append("Layout");
186: buffer.append(equals);
187: buffer.append(m_layout);
188: buffer.append(comma);
189: buffer.append("MinValue");
190: buffer.append(equals);
191: buffer.append(m_minValue);
192: buffer.append(comma);
193: buffer.append("MaxValue");
194: buffer.append(equals);
195: buffer.append(m_maxValue);
196:
197: return buffer.toString();
198: }
199:
200: /** Getter for property width.
201: * @return Value of property width.
202: */
203: public int getWidth() {
204: return DEFAULT_WIDTH;
205: }
206:
207: /** Returns an equivalent DateOnlyFieldMetaData object.
208: * @return an equivalent DateOnlyFieldMetaData object.
209: */
210: public DateOnlyFieldMetaData toDateOnlyFieldMetaData() {
211: String name = getName();
212: String labelToken = getLabelToken();
213: Boolean isMandatory = isMandatory();
214: String layout = getLayout();
215: DateOnly minValue = m_minValue == null ? null : DateTime
216: .toDateOnly(m_minValue);
217: DateOnly maxValue = m_maxValue == null ? null : DateTime
218: .toDateOnly(m_maxValue);
219:
220: return new DateOnlyFieldMetaData(name, labelToken, isMandatory,
221: layout, minValue, maxValue);
222: }
223:
224: public static String[] getDateTimeParse() {
225: StringTokenizer value = new StringTokenizer(LocaleHelper
226: .getProperty("datetime.parse"), ",");
227: String[] nameList = new String[value.countTokens()];
228: int counter = 0;
229: while (value.hasMoreTokens()) {
230: nameList[counter] = value.nextToken();
231: counter++;
232: }
233: return nameList;
234: }
235:
236: public static String getDateTimeFormat() {
237: return LocaleHelper.getProperty("datetime.format");
238: }
239: }
|