Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart8.png 29% of files have more coverage
68   440   32   3,4
18   131   0,47   20
20     1,6  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  Day       Line # 43 68 0% 32 30 71,7% 0.7169811
 
  (266)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
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
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import net.sourceforge.pebble.comparator.ReverseBlogEntryIdComparator;
35   
36    import java.util.*;
37   
38    /**
39    * Represents a blog at a daily level. This manages a collection of BlogEntry instances.
40    *
41    * @author Simon Brown
42    */
 
43    public class Day extends TimePeriod implements Permalinkable {
44   
45    /** the parent, Month instance */
46    private Month month;
47   
48    /** an integer representing the day that this Day is for */
49    private int day;
50   
51    /** the collection of blog entry keys */
52    private List<String> blogEntries = new ArrayList<String>();
53    private List<String> publishedBlogEntries = new ArrayList<String>();
54    private List<String> unpublishedBlogEntries = new ArrayList<String>();
55   
56    /**
57    * Creates a new Day for the specified month and day.
58    *
59    * @param month a Month instance representing the month
60    * @param day an int representing the day
61    */
 
62  129742 toggle Day(Month month, int day) {
63  129742 super(month.getBlog());
64   
65  129742 this.month = month;
66  129742 this.day = day;
67  129742 setDate(getCalendar().getTime());
68   
69    // if (getBlog() instanceof Blog) {
70    // try {
71    // Blog blog = getBlog();
72    //
73    // DAOFactory factory = DAOFactory.getConfiguredFactory();
74    // BlogEntryDAO dao = factory.getBlogEntryDAO();
75    // entries = dao.getBlogEntries(this);
76    // Collections.sort(entries, new BlogEntryComparator());
77    //
78    // Iterator it = entries.iterator();
79    // while (it.hasNext()) {
80    // BlogEntry blogEntry = (BlogEntry)it.next();
81    //
82    // if (blogEntry.isApproved()) {
83    // Iterator categories = blogEntry.getCategories().iterator();
84    // while (categories.hasNext()) {
85    // Category category = (Category)categories.next();
86    // category.addBlogEntry(blogEntry);
87    // }
88    // blog.getRootCategory().addBlogEntry(blogEntry);
89    //
90    // // and the blog entry specific tags
91    // Iterator tags = blogEntry.getTagsAsList().iterator();
92    // while (tags.hasNext()) {
93    // Tag tag = (Tag)tags.next();
94    // tag.addBlogEntry(blogEntry);
95    // }
96    // }
97    //
98    // // tell the owning blog that we might have some more recent
99    // // comments and TrackBacks
100    // Iterator comments = blogEntry.getComments().iterator();
101    // while (comments.hasNext()) {
102    // blog.getResponseManager().addRecentComment((Comment)comments.next());
103    // }
104    // Iterator trackBacks = blogEntry.getTrackBacks().iterator();
105    // while (trackBacks.hasNext()) {
106    // blog.getResponseManager().addRecentTrackBack((TrackBack)trackBacks.next());
107    // }
108    //
109    // // now that the entries have been loaded, enable events
110    // // so that listeners get notified when they change
111    // blogEntry.setEventsEnabled(true);
112    // }
113    // } catch (PersistenceException e) {
114    // e.printStackTrace();
115    // }
116    // }
117    }
118   
119    /**
120    * Gets a Calendar object representing today.
121    *
122    * @return a Calendar instance
123    */
 
124  129742 toggle private Calendar getCalendar() {
125   
126    // and set the actual date for this day
127  129742 Calendar cal = getBlog().getCalendar();
128  129742 cal.set(Calendar.YEAR, getMonth().getYear().getYear());
129  129742 cal.set(Calendar.MONTH, getMonth().getMonth() - 1);
130  129742 cal.set(Calendar.DAY_OF_MONTH, getDay());
131  129742 cal.set(Calendar.HOUR_OF_DAY, 12);
132  129742 cal.set(Calendar.MINUTE, 0);
133  129742 cal.set(Calendar.SECOND, 0);
134  129742 cal.set(Calendar.MILLISECOND, 0);
135   
136  129742 return cal;
137    }
138   
139    /**
140    * Gets a reference to the parent Month instance.
141    *
142    * @return a Month instance
143    */
 
144  259524 toggle public Month getMonth() {
145  259524 return month;
146    }
147   
148    /**
149    * Gets the day that this Day is for.
150    *
151    * @return an int representing the day in the month
152    */
 
153  129836 toggle public int getDay() {
154  129836 return day;
155    }
156   
157    /**
158    * Gets the permalink to display all entries for this Day.
159    *
160    * @return an absolute URL
161    */
 
162  4 toggle public String getPermalink() {
163  4 String s = getBlog().getPermalinkProvider().getPermalink(this);
164  4 if (s != null && s.length() > 0) {
165  4 return getBlog().getUrl() + s.substring(1);
166    } else {
167  0 return "";
168    }
169    }
170   
171    /**
172    * Gets a Collection containing all the blog entries for this day.
173    *
174    * @return an ordered List of BlogEntry instances
175    */
 
176  8176 toggle public List<String> getBlogEntries() {
177  8176 return new ArrayList<String>(blogEntries);
178    }
179   
 
180  0 toggle public int getNumberOfBlogEntries() {
181  0 return publishedBlogEntries.size();
182    }
183   
 
184  110 toggle public synchronized void addPublishedBlogEntry(String blogEntryId) {
185  110 if (!publishedBlogEntries.contains(blogEntryId)) {
186  108 publishedBlogEntries.add(blogEntryId);
187  108 Collections.sort(publishedBlogEntries, new ReverseBlogEntryIdComparator());
188    }
189  110 unpublishedBlogEntries.remove(blogEntryId);
190   
191  110 if (!blogEntries.contains(blogEntryId)) {
192    // and add to the aggregated view
193  108 blogEntries.add(blogEntryId);
194  108 Collections.sort(blogEntries, new ReverseBlogEntryIdComparator());
195    }
196    }
197   
 
198  202 toggle public synchronized void addUnpublishedBlogEntry(String blogEntryId) {
199  202 if (!unpublishedBlogEntries.contains(blogEntryId)) {
200  202 unpublishedBlogEntries.add(blogEntryId);
201  202 Collections.sort(unpublishedBlogEntries, new ReverseBlogEntryIdComparator());
202    }
203  202 publishedBlogEntries.remove(blogEntryId);
204   
205  202 if (!blogEntries.contains(blogEntryId)) {
206    // and add to the aggregated view
207  202 blogEntries.add(blogEntryId);
208  202 Collections.sort(blogEntries, new ReverseBlogEntryIdComparator());
209    }
210    }
211   
 
212  36 toggle public synchronized void removeBlogEntry(BlogEntry blogEntry) {
213  36 publishedBlogEntries.remove(blogEntry.getId());
214  36 unpublishedBlogEntries.remove(blogEntry.getId());
215  36 blogEntries.remove(blogEntry.getId());
216    }
217   
218    // /**
219    // * Gets a Collection containing all the blog entries for this day and the
220    // * specified category.
221    // *
222    // * @param category a Category instance, or null
223    // * @return an ordered List of BlogEntry instances
224    // */
225    // public List getEntries(Category category) {
226    // if (category == null) {
227    // return this.getEntries();
228    // } else {
229    // List blogEntries = new ArrayList();
230    // Iterator it = getEntries().iterator();
231    // while (it.hasNext()) {
232    // BlogEntry blogEntry = (BlogEntry)it.next();
233    // if (blogEntry.inCategory(category)) {
234    // blogEntries.add(blogEntry);
235    // }
236    // }
237    // return blogEntries;
238    // }
239    // }
240   
241    // /**
242    // * Gets a Collection containing all the blog entries for this day and the
243    // * specified tag.
244    // *
245    // * @param tag a Strng
246    // * @return an ordered List of BlogEntry instances
247    // */
248    // public List getEntries(String tag) {
249    // if (tag == null) {
250    // return this.getEntries();
251    // } else {
252    // List blogEntries = new ArrayList();
253    // Iterator it = getEntries().iterator();
254    // while (it.hasNext()) {
255    // BlogEntry blogEntry = (BlogEntry)it.next();
256    // if (blogEntry.hasTag(tag)) {
257    // blogEntries.add(blogEntry);
258    // }
259    // }
260    // return blogEntries;
261    // }
262    // }
263   
264    // /**
265    // * Gets a specific blog entry.
266    // *
267    // * @param entryId the blog entry id
268    // * @return the corresponding BlogEntry instance, or null if a BlogEntry
269    // * with the specified id doesn't exist
270    // */
271    // public BlogEntry getEntry(String entryId) {
272    // Iterator it = entries.iterator();
273    // BlogEntry blogEntry;
274    // while (it.hasNext()) {
275    // blogEntry = (BlogEntry)it.next();
276    // if (blogEntry.getId().equals(entryId)) {
277    // return blogEntry;
278    // }
279    // }
280    // return null;
281    // }
282   
283    // /**
284    // * Adds a given blog entry.
285    // *
286    // * @param entry the BlogEntry instance to add
287    // */
288    // public synchronized void addEntry(BlogEntry entry) {
289    // if (entry == null) {
290    // return;
291    // }
292    //
293    // BlogEntry existing = getEntry(entry.getId());
294    // if (existing != null && existing != entry) {
295    // // there is already an entry with the same ID, so increment and try again
296    // entry.setDate(new Date(entry.getDate().getTime() + 1));
297    // addEntry(entry);
298    // } else if (!entries.contains(entry)) {
299    // entries.add(entry);
300    // Collections.sort(entries, new BlogEntryComparator());
301    // entry.setDay(this);
302    // entry.setType(BlogEntry.PUBLISHED);
303    //
304    // // now that the entries have been loaded, enable events
305    // // so that listeners get notified when they change
306    // entry.setEventsEnabled(true);
307    //
308    // // and notify listeners
309    // ((Blog)getBlog()).getEventDispatcher().fireBlogEntryEvent(new BlogEntryEvent(entry, BlogEntryEvent.BLOG_ENTRY_ADDED));
310    // }
311    // }
312   
313    // /**
314    // * Removes a given blog entry.
315    // *
316    // * @param entry the BlogEntry instance to remove
317    // */
318    // public synchronized void removeEntry(BlogEntry entry) {
319    // if (entry != null) {
320    // getBlog().getEventDispatcher().fireBlogEntryEvent(new BlogEntryEvent(entry, BlogEntryEvent.BLOG_ENTRY_REMOVED));
321    // entries.remove(entry);
322    // entry.setDay(null);
323    // }
324    // }
325   
326    /**
327    * Determines whether this day has entries.
328    *
329    * @return true if this blog contains entries, false otherwise
330    */
 
331  0 toggle public boolean hasBlogEntries() {
332  0 return !publishedBlogEntries.isEmpty();
333    }
334   
335    /**
336    * Gets the Day instance for the previous day.
337    *
338    * @return a Day instance
339    */
 
340  34 toggle public Day getPreviousDay() {
341  34 return month.getBlogForPreviousDay(this);
342    }
343   
344    /**
345    * Gets the Day instance for the next day.
346    *
347    * @return a Day instance
348    */
 
349  4 toggle public Day getNextDay() {
350  4 return month.getBlogForNextDay(this);
351    }
352   
353    /**
354    * Gets the blog entry posted previous (before) to the one specified.
355    *
356    * @param blogEntry a BlogEntry
357    * @return the previous BlogEntry, or null if one doesn't exist
358    */
 
359  6 toggle public String getPreviousBlogEntry(String blogEntry) {
360  6 int index = publishedBlogEntries.indexOf(blogEntry);
361  6 if (index >= 0 && index < (publishedBlogEntries.size()-1)) {
362  0 return publishedBlogEntries.get(index+1);
363    } else {
364  6 return null;
365    }
366    }
367   
368    /**
369    * Gets the first entry that was posted on this day.
370    *
371    * @return a BlogEntry instance, or null is no entries have been posted
372    */
 
373  4 toggle public String getFirstBlogEntry() {
374  4 if (!publishedBlogEntries.isEmpty()) {
375  4 return publishedBlogEntries.get(publishedBlogEntries.size()-1);
376    } else {
377  0 return null;
378    }
379    }
380   
381    /**
382    * Gets the last entry that was posted on this day.
383    *
384    * @return a BlogEntry instance, or null is no entries have been posted
385    */
 
386  22 toggle public String getLastBlogEntry() {
387  22 if (!publishedBlogEntries.isEmpty()) {
388  4 return publishedBlogEntries.get(0);
389    } else {
390  18 return null;
391    }
392    }
393   
394    /**
395    * Gets the blog entry posted next (afterwards) to the one specified.
396    *
397    * @param blogEntry a BlogEntry
398    * @return the next BlogEntry, or null if one doesn't exist
399    */
 
400  6 toggle public String getNextBlogEntry(String blogEntry) {
401  6 int index = publishedBlogEntries.lastIndexOf(blogEntry);
402  6 if (index > 0 && index <= publishedBlogEntries.size()) {
403  0 return publishedBlogEntries.get(index-1);
404    } else {
405  6 return null;
406    }
407    }
408   
 
409  0 toggle public Date getStartOfDay() {
410  0 Calendar cal = getCalendar();
411  0 cal.set(Calendar.HOUR_OF_DAY, 0);
412  0 cal.set(Calendar.MINUTE, 0);
413  0 cal.set(Calendar.SECOND, 0);
414  0 cal.set(Calendar.MILLISECOND, 1);
415   
416  0 return cal.getTime();
417    }
418   
 
419  0 toggle public Date getEndOfDay() {
420  0 Calendar cal = getCalendar();
421  0 cal.set(Calendar.HOUR_OF_DAY, 23);
422  0 cal.set(Calendar.MINUTE, 59);
423  0 cal.set(Calendar.SECOND, 59);
424  0 cal.set(Calendar.MILLISECOND, 999);
425   
426  0 return cal.getTime();
427    }
428   
429    /**
430    * Determines if the this Day is before (in the calendar) the
431    * specified Day.
432    *
433    * @return true if this instance represents an earlier month than the
434    * specified Day instance, false otherwise
435    */
 
436  0 toggle public boolean before(Day day) {
437  0 return getDate().before(day.getDate());
438    }
439   
440    }