1 package net.sourceforge.blogentis.modules.fragments;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.sql.Connection;
26 import java.text.MessageFormat;
27 import java.util.ArrayList;
28 import java.util.Calendar;
29 import java.util.GregorianCalendar;
30 import java.util.List;
31
32 import net.sourceforge.blogentis.om.Blog;
33 import net.sourceforge.blogentis.om.PostPeer;
34 import net.sourceforge.blogentis.storage.VelocityFragment;
35 import net.sourceforge.blogentis.turbine.BlogParameterParser;
36 import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
37 import net.sourceforge.blogentis.utils.DateSpecification;
38
39 import org.apache.torque.TorqueException;
40 import org.apache.torque.util.Transaction;
41 import org.apache.turbine.util.RunData;
42 import org.apache.velocity.context.Context;
43
44 import com.workingdogs.village.QueryDataSet;
45 import com.workingdogs.village.Record;
46
47 /***
48 * @author abas
49 */
50
51 public class ArchivesByMonth extends VelocityFragment {
52 public static class Entry {
53 int month;
54
55 int year;
56
57 int posts;
58
59 Calendar cal;
60
61 String link;
62
63 public Entry(AbsoluteLinkURL link, String string, int posts) {
64 this.posts = posts;
65 year = Integer.parseInt(string.substring(0, string.indexOf('-')));
66 month = Integer.parseInt(string.substring(string.indexOf('-') + 1));
67 cal = GregorianCalendar.getInstance();
68 cal.set(year, month - 1, 1);
69 this.link = link
70 .setDate(new DateSpecification(year, month))
71 .toString();
72 }
73
74 public String format(String format) {
75 return MessageFormat.format(format, new Object[] {cal.getTime(),
76 new Integer(posts)});
77 }
78
79 public String getLink() {
80 return link;
81 }
82 }
83
84 public VelocityFragment invoke(Context context)
85 throws TorqueException {
86 RunData data = (RunData)context.get("data");
87 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
88 Blog blog = pp.getBlog();
89 if (pp.getPost() != null) {
90 context.remove("items");
91 return this;
92 }
93
94 Connection con = null;
95 try {
96 con = Transaction.begin(PostPeer.DATABASE_NAME);
97 String sql = " SELECT TO_CHAR(" + PostPeer.POSTED_TIME
98 + ", 'YYYY-MM') AS month, COUNT(1) FROM "
99 + PostPeer.TABLE_NAME + " WHERE " + PostPeer.BLOG_ID
100 + " = " + blog.getBlogId() + " AND "
101 + PostPeer.POSTED_TIME
102 + " IS NOT NULL GROUP BY month ORDER BY month DESC";
103 QueryDataSet qds = new QueryDataSet(con, sql);
104 qds.fetchRecords();
105
106 AbsoluteLinkURL link = new AbsoluteLinkURL();
107
108 List l = new ArrayList(qds.size());
109 for(int i = 0; i < qds.size(); i++) {
110 Record rec = qds.getRecord(i);
111 link.setBlog(pp.getBlog());
112 if (pp.getSection() != null)
113 link.setSection(pp.getSection());
114 l.add(new Entry(link, rec.getValue(1).asString(), rec.getValue(
115 2).asInt()));
116 }
117 Transaction.commit(con);
118 context.put("items", l);
119 } catch (Exception e) {
120 Transaction.safeRollback(con);
121 throw new TorqueException(e);
122 }
123 return this;
124 }
125 }