package org.madore.damlengine; import java.util.HashMap; import java.util.HashSet; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public final class WeblogSummary { public static final class EntrySummary { int id; String date; String lang; String title; String titleXml; String specialName; boolean hasCut; HashSet catSet; public EntrySummary(int id, String date, String lang, String title, String titleXml, String specialName, boolean hasCut) { this.id = id; this.date = date; this.lang = lang; this.title = title; this.titleXml = titleXml; this.specialName = specialName; this.hasCut = hasCut; this.catSet = null; } } public HashMap entries; private long obtainedTime; private static WeblogSummary singleton; private WeblogSummary() { this.entries = new HashMap(); } public static synchronized WeblogSummary getSummary(Context.DynamicContext dc) { if ( singleton != null && ( dc == null || ( singleton.obtainedTime > dc.modTime && singleton.obtainedTime > System.currentTimeMillis()-300000 ) ) ) return singleton; singleton = new WeblogSummary(); try { singleton.obtainedTime = System.currentTimeMillis(); final Connection conn = WeblogDatabaseConnection.getConnection(); final PreparedStatement selSt = conn.prepareStatement("SELECT id , edate , lang , title , title_xml , special_name , has_cut FROM entries"); final ResultSet selRes = selSt.executeQuery(); while ( selRes.next() ) { int id = selRes.getInt(1); String date = selRes.getString(2); String lang = selRes.getString(3); String title = selRes.getString(4); String titleXml = selRes.getString(5); String specialName = selRes.getString(6); boolean hasCut = selRes.getBoolean(7); singleton.entries.put(id, new EntrySummary(id, date, lang, title, titleXml, specialName, hasCut)); } final PreparedStatement catSt = conn.prepareStatement("SELECT id , code FROM incat"); final ResultSet catRes = catSt.executeQuery(); while ( catRes.next() ) { int id = catRes.getInt(1); String code = catRes.getString(2); HashSet catSet = singleton.entries.get(id).catSet; if ( catSet == null ) { catSet = new HashSet(); singleton.entries.get(id).catSet = catSet; } catSet.add(code); } } catch (SQLException e) { // Well, we'll have no summary. Too bad, but better than abort. singleton = null; } return singleton; } }