0
|
1 package nabble.view.web.template;
|
|
2
|
|
3 import nabble.model.Node;
|
|
4 import nabble.model.NodeIterator;
|
|
5 import nabble.naml.compiler.Command;
|
|
6 import nabble.naml.compiler.CommandSpec;
|
|
7 import nabble.naml.compiler.IPrintWriter;
|
|
8 import nabble.naml.compiler.Interpreter;
|
|
9 import nabble.naml.compiler.Namespace;
|
|
10 import nabble.naml.compiler.ScopedInterpreter;
|
|
11 import nabble.naml.namespaces.ListSequence;
|
|
12 import nabble.view.lib.Permissions;
|
|
13
|
|
14 import javax.servlet.ServletException;
|
|
15 import java.io.IOException;
|
|
16 import java.util.ArrayList;
|
|
17 import java.util.Calendar;
|
|
18 import java.util.Collections;
|
|
19 import java.util.List;
|
|
20 import java.util.Map;
|
|
21 import java.util.TreeMap;
|
|
22
|
|
23
|
|
24 @Namespace (
|
|
25 name = "monthly_archives",
|
|
26 global = false
|
|
27 )
|
|
28 public class MonthlyArchivesNamespace {
|
|
29
|
|
30 private int total = 0;
|
|
31 private Map<Short, Map<Byte, Integer>> years = new TreeMap<Short, Map<Byte, Integer>>(Collections.reverseOrder());
|
|
32
|
|
33 public MonthlyArchivesNamespace(Node node) {
|
|
34 NodeIterator<? extends Node> it = node.getTopicsByLastNodeDate(null,Permissions.canBeViewedByParentViewersFilter);
|
|
35 Node[] nodes = it.get(0, 5000).toArray(new Node[0]);
|
|
36 for (int i = 0; i < nodes.length; i++) {
|
|
37 Calendar cal = Calendar.getInstance();
|
|
38 cal.setTime(nodes[i].getWhenCreated());
|
|
39 nodes[i] = null;
|
|
40 short year = (short) cal.get(Calendar.YEAR);
|
|
41
|
|
42 Map<Byte, Integer> months = years.get(year);
|
|
43 if (months == null) {
|
|
44 months = new TreeMap<Byte, Integer>();
|
|
45 years.put(year, months);
|
|
46 }
|
|
47 byte month = (byte) (cal.get(Calendar.MONTH) + 1);
|
|
48 Integer count = months.get(month);
|
|
49 count = count == null? 1 : count+1;
|
|
50 months.put(month, count);
|
|
51 total++;
|
|
52 }
|
|
53 }
|
|
54
|
|
55 public static final CommandSpec year_list = CommandSpec.DO;
|
|
56
|
|
57 @Command public void year_list(IPrintWriter out, ScopedInterpreter<Years> interp)
|
|
58 throws IOException, ServletException
|
|
59 {
|
|
60 List<YearRow> yearRows = new ArrayList<YearRow>();
|
|
61 for (short year : years.keySet()) {
|
|
62 yearRows.add(new YearRow(year, years.get(year)));
|
|
63 }
|
|
64 out.print(interp.getArg(new Years(yearRows),"do"));
|
|
65 }
|
|
66
|
|
67 @Command("total") public void _total(IPrintWriter out, Interpreter interp) {
|
|
68 out.print(total);
|
|
69 }
|
|
70
|
|
71 @Namespace (
|
|
72 name = "year_list",
|
|
73 global = true
|
|
74 )
|
|
75 public static final class Years extends ListSequence<YearRow>
|
|
76 {
|
|
77 public Years(List<YearRow> elements) {
|
|
78 super(elements);
|
|
79 }
|
|
80
|
|
81 public static final CommandSpec current_year = CommandSpec.DO;
|
|
82
|
|
83 @Command public void current_year(IPrintWriter out,ScopedInterpreter<YearRow> interp) {
|
|
84 out.print(interp.getArg(get(),"do"));
|
|
85 }
|
|
86 }
|
|
87
|
|
88 @Namespace (
|
|
89 name = "year_row",
|
|
90 global = false
|
|
91 )
|
|
92 public static class YearRow {
|
|
93 private final short year;
|
|
94 private final Map<Byte, Integer> monthMap;
|
|
95
|
|
96 public YearRow(short year, Map<Byte, Integer> monthMap) {
|
|
97 this.year = year;
|
|
98 this.monthMap = monthMap;
|
|
99 }
|
|
100
|
|
101 @Command("year") public void _year(IPrintWriter out, Interpreter interp) {
|
|
102 out.print(year);
|
|
103 }
|
|
104
|
|
105 public static final CommandSpec month_list = CommandSpec.DO;
|
|
106
|
|
107 @Command public void month_list(IPrintWriter out,ScopedInterpreter<Months> interp) {
|
|
108 List<MonthRow> monthRows = new ArrayList<MonthRow>();
|
|
109 for (Map.Entry<Byte, Integer> entry : monthMap.entrySet()) {
|
|
110 monthRows.add(new MonthRow(year, entry.getKey(), entry.getValue()));
|
|
111 }
|
|
112 out.print( interp.getArg(new Months(monthRows),"do") );
|
|
113 }
|
|
114 }
|
|
115
|
|
116 @Namespace (
|
|
117 name = "month_list",
|
|
118 global = true
|
|
119 )
|
|
120 public static final class Months extends ListSequence<MonthRow>
|
|
121 {
|
|
122 public Months(List<MonthRow> elements) {
|
|
123 super(elements);
|
|
124 }
|
|
125
|
|
126 public static final CommandSpec current_month = CommandSpec.DO;
|
|
127
|
|
128 @Command public void current_month(IPrintWriter out,ScopedInterpreter<MonthRow> interp) {
|
|
129 out.print(interp.getArg(get(),"do"));
|
|
130 }
|
|
131 }
|
|
132
|
|
133 @Namespace (
|
|
134 name = "month_row",
|
|
135 global = false
|
|
136 )
|
|
137 public static class MonthRow {
|
|
138 private final short year;
|
|
139 private final byte month;
|
|
140 private final int count;
|
|
141
|
|
142 public MonthRow(short year, byte month, int count) {
|
|
143 this.year = year;
|
|
144 this.month = month;
|
|
145 this.count = count;
|
|
146 }
|
|
147
|
|
148 @Command("month") public void _month(IPrintWriter out, Interpreter interp) {
|
|
149 String m = String.valueOf(month);
|
|
150 out.print(m.length() ==1? '0'+m : m);
|
|
151 }
|
|
152
|
|
153 @Command("year") public void _year(IPrintWriter out, Interpreter interp) {
|
|
154 out.print(year);
|
|
155 }
|
|
156
|
|
157 @Command("count") public void _count(IPrintWriter out, Interpreter interp) {
|
|
158 out.print(count);
|
|
159 }
|
|
160 }
|
|
161 }
|