View Javadoc

1   /*
2    * The Working-Dogs.com License, Version 1.1
3    *
4    * Copyright (c) 1999 Working-Dogs.com.  All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in
15   *    the documentation and/or other materials provided with the
16   *    distribution.
17   *
18   * 3. The end-user documentation included with the redistribution, if
19   *    any, must include the following acknowlegement:
20   *       "This product includes software developed by the
21   *        Working-Dogs.com <http://www.Working-Dogs.com/>."
22   *    Alternately, this acknowlegement may appear in the software itself,
23   *    if and wherever such third-party acknowlegements normally appear.
24   *
25   * 4. The names "Working-Dogs.com" and "Village" must not be used to
26   *    endorse or promote products derived from this software without
27   *    prior written permission. For written permission, please contact
28   *    jon@working-dogs.com.
29   *
30   * 5. Products derived from this software may not be called
31   *    "Working-Dogs.com" nor may "Village" appear in their names
32   *    without prior written permission of Working-Dogs.com.
33   *
34   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
38   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45   * SUCH DAMAGE.
46   * ====================================================================
47   *
48   * This software consists of voluntary contributions made by many
49   * individuals on behalf of the Working-Dogs.com.  For more
50   * information on the Working-Dogs.com, please see
51   * <http://www.Working-Dogs.com/>.
52   */
53  package com.workingdogs.village;
54  
55  import java.util.Vector;
56  
57  /***
58   * A KeyDef is a way to define the key columns in a table. The KeyDef is generally used in conjunction with a <a
59   * href="TableDataSet.html">TableDataSet</a>. Essentially a KeyDef is what forms the WHERE clause for an UPDATE or DELETE.
60   *
61   * <P>
62   * In order to use the KeyDef, you simply use it like this:
63   * <PRE>
64   *  KeyDef kd = new KeyDef().addAttrib("key_column_a");
65   *  TableDataSet tds = new TableDataSet ( connection, "table", kd );
66   *  tds.fetchRecords();
67   *  Record rec = tds.getRecord(0);
68   *  rec.setValue("column_name", "new value" );
69   *  rec.save();
70   *  tds.close();
71   *  </PRE>
72   * In the above example, Record 0 is retrieved from the database table and the following update statement is generated:
73   * </p>
74   *
75   * <P>
76   * UPDATE table SET column_name=? WHERE key_column_a=?
77   * </p>
78   *
79   * <P></p>
80   *
81   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
82   * @version $Revision: 568 $
83   */
84  public class KeyDef
85  {
86      /*** TODO: DOCUMENT ME! */
87      private Vector mySelf = null;
88  
89      /***
90       * Constructor for KeyDef. Make sure to always initialize KeyDef with an initial element because it is 1 based.
91       */
92      public KeyDef()
93      {
94          mySelf = new Vector();
95          mySelf.addElement("");
96      }
97  
98      /***
99       * Adds the named attribute to the KeyDef.
100      *
101      * @param name TODO: DOCUMENT ME!
102      *
103      * @return a copy of itself
104      */
105     public KeyDef addAttrib(String name)
106     {
107         mySelf.addElement(name);
108 
109         return this;
110     }
111 
112     /***
113      * Determines if the KeyDef contains the requested Attribute.
114      *
115      * @param name TODO: DOCUMENT ME!
116      *
117      * @return true if the attribute has been defined. false otherwise.
118      */
119     public boolean containsAttrib(String name)
120     {
121         return (mySelf.indexOf((Object) name) == -1) ? false : true;
122     }
123 
124     /***
125      * getAttrib is 1 based. Setting pos to 0 will attempt to return pos 1.
126      *
127      * @param pos TODO: DOCUMENT ME!
128      *
129      * @return value of Attribute at pos as String. null if value is not found.
130      */
131     public String getAttrib(int pos)
132     {
133         if (pos == 0)
134         {
135             pos = 1;
136         }
137 
138         try
139         {
140             return (String) mySelf.elementAt(pos);
141         }
142         catch (ArrayIndexOutOfBoundsException e)
143         {
144             return null;
145         }
146     }
147 
148     /***
149      * KeyDef's are 1 based, returns size - 1
150      *
151      * @return the number of elements in the KeyDef that were set by addAttrib()
152      *
153      * @see #addAttrib(java.lang.String)
154      */
155     public int size()
156     {
157         return mySelf.size() - 1;
158     }
159 }