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(); wsc.xmlData = new ArrayList(); while ( selRes.next() ) { int id = selRes.getInt(1); String content = selRes.getString(2); wsc.sel.add(new Integer(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); } }