View Javadoc

1   package org.sourceforge.vlibrary.user.domain;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   
6   
7   /**
8    * Represents a book.
9    * @version $Revision$ $Date$
10   */
11  
12  public class Book {
13      private String title = null;
14      private long id = 0;
15      private String publisher = "";
16      private String pub_date = "";
17      private String created = "";
18      private long owner = 0;
19      private String isbn = "";
20      private int copy = 0;
21      private int editable =0;
22  
23      public Book() {
24      }
25  
26  
27      public Book(String title,
28       long id,
29       String publisher,
30       String pub_date,
31       String created,
32       long owner,
33       String isbn
34       ) {
35          this.title = title;
36          this.id = id;
37          this.publisher= publisher;
38          this.pub_date = pub_date;
39          this.created = created;
40          this.owner = owner;
41          this.isbn = isbn;
42      }
43  
44      public void setEditable( int editable ) {
45          this.editable = editable;
46      }
47  
48      public int getEditable() {
49          return this.editable;
50      }
51  
52      public String toString() {
53          return  "<Book "            +
54           "id=\""             + id +
55           "\" title=\""       + title +
56           "\" publisher=\""   + publisher +
57           "\" pub_date=\""    + pub_date +
58           "\" created=\""     + created +
59           "\" owner=\""       + owner +
60           "\" isbn=\""        + isbn +
61           "\" editable=\""    + editable +
62            "\" copy=\""    + copy +
63           "\" />";
64      }
65  
66      public String getCreated() {
67          return created;
68      }
69  
70      public void setCreated(String created) {
71          this.created = created;
72      }
73  
74      public long getId() {
75          return id;
76      }
77  
78      public void setId(long id) {
79          this.id = id;
80      }
81  
82      public void setId(Long id) {
83          this.id = id.longValue();
84      }
85  
86      public String getIsbn() {
87          return isbn;
88      }
89  
90      public void setIsbn(String isbn) {
91          this.isbn = isbn;
92      }
93  
94      public long getOwner() {
95          return owner;
96      }
97  
98      public void setOwner(long owner) {
99          this.owner = owner;
100     }
101 
102     public void setOwner(Long owner) {
103         this.owner = owner.longValue();
104     }
105 
106     public String getPub_date() {
107         return pub_date;
108     }
109 
110     public void setPub_date(String pub_date) {
111         this.pub_date = pub_date;
112     }
113 
114     public String getPublisher() {
115         return publisher;
116     }
117 
118     public void setPublisher(String publisher) {
119         this.publisher = publisher;
120     }
121 
122     public String getTitle() {
123         return title;
124     }
125 
126     public void setTitle(String title) {
127         this.title = title;
128     }
129 
130     public String getIsbnc() {
131         StringBuffer st = new StringBuffer();
132 
133         for (int i=0;i<isbn.length();i++) {
134             char thisChar = isbn.charAt(i);
135             if (thisChar != '-')
136                 st.append(isbn.charAt(i));
137         }
138 
139         return st.toString();
140     }
141 
142     public int getCopy() {
143         return copy;
144     }
145 
146     public void setCopy(int copy) {
147         this.copy = copy;
148     }
149 
150     /**
151      * Normalizes isbn numbers to n-nnn-nnnnn-n format by stripping
152      * all but digits and adding "-"'s in correct positions
153      *
154      * @param isbn - the input isbn string
155      */
156     public String normalizeIsbn(String isbn) {
157         if ((isbn == null) || (isbn.length() == 0)) {
158             return isbn;
159         }
160 
161         String inString = isbn.trim();
162         StringBuffer out = new StringBuffer();
163         int digitCount = 0;
164         String thisChar = "";
165         for (int i = 0; i < isbn.length(); i++) {
166             thisChar = inString.substring(i,i+1);
167 
168             if ((StringUtils.isNumeric(thisChar)) ||
169              (thisChar.equalsIgnoreCase("x"))) {
170                 digitCount++;
171                 out.append(thisChar);
172                 if ((digitCount == 1) || (digitCount == 4) || (digitCount == 9))
173                     out.append("-");
174             }
175         }
176         return out.toString();
177     }
178 
179     /**
180      * Returns true if obj is a book and id, owner, title, publisher, copy and
181      * pub_date fields match the values of this exactly and the isbn fields
182      * match when "-"'s are removed. Allows null field values in comparisons.
183      *
184      * @param obj the object to test
185      * @return true if obj equals this
186      */
187     public boolean equals(Object obj) {
188         if (this == obj) {
189             return true;
190         }
191 
192         if((obj == null) || !(obj instanceof Book)) {
193             return false;
194         }
195 
196         Book book = (Book) obj;
197 
198         if (this.title == null && book.getTitle() != null) {
199             return false;
200         }
201         if ((this.publisher != null && this.publisher.equals("")) &&
202          (book.getPublisher() != null && !book.getPublisher().equals(""))) {
203             return false;
204         }
205         if ((this.pub_date != null && this.pub_date.equals("")) &&
206          (book.getPub_date() != null && !book.getPub_date().equals(""))) {
207             return false;
208         }
209         if (( this.isbn != null && this.isbn.equals("")) &&
210          (book.getIsbn() != null && !book.getIsbn().equals(""))) {
211             return false;
212         }
213 
214         return
215          (this.id == book.getId()) &&
216          (this.publisher == null ||
217          this.publisher.equals(book.getPublisher())) &&
218          (this.title == null ||
219          this.title.equals(book.getTitle())) &&
220          (this.pub_date == null ||
221          this.pub_date.equals(book.getPub_date())) &&
222          (this.copy == book.copy) &&
223          (this.owner == book.getOwner()) &&
224          (this.isbn == null ||
225          this.getIsbnc().equals(book.getIsbnc()));
226     }
227 
228 
229     public int hashCode() {
230         int hash = (int) this.id * 7;
231         hash = 17 * hash + (int) this.owner;
232         String cat = this.publisher + "|" + this.title +
233          "|" + this.getIsbnc() + "|" + this.pub_date;
234         return 31 * hash + 19 * copy + cat.hashCode();
235     }
236 }