summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/WeblogSummary.java
blob: 6c2dc4cd76b3f2911fce36ad98c247dbae6382e7 (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
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<String> 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<Integer,EntrySummary> entries;
    private long obtainedTime;

    private static WeblogSummary singleton;

    private WeblogSummary() {
	this.entries = new HashMap<Integer,EntrySummary>();
    }

    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<String> catSet = singleton.entries.get(id).catSet;
		if ( catSet == null ) {
		    catSet = new HashSet<String>();
		    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;
    }

}