KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > hibernate > Item


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: Item.java,v 1.19 2004/09/02 09:07:56 spyromus Exp $
28

29 package de.nava.informa.impl.hibernate;
30
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Date JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.jdom.Element;
39
40 import de.nava.informa.core.CategoryIF;
41 import de.nava.informa.core.ChannelIF;
42 import de.nava.informa.core.ItemEnclosureIF;
43 import de.nava.informa.core.ItemGuidIF;
44 import de.nava.informa.core.ItemIF;
45 import de.nava.informa.core.ItemSourceIF;
46 import de.nava.informa.utils.XmlPathUtils;
47
48 /**
49  * Hibernate implementation of the ItemIF interface.
50  *
51  * @author Niko Schmuck (niko@nava.de)
52  *
53  * @hibernate.class
54  * table="ITEMS"
55  */

56 public class Item implements ItemIF, java.io.Serializable JavaDoc {
57
58   private static final Log LOG = LogFactory.getLog(Item.class);
59
60   private int id = -1;
61   private String JavaDoc title;
62   private String JavaDoc description;
63   private URL JavaDoc link;
64   private Collection JavaDoc categories;
65   private String JavaDoc creator;
66   private String JavaDoc subject;
67   private Date JavaDoc date;
68   private Date JavaDoc found;
69   private ItemGuidIF guid;
70   private URL JavaDoc comments;
71   private ItemEnclosureIF enclosure;
72   private ItemSourceIF source;
73   private ChannelIF channel;
74   private Element itemElement;
75   private boolean unRead;
76
77   public Item() {
78     this(null, null, "[Unnamed item]", null, null);
79   }
80
81   public Item(String JavaDoc title, String JavaDoc description, URL JavaDoc link) {
82     this(null, null, title, description, link);
83   }
84
85   public Item(ChannelIF channel, String JavaDoc title, String JavaDoc description, URL JavaDoc link) {
86     this(null, channel, title, description, link);
87   }
88
89   public Item(Element itemElement, String JavaDoc title, String JavaDoc description, URL JavaDoc link) {
90     this(itemElement, null, title, description, link);
91   }
92
93   public Item(Element itemElement, ChannelIF channel, String JavaDoc title, String JavaDoc description, URL JavaDoc link) {
94     this.itemElement = itemElement;
95     this.channel = channel;
96     this.title = title;
97     this.description = description;
98     this.link = link;
99     this.categories = new ArrayList JavaDoc();
100     this.unRead = true;
101   }
102
103   // --------------------------------------------------------------
104
// implementation of ItemIF interface
105
// --------------------------------------------------------------
106

107   /**
108    * @hibernate.id
109    * column="ITEM_ID"
110    * generator-class="native"
111    * type="integer"
112    * unsaved-value="-1"
113    *
114    * @return integer representation of identity.
115    */

116   public int getIntId() {
117     return id;
118   }
119
120   public void setIntId(int anId) {
121     this.id = anId;
122   }
123
124   public long getId() {
125     return id;
126   }
127
128   public void setId(long longid) {
129     this.id = (int) longid;
130   }
131
132   /**
133    * @hibernate.many-to-one
134    * column="CHANNEL_ID"
135    * class="de.nava.informa.impl.hibernate.Channel"
136    * not-null="true"
137    *
138    * @return parent channel.
139    */

140   public ChannelIF getChannel() {
141     return channel;
142   }
143
144   public void setChannel(ChannelIF parentChannel) {
145     this.channel = parentChannel;
146   }
147
148   /**
149    * @hibernate.property
150    * column="TITLE"
151    * not-null="true"
152    *
153    * @return title.
154    */

155   public String JavaDoc getTitle() {
156     return title;
157   }
158
159   public void setTitle(String JavaDoc aTitle) {
160     this.title = aTitle;
161   }
162
163   /**
164    * @hibernate.property
165    * column="DESCRIPTION"
166    *
167    * @return description.
168    */

169   public String JavaDoc getDescription() {
170     return description;
171   }
172
173   public void setDescription(String JavaDoc aDescription) {
174     this.description = aDescription;
175   }
176
177   /**
178    * @hibernate.property
179    * column="UNREAD"
180    *
181    * @return unread flag.
182    */

183   public boolean getUnRead() {
184     return unRead;
185   }
186
187   public void setUnRead(boolean val) {
188     this.unRead = val;
189   }
190
191   /**
192    * @hibernate.property
193    * column="LINK"
194    *
195    * @return link to original article.
196    */

197   public URL JavaDoc getLink() {
198     return link;
199   }
200
201   public void setLink(URL JavaDoc aLink) {
202     this.link = aLink;
203   }
204
205   /**
206    * @hibernate.bag
207    * table="CAT_ITEM_LINK"
208    * lazy="true"
209    * @hibernate.collection-key
210    * column="ITEM_ID"
211    * @hibernate.collection-many-to-many
212    * class="de.nava.informa.impl.hibernate.Category"
213    * column="CATEGORY_ID"
214    *
215    * @return categories.
216    */

217   public Collection JavaDoc getCategories() {
218     return categories;
219   }
220
221   public void setCategories(Collection JavaDoc aCategories) {
222     this.categories = aCategories;
223   }
224
225   public void addCategory(CategoryIF category) {
226     categories.add(category);
227   }
228
229   public void removeCategory(CategoryIF category) {
230     categories.remove(category);
231   }
232
233   /**
234    * @hibernate.property
235    * column="CREATOR"
236    *
237    * @return creator.
238    */

239   public String JavaDoc getCreator() {
240     return creator;
241   }
242
243   public void setCreator(String JavaDoc aCreator) {
244     this.creator = aCreator;
245   }
246
247   /**
248    * @hibernate.property
249    * column="SUBJECT"
250    *
251    * @return subject.
252    */

253   public String JavaDoc getSubject() {
254     return subject;
255   }
256
257   public void setSubject(String JavaDoc aSubject) {
258     this.subject = aSubject;
259   }
260
261   /**
262    * @hibernate.property
263    * column="DATE"
264    *
265    * @return date.
266    */

267   public Date JavaDoc getDate() {
268     return date;
269   }
270
271   public void setDate(Date JavaDoc aDate) {
272     this.date = aDate;
273   }
274
275   /**
276    * @hibernate.property
277    * column="FOUND"
278    *
279    * @return date when item was found.
280    */

281   public Date JavaDoc getFound() {
282     return found;
283   }
284
285   public void setFound(Date JavaDoc foundDate) {
286     this.found = foundDate;
287   }
288
289   /**
290    * @hibernate.many-to-one
291    * class="de.nava.informa.impl.hibernate.ItemGuid"
292    * column="GUID"
293    *
294    * @return guid.
295    */

296   public ItemGuidIF getGuid() {
297     return guid;
298   }
299
300   public void setGuid(ItemGuidIF guid) {
301     this.guid = guid;
302   }
303
304   /**
305    * @hibernate.property
306    * column="COMMENTS"
307    *
308    * @return comments.
309    */

310   public URL JavaDoc getComments() {
311     return comments;
312   }
313
314   public void setComments(URL JavaDoc comments) {
315     this.comments = comments;
316   }
317
318   /**
319    * @hibernate.many-to-one
320    * class="de.nava.informa.impl.hibernate.ItemSource"
321    * column="SOURCE"
322    *
323    * @return source.
324    */

325   public ItemSourceIF getSource() {
326     return source;
327   }
328
329   public void setSource(ItemSourceIF aSource) {
330     this.source = aSource;
331   }
332
333   /**
334    * @hibernate.many-to-one
335    * class="de.nava.informa.impl.hibernate.ItemEnclosure"
336    * column="ENCLOSURE"
337    *
338    * @return enclosure.
339    */

340   public ItemEnclosureIF getEnclosure() {
341     return enclosure;
342   }
343
344   public void setEnclosure(ItemEnclosureIF anEnclosure) {
345     this.enclosure = anEnclosure;
346   }
347
348   public String JavaDoc getElementValue(final String JavaDoc path) {
349     return XmlPathUtils.getElementValue(itemElement, path);
350   }
351
352   public String JavaDoc[] getElementValues(final String JavaDoc path, final String JavaDoc[] elements) {
353     return XmlPathUtils.getElementValues(itemElement, path, elements);
354   }
355
356   public String JavaDoc getAttributeValue(final String JavaDoc path, final String JavaDoc attribute) {
357     return XmlPathUtils.getAttributeValue(itemElement, path, attribute);
358   }
359
360   public String JavaDoc[] getAttributeValues(final String JavaDoc path, final String JavaDoc[] attributes) {
361     return XmlPathUtils.getAttributeValues(itemElement, path, attributes);
362   }
363
364   // ----------------------------------------------------------------------
365
// overwrite default method implementation from Object
366
// ----------------------------------------------------------------------
367

368   /**
369    * Returns a string representation of the object.
370    *
371    * @return a string representation of the object.
372    */

373   public String JavaDoc toString() {
374     return "[Item (" + id + "): " + title + "]";
375   }
376
377   // ----------------------------------------------------------------------
378
// overwrite default method implementation from Object
379
// ----------------------------------------------------------------------
380

381   /**
382    * Indicates whether some other object is "equal to" this one.
383    *
384    * @param obj the reference object with which to compare.
385    *
386    * @return <code>true</code> if this object is the same as the obj
387    * argument; <code>false</code> otherwise.
388    *
389    * @see #hashCode()
390    */

391   public boolean equals(Object JavaDoc obj) {
392     if (!(obj instanceof ItemIF)) {
393       return false;
394     }
395     ItemIF cmp = (ItemIF) obj;
396
397     boolean te;
398     if (title != null) {
399       te = title.equals(cmp.getTitle());
400     } else {
401       te = (cmp.getTitle() == null);
402     }
403 // TODO: please check if this part of comparison is necessary and remove commented code if not.
404
// boolean de;
405
// if (description != null) {
406
// de = description.equals(cmp.getDescription());
407
// } else {
408
// de = (cmp.getDescription() == null);
409
// }
410
boolean le;
411     if (link != null) {
412       // Comparison of links uses synchronized code of Java-NET.
413
// This may hurt multi-threaded applications. So, please think twice
414
// before using direct comparison of links.
415
le = link.toString().equals(cmp.getLink().toString());
416     } else {
417       le = (cmp.getLink() == null);
418     }
419     boolean result = te && le;
420     LOG.debug("Item.equals = " + result + "(te=" + te + ",le=" + le + ")");
421     return (result);
422   }
423
424   /**
425    * Returns a hash code value for the object. This method is
426    * supported for the benefit of hashtables such as those provided by
427    * <code>java.util.Hashtable</code>.
428    *
429    * @return a hash code value for this object.
430    */

431   public int hashCode() {
432     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
433     // This looks like a bug because it is not symmetrical with the accompanying equals().
434
// sb.append(title).append(description).append(link);
435
sb.append(title).append(link);
436     return sb.toString().hashCode();
437   }
438 }
439
Popular Tags