KickJava   Java API By Example, From Geeks To Geeks.

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


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: Channel.java,v 1.30 2004/09/02 09:07:56 spyromus Exp $
28

29 package de.nava.informa.impl.hibernate;
30
31 import de.nava.informa.core.CategoryIF;
32 import de.nava.informa.core.ChannelFormat;
33 import de.nava.informa.core.ChannelIF;
34 import de.nava.informa.core.ChannelObserverIF;
35 import de.nava.informa.core.CloudIF;
36 import de.nava.informa.core.ImageIF;
37 import de.nava.informa.core.ItemIF;
38 import de.nava.informa.core.TextInputIF;
39 import de.nava.informa.utils.XmlPathUtils;
40 import org.jdom.Element;
41
42 import java.net.MalformedURLException JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.HashSet JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Set JavaDoc;
50
51 /**
52  * Hibernate implementation of the ChannelIF interface.
53  *
54  * @author Niko Schmuck (niko@nava.de)
55  *
56  * @hibernate.class
57  * table="CHANNELS"
58  */

59 public class Channel implements ChannelIF, java.io.Serializable JavaDoc {
60
61   private int id = -1;
62   private String JavaDoc title;
63   private String JavaDoc description;
64   private URL JavaDoc location;
65   private URL JavaDoc site;
66   private String JavaDoc creator;
67   private String JavaDoc publisher;
68   private String JavaDoc language;
69   private ChannelFormat format;
70   private Collection JavaDoc items;
71   private Set JavaDoc groups;
72   private CloudIF cloud;
73   private ImageIF image;
74   private TextInputIF textInput;
75   private String JavaDoc copyright;
76   private Collection JavaDoc categories;
77   private Date JavaDoc lastUpdated;
78   private Date JavaDoc lastBuild;
79   private Date JavaDoc pubDate;
80
81   private String JavaDoc rating;
82   private String JavaDoc generator;
83   private String JavaDoc docs;
84   private int ttl = -1;
85   private Element channelElement;
86
87   // RSS 1.0 Syndication Module values
88
private String JavaDoc updatePeriod = UPDATE_DAILY;
89   private int updateFrequency = 1;
90   private Date JavaDoc updateBase;
91
92   private transient Collection JavaDoc observers;
93
94   public Channel() {
95     this((String JavaDoc) null);
96   }
97
98   public Channel(String JavaDoc title) {
99     this(null, title);
100   }
101
102   public Channel(Element channelElement) {
103     this(channelElement, "Unnamed channel");
104   }
105
106   public Channel(Element channelElement, String JavaDoc title) {
107     this.channelElement = channelElement;
108     this.title = title;
109     this.items = new ArrayList JavaDoc();
110     this.categories = new ArrayList JavaDoc();
111     this.observers = new ArrayList JavaDoc();
112     this.groups = new HashSet JavaDoc();
113     this.format = ChannelFormat.UNKNOWN_CHANNEL_FORMAT;
114     this.lastUpdated = new Date JavaDoc();
115   }
116
117   // --------------------------------------------------------------
118
// implementation of ChannelIF interface
119
// --------------------------------------------------------------
120

121   /**
122    * @hibernate.id
123    * generator-class="native"
124    * column="CHANNEL_ID"
125    * type="integer"
126    * unsaved-value="-1"
127    *
128    * @return integer representation of identity.
129    */

130   public int getIntId() {
131     return id;
132   }
133
134   public void setIntId(int anId) {
135     this.id = anId;
136   }
137
138   public long getId() {
139     return id;
140   }
141
142   public void setId(long longid) {
143     this.id = (int) longid;
144   }
145
146   /**
147    * @hibernate.property
148    * column="TITLE"
149    * not-null="true"
150    *
151    * @return title.
152    */

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

167   public String JavaDoc getDescription() {
168     return description;
169   }
170
171   public void setDescription(String JavaDoc aDescription) {
172     this.description = aDescription;
173   }
174
175 // We store the Location as a text string in the database, but as a URL in the memory based object.
176
// As far as Hibernate is concerned this is a STRING property. However the getter and setter
177
// convert to and from text for Informa.
178

179   /**
180    * @hibernate.property
181    * column="LOCSTRING"
182    * type="string"
183    *
184    * @return location as a string.
185    */

186   public String JavaDoc getLocationString() {
187     return (location == null) ? "" : location.toString();
188   }
189
190   public void setLocationString(String JavaDoc loc) {
191     if (loc == null || loc.trim().length() == 0) {
192       location = null;
193       return;
194     } else {
195       try {
196         this.location = new URL JavaDoc(loc);
197       } catch (MalformedURLException JavaDoc e) {
198         e.printStackTrace();
199       }
200     }
201   }
202
203   public URL JavaDoc getLocation() {
204     return location;
205   }
206
207   public void setLocation(URL JavaDoc location) {
208     this.location = location;
209   }
210
211
212   /**
213    * @hibernate.property
214    * column="SITE"
215    *
216    * @return URL of the site.
217    */

218   public URL JavaDoc getSite() {
219     return site;
220   }
221
222   public void setSite(URL JavaDoc siteUrl) {
223     this.site = siteUrl;
224   }
225
226   /**
227    * @hibernate.property
228    * column="CREATOR"
229    *
230    * @return name of creator.
231    */

232   public String JavaDoc getCreator() {
233     return creator;
234   }
235
236   public void setCreator(String JavaDoc aCreator) {
237     this.creator = aCreator;
238   }
239
240   /**
241    * @hibernate.property
242    * column="PUBLISHER"
243    *
244    * @return publisher.
245    */

246   public String JavaDoc getPublisher() {
247     return publisher;
248   }
249
250   public void setPublisher(String JavaDoc aPublisher) {
251     this.publisher = aPublisher;
252   }
253
254   /**
255    * @hibernate.property
256    * column="LANGUAGE"
257    *
258    * @return language of channel.
259    */

260   public String JavaDoc getLanguage() {
261     return language;
262   }
263
264   public void setLanguage(String JavaDoc aLanguage) {
265     this.language = aLanguage;
266   }
267
268   /**
269    * @hibernate.property
270    * column="FORMAT"
271    * type="string"
272    *
273    * @return format string.
274    */

275   public String JavaDoc getFormatString() {
276     return format.toString();
277   }
278
279   public void setFormatString(String JavaDoc strFormat) {
280     // TODO: this could be improved by a format resolver
281
if (strFormat.equals(ChannelFormat.RSS_0_90.toString())) {
282       format = ChannelFormat.RSS_0_90;
283     } else if (strFormat.equals(ChannelFormat.RSS_0_91.toString())) {
284       format = ChannelFormat.RSS_0_91;
285     } else if (strFormat.equals(ChannelFormat.RSS_0_92.toString())) {
286       format = ChannelFormat.RSS_0_92;
287     } else if (strFormat.equals(ChannelFormat.RSS_0_93.toString())) {
288       format = ChannelFormat.RSS_0_93;
289     } else if (strFormat.equals(ChannelFormat.RSS_0_94.toString())) {
290       format = ChannelFormat.RSS_0_94;
291     } else if (strFormat.equals(ChannelFormat.RSS_1_0.toString())) {
292       format = ChannelFormat.RSS_1_0;
293     } else if (strFormat.equals(ChannelFormat.RSS_2_0.toString())) {
294       format = ChannelFormat.RSS_2_0;
295     }
296   }
297
298   public ChannelFormat getFormat() {
299     return format;
300   }
301
302   public void setFormat(ChannelFormat aFormat) {
303     this.format = aFormat;
304   }
305
306   /**
307    * @hibernate.set
308    * table="CAT_GROUP_CHANNEL"
309    * lazy="true"
310    * inverse="true"
311    * @hibernate.collection-key
312    * column="CHANNEL_ID"
313    * @hibernate.collection-many-to-many
314    * class="de.nava.informa.impl.hibernate.ChannelGroup"
315    * column="GROUP_ID"
316    *
317    * @return set of groups.
318    */

319   public Set JavaDoc getGroups() {
320     return groups;
321   }
322
323   public void setGroups(Set JavaDoc aGroups) {
324     this.groups = aGroups;
325   }
326
327   /**
328    * @hibernate.bag
329    * table="ITEMS"
330    * cascade="all"
331    * inverse="true"
332    * @hibernate.collection-key
333    * column="CHANNEL_ID"
334    * @hibernate.collection-one-to-many
335    * class="de.nava.informa.impl.hibernate.Item"
336    *
337    * @return items of channel.
338    */

339   public Collection JavaDoc getItems() {
340     return items;
341   }
342
343   public void setItems(Collection JavaDoc anItems) {
344     this.items = anItems;
345   }
346
347   public void addItem(ItemIF item) {
348     items.add(item);
349     item.setChannel(this);
350     notifyObserversItemAdded(item);
351   }
352
353   public void removeItem(ItemIF item) {
354     items.remove(item);
355   }
356
357   public ItemIF getItem(long itemId) {
358     // TODO: improve performance
359
// hibernate query cannot be used (not possible: no session object)
360
// may be use transient map: items.get(new Long(id));
361
ItemIF theItem = null;
362     Iterator JavaDoc it = items.iterator();
363     while (it.hasNext()) {
364       ItemIF curItem = (ItemIF) it.next();
365       if (curItem.getId() == itemId) {
366         theItem = curItem;
367         break;
368       }
369     }
370     return theItem;
371   }
372
373   /**
374    * @hibernate.many-to-one
375    * column="IMAGE_ID"
376    * class="de.nava.informa.impl.hibernate.Image"
377    * not-null="false"
378    *
379    * @return image.
380    */

381   public ImageIF getImage() {
382     return image;
383   }
384
385   public void setImage(ImageIF anImage) {
386     this.image = anImage;
387   }
388
389   /**
390    * @hibernate.many-to-one
391    * column="TEXTINPUT_ID"
392    * class="de.nava.informa.impl.hibernate.TextInput"
393    * not-null="false"
394    *
395    * @return text input.
396    */

397   public TextInputIF getTextInput() {
398     return textInput;
399   }
400
401   public void setTextInput(TextInputIF aTextInput) {
402     this.textInput = aTextInput;
403   }
404
405   /**
406    * @hibernate.property
407    * column="COPYRIGHT"
408    *
409    * @return copyright note.
410    */

411   public String JavaDoc getCopyright() {
412     return copyright;
413   }
414
415   public void setCopyright(String JavaDoc aCopyright) {
416     this.copyright = aCopyright;
417   }
418
419   /**
420    * @hibernate.property
421    * column="RATING"
422    *
423    * @return rating.
424    */

425   public String JavaDoc getRating() {
426     return rating;
427   }
428
429   public void setRating(String JavaDoc aRating) {
430     this.rating = aRating;
431   }
432
433   /**
434    * @hibernate.many-to-one
435    * column="CLOUD_ID"
436    * class="de.nava.informa.impl.hibernate.Cloud"
437    * not-null="false"
438    *
439    * @return cloud.
440    */

441   public CloudIF getCloud() {
442     return cloud;
443   }
444
445   public void setCloud(CloudIF aCloud) {
446     this.cloud = aCloud;
447   }
448
449   /**
450    * @hibernate.property
451    * column="GENERATOR"
452    *
453    * @return generator.
454    */

455   public String JavaDoc getGenerator() {
456     return generator;
457   }
458
459   public void setGenerator(String JavaDoc aGenerator) {
460     this.generator = aGenerator;
461   }
462
463   /**
464    * @hibernate.property
465    * column="DOCS"
466    *
467    * @return docs.
468    */

469   public String JavaDoc getDocs() {
470     return docs;
471   }
472
473   public void setDocs(String JavaDoc aDocs) {
474     this.docs = aDocs;
475   }
476
477   /**
478    * @hibernate.property
479    * column="TTL"
480    *
481    * @return TTL value.
482    */

483   public int getTtl() {
484     return ttl;
485   }
486
487   public void setTtl(int aTtl) {
488     this.ttl = aTtl;
489   }
490
491   /**
492    * @hibernate.bag
493    * table="CAT_CHANNEL_LINK"
494    * lazy="true"
495    * @hibernate.collection-key
496    * column="CHANNEL_ID"
497    * @hibernate.collection-many-to-many
498    * class="de.nava.informa.impl.hibernate.Category"
499    * column="CATEGORY_ID"
500    *
501    * @return categories.
502    */

503   public Collection JavaDoc getCategories() {
504     return categories;
505   }
506
507   public void setCategories(Collection JavaDoc aCategories) {
508     this.categories = aCategories;
509   }
510
511   public void addCategory(CategoryIF category) {
512     categories.add(category);
513   }
514
515   public void removeCategory(CategoryIF category) {
516     categories.remove(category);
517   }
518
519   /**
520    * @hibernate.property
521    * column="LAST_UPDATED"
522    *
523    * @return date of last update.
524    */

525   public Date JavaDoc getLastUpdated() {
526     return lastUpdated;
527   }
528
529   public void setLastUpdated(Date JavaDoc date) {
530     this.lastUpdated = date;
531     notifyObserversChannelUpdated();
532   }
533
534   /**
535    * @hibernate.property
536    * column="LAST_BUILD_DATE"
537    *
538    * @return date of last builing.
539    */

540   public Date JavaDoc getLastBuildDate() {
541     return lastBuild;
542   }
543
544   public void setLastBuildDate(Date JavaDoc date) {
545     this.lastBuild = date;
546   }
547
548   /**
549    * @hibernate.property
550    * column="PUB_DATE"
551    *
552    * @return publication date.
553    */

554   public Date JavaDoc getPubDate() {
555     return pubDate;
556   }
557
558   public void setPubDate(Date JavaDoc date) {
559     this.pubDate = date;
560   }
561
562   // RSS 1.0 Syndication Module methods
563

564   /**
565    * @hibernate.property
566    * column="UPDATE_PERIOD"
567    *
568    * @return update period.
569    */

570   public String JavaDoc getUpdatePeriod() {
571     return updatePeriod;
572   }
573
574   public void setUpdatePeriod(String JavaDoc anUpdatePeriod) {
575     this.updatePeriod = anUpdatePeriod;
576   }
577
578   /**
579    * @hibernate.property
580    * column="UPDATE_FREQUENCY"
581    *
582    * @return update frequency.
583    */

584   public int getUpdateFrequency() {
585     return updateFrequency;
586   }
587
588   public void setUpdateFrequency(int anUpdateFrequency) {
589     this.updateFrequency = anUpdateFrequency;
590   }
591
592   /**
593    * @hibernate.property
594    * column="UPDATE_BASE"
595    *
596    * @return update base.
597    */

598   public Date JavaDoc getUpdateBase() {
599     return updateBase;
600   }
601
602   public void setUpdateBase(Date JavaDoc date) {
603     this.updateBase = date;
604   }
605
606   public String JavaDoc getElementValue(final String JavaDoc path) {
607     return XmlPathUtils.getElementValue(channelElement, path);
608   }
609
610   public String JavaDoc[] getElementValues(final String JavaDoc path, final String JavaDoc[] elements) {
611     return XmlPathUtils.getElementValues(channelElement, path, elements);
612   }
613
614   public String JavaDoc getAttributeValue(final String JavaDoc path, final String JavaDoc attribute) {
615     return XmlPathUtils.getAttributeValue(channelElement, path, attribute);
616   }
617
618   public String JavaDoc[] getAttributeValues(final String JavaDoc path, final String JavaDoc[] attributes) {
619     return XmlPathUtils.getAttributeValues(channelElement, path, attributes);
620   }
621
622   // --------------------------------------------------------------
623
// implementation of ChannelObservableIF interface
624
// --------------------------------------------------------------
625

626   public void addObserver(ChannelObserverIF o) {
627     observers.add(o);
628   }
629
630   public void removeObserver(ChannelObserverIF o) {
631     observers.remove(o);
632   }
633
634   // --------------------------------------------------------------
635
// overwrite default method implementation from Object
636
// --------------------------------------------------------------
637

638   /**
639    * Returns a string representation of the object.
640    *
641    * @return a string representation of the object.
642    */

643   public String JavaDoc toString() {
644     return "[Hibernate Channel (" + id + "): " + title + "("
645       + getItems().size() + ")( " + location + " )]";
646   }
647
648   /**
649    * Compare two Channels for equality. Implementing this method and hashCode
650    * correctly is CRITICAL for Hibernate to function correctly. The semantic is
651    * that two Channels arensidered the 'same' RSS Channel. They may at one
652    * point in time have different values for different properties, but are
653    * they the SAME Channel? This is a very subtle Hibernate point, WHICH I
654    * AM 90% Sure I got right. In this case, two Channels are equal specifically
655    * if their RSS URL are the same. In other words, even if the title is
656    * different or the description is different, it's still the same Channel.
657    *
658    * @param obj the reference object with which to compare.
659    *
660    * @return <code>true</code> if this object is the same as the obj
661    * argument; <code>false</code> otherwise.
662    */

663    public boolean equals(Object JavaDoc obj) {
664     if (!(obj instanceof ChannelIF)) {
665       return false;
666     }
667     ChannelIF cmp = (ChannelIF) obj;
668
669     // Comparison of links uses synchronized code of Java-NET.
670
// This may hurt multi-threaded applications. So, please think twice
671
// before using direct comparison of links.
672
boolean res = getLocation().toString().equals(cmp.getLocation().toString());
673
674     return res;
675     }
676
677   /**
678    * Hashcode, like equals, is touchy and critical for proper functioning of
679    * Hibernate.
680    *
681    * @return a hash code value for this object.
682    */

683   public int hashCode() {
684     return location.toString().hashCode();
685   }
686
687   /**
688    * Loops through and notifies each observer if a new item was
689    * detected.
690    *
691    * @param newItem item added.
692    */

693   public void notifyObserversItemAdded(ItemIF newItem) {
694     Iterator JavaDoc it = observers.iterator();
695     while (it.hasNext()) {
696       ChannelObserverIF o = (ChannelObserverIF) it.next();
697       o.itemAdded(newItem);
698     }
699   }
700
701   /**
702    * Loops through and notifies each observer if a new item was
703    * detected.
704    */

705   public void notifyObserversChannelUpdated() {
706     Iterator JavaDoc it = observers.iterator();
707     while (it.hasNext()) {
708       ChannelObserverIF o = (ChannelObserverIF) it.next();
709       o.channelRetrieved(this);
710     }
711   }
712 }
713
Popular Tags