summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoWeblogMonthsCalendar.java
blob: 481f05a9ec36d9dad2dc97a321e7a46ea293d820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.madore.damlengine;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSInput;

public final class TodoWeblogMonthsCalendar extends TodoDefaultElement {

    public static final String[] monthAbbr = {
	null,
	"Jan", "Feb", "Mar", "Apr",
	"May", "Jun", "Jul", "Aug",
	"Sep", "Oct", "Nov", "Dec",
    };

    public static final String[] monthNamesEn = {
	null,
	"January", "February", "March", "April",
	"May", "June", "July", "August",
	"September", "October", "November", "December",
    };

    public static final String[] monthNamesFr = {
	null,
	"janvier", "f\u00e9vrier", "mars", "avril",
	"mai", "juin", "juillet", "ao\u00fbt",
	"septembre", "octobre", "novembre", "d\u00e9cembre",
    };

    public static final Map<String,String[]> monthNames;

    static {
	monthNames = new HashMap<String,String[]>();
	monthNames.put("en", monthNamesEn);
	monthNames.put("fr", monthNamesFr);
    }

    public static class Factory extends TodoElement.Factory {
	@Override
	public TodoWeblogMonthsCalendar newItem(Element node,
						Context ctx,
						TodoItem caller) {
	    return new TodoWeblogMonthsCalendar(node, ctx, caller);
	}
    }

    public TodoWeblogMonthsCalendar(Element node,
				    Context ctx,
				    TodoItem caller) {
	super(node, ctx, caller);
    }

    @Override
    public void handleNodeOnly() {
	Element table = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "table");
	node.getParentNode().replaceChild(table, node);
	table.setAttributeNS(null, "border", "1");
	table.setAttributeNS(null, "class", "weblog-calendar");
	table.appendChild(ctx.doc.createTextNode("\n\n"));
	String targetStdDir = (ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/");
	try {
	    final Connection conn = WeblogDatabaseConnection.getConnection();
	    final PreparedStatement selSt
		= conn.prepareStatement("SELECT min(edate) , max(edate) FROM entries");
	    final ResultSet selRes = selSt.executeQuery();
	    int minedateYear, minedateMonth, maxedateYear, maxedateMonth;
	    if ( selRes.next() ) {
		String minedate = selRes.getString(1);
		String maxedate = selRes.getString(2);
		Matcher matcher;
		if ( (matcher=Pattern.compile("(\\d{4})-(\\d{2})-\\d{2}").matcher(minedate)).matches() ) {
		    minedateYear = Integer.parseInt(matcher.group(1));
		    minedateMonth = Integer.parseInt(matcher.group(2));
		} else
		    throw new IllegalStateException("entry date field must be of the form YYYY-MM-DD");
		if ( (matcher=Pattern.compile("(\\d{4})-(\\d{2})-\\d{2}").matcher(maxedate)).matches() ) {
		    maxedateYear = Integer.parseInt(matcher.group(1));
		    maxedateMonth = Integer.parseInt(matcher.group(2));
		} else
		    throw new IllegalStateException("entry date field must be of the form YYYY-MM-DD");
	    } else
		throw new IllegalStateException("failed to extract bounding dates");
	    for ( int year = maxedateYear ; year >= minedateYear ; year-- ) {
		Element tr = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "tr");
		table.appendChild(tr);
		table.appendChild(ctx.doc.createTextNode("\n\n"));
		Element th = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "th");
		tr.appendChild(th);
		th.appendChild(ctx.doc.createTextNode(Integer.toString(year)));
		tr.appendChild(ctx.doc.createTextNode("\n"));
		if ( year==minedateYear && minedateMonth > 1 ) {
		    Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td");
		    td.setAttributeNS(null, "colspan",
				      Integer.toString(minedateMonth-1));
		    tr.appendChild(td);
		    tr.appendChild(ctx.doc.createTextNode("\n"));
		}
		for ( int month = (year==minedateYear?minedateMonth:1) ;
		      month <= (year==maxedateYear?maxedateMonth:12) ;
		      month++ ) {
		    Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td");
		    tr.appendChild(td);
		    if ( ( ctx.wsc instanceof Context.WeblogMonthSelectionContext )
			 && year == Integer.parseInt(((Context.WeblogMonthSelectionContext)(ctx.wsc)).year)
			 && month == Integer.parseInt(((Context.WeblogMonthSelectionContext)(ctx.wsc)).month) )
			td.setAttributeNS(null, "class", "weblog-calendar-selected-month");
		    Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
		    td.appendChild(a);
		    a.setAttributeNS(null, "href",
				     targetStdDir + Integer.toString(year) + "-"
				     + String.format("%02d",month) + ".html");
		    a.appendChild(ctx.doc.createTextNode(monthAbbr[month] + " " + Integer.toString(year)));
		    tr.appendChild(ctx.doc.createTextNode("\n"));
		}
		if ( year==maxedateYear && maxedateMonth < 12 ) {
		    Element td = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "td");
		    td.setAttributeNS(null, "colspan",
				      Integer.toString(12-maxedateMonth));
		    tr.appendChild(td);
		    tr.appendChild(ctx.doc.createTextNode("\n"));
		}
	    }
	} catch (SQLException e) {
	}
    }

}