KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > storage > rdbms > TableRow


1 /*
2  * TableRow.java
3  *
4  * Version: $Revision: 1.11 $
5  *
6  * Date: $Date: 2006/11/16 23:40:47 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.storage.rdbms;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import org.dspace.core.ConfigurationManager;
48
49 /**
50  * Represents a database row.
51  *
52  * @author Peter Breton
53  * @version $Revision: 1.11 $
54  */

55 public class TableRow
56 {
57     /** Marker object to indicate NULLs. */
58     private static final Object JavaDoc NULL_OBJECT = new Object JavaDoc();
59
60     /** The name of the database table containing this row */
61     private String JavaDoc table;
62
63     /**
64      * A map of column names to column values. The key of the map is a String,
65      * the column name; the value is an Object, either an Integer, Boolean,
66      * Date, or String. If the value is NULL_OBJECT, then the column was NULL.
67      */

68     private Map JavaDoc data = new HashMap JavaDoc();
69
70     /**
71      * Constructor
72      *
73      * @param table
74      * The name of the database table containing this row.
75      * @param columns
76      * A list of column names. Each member of the List is a String.
77      * After construction, the list of columns is fixed; attempting
78      * to access a column not in the list will cause an
79      * IllegalArgumentException to be thrown.
80      */

81     public TableRow(String JavaDoc table, List JavaDoc columns)
82     {
83         this.table = table;
84         nullColumns(columns);
85     }
86
87     /**
88      * Return the name of the table containing this row, or null if this row is
89      * not associated with a database table.
90      *
91      * @return The name of the table containing this row
92      */

93     public String JavaDoc getTable()
94     {
95         return table;
96     }
97
98     /**
99      * Return true if this row contains a column with this name.
100      *
101      * @param column
102      * The column name (case-insensitive)
103      * @return True if this row contains a column with this name.
104      */

105     public boolean hasColumn(String JavaDoc column)
106     {
107         return data.get(canonicalize(column)) != null;
108     }
109
110     /**
111      * Return true if the column is an SQL NULL.
112      *
113      * @param column
114      * The column name (case-insensitive)
115      * @return True if the column is an SQL NULL
116      */

117     public boolean isColumnNull(String JavaDoc column)
118     {
119         if (!hasColumn(column))
120         {
121             throw new IllegalArgumentException JavaDoc("No such column " + column);
122         }
123
124         return data.get(canonicalize(column)) == NULL_OBJECT;
125     }
126
127     /**
128      * Return the integer value of column.
129      *
130      * If the column's type is not an integer, or the column does not exist, an
131      * IllegalArgumentException is thrown.
132      *
133      * @param column
134      * The column name (case-insensitive)
135      * @return The integer value of the column, or -1 if the column is an SQL
136      * null.
137      */

138     public int getIntColumn(String JavaDoc column)
139     {
140         if (!hasColumn(column))
141         {
142             throw new IllegalArgumentException JavaDoc("No such column " + column);
143         }
144
145         String JavaDoc name = canonicalize(column);
146
147         if (isColumnNull(name))
148         {
149             return -1;
150         }
151
152         Object JavaDoc value = data.get(name);
153
154         if (value == null)
155         {
156             throw new IllegalArgumentException JavaDoc("Column " + column
157                     + " not present");
158         }
159
160         if (!(value instanceof Integer JavaDoc))
161         {
162             throw new IllegalArgumentException JavaDoc("Value for " + column
163                     + " is not an integer");
164         }
165
166         return ((Integer JavaDoc) value).intValue();
167     }
168
169     /**
170      * Return the long value of column.
171      *
172      * If the column's type is not an long, or the column does not exist, an
173      * IllegalArgumentException is thrown.
174      *
175      * @param column
176      * The column name (case-insensitive)
177      * @return The long value of the column, or -1 if the column is an SQL null.
178      */

179     public long getLongColumn(String JavaDoc column)
180     {
181         if (!hasColumn(column))
182         {
183             throw new IllegalArgumentException JavaDoc("No such column " + column);
184         }
185
186         String JavaDoc name = canonicalize(column);
187
188         if (isColumnNull(name))
189         {
190             return -1;
191         }
192
193         Object JavaDoc value = data.get(name);
194
195         if (value == null)
196         {
197             throw new IllegalArgumentException JavaDoc("Column " + column
198                     + " not present");
199         }
200
201         if (!(value instanceof Long JavaDoc))
202         {
203             throw new IllegalArgumentException JavaDoc("Value is not an long");
204         }
205
206         return ((Long JavaDoc) value).longValue();
207     }
208
209     /**
210      * Return the String value of column.
211      *
212      * If the column's type is not a String, or the column does not exist, an
213      * IllegalArgumentException is thrown.
214      *
215      * @param column
216      * The column name (case-insensitive)
217      * @return The String value of the column, or null if the column is an SQL
218      * null.
219      */

220     public String JavaDoc getStringColumn(String JavaDoc column)
221     {
222         if (!hasColumn(column))
223         {
224             throw new IllegalArgumentException JavaDoc("No such column " + column);
225         }
226
227         String JavaDoc name = canonicalize(column);
228
229         if (isColumnNull(name))
230         {
231             return null;
232         }
233
234         Object JavaDoc value = data.get(name);
235
236         if (value == null)
237         {
238             throw new IllegalArgumentException JavaDoc("Column " + column
239                     + " not present");
240         }
241
242         if (!(value instanceof String JavaDoc))
243         {
244             throw new IllegalArgumentException JavaDoc("Value is not an string");
245         }
246
247         return (String JavaDoc) value;
248     }
249
250     /**
251      * Return the boolean value of column.
252      *
253      * If the column's type is not a boolean, or the column does not exist, an
254      * IllegalArgumentException is thrown.
255      *
256      * @param column
257      * The column name (case-insensitive)
258      * @return The boolean value of the column, or false if the column is an SQL
259      * null.
260      */

261     public boolean getBooleanColumn(String JavaDoc column)
262     {
263         if (!hasColumn(column))
264         {
265             throw new IllegalArgumentException JavaDoc("No such column " + column);
266         }
267
268         String JavaDoc name = canonicalize(column);
269
270         if (isColumnNull(name))
271         {
272             return false;
273         }
274
275         Object JavaDoc value = data.get(name);
276
277         // make sure that we tolerate integers or booleans
278
if (value == null)
279         {
280             throw new IllegalArgumentException JavaDoc("Column " + column
281                     + " not present");
282         }
283
284         if ((value instanceof Boolean JavaDoc))
285         {
286             return ((Boolean JavaDoc) value).booleanValue();
287         }
288         else if ((value instanceof Integer JavaDoc))
289         {
290             int i = ((Integer JavaDoc) value).intValue();
291
292             if (i == 0)
293             {
294                 return false; // 0 is false
295
}
296
297             return true; // nonzero is true
298
}
299         else
300         {
301             throw new IllegalArgumentException JavaDoc(
302                     "Value is not a boolean or an integer");
303         }
304     }
305
306     /**
307      * Return the date value of column.
308      *
309      * If the column's type is not a date, or the column does not exist, an
310      * IllegalArgumentException is thrown.
311      *
312      * @param column
313      * The column name (case-insensitive)
314      * @return - The date value of the column, or null if the column is an SQL
315      * null.
316      */

317     public java.util.Date JavaDoc getDateColumn(String JavaDoc column)
318     {
319         if (!hasColumn(column))
320         {
321             throw new IllegalArgumentException JavaDoc("No such column " + column);
322         }
323
324         String JavaDoc name = canonicalize(column);
325
326         if (isColumnNull(name))
327         {
328             return null;
329         }
330
331         Object JavaDoc value = data.get(name);
332
333         if (value == null)
334         {
335             throw new IllegalArgumentException JavaDoc("Column " + column
336                     + " not present");
337         }
338
339         if (!(value instanceof java.util.Date JavaDoc))
340         {
341             throw new IllegalArgumentException JavaDoc("Value is not a Date");
342         }
343
344         return (java.util.Date JavaDoc) value;
345     }
346
347     /**
348      * Set column to an SQL NULL.
349      *
350      * If the column does not exist, an IllegalArgumentException is thrown.
351      *
352      * @param column
353      * The column name (case-insensitive)
354      */

355     public void setColumnNull(String JavaDoc column)
356     {
357         if (!hasColumn(column))
358         {
359             throw new IllegalArgumentException JavaDoc("No such column " + column);
360         }
361
362         setColumnNullInternal(canonicalize(column));
363     }
364
365     /**
366      * Set column to the boolean b.
367      *
368      * If the column does not exist, an IllegalArgumentException is thrown.
369      *
370      * @param column
371      * The column name (case-insensitive)
372      * @param b
373      * The boolean value
374      */

375     public void setColumn(String JavaDoc column, boolean b)
376     {
377         if (!hasColumn(column))
378         {
379             throw new IllegalArgumentException JavaDoc("No such column " + column);
380         }
381
382         if ("oracle".equals(ConfigurationManager.getProperty("db.name")))
383         {
384             // if oracle, use 1 or 0 for true/false
385
data.put(canonicalize(column), b ? new Integer JavaDoc(1) : new Integer JavaDoc(0));
386         }
387         else
388         {
389             // default to postgres true/false
390
data.put(canonicalize(column), b ? Boolean.TRUE : Boolean.FALSE);
391         }
392     }
393
394     /**
395      * Set column to the String s. If s is null, the column is set to null.
396      *
397      * If the column does not exist, an IllegalArgumentException is thrown.
398      *
399      * @param column
400      * The column name (case-insensitive)
401      * @param s
402      * The String value
403      */

404     public void setColumn(String JavaDoc column, String JavaDoc s)
405     {
406         if (!hasColumn(column))
407         {
408             throw new IllegalArgumentException JavaDoc("No such column " + column);
409         }
410
411         data.put(canonicalize(column), (s == null) ? NULL_OBJECT : s);
412     }
413
414     /**
415      * Set column to the integer i.
416      *
417      * If the column does not exist, an IllegalArgumentException is thrown.
418      *
419      * @param column
420      * The column name (case-insensitive)
421      * @param i
422      * The integer value
423      */

424     public void setColumn(String JavaDoc column, int i)
425     {
426         if (!hasColumn(column))
427         {
428             throw new IllegalArgumentException JavaDoc("No such column " + column);
429         }
430
431         data.put(canonicalize(column), new Integer JavaDoc(i));
432     }
433
434     /**
435      * Set column to the long l.
436      *
437      * If the column does not exist, an IllegalArgumentException is thrown.
438      *
439      * @param column
440      * The column name (case-insensitive)
441      * @param l
442      * The long value
443      */

444     public void setColumn(String JavaDoc column, long l)
445     {
446         if (!hasColumn(column))
447         {
448             throw new IllegalArgumentException JavaDoc("No such column " + column);
449         }
450
451         data.put(canonicalize(column), new Long JavaDoc(l));
452     }
453
454     /**
455      * Set column to the date d. If the date is null, the column is set to NULL
456      * as well.
457      *
458      * If the column does not exist, an IllegalArgumentException is thrown.
459      *
460      * @param column
461      * The column name (case-insensitive)
462      * @param d
463      * The date value
464      */

465     public void setColumn(String JavaDoc column, java.util.Date JavaDoc d)
466     {
467         if (!hasColumn(column))
468         {
469             throw new IllegalArgumentException JavaDoc("No such column " + column);
470         }
471
472         if (d == null)
473         {
474             setColumnNull(canonicalize(column));
475
476             return;
477         }
478
479         data.put(canonicalize(column), d);
480     }
481
482     ////////////////////////////////////////
483
// Utility methods
484
////////////////////////////////////////
485

486     /**
487      * Return a String representation of this object.
488      *
489      * @return String representaton
490      */

491     public String JavaDoc toString()
492     {
493         final String JavaDoc NEWLINE = System.getProperty("line.separator");
494         StringBuffer JavaDoc result;
495         
496         if (table==null)
497         {
498             result = new StringBuffer JavaDoc("no_table");
499         }
500         else
501         {
502             result = new StringBuffer JavaDoc(table);
503         }
504         
505         result.append(NEWLINE);
506
507         for (Iterator JavaDoc iterator = data.keySet().iterator(); iterator.hasNext();)
508         {
509             String JavaDoc column = (String JavaDoc) iterator.next();
510             result.append("\t").append(column).append(" = ").append(
511                     isColumnNull(column) ? "NULL" : data.get(column)).append(
512                     NEWLINE);
513         }
514
515         return result.toString();
516     }
517
518     /**
519      * Return a hash code for this object.
520      *
521      * @return int hash of object
522      */

523     public int hashCode()
524     {
525         return toString().hashCode();
526     }
527
528     /**
529      * Return true if this object equals obj, false otherwise.
530      *
531      * @param obj
532      * @return true if TableRow objects are equal
533      */

534     public boolean equals(Object JavaDoc obj)
535     {
536         if (!(obj instanceof TableRow))
537         {
538             return false;
539         }
540
541         return data.equals(((TableRow) obj).data);
542     }
543
544     /**
545      * Return the canonical name for column.
546      *
547      * @param column
548      * The name of the column.
549      * @return The canonical name of the column.
550      */

551     static String JavaDoc canonicalize(String JavaDoc column)
552     {
553         if ("oracle".equals(ConfigurationManager.getProperty("db.name")))
554         {
555             // oracle requires uppercase
556
return column.toUpperCase();
557         }
558
559         // postgres default lowercase
560
return column.toLowerCase();
561     }
562
563     /**
564      * Set columns to null.
565      *
566      * @param columns -
567      * A list of the columns to set to null. Each element of the list
568      * is a String.
569      */

570     private void nullColumns(List JavaDoc columns)
571     {
572         for (Iterator JavaDoc iterator = columns.iterator(); iterator.hasNext();)
573         {
574             setColumnNullInternal((String JavaDoc) iterator.next());
575         }
576     }
577
578     /**
579      * Internal method to set column to null. The public method ensures that
580      * column actually exists.
581      *
582      * @param column
583      */

584     private void setColumnNullInternal(String JavaDoc column)
585     {
586         data.put(canonicalize(column), NULL_OBJECT);
587     }
588 }
589
Popular Tags