Show Archive.java syntax highlighted
package com.witfriend.arthemis.domain;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import org.apache.commons.lang.time.DateUtils;
/**
*
* @author wangyz
* @since 2006.10.05
* @version $Id$
*/
public class Archive implements Serializable {
private static final long serialVersionUID = 193370535157531140L;
private Calendar date;
private Boolean monthly;
private Weblog weblog;
public Calendar getDate() {
return date;
}
public void setDate(Calendar date) {
this.date = date;
}
public Boolean getMonthly() {
return monthly;
}
public void setMonthly(Boolean monthly) {
this.monthly = monthly;
}
public Weblog getWeblog() {
return weblog;
}
public void setWeblog(Weblog weblog) {
this.weblog = weblog;
}
public List<Article> getArticles() {
List<Article> articles = new ArrayList<Article>();
for(Article article: weblog.getArticles()) {
Calendar time = article.getTime();
time.setTimeZone(date.getTimeZone());
if(DateUtils.isSameInstant(date, DateUtils.truncate(time, getMonthly() ? Calendar.MONTH : Calendar.DAY_OF_MONTH))) {
articles.add(article);
}
}
return articles;
}
public List<Category> getCategories() {
List<Article> articles = getArticles();
List<Category> categories = new ArrayList<Category>();
for(Article article: articles) {
Category category = article.getCategory();
if(!categories.contains(category)) {
for(Iterator<Article> iterator = category.getArticles().iterator(); iterator.hasNext();) {
if(!articles.contains(iterator.next())) {
iterator.remove();
}
}
categories.add(category);
}
}
return categories;
}
public List<Comment> getComments() {
return Comment.getComments(getArticles());
}
public List<Trackback> getTrackbacks() {
return Trackback.getTrackbacks(getArticles());
}
/**
* Extract all available archives from a list
* of articles according to a given time zone.
* @param articles
* @param timeZone
* @return
*/
static List<Archive> getArchives(List<Article> articles, TimeZone timeZone) {
List<Archive> archives = new ArrayList<Archive>();
for(Article article: articles) {
Calendar time = article.getTime();
time.setTimeZone(timeZone);
time = DateUtils.truncate(time, Calendar.MONTH);
boolean exists = false;
for(Archive archive: archives) {
if(DateUtils.isSameInstant(time, archive.getDate())) {
exists = true;
}
}
if(!exists) {
Archive archive = new Archive();
archive.setDate(time);
archive.setMonthly(true);
archive.setWeblog(article.getCategory().getWeblog());
archives.add(archive);
}
}
return archives;
}
}
See more files for this project here