summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/WeblogSelect.java
blob: d6ede8be27e8f3283e1fd492f700aabba66351ef (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
package org.madore.damlengine;

import java.util.TreeSet;
import java.util.ArrayList;
import java.io.OutputStream;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public final class WeblogSelect {

    private WeblogSelect() { // Forbid instantiation
	throw new AssertionError("WeblogSelect cannot be instantiated");
    }

    public static void fullProcess(Context.WeblogSelectionContext wsc,
				   Context.DynamicContext dc,
				   OutputStream out)
	throws Exception {

	final Connection conn = WeblogDatabaseConnection.getConnection();

	String templateResourceName;
	final PreparedStatement selSt;
	if ( wsc instanceof Context.WeblogMonthSelectionContext ) {
	    templateResourceName = "weblog-month-template.daml";
	    selSt = conn.prepareStatement("SELECT id , content FROM entries WHERE edate LIKE ? ORDER BY id DESC");
	    selSt.setString(1,((Context.WeblogMonthSelectionContext)wsc).year+"-"+((Context.WeblogMonthSelectionContext)wsc).month+"-__");
	} else if ( wsc instanceof Context.WeblogCategorySelectionContext ) {
	    templateResourceName = "weblog-cat-template.daml";
	    selSt = conn.prepareStatement("SELECT entries.id , entries.content FROM entries , incat WHERE entries.id=incat.id AND incat.code=? ORDER BY entries.id DESC");
	    selSt.setString(1,((Context.WeblogCategorySelectionContext)wsc).code);
	} else if ( wsc instanceof Context.WeblogRecentSelectionContext ) {
	    templateResourceName = "weblog-recent-template.daml";
	    selSt = conn.prepareStatement("SELECT id , content FROM entries ORDER BY id DESC LIMIT ?");
	    selSt.setInt(1,((Context.WeblogRecentSelectionContext)wsc).count);
	} else if ( wsc instanceof Context.WeblogSingleSelectionContext ) {
	    templateResourceName = "weblog-single-template.daml";
	    selSt = conn.prepareStatement("SELECT id , content FROM entries WHERE id=?");
	    selSt.setInt(1,((Context.WeblogSingleSelectionContext)wsc).number);
	} else
	    throw new IllegalArgumentException("don't know how to perform this selection");

	final ResultSet selRes = selSt.executeQuery();
	wsc.sel = new TreeSet<Integer>();
	wsc.xmlData = new ArrayList<String>();
	while ( selRes.next() ) {
	    int id = selRes.getInt(1);
	    String content = selRes.getString(2);
	    wsc.sel.add(Integer.valueOf(id));
	    wsc.xmlData.add(content);
	}

	DamlEngine.fullProcess(Files.newInputStream(DamlEngine.templatePath.resolve(templateResourceName)),
			       out, wsc, dc);

    }

    public static void fullProcess(Context.WeblogSelectionContext wsc,
				   OutputStream out)
	throws Exception {
	fullProcess(wsc, null, out);
    }

}