src/org/sonews/storage/impl/DrupalDatabase.java
changeset 69 b51612c18a54
parent 68 6e16e3bee1ca
child 70 2177f9b14688
     1.1 --- a/src/org/sonews/storage/impl/DrupalDatabase.java	Tue Oct 11 00:38:41 2011 +0200
     1.2 +++ b/src/org/sonews/storage/impl/DrupalDatabase.java	Tue Oct 11 01:17:30 2011 +0200
     1.3 @@ -30,6 +30,7 @@
     1.4  import java.util.logging.Level;
     1.5  import java.util.logging.Logger;
     1.6  import javax.mail.internet.MimeUtility;
     1.7 +import org.apache.commons.codec.net.QuotedPrintableCodec;
     1.8  import org.sonews.config.Config;
     1.9  import org.sonews.feed.Subscription;
    1.10  import org.sonews.storage.Article;
    1.11 @@ -51,6 +52,7 @@
    1.12  	/** How many times the database connection was reinitialized */
    1.13  	protected int restarts = 0;
    1.14  	protected Connection conn = null;
    1.15 +	private QuotedPrintableCodec qpc = new QuotedPrintableCodec("UTF-8");
    1.16  
    1.17  	public DrupalDatabase() throws StorageBackendException {
    1.18  		connectDatabase();
    1.19 @@ -140,27 +142,31 @@
    1.20  
    1.21  	private static String constructHeaders(ResultSet rs) throws SQLException, UnsupportedEncodingException {
    1.22  		StringBuilder sb = new StringBuilder();
    1.23 -		
    1.24 -		sb.append("Message-id: ");
    1.25 +
    1.26 +		sb.append("Message-id: <");
    1.27  		sb.append(rs.getInt("id"));
    1.28  		sb.append("-");
    1.29  		sb.append(rs.getInt("group_id"));
    1.30  		sb.append("-");
    1.31  		sb.append(rs.getString("group_name"));
    1.32  		sb.append("@");
    1.33 -		sb.append("nntp.kinderporno.cz");
    1.34 +		sb.append("nntp.kinderporno.cz>");
    1.35  		sb.append(CRLF);
    1.36 -		
    1.37 +
    1.38  		sb.append("From: ");
    1.39  		sb.append(MimeUtility.encodeWord(rs.getString("sender_name")));
    1.40  		sb.append(" <>");
    1.41  		sb.append(CRLF);
    1.42 -		
    1.43 +
    1.44  		sb.append("Subject: ");
    1.45  		sb.append(MimeUtility.encodeWord(rs.getString("subject")));
    1.46  		sb.append(CRLF);
    1.47 -		
    1.48 -		
    1.49 +
    1.50 +
    1.51 +		sb.append("Content-Type: text/html; charset=UTF-8");
    1.52 +		sb.append(CRLF);
    1.53 +		sb.append("Content-Transfer-Encoding: quoted-printable");
    1.54 +		sb.append(CRLF);
    1.55  
    1.56  		return sb.toString();
    1.57  	}
    1.58 @@ -237,8 +243,8 @@
    1.59  
    1.60  			if (rs.next()) {
    1.61  				String headers = constructHeaders(rs);
    1.62 -				byte[] body = rs.getString("text").getBytes();
    1.63 -				
    1.64 +				byte[] body = qpc.encode(rs.getString("text")).getBytes();
    1.65 +
    1.66  				return new Article(headers, body);
    1.67  			} else {
    1.68  				return null;
    1.69 @@ -393,14 +399,34 @@
    1.70  
    1.71  	@Override
    1.72  	public int countArticles() throws StorageBackendException {
    1.73 -		log.log(Level.SEVERE, "TODO: countArticles");
    1.74 -		return 0;
    1.75 +		PreparedStatement ps = null;
    1.76 +		ResultSet rs = null;
    1.77 +		try {
    1.78 +			ps = conn.prepareStatement("SELECT count(*) FROM nntp_article");
    1.79 +			rs = ps.executeQuery();
    1.80 +			rs.next();
    1.81 +			return rs.getInt(1);
    1.82 +		} catch (Exception e) {
    1.83 +			throw new StorageBackendException(e);
    1.84 +		} finally {
    1.85 +			close(null, ps, rs);
    1.86 +		}
    1.87  	}
    1.88  
    1.89  	@Override
    1.90  	public int countGroups() throws StorageBackendException {
    1.91 -		log.log(Level.SEVERE, "TODO: countGroups");
    1.92 -		return 0;
    1.93 +		PreparedStatement ps = null;
    1.94 +		ResultSet rs = null;
    1.95 +		try {
    1.96 +			ps = conn.prepareStatement("SELECT count(*) FROM nntp_group");
    1.97 +			rs = ps.executeQuery();
    1.98 +			rs.next();
    1.99 +			return rs.getInt(1);
   1.100 +		} catch (Exception e) {
   1.101 +			throw new StorageBackendException(e);
   1.102 +		} finally {
   1.103 +			close(null, ps, rs);
   1.104 +		}
   1.105  	}
   1.106  
   1.107  	@Override
   1.108 @@ -410,7 +436,7 @@
   1.109  
   1.110  	@Override
   1.111  	public String getConfigValue(String key) throws StorageBackendException {
   1.112 -		log.log(Level.SEVERE, "TODO: getConfigValue {0}", new Object[]{key});
   1.113 +		//log.log(Level.SEVERE, "TODO: getConfigValue {0}", new Object[]{key});
   1.114  		return null;
   1.115  	}
   1.116  
   1.117 @@ -446,8 +472,19 @@
   1.118  
   1.119  	@Override
   1.120  	public int getPostingsCount(String groupname) throws StorageBackendException {
   1.121 -		log.log(Level.SEVERE, "TODO: getPostingsCount {0}", new Object[]{groupname});
   1.122 -		return 0;
   1.123 +		PreparedStatement ps = null;
   1.124 +		ResultSet rs = null;
   1.125 +		try {
   1.126 +			ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE group_name = ?");
   1.127 +			ps.setString(1, groupname);
   1.128 +			rs = ps.executeQuery();
   1.129 +			rs.next();
   1.130 +			return rs.getInt(1);
   1.131 +		} catch (Exception e) {
   1.132 +			throw new StorageBackendException(e);
   1.133 +		} finally {
   1.134 +			close(null, ps, rs);
   1.135 +		}
   1.136  	}
   1.137  
   1.138  	@Override