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
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 "\" />";
63 }
64
65 public String getCreated() {
66 return created;
67 }
68
69 public void setCreated(String created) {
70 this.created = created;
71 }
72
73 public long getId() {
74 return id;
75 }
76
77 public void setId(long id) {
78 this.id = id;
79 }
80
81 public void setId(Long id) {
82 this.id = id.longValue();
83 }
84
85 public String getIsbn() {
86 return isbn;
87 }
88
89 public void setIsbn(String isbn) {
90 this.isbn = isbn;
91 }
92
93 public long getOwner() {
94 return owner;
95 }
96
97 public void setOwner(long owner) {
98 this.owner = owner;
99 }
100
101 public void setOwner(Long owner) {
102 this.owner = owner.longValue();
103 }
104
105 public String getPub_date() {
106 return pub_date;
107 }
108
109 public void setPub_date(String pub_date) {
110 this.pub_date = pub_date;
111 }
112
113 public String getPublisher() {
114 return publisher;
115 }
116
117 public void setPublisher(String publisher) {
118 this.publisher = publisher;
119 }
120
121 public String getTitle() {
122 return title;
123 }
124
125 public void setTitle(String title) {
126 this.title = title;
127 }
128
129 public String getIsbnc() {
130 StringBuffer st = new StringBuffer();
131
132 for (int i=0;i<isbn.length();i++) {
133 char thisChar = isbn.charAt(i);
134 if (thisChar != '-')
135 st.append(isbn.charAt(i));
136 }
137
138 return st.toString();
139 }
140
141 /**
142 * Normalizes isbn numbers to n-nnn-nnnnn-n format by stripping
143 * all but digits and adding "-"'s in correct positions
144 *
145 * @param isbn - the input isbn string
146 */
147 public String normalizeIsbn(String isbn) {
148 if ((isbn == null) || (isbn.length() == 0)) {
149 return isbn;
150 }
151
152 String inString = isbn.trim();
153 StringBuffer out = new StringBuffer();
154 int digitCount = 0;
155 String thisChar = "";
156 for (int i = 0; i < isbn.length(); i++) {
157 thisChar = inString.substring(i,i+1);
158
159 if ((StringUtils.isNumeric(thisChar)) ||
160 (thisChar.equalsIgnoreCase("x"))) {
161 digitCount++;
162 out.append(thisChar);
163 if ((digitCount == 1) || (digitCount == 4) || (digitCount == 9))
164 out.append("-");
165 }
166 }
167 return out.toString();
168 }
169
170 /**
171 * Returns true if obj is a book and id, owner, title, publisher, and
172 * pub_date fields match the values of this exactly and the isbn fields
173 * match when "-"'s are removed. Allows null field values in comparisons.
174 *
175 * @param obj the object to test
176 * @return true if obj equals this
177 */
178 public boolean equals(Object obj) {
179 if (this == obj) {
180 return true;
181 }
182
183 if((obj == null) || !(obj instanceof Book)) {
184 return false;
185 }
186
187 Book book = (Book) obj;
188
189 if (this.title == null && book.getTitle() != null) {
190 return false;
191 }
192 if ((this.publisher != null && this.publisher.equals("")) &&
193 (book.getPublisher() != null && !book.getPublisher().equals(""))) {
194 return false;
195 }
196 if ((this.pub_date != null && this.pub_date.equals("")) &&
197 (book.getPub_date() != null && !book.getPub_date().equals(""))) {
198 return false;
199 }
200 if (( this.isbn != null && this.isbn.equals("")) &&
201 (book.getIsbn() != null && !book.getIsbn().equals(""))) {
202 return false;
203 }
204
205 return
206 (this.id == book.getId()) &&
207 (this.publisher == null ||
208 this.publisher.equals(book.getPublisher())) &&
209 (this.title == null ||
210 this.title.equals(book.getTitle())) &&
211 (this.pub_date == null ||
212 this.pub_date.equals(book.getPub_date())) &&
213 (this.owner == book.getOwner()) &&
214 (this.isbn == null ||
215 this.getIsbnc().equals(book.getIsbnc()));
216 }
217
218 public int hashCode() {
219 int hash = (int) this.id * 7;
220 hash = 17 * hash + (int) this.owner;
221 String cat = this.publisher + "|" + this.title +
222 "|" + this.getIsbnc() + "|" + this.pub_date;
223 return 31 * hash + cat.hashCode();
224 }
225 }