KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > ejb > Book


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.ejb;
16
17 import java.io.Serializable JavaDoc;
18 import java.sql.Timestamp JavaDoc;
19
20 /**
21  * Represents a single result row from a {@link IBookQuery}. In effect,
22  * this is a light-wieght, serializable, read-only version of an {@link IBook}
23  * bean, plus it contains the owner and holder name (which means we don't
24  * have to go find the correct {@link IPerson} to dig out the name).
25  *
26  * <p>This is provided for efficient access when doing various queries.
27  *
28  * @version $Id: Book.java,v 1.4 2004/02/19 17:37:39 hlship Exp $
29  * @author Howard Lewis Ship
30  *
31  **/

32
33 public class Book implements Serializable JavaDoc
34 {
35     private static final long serialVersionUID = -3423550323411938995L;
36     
37     /**
38      * Column index for the Book's primary key.
39      *
40      **/

41
42     public static final int ID_COLUMN = 0;
43
44     /**
45      * Column index for the book title.
46      *
47      **/

48
49     public static final int TITLE_COLUMN = 1;
50
51     public static final int DESCRIPTION_COLUMN = 2;
52     public static final int ISBN_COLUMN = 3;
53     public static final int OWNER_ID_COLUMN = 4;
54
55     /**
56      * Column index for a presentable version of the holder's name.
57      *
58      * @see IPerson#getNaturalName()
59      *
60      **/

61
62     public static final int OWNER_NAME_COLUMN = 5;
63
64     public static final int HOLDER_ID_COLUMN = 6;
65     public static final int HOLDER_NAME_COLUMN = 7;
66     public static final int PUBLISHER_ID_COLUMN = 8;
67     public static final int PUBLISHER_NAME_COLUMN = 9;
68     public static final int AUTHOR_COLUMN = 10;
69
70     public static final int HIDDEN_COLUMN = 11;
71     public static final int LENDABLE_COLUMN = 12;
72     public static final int DATE_ADDED_COLUMN = 13;
73
74     /**
75      * Number of columns in the result.
76      *
77      **/

78
79     public static final int N_COLUMNS = 14;
80
81     private Object JavaDoc[] columns;
82
83     /**
84      * Constructs a new BookQueryResult, making an internal copy of the columns passed.
85      *
86      **/

87
88     public Book(Object JavaDoc[] columns)
89     {
90         if (columns == null)
91             throw new IllegalArgumentException JavaDoc("Must provide a non-null columns.");
92
93         if (columns.length != N_COLUMNS)
94             throw new IllegalArgumentException JavaDoc("Wrong number of columns for a Book.");
95
96         this.columns = new Object JavaDoc[N_COLUMNS];
97         System.arraycopy(columns, 0, this.columns, 0, N_COLUMNS);
98     }
99
100     private Object JavaDoc get(int index)
101     {
102         return columns[index];
103     }
104
105     public Integer JavaDoc getId()
106     {
107         return (Integer JavaDoc) get(ID_COLUMN);
108     }
109
110     public String JavaDoc getTitle()
111     {
112         return (String JavaDoc) get(TITLE_COLUMN);
113     }
114
115     public String JavaDoc getDescription()
116     {
117         return (String JavaDoc) get(DESCRIPTION_COLUMN);
118     }
119
120     public String JavaDoc getISBN()
121     {
122         return (String JavaDoc) get(ISBN_COLUMN);
123     }
124
125     public Integer JavaDoc getOwnerId()
126     {
127         return (Integer JavaDoc) get(OWNER_ID_COLUMN);
128     }
129
130     public String JavaDoc getOwnerName()
131     {
132         return (String JavaDoc) get(OWNER_NAME_COLUMN);
133     }
134
135     public Integer JavaDoc getHolderId()
136     {
137         return (Integer JavaDoc) get(HOLDER_ID_COLUMN);
138     }
139
140     public String JavaDoc getHolderName()
141     {
142         return (String JavaDoc) get(HOLDER_NAME_COLUMN);
143     }
144
145     public Integer JavaDoc getPublisherId()
146     {
147         return (Integer JavaDoc) get(PUBLISHER_ID_COLUMN);
148     }
149
150     public String JavaDoc getPublisherName()
151     {
152         return (String JavaDoc) get(PUBLISHER_NAME_COLUMN);
153     }
154
155     public String JavaDoc getAuthor()
156     {
157         return (String JavaDoc) get(AUTHOR_COLUMN);
158     }
159
160     public Timestamp JavaDoc getDateAdded()
161     {
162         return (Timestamp JavaDoc) get(DATE_ADDED_COLUMN);
163     }
164
165     public String JavaDoc toString()
166     {
167         StringBuffer JavaDoc buffer;
168
169         buffer = new StringBuffer JavaDoc("Book[");
170         buffer.append(get(ID_COLUMN));
171         buffer.append(' ');
172         buffer.append(get(TITLE_COLUMN));
173         buffer.append(']');
174
175         return buffer.toString();
176     }
177
178     /**
179      * Returns true if the book is borrowed; that is, if its holder doesn't
180      * match its owner.
181      *
182      **/

183
184     public boolean isBorrowed()
185     {
186         return !get(HOLDER_ID_COLUMN).equals(get(OWNER_ID_COLUMN));
187     }
188
189     public boolean isHidden()
190     {
191         return getBit(HIDDEN_COLUMN);
192     }
193
194     public boolean isLendable()
195     {
196         return getBit(LENDABLE_COLUMN);
197     }
198
199     private boolean getBit(int column)
200     {
201         Boolean JavaDoc b = (Boolean JavaDoc) get(column);
202
203         return b.booleanValue();
204     }
205 }
Popular Tags