001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.blog.database;
031:
032: import org.blojsom.blog.*;
033: import org.blojsom.util.BlojsomUtils;
034:
035: import java.io.Serializable;
036: import java.util.*;
037: import java.text.SimpleDateFormat;
038:
039: /**
040: * DatabaseEntry
041: *
042: * @author David Czarnecki
043: * @since blojsom 3.0
044: * @version $Id: DatabaseEntry.java,v 1.7 2007/01/17 02:35:16 czarneckid Exp $
045: */
046: public class DatabaseEntry implements Entry, Serializable {
047:
048: protected Integer _id;
049: protected Integer _blogCategoryId;
050: protected Integer _blogId;
051:
052: protected String _title;
053: protected String _link;
054: protected String _description;
055: protected Date _entryDate;
056: protected Date _modifiedDate;
057: protected List _comments;
058: protected List _trackbacks;
059: protected List _pingbacks;
060: protected Category _category;
061: protected Map _metaData;
062: protected Integer _allowComments;
063: protected Integer _allowTrackbacks;
064: protected Integer _allowPingbacks;
065: protected String _status;
066: protected String _author;
067: protected String _postSlug;
068:
069: /**
070: * Create a new instance of the database entry
071: */
072: public DatabaseEntry() {
073: }
074:
075: /**
076: * Set the entry ID
077: *
078: * @param id Entry ID
079: */
080: public void setId(Integer id) {
081: _id = id;
082: }
083:
084: /**
085: * Get the entry ID
086: *
087: * @return Entry ID
088: */
089: public Integer getId() {
090: return _id;
091: }
092:
093: /**
094: * Get the blog category ID
095: *
096: * @return Blog category ID
097: */
098: public Integer getBlogCategoryId() {
099: return _blogCategoryId;
100: }
101:
102: /**
103: * Set the blog category ID
104: *
105: * @param blogCategoryId Blog category ID
106: */
107: public void setBlogCategoryId(Integer blogCategoryId) {
108: _blogCategoryId = blogCategoryId;
109: }
110:
111: /**
112: * Get the blog ID
113: *
114: * @return Blog ID
115: */
116: public Integer getBlogId() {
117: return _blogId;
118: }
119:
120: /**
121: * Set the blog ID
122: *
123: * @param blogId Blog ID
124: */
125: public void setBlogId(Integer blogId) {
126: _blogId = blogId;
127: }
128:
129: /**
130: * Date of the blog entry
131: * <p/>
132: * This value is constructed from the lastModified value of the file
133: *
134: * @return Date of the blog entry
135: */
136: public Date getDate() {
137: return _entryDate;
138: }
139:
140: /**
141: * Date of this blog entry
142: *
143: * @param entryDate Date of the blog entry
144: */
145: public void setDate(Date entryDate) {
146: _entryDate = entryDate;
147: }
148:
149: /**
150: * Get the last modified date
151: *
152: * @return Last modified date
153: */
154: public Date getModifiedDate() {
155: return _modifiedDate;
156: }
157:
158: /**
159: * Set the last modified date
160: *
161: * @param modifiedDate Last modified date
162: */
163: public void setModifiedDate(Date modifiedDate) {
164: _modifiedDate = modifiedDate;
165: }
166:
167: /**
168: * Return an RFC 822 style date
169: *
170: * @return Date formatted in RFC 822 format
171: */
172: public String getRFC822Date() {
173: return BlojsomUtils.getRFC822Date(_entryDate);
174: }
175:
176: /**
177: * Return an UTC style date
178: *
179: * @return Date formatted in UTC format
180: */
181: public String getUTCDate() {
182: return BlojsomUtils.getUTCDate(_entryDate);
183: }
184:
185: /**
186: * Return an ISO 8601 style date
187: * http://www.w3.org/TR/NOTE-datetime
188: *
189: * @return Date formatted in ISO 8601 format
190: */
191: public String getISO8601Date() {
192: return BlojsomUtils.getISO8601Date(_entryDate);
193: }
194:
195: /**
196: * Return the blog entry date formatted with a specified date format
197: *
198: * @param format Date format
199: * @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>
200: */
201: public String getDateAsFormat(String format) {
202: return getDateAsFormat(format, null);
203: }
204:
205: /**
206: * Return the blog entry date formatted with a specified date format
207: *
208: * @param format Date format
209: * @param locale Locale for date formatting
210: * @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>
211: */
212: public String getDateAsFormat(String format, Locale locale) {
213: if (_entryDate == null || format == null) {
214: return null;
215: }
216:
217: SimpleDateFormat sdf;
218: try {
219: if (locale == null) {
220: sdf = new SimpleDateFormat(format);
221: } else {
222: sdf = new SimpleDateFormat(format, locale);
223: }
224:
225: return sdf.format(_entryDate);
226: } catch (IllegalArgumentException e) {
227: return _entryDate.toString();
228: }
229: }
230:
231: /**
232: * Title of the blog entry
233: *
234: * @return Blog title
235: */
236: public String getTitle() {
237: return _title;
238: }
239:
240: /**
241: * Set the title of the blog entry
242: *
243: * @param title Title for the blog entry
244: */
245: public void setTitle(String title) {
246: _title = title;
247: }
248:
249: /**
250: * Title for the entry where the <, >, and & characters are escaped
251: *
252: * @return Escaped entry title
253: */
254: public String getEscapedTitle() {
255: return BlojsomUtils.escapeString(_title);
256: }
257:
258: /**
259: * Description of the blog entry
260: *
261: * @return Blog entry description
262: */
263: public String getDescription() {
264: return _description;
265: }
266:
267: /**
268: * Escaped description of the blog entry
269: * This method would be used for generating RSS feeds where the <, >, and & characters are escaped
270: *
271: * @return Blog entry description where &, <, and > have been escaped
272: */
273: public String getEscapedDescription() {
274: return BlojsomUtils.escapeString(_description);
275: }
276:
277: /**
278: * Set the description for the blog entry
279: *
280: * @param description Description for the blog entry
281: */
282: public void setDescription(String description) {
283: _description = description;
284: }
285:
286: /**
287: * Category for the blog entry. This corresponds to the category directory name.
288: *
289: * @return Blog entry category
290: */
291: public String getCategory() {
292: return _category.getName();
293: }
294:
295: /**
296: * Return the category name encoded.
297: *
298: * @return Category name encoded as UTF-8
299: */
300: public String getEncodedCategory() {
301: return _category.getEncodedName();
302: }
303:
304: /**
305: * Determines whether or not this blog entry supports comments.
306: *
307: * @return <code>true</code> if the blog entry supports comments, <code>false</code> otherwise
308: */
309: public Integer getAllowComments() {
310: return _allowComments;
311: }
312:
313: /**
314: * Get the comments
315: *
316: * @return List of comments
317: */
318: public List getComments() {
319: if (_comments == null) {
320: return new ArrayList();
321: }
322:
323: return _comments;
324: }
325:
326: /**
327: * Set the comments for this blog entry. The comments must be an <code>List</code>
328: * of {@link Comment}. This method will not writeback or change the comments on disk.
329: *
330: * @param comments Comments for this entry
331: */
332: public void setComments(List comments) {
333: _comments = comments;
334: }
335:
336: /**
337: * Get the comments as an array of {@link Comment} objects
338: *
339: * @return BlogComment[] array
340: */
341: public Comment[] getCommentsAsArray() {
342: if (_comments == null) {
343: return new Comment[0];
344: } else {
345: return (Comment[]) _comments.toArray(new Comment[_comments
346: .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 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
405: .toArray(new Trackback[_trackbacks.size()]);
406: }
407: }
408:
409: /**
410: * Get the number of trackbacks for this entry
411: *
412: * @return 0 if trackbacks is <code>null</code>, or the number of trackbacks otherwise, which could be 0
413: */
414: public int getNumTrackbacks() {
415: if (_trackbacks == null) {
416: return 0;
417: } else {
418: return _trackbacks.size();
419: }
420: }
421:
422: /**
423: * Get the {@link org.blojsom.blog.Category} object for this blog entry
424: *
425: * @return {@link org.blojsom.blog.Category} object
426: */
427: public Category getBlogCategory() {
428: return _category;
429: }
430:
431: /**
432: * Set the {@link org.blojsom.blog.Category} object for this blog entry
433: *
434: * @param blogCategory New {@link org.blojsom.blog.Category} object
435: */
436: public void setBlogCategory(Category blogCategory) {
437: _category = blogCategory;
438: }
439:
440: /**
441: * Return meta data for this blog entry. This method may return <code>null</code>.
442: *
443: * @return Meta data
444: */
445: public Map getMetaData() {
446: if (_metaData == null) {
447: return new HashMap();
448: }
449:
450: return _metaData;
451: }
452:
453: /**
454: * Set the meta-data associated with this blog entry
455: *
456: * @param metaData Meta-data
457: */
458: public void setMetaData(Map metaData) {
459: _metaData = metaData;
460: }
461:
462: /**
463: * Determines whether or not this blog entry supports pingbacks.
464: *
465: * @return <code>true</code> if the blog entry supports pingbacks, <code>false</code> otherwise
466: */
467: public Integer getAllowPingbacks() {
468: return _allowPingbacks;
469: }
470:
471: /**
472: * Get the pingbacks for this entry
473: *
474: * @return List of {@link org.blojsom.blog.Pingback}
475: */
476: public List getPingbacks() {
477: if (_pingbacks == null) {
478: return new ArrayList();
479: }
480:
481: return _pingbacks;
482: }
483:
484: /**
485: * Set the pingbacks for this blog entry. The pingbacks must be a <code>List</code>
486: * of {@link org.blojsom.blog.Pingback}. This method will not writeback or change the pingbacks to disk.
487: *
488: * @param pingbacks {@link org.blojsom.blog.Pingback}s for this entry
489: */
490: public void setPingbacks(List pingbacks) {
491: _pingbacks = pingbacks;
492: }
493:
494: /**
495: * Get the pingbacks as an array of {@link org.blojsom.blog.Pingback}s objects
496: *
497: * @return {@link org.blojsom.blog.Pingback}[] array
498: */
499: public Pingback[] getPingbacksAsArray() {
500: if (_pingbacks == null) {
501: return new Pingback[0];
502: } else {
503: return (Pingback[]) _pingbacks
504: .toArray(new Pingback[_pingbacks.size()]);
505: }
506: }
507:
508: /**
509: * Get the number of pingbacks for this entry
510: *
511: * @return 0 if pingbacks is <code>null</code>, or the number of pingbacks otherwise, which could be 0
512: */
513: public int getNumPingbacks() {
514: if (_pingbacks == null) {
515: return 0;
516: } else {
517: return _pingbacks.size();
518: }
519: }
520:
521: /**
522: * Set whether comments are allowed
523: *
524: * @param allowComments <code>true</code> if comments are allowed, <code>false</code> otherwise
525: */
526: public void setAllowComments(Integer allowComments) {
527: _allowComments = allowComments;
528: }
529:
530: /**
531: * Set whether trackbacks are allowed
532: *
533: * @param allowTrackbacks <code>true</code> if trackbacks are allowed, <code>false</code> otherwise
534: */
535: public void setAllowTrackbacks(Integer allowTrackbacks) {
536: _allowTrackbacks = allowTrackbacks;
537: }
538:
539: /**
540: * Set whether pingbacks are allowed
541: *
542: * @param allowPingbacks <code>true</code> if pingbacks are allowed, <code>false</code> otherwise
543: */
544: public void setAllowPingbacks(Integer allowPingbacks) {
545: _allowPingbacks = allowPingbacks;
546: }
547:
548: /**
549: * Get the status
550: *
551: * @return Status
552: */
553: public String getStatus() {
554: return _status;
555: }
556:
557: /**
558: * Set the status
559: *
560: * @param status Status
561: */
562: public void setStatus(String status) {
563: _status = status;
564: }
565:
566: /**
567: * Get the author
568: *
569: * @return Author
570: */
571: public String getAuthor() {
572: return _author;
573: }
574:
575: /**
576: * Set the author
577: *
578: * @param author Author
579: */
580: public void setAuthor(String author) {
581: _author = author;
582: }
583:
584: /**
585: * Whether or not comments are allowed
586: *
587: * @return <code>true</code> if comments are allowed, <code>false</code> otherwise
588: */
589: public Boolean allowsComments() {
590: if (_allowComments == null) {
591: return Boolean.TRUE;
592: }
593:
594: return Boolean.valueOf((_allowComments.intValue() == 1));
595: }
596:
597: /**
598: * Whether or not trackbacks are allowed
599: *
600: * @return <code>true</code> if trackbacks are allowed, <code>false</code> otherwise
601: */
602: public Boolean allowsTrackbacks() {
603: if (_allowTrackbacks == null) {
604: return Boolean.TRUE;
605: }
606:
607: return Boolean.valueOf((_allowTrackbacks.intValue() == 1));
608: }
609:
610: /**
611: * Whether or not pingbacks are allowed
612: *
613: * @return <code>true</code> if pingbacks are allowed, <code>false</code> otherwise
614: */
615: public Boolean allowsPingbacks() {
616: if (_allowPingbacks == null) {
617: return Boolean.TRUE;
618: }
619:
620: return Boolean.valueOf((_allowPingbacks.intValue() == 1));
621: }
622:
623: /**
624: * Get the post slug
625: *
626: * @return Post slug
627: */
628: public String getPostSlug() {
629: return _postSlug;
630: }
631:
632: /**
633: * Get the post slug encoded as UTF-8
634: *
635: * @return Post slug encoded as UTF-8
636: */
637: public String getEncodedPostSlug() {
638: return BlojsomUtils.urlEncode(_postSlug);
639: }
640:
641: /**
642: * Set the post slug
643: *
644: * @param postSlug Post slug
645: */
646: public void setPostSlug(String postSlug) {
647: _postSlug = postSlug;
648: }
649:
650: /**
651: * Get the responses (comments, trackbacks, pingbacks)
652: *
653: * @return Responses (comments, trackbacks, pingbacks)
654: */
655: public List getResponses() {
656: List responses = new ArrayList();
657:
658: responses.addAll(getComments());
659: responses.addAll(getTrackbacks());
660: responses.addAll(getPingbacks());
661:
662: Collections.sort(responses, BlojsomUtils.RESPONSE_COMPARATOR);
663:
664: return responses;
665: }
666:
667: /**
668: * Get the responses (comments, trackbacks, pingbacks) matching some status code
669: *
670: * @param status Status code
671: * @return Responses (comments, trackbacks, pingbacks) matching some status code
672: */
673: public List getResponsesMatchingStatus(String status) {
674: List responses = getResponses();
675:
676: for (int i = 0; i < responses.size(); i++) {
677: Response response = (Response) responses.get(i);
678: if (!status.equals(response.getStatus())) {
679: responses.set(i, null);
680: }
681: }
682:
683: return BlojsomUtils.removeNullValues(responses);
684: }
685:
686: /**
687: * Get the responses (comments, trackbacks, pingbacks) not matching some status code
688: *
689: * @param status Status code
690: * @return Responses (comments, trackbacks, pingbacks) not matching some status code
691: */
692: public List getResponsesNotMatchingStatus(String status) {
693: List responses = getResponses();
694:
695: for (int i = 0; i < responses.size(); i++) {
696: Response response = (Response) responses.get(i);
697: if (status.equals(response.getStatus())) {
698: responses.set(i, null);
699: }
700: }
701:
702: return BlojsomUtils.removeNullValues(responses);
703: }
704: }
|