KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > blog > database > DatabaseEntry


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.blog.database;
32
33 import org.blojsom.blog.*;
34 import org.blojsom.util.BlojsomUtils;
35
36 import java.io.Serializable JavaDoc;
37 import java.util.*;
38 import java.text.SimpleDateFormat JavaDoc;
39
40 /**
41  * DatabaseEntry
42  *
43  * @author David Czarnecki
44  * @since blojsom 3.0
45  * @version $Id: DatabaseEntry.java,v 1.5 2006/09/26 02:55:21 czarneckid Exp $
46  */

47 public class DatabaseEntry implements Entry, Serializable JavaDoc {
48
49     protected Integer JavaDoc _id;
50     protected Integer JavaDoc _blogCategoryId;
51     protected Integer JavaDoc _blogId;
52
53     protected String JavaDoc _title;
54     protected String JavaDoc _link;
55     protected String JavaDoc _description;
56     protected Date _entryDate;
57     protected Date _modifiedDate;
58     protected List _comments;
59     protected List _trackbacks;
60     protected List _pingbacks;
61     protected Category _category;
62     protected Map _metaData;
63     protected Integer JavaDoc _allowComments;
64     protected Integer JavaDoc _allowTrackbacks;
65     protected Integer JavaDoc _allowPingbacks;
66     protected String JavaDoc _status;
67     protected String JavaDoc _author;
68     protected String JavaDoc _postSlug;
69
70     /**
71      * Create a new instance of the database entry
72      */

73     public DatabaseEntry() {
74     }
75
76     /**
77      * Set the entry ID
78      *
79      * @param id Entry ID
80      */

81     public void setId(Integer JavaDoc id) {
82         _id = id;
83     }
84
85     /**
86      * Get the entry ID
87      *
88      * @return Entry ID
89      */

90     public Integer JavaDoc getId() {
91         return _id;
92     }
93
94     /**
95      * Get the blog category ID
96      *
97      * @return Blog category ID
98      */

99     public Integer JavaDoc getBlogCategoryId() {
100         return _blogCategoryId;
101     }
102
103     /**
104      * Set the blog category ID
105      *
106      * @param blogCategoryId Blog category ID
107      */

108     public void setBlogCategoryId(Integer JavaDoc blogCategoryId) {
109         _blogCategoryId = blogCategoryId;
110     }
111
112     /**
113      * Get the blog ID
114      *
115      * @return Blog ID
116      */

117     public Integer JavaDoc getBlogId() {
118         return _blogId;
119     }
120
121     /**
122      * Set the blog ID
123      *
124      * @param blogId Blog ID
125      */

126     public void setBlogId(Integer JavaDoc blogId) {
127         _blogId = blogId;
128     }
129
130     /**
131      * Date of the blog entry
132      * <p/>
133      * This value is constructed from the lastModified value of the file
134      *
135      * @return Date of the blog entry
136      */

137     public Date getDate() {
138         return _entryDate;
139     }
140
141     /**
142      * Date of this blog entry
143      *
144      * @param entryDate Date of the blog entry
145      */

146     public void setDate(Date entryDate) {
147         _entryDate = entryDate;
148     }
149
150     /**
151      * Get the last modified date
152      *
153      * @return Last modified date
154      */

155     public Date getModifiedDate() {
156         return _modifiedDate;
157     }
158
159     /**
160      * Set the last modified date
161      *
162      * @param modifiedDate Last modified date
163      */

164     public void setModifiedDate(Date modifiedDate) {
165         _modifiedDate = modifiedDate;
166     }
167
168     /**
169      * Return an RFC 822 style date
170      *
171      * @return Date formatted in RFC 822 format
172      */

173     public String JavaDoc getRFC822Date() {
174         return BlojsomUtils.getRFC822Date(_entryDate);
175     }
176
177     /**
178      * Return an UTC style date
179      *
180      * @return Date formatted in UTC format
181      */

182     public String JavaDoc getUTCDate() {
183         return BlojsomUtils.getUTCDate(_entryDate);
184     }
185
186     /**
187      * Return an ISO 8601 style date
188      * http://www.w3.org/TR/NOTE-datetime
189      *
190      * @return Date formatted in ISO 8601 format
191      */

192     public String JavaDoc getISO8601Date() {
193         return BlojsomUtils.getISO8601Date(_entryDate);
194     }
195
196     /**
197      * Return the blog entry date formatted with a specified date format
198      *
199      * @param format Date format
200      * @return <code>null</code> if the entry date or format is null, otherwise returns the entry date formatted to the specified format. If the format is invalid, returns <tt>entryDate.toString()</tt>
201      */

202     public String JavaDoc getDateAsFormat(String JavaDoc format) {
203         return getDateAsFormat(format, null);
204     }
205
206     /**
207      * Return the blog entry date formatted with a specified date format
208      *
209      * @param format Date format
210      * @param locale Locale for date formatting
211      * @return <code>null</code> if the entry date or format is null, otherwise returns the entry date formatted to the specified format. If the format is invalid, returns <tt>entryDate.toString()</tt>
212      */

213     public String JavaDoc getDateAsFormat(String JavaDoc format, Locale locale) {
214         if (_entryDate == null || format == null) {
215             return null;
216         }
217
218         SimpleDateFormat JavaDoc sdf;
219         try {
220             if (locale == null) {
221                 sdf = new SimpleDateFormat JavaDoc(format);
222             } else {
223                 sdf = new SimpleDateFormat JavaDoc(format, locale);
224             }
225
226             return sdf.format(_entryDate);
227         } catch (IllegalArgumentException JavaDoc e) {
228             return _entryDate.toString();
229         }
230     }
231
232     /**
233      * Title of the blog entry
234      *
235      * @return Blog title
236      */

237     public String JavaDoc getTitle() {
238         return _title;
239     }
240
241     /**
242      * Set the title of the blog entry
243      *
244      * @param title Title for the blog entry
245      */

246     public void setTitle(String JavaDoc title) {
247         _title = title;
248     }
249
250     /**
251      * Title for the entry where the &lt;, &gt;, and &amp; characters are escaped
252      *
253      * @return Escaped entry title
254      */

255     public String JavaDoc getEscapedTitle() {
256         return BlojsomUtils.escapeString(_title);
257     }
258
259     /**
260      * Description of the blog entry
261      *
262      * @return Blog entry description
263      */

264     public String JavaDoc getDescription() {
265         return _description;
266     }
267
268     /**
269      * Escaped description of the blog entry
270      * This method would be used for generating RSS feeds where the &lt;, &gt;, and &amp; characters are escaped
271      *
272      * @return Blog entry description where &amp;, &lt;, and &gt; have been escaped
273      */

274     public String JavaDoc getEscapedDescription() {
275         return BlojsomUtils.escapeString(_description);
276     }
277
278     /**
279      * Set the description for the blog entry
280      *
281      * @param description Description for the blog entry
282      */

283     public void setDescription(String JavaDoc description) {
284         _description = description;
285     }
286
287     /**
288      * Category for the blog entry. This corresponds to the category directory name.
289      *
290      * @return Blog entry category
291      */

292     public String JavaDoc getCategory() {
293         return _category.getName();
294     }
295
296     /**
297      * Return the category name encoded.
298      *
299      * @return Category name encoded as UTF-8
300      */

301     public String JavaDoc getEncodedCategory() {
302         return _category.getEncodedName();
303     }
304
305     /**
306      * Determines whether or not this blog entry supports comments.
307      *
308      * @return <code>true</code> if the blog entry supports comments, <code>false</code> otherwise
309      */

310     public Integer JavaDoc getAllowComments() {
311         return _allowComments;
312     }
313
314     /**
315      * Get the comments
316      *
317      * @return List of comments
318      */

319     public List getComments() {
320         if (_comments == null) {
321             return new ArrayList();
322         }
323
324         return _comments;
325     }
326
327     /**
328      * Set the comments for this blog entry. The comments must be an <code>List</code>
329      * of {@link Comment}. This method will not writeback or change the comments on disk.
330      *
331      * @param comments Comments for this entry
332      */

333     public void setComments(List comments) {
334         _comments = comments;
335     }
336
337     /**
338      * Get the comments as an array of {@link Comment} objects
339      *
340      * @return BlogComment[] array
341      */

342     public Comment[] getCommentsAsArray() {
343         if (_comments == null) {
344             return new Comment[0];
345         } else {
346             return (Comment[]) _comments.toArray(new Comment[_comments.size()]);
347         }
348     }
349
350     /**
351      * Get the number of comments for this entry
352      *
353      * @return 0 if comments is <code>null</code>, or the number of comments otherwise, which could be 0
354      */

355     public int getNumComments() {
356         if (_comments == null) {
357             return 0;
358         } else {
359             return _comments.size();
360         }
361     }
362
363     /**
364      * Determines whether or not this blog entry supports trackbacks.
365      *
366      * @return <code>true</code> if the blog entry supports trackbacks, <code>false</code> otherwise
367      */

368     public Integer JavaDoc getAllowTrackbacks() {
369         return _allowTrackbacks;
370     }
371
372     /**
373      * Get the trackbacks
374      *
375      * @return List of trackbacks
376      */

377     public List getTrackbacks() {
378         if (_trackbacks == null) {
379             return new ArrayList();
380         }
381
382         return _trackbacks;
383     }
384
385     /**
386      * Set the trackbacks for this blog entry. The trackbacks must be an <code>List</code>
387      * of {@link org.blojsom.blog.Trackback}. This method will not writeback or change the trackbacks to disk.
388      *
389      * @param trackbacks Trackbacks for this entry
390      */

391     public void setTrackbacks(List trackbacks) {
392         _trackbacks = trackbacks;
393     }
394
395     /**
396      * Get the trackbacks as an array of Trackback objects
397      *
398      * @return Trackback[] array
399      */

400     public Trackback[] getTrackbacksAsArray() {
401         if (_trackbacks == null) {
402             return new Trackback[0];
403         } else {
404             return (Trackback[]) _trackbacks.toArray(new Trackback[_trackbacks.size()]);
405         }
406     }
407
408     /**
409      * Get the number of trackbacks for this entry
410      *
411      * @return 0 if trackbacks is <code>null</code>, or the number of trackbacks otherwise, which could be 0
412      */

413     public int getNumTrackbacks() {
414         if (_trackbacks == null) {
415             return 0;
416         } else {
417             return _trackbacks.size();
418         }
419     }
420
421     /**
422      * Get the {@link org.blojsom.blog.Category} object for this blog entry
423      *
424      * @return {@link org.blojsom.blog.Category} object
425      */

426     public Category getBlogCategory() {
427         return _category;
428     }
429
430     /**
431      * Set the {@link org.blojsom.blog.Category} object for this blog entry
432      *
433      * @param blogCategory New {@link org.blojsom.blog.Category} object
434      */

435     public void setBlogCategory(Category blogCategory) {
436         _category = blogCategory;
437     }
438
439     /**
440      * Return meta data for this blog entry. This method may return <code>null</code>.
441      *
442      * @return Meta data
443      */

444     public Map getMetaData() {
445         if (_metaData == null) {
446             return new HashMap();
447         }
448
449         return _metaData;
450     }
451
452     /**
453      * Set the meta-data associated with this blog entry
454      *
455      * @param metaData Meta-data
456      */

457     public void setMetaData(Map metaData) {
458         _metaData = metaData;
459     }
460
461     /**
462      * Determines whether or not this blog entry supports pingbacks.
463      *
464      * @return <code>true</code> if the blog entry supports pingbacks, <code>false</code> otherwise
465      */

466     public Integer JavaDoc getAllowPingbacks() {
467         return _allowPingbacks;
468     }
469
470     /**
471      * Get the pingbacks for this entry
472      *
473      * @return List of {@link org.blojsom.blog.Pingback}
474      */

475     public List getPingbacks() {
476         if (_pingbacks == null) {
477             return new ArrayList();
478         }
479
480         return _pingbacks;
481     }
482
483     /**
484      * Set the pingbacks for this blog entry. The pingbacks must be a <code>List</code>
485      * of {@link org.blojsom.blog.Pingback}. This method will not writeback or change the pingbacks to disk.
486      *
487      * @param pingbacks {@link org.blojsom.blog.Pingback}s for this entry
488      */

489     public void setPingbacks(List pingbacks) {
490         _pingbacks = pingbacks;
491     }
492
493     /**
494      * Get the pingbacks as an array of {@link org.blojsom.blog.Pingback}s objects
495      *
496      * @return {@link org.blojsom.blog.Pingback}[] array
497      */

498     public Pingback[] getPingbacksAsArray() {
499         if (_pingbacks == null) {
500             return new Pingback[0];
501         } else {
502             return (Pingback[]) _pingbacks.toArray(new Pingback[_pingbacks.size()]);
503         }
504     }
505
506     /**
507      * Get the number of pingbacks for this entry
508      *
509      * @return 0 if pingbacks is <code>null</code>, or the number of pingbacks otherwise, which could be 0
510      */

511     public int getNumPingbacks() {
512         if (_pingbacks == null) {
513             return 0;
514         } else {
515             return _pingbacks.size();
516         }
517     }
518
519     /**
520      * Set whether comments are allowed
521      *
522      * @param allowComments <code>true</code> if comments are allowed, <code>false</code> otherwise
523      */

524     public void setAllowComments(Integer JavaDoc allowComments) {
525         _allowComments = allowComments;
526     }
527
528     /**
529      * Set whether trackbacks are allowed
530      *
531      * @param allowTrackbacks <code>true</code> if trackbacks are allowed, <code>false</code> otherwise
532      */

533     public void setAllowTrackbacks(Integer JavaDoc allowTrackbacks) {
534         _allowTrackbacks = allowTrackbacks;
535     }
536 /**
537      * Set whether pingbacks are allowed
538      *
539      * @param allowPingbacks <code>true</code> if pingbacks are allowed, <code>false</code> otherwise
540      */

541     public void setAllowPingbacks(Integer JavaDoc allowPingbacks) {
542         _allowPingbacks = allowPingbacks;
543     }
544
545     /**
546      * Get the status
547      *
548      * @return Status
549      */

550     public String JavaDoc getStatus() {
551         return _status;
552     }
553
554     /**
555      * Set the status
556      *
557      * @param status Status
558      */

559     public void setStatus(String JavaDoc status) {
560         _status = status;
561     }
562
563     /**
564      * Get the author
565      *
566      * @return Author
567      */

568     public String JavaDoc getAuthor() {
569         return _author;
570     }
571
572     /**
573      * Set the author
574      *
575      * @param author Author
576      */

577     public void setAuthor(String JavaDoc author) {
578         _author = author;
579     }
580
581     /**
582      * Whether or not comments are allowed
583      *
584      * @return <code>true</code> if comments are allowed, <code>false</code> otherwise
585      */

586     public Boolean JavaDoc allowsComments() {
587         if (_allowComments == null) {
588             return Boolean.TRUE;
589         }
590
591         return Boolean.valueOf((_allowComments.intValue() == 1));
592     }
593
594     /**
595      * Whether or not trackbacks are allowed
596      *
597      * @return <code>true</code> if trackbacks are allowed, <code>false</code> otherwise
598      */

599     public Boolean JavaDoc allowsTrackbacks() {
600         if (_allowTrackbacks == null) {
601             return Boolean.TRUE;
602         }
603
604         return Boolean.valueOf((_allowTrackbacks.intValue() == 1));
605     }
606
607     /**
608      * Whether or not pingbacks are allowed
609      *
610      * @return <code>true</code> if pingbacks are allowed, <code>false</code> otherwise
611      */

612     public Boolean JavaDoc allowsPingbacks() {
613         if (_allowPingbacks == null) {
614             return Boolean.TRUE;
615         }
616
617         return Boolean.valueOf((_allowPingbacks.intValue() == 1));
618     }
619
620     /**
621      * Get the post slug
622      *
623      * @return Post slug
624      */

625     public String JavaDoc getPostSlug() {
626         return _postSlug;
627     }
628
629     /**
630      * Set the post slug
631      *
632      * @param postSlug Post slug
633      */

634     public void setPostSlug(String JavaDoc postSlug) {
635         _postSlug = postSlug;
636     }
637
638     /**
639      * Get the responses (comments, trackbacks, pingbacks)
640      *
641      * @return Responses (comments, trackbacks, pingbacks)
642      */

643     public List getResponses() {
644         List responses = new ArrayList();
645
646         responses.addAll(getComments());
647         responses.addAll(getTrackbacks());
648         responses.addAll(getPingbacks());
649
650         Collections.sort(responses, BlojsomUtils.RESPONSE_COMPARATOR);
651
652         return responses;
653     }
654
655     /**
656      * Get the responses (comments, trackbacks, pingbacks) matching some status code
657      *
658      * @param status Status code
659      * @return Responses (comments, trackbacks, pingbacks) matching some status code
660      */

661     public List getResponsesMatchingStatus(String JavaDoc status) {
662         List responses = getResponses();
663
664         for (int i = 0; i < responses.size(); i++) {
665             Response response = (Response) responses.get(i);
666             if (!status.equals(response.getStatus())) {
667                 responses.set(i, null);
668             }
669         }
670
671         return BlojsomUtils.removeNullValues(responses);
672     }
673
674     /**
675      * Get the responses (comments, trackbacks, pingbacks) not matching some status code
676      *
677      * @param status Status code
678      * @return Responses (comments, trackbacks, pingbacks) not matching some status code
679      */

680     public List getResponsesNotMatchingStatus(String JavaDoc status) {
681         List responses = getResponses();
682
683         for (int i = 0; i < responses.size(); i++) {
684             Response response = (Response) responses.get(i);
685             if (status.equals(response.getStatus())) {
686                 responses.set(i, null);
687             }
688         }
689
690         return BlojsomUtils.removeNullValues(responses);
691     }
692 }
693
Popular Tags