001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tutorial.shared;
032:
033: import java.util.Date;
034:
035: import com.jgoodies.binding.beans.Model;
036:
037: /**
038: * Describes an order that demonstrates different types of constraints.
039: * The property constraints are: mandatory vs. optional,
040: * minimum and maximum length, minimum and maximum values,
041: * linked constraints.
042: * Provides the following bound properties:
043: * <em>orderNo, orderDate, deliveryDate, deliveryNotes</em>.
044: *
045: * @author Karsten Lentzsch
046: * @version $Revision: 1.8 $
047: */
048: public class Order extends Model {
049:
050: // Names of the Bound Bean Properties *************************************
051:
052: public static final String PROPERTYNAME_DELIVERY_DATE = "deliveryDate";
053: public static final String PROPERTYNAME_DELIVERY_NOTES = "deliveryNotes";
054: public static final String PROPERTYNAME_ORDER_DATE = "orderDate";
055: public static final String PROPERTYNAME_ORDER_NO = "orderNo";
056:
057: // Constants **************************************************************
058:
059: /**
060: * The time value used to map <code>null</code> dates.
061: *
062: * @see #getOrderDate()
063: * @see #setOrderDate(Date)
064: * @see #getDeliveryDate()
065: * @see #setDeliveryDate(Date)
066: */
067: private static final long NO_DATE = -1;
068:
069: // Instance Fields ********************************************************
070:
071: /**
072: * Holds a number that identifies the order.
073: *
074: * @see #getOrderNo()
075: * @see #setOrderNo(String)
076: *
077: * validation-constraints:
078: * mandatory
079: * min-length: 5
080: * max-length: 10
081: */
082: private String orderNo;
083:
084: /**
085: * Holds the date this order has been created.
086: * Stores the Date's time to avoid external mutations.
087: * -1 indicates no Date has been set.
088: *
089: * @see #getOrderDate()
090: * @see #setOrderDate(Date)
091: *
092: * validation-constraints:
093: * linked: value <= valueOf(deliveryDate)
094: */
095: private long orderDate;
096:
097: /**
098: * Holds the date where the goods are delivered.
099: * Stores the Date's time to avoid external mutations.
100: * -1 indicates no Date has been set.
101: *
102: * @see #getDeliveryDate()
103: * @see #setDeliveryDate(Date)
104: *
105: * validation-constraints:
106: * mandatory
107: * linked: value >= valueOf(orderDate)
108: */
109: private long deliveryDate;
110:
111: /**
112: * Holds optional notes for the delivery, packaging or shipment.
113: *
114: * @see #getDeliveryNotes()
115: * @see #setDeliveryNotes(String)
116: *
117: * validation-constraints:
118: * maxLength(30)
119: */
120: private String deliveryNotes;
121:
122: // Instance Creation ******************************************************
123:
124: /**
125: * Constructs an empty <code>Order</code>.
126: */
127: public Order() {
128: orderDate = -1;
129: deliveryDate = -1;
130: }
131:
132: // Access to Bound Properties *********************************************
133:
134: /**
135: * Returns this order's unique id.
136: *
137: * @return the order's no, a system wide unique id
138: *
139: * @see #setOrderNo(String)
140: */
141: public String getOrderNo() {
142: return orderNo;
143: }
144:
145: /**
146: * Sets a new unique order number.
147: *
148: * @param newValue the order number to be set
149: *
150: * @see #getOrderNo()
151: */
152: public void setOrderNo(String newValue) {
153: String oldValue = getOrderNo();
154: orderNo = newValue;
155: firePropertyChange(PROPERTYNAME_ORDER_NO, oldValue, newValue);
156: }
157:
158: /**
159: * Returns the date that describes when this order has been initiated
160: * by the customer.
161: *
162: * @return the order date
163: *
164: * @see #setOrderDate(Date)
165: */
166: public Date getOrderDate() {
167: return orderDate == NO_DATE ? null : new Date(orderDate);
168: }
169:
170: /**
171: * Sets this order's order date that describes when the order has
172: * been initiated by the customer. Stores the new Date's time,
173: * not the Date itself, to avoid external modifications.
174: *
175: * @param newDate the order date to be set
176: *
177: * @see #getOrderDate()
178: * @see #getDeliveryDate()
179: */
180: public void setOrderDate(Date newDate) {
181: Date oldDate = getOrderDate();
182: orderDate = newDate == null ? NO_DATE : newDate.getTime();
183: firePropertyChange(PROPERTYNAME_ORDER_DATE, oldDate, newDate);
184: }
185:
186: /**
187: * Returns the date that describes when the product has been delivered.
188: *
189: * @return the delivery date
190: *
191: * @see #setDeliveryDate(Date)
192: * @see #getOrderDate()
193: */
194: public Date getDeliveryDate() {
195: return deliveryDate == NO_DATE ? null : new Date(deliveryDate);
196: }
197:
198: /**
199: * Sets this order's delivery date. Stores the new Date's time,
200: * not the Date itself, to avoid external modifications.
201: *
202: * @param newDate the delivery date to be set
203: *
204: * @see #getDeliveryDate()
205: * @see #getOrderDate()
206: */
207: public void setDeliveryDate(Date newDate) {
208: Date oldDate = getDeliveryDate();
209: deliveryDate = newDate == null ? NO_DATE : newDate.getTime();
210: firePropertyChange(PROPERTYNAME_DELIVERY_DATE, oldDate, newDate);
211: }
212:
213: /**
214: * Returns this order's optional delivery notes that describe
215: * the delivery, packaging or shipment.
216: *
217: * @return the delivery notes
218: *
219: * @see #setDeliveryNotes(String)
220: */
221: public String getDeliveryNotes() {
222: return deliveryNotes;
223: }
224:
225: /**
226: * Sets this order's optional delivery notes that describe
227: * the delivery, packaging or shipment.
228: *
229: * @param newNotes the delivery notes to be set
230: *
231: * @see #getDeliveryNotes()
232: */
233: public void setDeliveryNotes(String newNotes) {
234: String oldNotes = getDeliveryNotes();
235: deliveryNotes = newNotes;
236: firePropertyChange(PROPERTYNAME_DELIVERY_NOTES, oldNotes,
237: newNotes);
238: }
239:
240: }
|