Mercurial Hosting > nabble
view src/nabble/view/web/template/MonthlyArchivesNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line source
package nabble.view.web.template; import nabble.model.Node; import nabble.model.NodeIterator; import nabble.naml.compiler.Command; import nabble.naml.compiler.CommandSpec; import nabble.naml.compiler.IPrintWriter; import nabble.naml.compiler.Interpreter; import nabble.naml.compiler.Namespace; import nabble.naml.compiler.ScopedInterpreter; import nabble.naml.namespaces.ListSequence; import nabble.view.lib.Permissions; import javax.servlet.ServletException; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.TreeMap; @Namespace ( name = "monthly_archives", global = false ) public class MonthlyArchivesNamespace { private int total = 0; private Map<Short, Map<Byte, Integer>> years = new TreeMap<Short, Map<Byte, Integer>>(Collections.reverseOrder()); public MonthlyArchivesNamespace(Node node) { NodeIterator<? extends Node> it = node.getTopicsByLastNodeDate(null,Permissions.canBeViewedByParentViewersFilter); Node[] nodes = it.get(0, 5000).toArray(new Node[0]); for (int i = 0; i < nodes.length; i++) { Calendar cal = Calendar.getInstance(); cal.setTime(nodes[i].getWhenCreated()); nodes[i] = null; short year = (short) cal.get(Calendar.YEAR); Map<Byte, Integer> months = years.get(year); if (months == null) { months = new TreeMap<Byte, Integer>(); years.put(year, months); } byte month = (byte) (cal.get(Calendar.MONTH) + 1); Integer count = months.get(month); count = count == null? 1 : count+1; months.put(month, count); total++; } } public static final CommandSpec year_list = CommandSpec.DO; @Command public void year_list(IPrintWriter out, ScopedInterpreter<Years> interp) throws IOException, ServletException { List<YearRow> yearRows = new ArrayList<YearRow>(); for (short year : years.keySet()) { yearRows.add(new YearRow(year, years.get(year))); } out.print(interp.getArg(new Years(yearRows),"do")); } @Command("total") public void _total(IPrintWriter out, Interpreter interp) { out.print(total); } @Namespace ( name = "year_list", global = true ) public static final class Years extends ListSequence<YearRow> { public Years(List<YearRow> elements) { super(elements); } public static final CommandSpec current_year = CommandSpec.DO; @Command public void current_year(IPrintWriter out,ScopedInterpreter<YearRow> interp) { out.print(interp.getArg(get(),"do")); } } @Namespace ( name = "year_row", global = false ) public static class YearRow { private final short year; private final Map<Byte, Integer> monthMap; public YearRow(short year, Map<Byte, Integer> monthMap) { this.year = year; this.monthMap = monthMap; } @Command("year") public void _year(IPrintWriter out, Interpreter interp) { out.print(year); } public static final CommandSpec month_list = CommandSpec.DO; @Command public void month_list(IPrintWriter out,ScopedInterpreter<Months> interp) { List<MonthRow> monthRows = new ArrayList<MonthRow>(); for (Map.Entry<Byte, Integer> entry : monthMap.entrySet()) { monthRows.add(new MonthRow(year, entry.getKey(), entry.getValue())); } out.print( interp.getArg(new Months(monthRows),"do") ); } } @Namespace ( name = "month_list", global = true ) public static final class Months extends ListSequence<MonthRow> { public Months(List<MonthRow> elements) { super(elements); } public static final CommandSpec current_month = CommandSpec.DO; @Command public void current_month(IPrintWriter out,ScopedInterpreter<MonthRow> interp) { out.print(interp.getArg(get(),"do")); } } @Namespace ( name = "month_row", global = false ) public static class MonthRow { private final short year; private final byte month; private final int count; public MonthRow(short year, byte month, int count) { this.year = year; this.month = month; this.count = count; } @Command("month") public void _month(IPrintWriter out, Interpreter interp) { String m = String.valueOf(month); out.print(m.length() ==1? '0'+m : m); } @Command("year") public void _year(IPrintWriter out, Interpreter interp) { out.print(year); } @Command("count") public void _count(IPrintWriter out, Interpreter interp) { out.print(count); } } }