1 26 27 29 package de.nava.informa.impl.basic; 30 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Date ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 39 import org.jdom.Element; 40 41 import de.nava.informa.core.CategoryIF; 42 import de.nava.informa.core.ChannelFormat; 43 import de.nava.informa.core.ChannelIF; 44 import de.nava.informa.core.ChannelObserverIF; 45 import de.nava.informa.core.CloudIF; 46 import de.nava.informa.core.ImageIF; 47 import de.nava.informa.core.ItemIF; 48 import de.nava.informa.core.TextInputIF; 49 import de.nava.informa.utils.XmlPathUtils; 50 51 56 public class Channel implements ChannelIF, java.io.Serializable { 57 58 private long id; 59 private String title; 60 private String description; 61 private URL location; 62 private URL site; 63 private String creator; 64 private String publisher; 65 private String language; 66 private ChannelFormat format; 67 private Map items; 68 private ImageIF image; 69 private CloudIF cloud; 70 private TextInputIF textInput; 71 private String copyright; 72 private Collection categories; 73 private Date lastUpdated; 74 private Date lastBuild; 75 private Date pubDate; 76 private String rating; 77 private String generator; 78 private String docs; 79 private int ttl = -1; 80 private Element channelElement; 81 82 private String updatePeriod = null; 86 private int updateFrequency = -1; 87 private Date updateBase; 88 89 private static int hasLinkedMap = -1; 94 95 private static int hasLinkedHashMap = -1; 100 101 private transient Collection observers; 102 103 public Channel() { 104 this((String ) null); 105 } 106 107 public Channel(String title) { 108 this(null, title); 109 } 110 111 public Channel(Element channelElement) { 112 this(channelElement, "Unnamed channel"); 113 } 114 115 public Channel(Element channelElement, String title) { 116 this.id = IdGenerator.getInstance().getId(); 117 this.channelElement = channelElement; 118 this.title = title; 119 this.items = getMap(); 120 this.categories = new ArrayList (); 121 this.observers = new ArrayList (); 122 this.format = ChannelFormat.UNKNOWN_CHANNEL_FORMAT; 123 this.lastUpdated = new Date (); 124 } 125 126 130 public long getId() { 131 return id; 132 } 133 134 public void setId(long id) { 135 this.id = id; 136 } 137 138 public String getTitle() { 139 return title; 140 } 141 142 public void setTitle(String title) { 143 this.title = title; 144 } 145 146 public String getDescription() { 147 return description; 148 } 149 150 public void setDescription(String description) { 151 this.description = description; 152 } 153 154 public URL getLocation() { 155 return location; 156 } 157 158 public void setLocation(URL location) { 159 this.location = location; 160 } 161 162 public URL getSite() { 163 return site; 164 } 165 166 public void setSite(URL site) { 167 this.site = site; 168 } 169 170 public String getCreator() { 171 return creator; 172 } 173 174 public void setCreator(String creator) { 175 this.creator = creator; 176 } 177 178 public String getPublisher() { 179 return publisher; 180 } 181 182 public void setPublisher(String publisher) { 183 this.publisher = publisher; 184 } 185 186 public String getLanguage() { 187 return language; 188 } 189 190 public void setLanguage(String language) { 191 this.language = language; 192 } 193 194 public String getRating() { 195 return rating; 196 } 197 198 public void setRating(String rating) { 199 this.rating = rating; 200 } 201 202 public CloudIF getCloud() { 203 return cloud; 204 } 205 206 public void setCloud(CloudIF cloud) { 207 this.cloud = cloud; 208 } 209 210 public String getGenerator() { 211 return generator; 212 } 213 214 public void setGenerator(String generator) { 215 this.generator = generator; 216 } 217 218 public String getDocs() { 219 return docs; 220 } 221 222 public void setDocs(String docs) { 223 this.docs = docs; 224 } 225 226 public int getTtl() { 227 return ttl; 228 } 229 230 public void setTtl(int ttl) { 231 this.ttl = ttl; 232 } 233 234 public ChannelFormat getFormat() { 235 return format; 236 } 237 238 public void setFormat(ChannelFormat format) { 239 this.format = format; 240 } 241 242 public Collection getItems() { 243 return items.values(); 244 } 245 246 public void addItem(ItemIF item) { 247 items.put(new Long (item.getId()), item); 248 item.setChannel(this); 249 notifyObserversItemAdded(item); 250 } 251 252 public void removeItem(ItemIF item) { 253 items.remove(new Long (item.getId())); 254 } 255 256 public ItemIF getItem(long anId) { 257 return (ItemIF) items.get(new Long (anId)); 258 } 259 260 public ImageIF getImage() { 261 return image; 262 } 263 264 public void setImage(ImageIF image) { 265 this.image = image; 266 } 267 268 public TextInputIF getTextInput() { 269 return textInput; 270 } 271 272 public void setTextInput(TextInputIF textInput) { 273 this.textInput = textInput; 274 } 275 276 public String getCopyright() { 277 return copyright; 278 } 279 280 public void setCopyright(String copyright) { 281 this.copyright = copyright; 282 } 283 284 public Collection getCategories() { 285 return categories; 286 } 287 288 public void setCategories(Collection categories) { 289 this.categories = categories; 290 } 291 292 public void addCategory(CategoryIF category) { 293 categories.add(category); 294 } 295 296 public void removeCategory(CategoryIF category) { 297 categories.remove(category); 298 } 299 300 public Date getLastUpdated() { 301 return lastUpdated; 302 } 303 304 public void setLastUpdated(Date lastUpdated) { 305 this.lastUpdated = lastUpdated; 306 notifyObserversChannelUpdated(); 307 } 308 309 public Date getLastBuildDate() { 310 return lastBuild; 311 } 312 313 public void setLastBuildDate(Date date) { 314 this.lastBuild = date; 315 } 316 317 public Date getPubDate() { 318 return pubDate; 319 } 320 321 public void setPubDate(Date pubDate) { 322 this.pubDate = pubDate; 323 } 324 325 333 public void setAllProperties(ChannelIF sourceChan) { 334 setTitle(sourceChan.getTitle()); 335 setDescription(sourceChan.getDescription()); 336 setSite(sourceChan.getSite()); 337 setCreator(sourceChan.getCreator()); 338 setCopyright(sourceChan.getCopyright()); 339 setPublisher(sourceChan.getPublisher()); 340 setLanguage(sourceChan.getLanguage()); 341 setImage(sourceChan.getImage()); 342 setTextInput(sourceChan.getTextInput()); 343 setRating(sourceChan.getRating()); 344 setGenerator(sourceChan.getGenerator()); 345 setDocs(sourceChan.getDocs()); 346 setTtl(sourceChan.getTtl()); 347 setCloud(sourceChan.getCloud()); 348 setLastBuildDate(sourceChan.getLastBuildDate()); 349 setUpdateBase(sourceChan.getUpdateBase()); 350 setUpdateFrequency(sourceChan.getUpdateFrequency()); 351 setUpdatePeriod(sourceChan.getUpdatePeriod()); 352 setPubDate(sourceChan.getPubDate()); 353 } 354 355 356 358 public String getUpdatePeriod() { 359 return updatePeriod; 360 } 361 362 public void setUpdatePeriod(String updatePeriod) { 363 this.updatePeriod = updatePeriod; 364 } 365 366 public int getUpdateFrequency() { 367 return updateFrequency; 368 } 369 370 public void setUpdateFrequency(int updateFrequency) { 371 this.updateFrequency = updateFrequency; 372 } 373 374 public Date getUpdateBase() { 375 return updateBase; 376 } 377 378 public void setUpdateBase(Date updateBase) { 379 this.updateBase = updateBase; 380 } 381 382 public String getElementValue(final String path) { 383 return XmlPathUtils.getElementValue(channelElement, path); 384 } 385 386 public String [] getElementValues(final String path, final String [] elements) { 387 return XmlPathUtils.getElementValues(channelElement, path, elements); 388 } 389 390 public String getAttributeValue(final String path, final String attribute) { 391 return XmlPathUtils.getAttributeValue(channelElement, path, attribute); 392 } 393 394 public String [] getAttributeValues(final String path, final String [] attributes) { 395 return XmlPathUtils.getAttributeValues(channelElement, path, attributes); 396 } 397 398 402 public void addObserver(ChannelObserverIF o) { 403 observers.add(o); 404 } 405 406 public void removeObserver(ChannelObserverIF o) { 407 observers.remove(o); 408 } 409 410 414 424 public boolean equals(Object obj) { 425 if (!(obj instanceof ChannelIF)) { 426 return false; 427 } 428 ChannelIF cmp = (ChannelIF) obj; 429 430 boolean te; 431 if (title != null) { 432 te = title.equals(cmp.getTitle()); 433 } else { 434 te = (cmp.getTitle() == null); 435 } 436 boolean de; 437 if (description != null) { 438 de = description.equals(cmp.getDescription()); 439 } else { 440 de = (cmp.getDescription() == null); 441 } 442 boolean le; 443 if (location != null) { 444 le = location.toString().equals(cmp.getLocation().toString()); 448 } else { 449 le = (cmp.getLocation() == null); 450 } 451 452 return (te && de && le); 453 } 454 455 462 public int hashCode() { 463 StringBuffer sb = new StringBuffer (64); 464 sb.append(title).append(description).append(location); 465 return sb.toString().hashCode(); 466 } 467 468 473 public String toString() { 474 return "[Basic Channel (" + id + "): " + title 475 + " (" + location + " )]"; 476 } 477 478 482 487 private void notifyObserversItemAdded(ItemIF newItem) { 488 Iterator it = observers.iterator(); 489 while (it.hasNext()) { 490 ChannelObserverIF o = (ChannelObserverIF) it.next(); 491 o.itemAdded(newItem); 492 } 493 } 494 495 499 private void notifyObserversChannelUpdated() { 500 Iterator it = observers.iterator(); 501 while (it.hasNext()) { 502 ChannelObserverIF o = (ChannelObserverIF) it.next(); 503 o.channelRetrieved(this); 504 } 505 } 506 507 511 private static Map getMap() { 512 if (checkLinkedMap3()) { 513 return CommonsCollections3MapFactory.getLinkedMap(); 514 } else if (checkLinkedHashMap()) { 515 return JDK14MapFactory.getLinkedHashMap(); 516 } else { 517 return new HashMap (); 518 } 519 } 520 521 524 private static boolean checkLinkedMap3() { 525 if (hasLinkedMap == -1) { 526 try { 527 Class.forName("org.apache.commons.collections.map.LinkedMap"); 528 hasLinkedMap = 1; 529 } catch (ClassNotFoundException e) { 530 hasLinkedMap = 0; 531 } 532 } 533 return (hasLinkedMap == 1); 534 } 535 536 540 private static final class JDK14MapFactory { 541 542 private JDK14MapFactory() { 543 } 544 545 static Map getLinkedHashMap() { 546 return new java.util.LinkedHashMap (); 547 } 548 } 549 550 553 private static boolean checkLinkedHashMap() { 554 if (hasLinkedHashMap == -1) { 555 try { 556 Class.forName("java.util.LinkedHashMap"); 557 hasLinkedHashMap = 1; 558 } catch (ClassNotFoundException e) { 559 hasLinkedHashMap = 0; 560 } 561 } 562 return (hasLinkedHashMap == 1); 563 } 564 565 569 private static final class CommonsCollections3MapFactory { 570 571 private CommonsCollections3MapFactory() { 572 } 573 574 static Map getLinkedMap() { 575 return new org.apache.commons.collections.map.LinkedMap(); 576 } 577 } 578 } 579 | Popular Tags |