001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: DomainObject.java,v $
023   Revision 1.12  2004/05/05 21:37:04  markl
024   comment block updates
025
026   Revision 1.11  2004/03/16 06:43:48  markl
027   LocaleManager method change
028
029   Revision 1.10  2003/01/19 09:35:41  markl
030   Javadoc & comment header updates.
031
032   Revision 1.9  2001/03/20 00:54:52  markl
033   Fixed deprecated calls.
034
035   Revision 1.8  2001/03/12 05:58:46  markl
036   Javadoc cleanup.
037
038   Revision 1.7  2001/03/12 01:17:30  markl
039   Source code cleanup.
040
041   Revision 1.6  2000/10/24 01:31:42  markl
042   Fixed locale bundle name.
043
044   Revision 1.5  1999/08/13 07:11:50  markl
045   Minor fixes.
046
047   Revision 1.4  1999/08/03 04:49:52  markl
048   Fixed a bug.
049
050   Revision 1.3  1999/07/25 13:39:10  markl
051   Minor fixes.
052
053   Revision 1.2  1999/06/28 09:37:53  markl
054   I18N.
055
056   Revision 1.1  1999/02/28 00:44:03  markl
057   Initial revision
058   ----------------------------------------------------------------------------
059*/
060
061package kiwi.db;
062
063import java.util.*;
064
065import kiwi.util.*;
066
067/** A base class for problem domain objects; the class provides some
068  * rudimentary data validation methods that are intended for use by
069  * mutators. For example, if a domain object has a member called 'lastName'
070  * which cannot be a null or empty string, the mutator can be implemented as
071  * follows:
072  * <p>
073  * <pre>
074  * public void setLastName(String lastName) throws MutatorException
075  *   {
076  *   this.lastName = ensureNotNull(lastName, "Last Name");
077  *   }
078  * </pre>
079  *
080  * @author Mark Lindner
081  */
082
083public abstract class DomainObject
084  {
085
086  /** Construct a new <code>DomainObject</code>.
087   */
088
089  public DomainObject()
090    {
091    }
092  
093  /** Ensure that a String value is not <code>null</code>.
094   *
095   * @param s The string check.
096   * @return An empty string if <code>s</code> is <code>null</code>,
097   * and <code>s</code> otherwise.
098   */
099  
100  protected String trapNull(String s)
101    {
102    return((s == null) ? "" : s);
103    }
104  
105  /** Ensure that an integer value is greater than zero.
106   *
107   * @param value The value to check.
108   * @param desc A description of the field that this value represents.
109   *
110   * @exception kiwi.db.MutatorException If the validation fails.
111   */
112  
113  public int ensurePositive(int value, String desc) throws MutatorException
114    {
115    if(value <= 0)
116      positiveWarning(desc);
117
118    return(value);
119    }
120
121  /** Ensure that a floating point value is greater than zero.
122   *
123   * @param value The value to check.
124   * @param desc A description of the field that this value represents.
125   *
126   * @exception kiwi.db.MutatorException If the validation fails.
127   */
128  
129  public float ensurePositive(float value, String desc) throws MutatorException
130    {
131    if(value <= 0.0)
132      positiveWarning(desc);
133    
134    return(value);
135    }
136
137  /** Ensure that a double precision value is greater than zero.
138   *
139   * @param value The value to check.
140   * @param desc A description of the field that this value represents.
141   *
142   * @exception kiwi.db.MutatorException If the validation fails.
143   */
144  
145  public double ensurePositive(double value, String desc)
146    throws MutatorException
147    {
148    if(value <= 0.0)
149      positiveWarning(desc);
150    
151    return(value);
152    }
153  
154  /** Ensure that an integer value is greater than or equal to zero.
155   *
156   * @param value The value to check.
157   * @param desc A description of the field that this value represents.
158   *
159   * @exception kiwi.db.MutatorException If the validation fails.
160   */
161  
162  public int ensureNonNegative(int value, String desc) throws MutatorException
163    {
164    if(value < 0)
165      negativeWarning(desc);
166
167    return(value);
168    }
169
170  /** Ensure that a floating point value is greater than or equal to zero.
171   *
172   * @param value The value to check.
173   * @param desc A description of the field that this value represents.
174   *
175   * @exception kiwi.db.MutatorException If the validation fails.
176   */
177  
178  public float ensureNonNegative(float value, String desc)
179    throws MutatorException
180    {
181    if(value < 0.0)
182      negativeWarning(desc);
183    
184    return(value);
185    }
186
187  /** Ensure that a double precision value is greater than or equal to zero.
188   *
189   * @param value The value to check.
190   * @param desc A description of the field that this value represents.
191   *
192   * @exception kiwi.db.MutatorException If the validation fails.
193   */
194  
195  public double ensureNonNegative(double value, String desc)
196    throws MutatorException
197    {
198    if(value < 0.0)
199      negativeWarning(desc);
200    
201    return(value);
202    }
203  
204  /** Ensure that a String is neither null nor of length zero.
205   *
206   * @param value The value to check.
207   * @param desc A description of the field that this value represents.
208   *
209   * @exception kiwi.db.MutatorException If the validation fails.
210   */
211  
212  public String ensureNotNull(String value, String desc)
213    throws MutatorException
214    {
215    if(value == null)
216      nullWarning(desc);
217    
218    value = value.trim();
219
220    if(value.length() == 0)
221      nullWarning(desc);
222
223    return(value);
224    }
225
226  /* Generate a null value warning. */
227  
228  private static void nullWarning(String fieldName) throws MutatorException
229    {
230    String msg = LocaleManager.getDefault()
231      .getLocaleData("KiwiDB").getMessage("kiwi.db.message.not_null",
232                                          fieldName);
233    
234    throw(new MutatorException(msg));
235    }
236
237  /* Generate a <= 0 value warning. */
238
239  private static void positiveWarning(String fieldName)
240    throws MutatorException
241    {
242    String msg = LocaleManager.getDefault()
243      .getLocaleData("KiwiDB").getMessage("kiwi.db.message.greater_than_zero",
244                                          fieldName);
245
246    throw(new MutatorException(msg));
247    }
248
249  /* Generate a < 0 value warning. */
250
251  private static void negativeWarning(String fieldName)
252    throws MutatorException
253    {
254    String msg = LocaleManager.getDefault()
255      .getLocaleData("KiwiDB").getMessage("kiwi.db.message.not_negative",
256                                          fieldName);
257
258    throw(new MutatorException(msg));
259    }
260
261  /* Generate an item selection warning. */
262  
263  private static void selectionWarning(String fieldName)
264    throws MutatorException
265    {
266    String msg = LocaleManager.getDefault()
267      .getLocaleData("KiwiDB").getMessage("kiwi.db.message.no_selection",
268                                          fieldName);
269
270    throw(new MutatorException(msg));
271    }
272  }
273
274/* end of source file */