Coverage Report - net.sourceforge.pebble.domain.Day
 
Classes in this File Line Coverage Branch Coverage Complexity
Day
74%
56/75
53%
14/26
1.85
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  274552
   private List<String> blogEntries = new ArrayList<String>();
 53  274552
   private List<String> publishedBlogEntries = new ArrayList<String>();
 54  274552
   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  
   Day(Month month, int day) {
 63  274552
     super(month.getBlog());
 64  
 
 65  274552
     this.month = month;
 66  274552
     this.day = day;
 67  274552
     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  274552
   }
 118  
 
 119  
   /**
 120  
    * Gets a Calendar object representing today.
 121  
    *
 122  
    * @return    a Calendar instance
 123  
    */
 124  
   private Calendar getCalendar() {
 125  
 
 126  
     // and set the actual date for this day
 127  274552
     Calendar cal = getBlog().getCalendar();
 128  274552
     cal.set(Calendar.YEAR, getMonth().getYear().getYear());
 129  274552
     cal.set(Calendar.MONTH, getMonth().getMonth() - 1);
 130  274552
     cal.set(Calendar.DAY_OF_MONTH, getDay());
 131  274552
     cal.set(Calendar.HOUR_OF_DAY, 12);
 132  274552
     cal.set(Calendar.MINUTE, 0);
 133  274552
     cal.set(Calendar.SECOND, 0);
 134  274552
     cal.set(Calendar.MILLISECOND, 0);
 135  
 
 136  274552
     return cal;
 137  
   }
 138  
 
 139  
   /**
 140  
    * Gets a reference to the parent Month instance.
 141  
    *
 142  
    * @return  a Month instance
 143  
    */
 144  
   public Month getMonth() {
 145  549192
     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  
   public int getDay() {
 154  274788
     return day;
 155  
   }
 156  
 
 157  
   /**
 158  
    * Gets the permalink to display all entries for this Day.
 159  
    *
 160  
    * @return  an absolute URL
 161  
    */
 162  
   public String getPermalink() {
 163  8
     String s = getBlog().getPermalinkProvider().getPermalink(this);
 164  8
     if (s != null && s.length() > 0) {
 165  8
       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  
   public List<String> getBlogEntries() {
 177  93936
     return new ArrayList<String>(blogEntries);
 178  
   }
 179  
 
 180  
   public int getNumberOfBlogEntries() {
 181  0
     return publishedBlogEntries.size();
 182  
   }
 183  
 
 184  
   public synchronized void addPublishedBlogEntry(String blogEntryId) {
 185  220
     if (!publishedBlogEntries.contains(blogEntryId)) {
 186  216
       publishedBlogEntries.add(blogEntryId);
 187  216
       Collections.sort(publishedBlogEntries, new ReverseBlogEntryIdComparator());
 188  
     }
 189  220
     unpublishedBlogEntries.remove(blogEntryId);
 190  
 
 191  220
     if (!blogEntries.contains(blogEntryId)) {
 192  
       // and add to the aggregated view
 193  216
       blogEntries.add(blogEntryId);
 194  216
       Collections.sort(blogEntries, new ReverseBlogEntryIdComparator());
 195  
     }
 196  220
   }
 197  
 
 198  
   public synchronized void addUnpublishedBlogEntry(String blogEntryId) {
 199  444
     if (!unpublishedBlogEntries.contains(blogEntryId)) {
 200  444
       unpublishedBlogEntries.add(blogEntryId);
 201  444
       Collections.sort(unpublishedBlogEntries, new ReverseBlogEntryIdComparator());
 202  
     }
 203  444
     publishedBlogEntries.remove(blogEntryId);
 204  
 
 205  444
     if (!blogEntries.contains(blogEntryId)) {
 206  
       // and add to the aggregated view
 207  444
       blogEntries.add(blogEntryId);
 208  444
       Collections.sort(blogEntries, new ReverseBlogEntryIdComparator());
 209  
     }
 210  444
   }
 211  
 
 212  
   public synchronized void removeBlogEntry(BlogEntry blogEntry) {
 213  72
     publishedBlogEntries.remove(blogEntry.getId());
 214  72
     unpublishedBlogEntries.remove(blogEntry.getId());
 215  72
     blogEntries.remove(blogEntry.getId());
 216  72
   }
 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  
   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  
   public Day getPreviousDay() {
 341  90
     return month.getBlogForPreviousDay(this);
 342  
   }
 343  
 
 344  
   /**
 345  
    * Gets the Day instance for the next day.
 346  
    *
 347  
    * @return    a Day instance
 348  
    */
 349  
   public Day getNextDay() {
 350  8
     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  
   public String getPreviousBlogEntry(String blogEntry) {
 360  12
     int index = publishedBlogEntries.indexOf(blogEntry);
 361  12
     if (index >= 0 && index < (publishedBlogEntries.size()-1)) {
 362  0
       return publishedBlogEntries.get(index+1);
 363  
     } else {
 364  12
       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  
   public String getFirstBlogEntry() {
 374  8
     if (!publishedBlogEntries.isEmpty()) {
 375  8
       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  
   public String getLastBlogEntry() {
 387  66
     if (!publishedBlogEntries.isEmpty()) {
 388  8
       return publishedBlogEntries.get(0);
 389  
     } else {
 390  58
       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  
   public String getNextBlogEntry(String blogEntry) {
 401  12
     int index = publishedBlogEntries.lastIndexOf(blogEntry);
 402  12
     if (index > 0 && index <= publishedBlogEntries.size()) {
 403  0
       return publishedBlogEntries.get(index-1);
 404  
     } else {
 405  12
       return null;
 406  
     }
 407  
   }
 408  
 
 409  
   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  
   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  
   public boolean before(Day day) {
 437  0
     return getDate().before(day.getDate());
 438  
   }
 439  
 
 440  
 }