src/org/sonews/storage/impl/DrupalDatabase.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 11 Oct 2011 16:17:51 +0200
changeset 70 2177f9b14688
parent 69 b51612c18a54
child 71 beb11d70f0eb
permissions -rw-r--r--
Drupal: hlavičky, poměrně funkční čtení příspěvků (vlánka).
franta-hg@63
     1
/*
franta-hg@63
     2
 *   SONEWS News Server
franta-hg@63
     3
 *   see AUTHORS for the list of contributors
franta-hg@63
     4
 *
franta-hg@63
     5
 *   This program is free software: you can redistribute it and/or modify
franta-hg@63
     6
 *   it under the terms of the GNU General Public License as published by
franta-hg@63
     7
 *   the Free Software Foundation, either version 3 of the License, or
franta-hg@63
     8
 *   (at your option) any later version.
franta-hg@63
     9
 *
franta-hg@63
    10
 *   This program is distributed in the hope that it will be useful,
franta-hg@63
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@63
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
franta-hg@63
    13
 *   GNU General Public License for more details.
franta-hg@63
    14
 *
franta-hg@63
    15
 *   You should have received a copy of the GNU General Public License
franta-hg@63
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
franta-hg@63
    17
 */
franta-hg@63
    18
package org.sonews.storage.impl;
franta-hg@63
    19
franta-hg@68
    20
import java.io.UnsupportedEncodingException;
franta-hg@68
    21
import java.sql.Connection;
franta-hg@68
    22
import java.sql.DriverManager;
franta-hg@68
    23
import java.sql.PreparedStatement;
franta-hg@68
    24
import java.sql.ResultSet;
franta-hg@64
    25
import java.sql.SQLException;
franta-hg@68
    26
import java.sql.Statement;
franta-hg@68
    27
import java.util.ArrayList;
franta-hg@65
    28
import java.util.Collections;
franta-hg@70
    29
import java.util.Date;
franta-hg@64
    30
import java.util.List;
franta-hg@65
    31
import java.util.logging.Level;
franta-hg@66
    32
import java.util.logging.Logger;
franta-hg@70
    33
import javax.mail.internet.MailDateFormat;
franta-hg@68
    34
import javax.mail.internet.MimeUtility;
franta-hg@70
    35
import org.apache.commons.codec.net.BCodec;
franta-hg@69
    36
import org.apache.commons.codec.net.QuotedPrintableCodec;
franta-hg@68
    37
import org.sonews.config.Config;
franta-hg@64
    38
import org.sonews.feed.Subscription;
franta-hg@64
    39
import org.sonews.storage.Article;
franta-hg@64
    40
import org.sonews.storage.ArticleHead;
franta-hg@64
    41
import org.sonews.storage.Group;
franta-hg@64
    42
import org.sonews.storage.Storage;
franta-hg@64
    43
import org.sonews.storage.StorageBackendException;
franta-hg@64
    44
import org.sonews.util.Pair;
franta-hg@64
    45
franta-hg@63
    46
/**
franta-hg@63
    47
 *
franta-hg@63
    48
 * @author František Kučera (frantovo.cz)
franta-hg@63
    49
 */
franta-hg@64
    50
public class DrupalDatabase implements Storage {
franta-hg@67
    51
franta-hg@66
    52
	private static final Logger log = Logger.getLogger(DrupalDatabase.class.getName());
franta-hg@70
    53
	public static final String CHARSET = "UTF-8";
franta-hg@68
    54
	public static final String CRLF = "\r\n";
franta-hg@68
    55
	public static final int MAX_RESTARTS = 2;
franta-hg@68
    56
	/** How many times the database connection was reinitialized */
franta-hg@68
    57
	protected int restarts = 0;
franta-hg@68
    58
	protected Connection conn = null;
franta-hg@70
    59
	private QuotedPrintableCodec qpc = new QuotedPrintableCodec(CHARSET);
franta-hg@70
    60
	// TODO: správná doména
franta-hg@70
    61
	private String myDomain = "kinderporno.cz";
franta-hg@68
    62
franta-hg@68
    63
	public DrupalDatabase() throws StorageBackendException {
franta-hg@68
    64
		connectDatabase();
franta-hg@68
    65
	}
franta-hg@68
    66
franta-hg@68
    67
	private void connectDatabase() throws StorageBackendException {
franta-hg@68
    68
		try {
franta-hg@68
    69
			// Load database driver
franta-hg@68
    70
			String driverClass = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object");
franta-hg@68
    71
			Class.forName(driverClass);
franta-hg@68
    72
franta-hg@68
    73
			// Establish database connection
franta-hg@68
    74
			String url = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>");
franta-hg@68
    75
			String username = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root");
franta-hg@68
    76
			String password = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, "");
franta-hg@68
    77
			conn = DriverManager.getConnection(url, username, password);
franta-hg@68
    78
franta-hg@68
    79
			conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
franta-hg@68
    80
			if (conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE) {
franta-hg@68
    81
				log.warning("Database is NOT fully serializable!");
franta-hg@68
    82
			}
franta-hg@68
    83
		} catch (Exception e) {
franta-hg@68
    84
			throw new StorageBackendException(e);
franta-hg@68
    85
		}
franta-hg@68
    86
	}
franta-hg@68
    87
franta-hg@68
    88
	protected static void close(Connection connection, Statement statement, ResultSet resultSet) {
franta-hg@68
    89
		if (resultSet != null) {
franta-hg@68
    90
			try {
franta-hg@68
    91
				resultSet.close();
franta-hg@68
    92
			} catch (Exception e) {
franta-hg@68
    93
			}
franta-hg@68
    94
		}
franta-hg@68
    95
		if (statement != null) {
franta-hg@68
    96
			try {
franta-hg@68
    97
				statement.close();
franta-hg@68
    98
			} catch (Exception e) {
franta-hg@68
    99
			}
franta-hg@68
   100
		}
franta-hg@68
   101
		if (connection != null) {
franta-hg@68
   102
			try {
franta-hg@68
   103
				connection.close();
franta-hg@68
   104
			} catch (Exception e) {
franta-hg@68
   105
			}
franta-hg@68
   106
		}
franta-hg@68
   107
	}
franta-hg@64
   108
franta-hg@64
   109
	/**
franta-hg@68
   110
	 * 
franta-hg@70
   111
	 * @param messageID &lt;{0}-{1}-{2}@domain.tld&gt; where {0} is nntp_id and {1} is group_id and {2} is group_name
franta-hg@68
   112
	 * @return array where [0] = nntp_id and [1] = group_id and [2] = group_name or returns null if messageID is invalid
franta-hg@64
   113
	 */
franta-hg@68
   114
	private static String[] parseMessageID(String messageID) {
franta-hg@70
   115
		if (messageID.matches("<[0-9]+\\-[0-9]+\\-[a-z0-9\\.]+@.+>")) {
franta-hg@70
   116
			return messageID.substring(1).split("@")[0].split("\\-");
franta-hg@68
   117
		} else {
franta-hg@68
   118
			return null;
franta-hg@68
   119
		}
franta-hg@68
   120
	}
franta-hg@68
   121
franta-hg@68
   122
	private static Long parseArticleID(String messageID) {
franta-hg@68
   123
		String[] localPart = parseMessageID(messageID);
franta-hg@68
   124
		if (localPart == null) {
franta-hg@68
   125
			return null;
franta-hg@68
   126
		} else {
franta-hg@68
   127
			return Long.parseLong(localPart[0]);
franta-hg@68
   128
		}
franta-hg@68
   129
	}
franta-hg@68
   130
franta-hg@68
   131
	private static Long parseGroupID(String messageID) {
franta-hg@68
   132
		String[] localPart = parseMessageID(messageID);
franta-hg@68
   133
		if (localPart == null) {
franta-hg@68
   134
			return null;
franta-hg@68
   135
		} else {
franta-hg@68
   136
			return Long.parseLong(localPart[1]);
franta-hg@68
   137
		}
franta-hg@68
   138
	}
franta-hg@68
   139
franta-hg@68
   140
	private static String parseGroupName(String messageID) {
franta-hg@68
   141
		String[] localPart = parseMessageID(messageID);
franta-hg@68
   142
		if (localPart == null) {
franta-hg@68
   143
			return null;
franta-hg@68
   144
		} else {
franta-hg@68
   145
			return localPart[2];
franta-hg@68
   146
		}
franta-hg@68
   147
	}
franta-hg@68
   148
franta-hg@70
   149
	private static String constructMessageId(int articleID, int groupID, String groupName, String domainName) {
franta-hg@70
   150
		StringBuilder sb = new StringBuilder();
franta-hg@70
   151
		sb.append("<");
franta-hg@70
   152
		sb.append(articleID);
franta-hg@70
   153
		sb.append("-");
franta-hg@70
   154
		sb.append(groupID);
franta-hg@70
   155
		sb.append("-");
franta-hg@70
   156
		sb.append(groupName);
franta-hg@70
   157
		sb.append("@");
franta-hg@70
   158
		sb.append(domainName);
franta-hg@70
   159
		sb.append(">");
franta-hg@70
   160
		return sb.toString();
franta-hg@70
   161
	}
franta-hg@70
   162
franta-hg@70
   163
	/**
franta-hg@70
   164
	 * 
franta-hg@70
   165
	 * @param sb header list to be appended with new header. List must be terminated by line end.
franta-hg@70
   166
	 * @param key header name (without : and space)
franta-hg@70
   167
	 * @param value header value
franta-hg@70
   168
	 * @param encode true if value should be encoded/escaped before appending
franta-hg@70
   169
	 * @throws UnsupportedEncodingException 
franta-hg@70
   170
	 */
franta-hg@70
   171
	private static void addHeader(StringBuilder sb, String key, String value, boolean encode) throws UnsupportedEncodingException {
franta-hg@70
   172
		sb.append(key);
franta-hg@70
   173
		sb.append(": ");
franta-hg@70
   174
		if (encode) {
franta-hg@70
   175
			sb.append(MimeUtility.encodeWord(value));
franta-hg@70
   176
		} else {
franta-hg@70
   177
			sb.append(value);
franta-hg@70
   178
		}
franta-hg@70
   179
		sb.append(CRLF);
franta-hg@70
   180
	}
franta-hg@70
   181
franta-hg@70
   182
	private String constructHeaders(ResultSet rs) throws SQLException, UnsupportedEncodingException {
franta-hg@68
   183
		StringBuilder sb = new StringBuilder();
franta-hg@69
   184
franta-hg@70
   185
		addHeader(sb, "Message-id", constructMessageId(rs.getInt("id"), rs.getInt("group_id"), rs.getString("group_name"), myDomain), false);
franta-hg@70
   186
		addHeader(sb, "From", MimeUtility.encodeWord(rs.getString("sender_name")) + " <>", false);
franta-hg@70
   187
		addHeader(sb, "Subject", rs.getString("subject"), true);
franta-hg@70
   188
		/** TODO: správný formát data: */
franta-hg@70
   189
		addHeader(sb, "Date", MailDateFormat.getInstance().format(new Date(rs.getLong("created") * 1000)), false);
franta-hg@70
   190
		addHeader(sb, "Content-Type", "text/html; charset=" + CHARSET, false);
franta-hg@70
   191
		addHeader(sb, "Content-Transfer-Encoding", "quoted-printable", false);
franta-hg@70
   192
		//addHeader(sb, "Content-Transfer-Encoding", "base64", false);
franta-hg@69
   193
franta-hg@70
   194
		Integer parentID = rs.getInt("parent_id");
franta-hg@70
   195
		if (parentID != null && parentID > 0) {
franta-hg@70
   196
			String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain);
franta-hg@70
   197
			addHeader(sb, "In-Reply-To", parentMessageID, false);
franta-hg@70
   198
			addHeader(sb, "References", parentMessageID, false);
franta-hg@70
   199
		}
franta-hg@68
   200
franta-hg@68
   201
		return sb.toString();
franta-hg@64
   202
	}
franta-hg@64
   203
franta-hg@64
   204
	@Override
franta-hg@67
   205
	public List<Group> getGroups() throws StorageBackendException {
franta-hg@68
   206
		PreparedStatement ps = null;
franta-hg@68
   207
		ResultSet rs = null;
franta-hg@68
   208
		try {
franta-hg@68
   209
			ps = conn.prepareStatement("SELECT * FROM nntp_group");
franta-hg@68
   210
			rs = ps.executeQuery();
franta-hg@68
   211
			List<Group> skupiny = new ArrayList<Group>();
franta-hg@68
   212
franta-hg@68
   213
			while (rs.next()) {
franta-hg@68
   214
				skupiny.add(new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY));
franta-hg@68
   215
			}
franta-hg@68
   216
franta-hg@68
   217
			return skupiny;
franta-hg@68
   218
		} catch (Exception e) {
franta-hg@68
   219
			throw new StorageBackendException(e);
franta-hg@68
   220
		} finally {
franta-hg@68
   221
			close(null, ps, rs);
franta-hg@68
   222
		}
franta-hg@64
   223
	}
franta-hg@64
   224
franta-hg@64
   225
	@Override
franta-hg@67
   226
	public Group getGroup(String name) throws StorageBackendException {
franta-hg@68
   227
		PreparedStatement ps = null;
franta-hg@68
   228
		ResultSet rs = null;
franta-hg@68
   229
		try {
franta-hg@68
   230
			ps = conn.prepareStatement("SELECT * FROM nntp_group WHERE name = ?");
franta-hg@68
   231
			ps.setString(1, name);
franta-hg@68
   232
			rs = ps.executeQuery();
franta-hg@68
   233
franta-hg@68
   234
			while (rs.next()) {
franta-hg@68
   235
				return new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY);
franta-hg@68
   236
			}
franta-hg@68
   237
franta-hg@68
   238
			return null;
franta-hg@68
   239
		} catch (Exception e) {
franta-hg@68
   240
			throw new StorageBackendException(e);
franta-hg@68
   241
		} finally {
franta-hg@68
   242
			close(null, ps, rs);
franta-hg@68
   243
		}
franta-hg@64
   244
	}
franta-hg@64
   245
franta-hg@64
   246
	@Override
franta-hg@67
   247
	public boolean isGroupExisting(String groupname) throws StorageBackendException {
franta-hg@68
   248
		return getGroup(groupname) != null;
franta-hg@64
   249
	}
franta-hg@64
   250
franta-hg@64
   251
	@Override
franta-hg@64
   252
	public Article getArticle(String messageID) throws StorageBackendException {
franta-hg@68
   253
		Long articleID = parseArticleID(messageID);
franta-hg@68
   254
		Long groupID = parseGroupID(messageID);
franta-hg@68
   255
franta-hg@68
   256
		if (articleID == null || groupID == null) {
franta-hg@68
   257
			log.log(Level.SEVERE, "Invalid messageID: {0}", new Object[]{messageID});
franta-hg@68
   258
			return null;
franta-hg@68
   259
		} else {
franta-hg@68
   260
			return getArticle(articleID, groupID);
franta-hg@68
   261
		}
franta-hg@64
   262
	}
franta-hg@64
   263
franta-hg@64
   264
	@Override
franta-hg@68
   265
	public Article getArticle(long articleID, long groupID) throws StorageBackendException {
franta-hg@68
   266
		PreparedStatement ps = null;
franta-hg@68
   267
		ResultSet rs = null;
franta-hg@68
   268
		try {
franta-hg@68
   269
			ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE id = ? AND group_id = ?");
franta-hg@68
   270
			ps.setLong(1, articleID);
franta-hg@68
   271
			ps.setLong(2, groupID);
franta-hg@68
   272
			rs = ps.executeQuery();
franta-hg@68
   273
franta-hg@68
   274
			if (rs.next()) {
franta-hg@68
   275
				String headers = constructHeaders(rs);
franta-hg@70
   276
				// TODO: fold?
franta-hg@70
   277
				BCodec bc = new BCodec(CHARSET);
franta-hg@69
   278
				byte[] body = qpc.encode(rs.getString("text")).getBytes();
franta-hg@70
   279
				//byte[] body = bc.encode(rs.getString("text")).getBytes();
franta-hg@69
   280
franta-hg@68
   281
				return new Article(headers, body);
franta-hg@68
   282
			} else {
franta-hg@68
   283
				return null;
franta-hg@68
   284
			}
franta-hg@68
   285
		} catch (Exception e) {
franta-hg@68
   286
			throw new StorageBackendException(e);
franta-hg@68
   287
		} finally {
franta-hg@68
   288
			close(null, ps, rs);
franta-hg@68
   289
		}
franta-hg@64
   290
	}
franta-hg@64
   291
franta-hg@64
   292
	@Override
franta-hg@64
   293
	public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first, long last) throws StorageBackendException {
franta-hg@68
   294
		PreparedStatement ps = null;
franta-hg@68
   295
		ResultSet rs = null;
franta-hg@68
   296
		try {
franta-hg@70
   297
			// TODO: je nutné řazení?
franta-hg@70
   298
			ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE group_id = ? AND id >= ? AND id <= ? ORDER BY id");
franta-hg@68
   299
			ps.setLong(1, group.getInternalID());
franta-hg@68
   300
			ps.setLong(2, first);
franta-hg@68
   301
			ps.setLong(3, last);
franta-hg@68
   302
			rs = ps.executeQuery();
franta-hg@68
   303
franta-hg@68
   304
			List<Pair<Long, ArticleHead>> heads = new ArrayList<Pair<Long, ArticleHead>>();
franta-hg@68
   305
franta-hg@68
   306
			while (rs.next()) {
franta-hg@68
   307
				String headers = constructHeaders(rs);
franta-hg@68
   308
				heads.add(new Pair<Long, ArticleHead>(rs.getLong("id"), new ArticleHead(headers)));
franta-hg@68
   309
			}
franta-hg@68
   310
franta-hg@68
   311
			return heads;
franta-hg@68
   312
		} catch (Exception e) {
franta-hg@68
   313
			throw new StorageBackendException(e);
franta-hg@68
   314
		} finally {
franta-hg@68
   315
			close(null, ps, rs);
franta-hg@68
   316
		}
franta-hg@64
   317
	}
franta-hg@64
   318
franta-hg@64
   319
	@Override
franta-hg@64
   320
	public List<Pair<Long, String>> getArticleHeaders(Group group, long start, long end, String header, String pattern) throws StorageBackendException {
franta-hg@66
   321
		log.log(Level.SEVERE, "TODO: getArticleHeaders {0} / {1} / {2} / {3} / {4}", new Object[]{group, start, end, header, pattern});
franta-hg@65
   322
		/** TODO: */
franta-hg@65
   323
		return Collections.emptyList();
franta-hg@64
   324
	}
franta-hg@64
   325
franta-hg@64
   326
	@Override
franta-hg@68
   327
	public long getArticleIndex(Article article, Group group) throws StorageBackendException {
franta-hg@68
   328
		Long id = parseArticleID(article.getMessageID());
franta-hg@68
   329
		if (id == null) {
franta-hg@68
   330
			throw new StorageBackendException("Invalid messageID: " + article.getMessageID());
franta-hg@68
   331
		} else {
franta-hg@68
   332
			return id;
franta-hg@68
   333
		}
franta-hg@64
   334
	}
franta-hg@64
   335
franta-hg@64
   336
	@Override
franta-hg@64
   337
	public List<Long> getArticleNumbers(long groupID) throws StorageBackendException {
franta-hg@68
   338
		PreparedStatement ps = null;
franta-hg@68
   339
		ResultSet rs = null;
franta-hg@68
   340
		try {
franta-hg@68
   341
			ps = conn.prepareStatement("SELECT id FROM nntp_article WHERE group_id = ?");
franta-hg@68
   342
			ps.setLong(1, groupID);
franta-hg@68
   343
			rs = ps.executeQuery();
franta-hg@68
   344
			List<Long> articleNumbers = new ArrayList<Long>();
franta-hg@68
   345
			while (rs.next()) {
franta-hg@68
   346
				articleNumbers.add(rs.getLong(1));
franta-hg@68
   347
			}
franta-hg@68
   348
			return articleNumbers;
franta-hg@68
   349
		} catch (Exception e) {
franta-hg@68
   350
			throw new StorageBackendException(e);
franta-hg@68
   351
		} finally {
franta-hg@68
   352
			close(null, ps, rs);
franta-hg@68
   353
		}
franta-hg@64
   354
	}
franta-hg@64
   355
franta-hg@64
   356
	@Override
franta-hg@67
   357
	public int getFirstArticleNumber(Group group) throws StorageBackendException {
franta-hg@68
   358
		PreparedStatement ps = null;
franta-hg@68
   359
		ResultSet rs = null;
franta-hg@68
   360
		try {
franta-hg@68
   361
			ps = conn.prepareStatement("SELECT min(id) FROM nntp_article WHERE group_id = ?");
franta-hg@68
   362
			ps.setLong(1, group.getInternalID());
franta-hg@68
   363
			rs = ps.executeQuery();
franta-hg@68
   364
			rs.next();
franta-hg@68
   365
			return rs.getInt(1);
franta-hg@68
   366
		} catch (Exception e) {
franta-hg@68
   367
			throw new StorageBackendException(e);
franta-hg@68
   368
		} finally {
franta-hg@68
   369
			close(null, ps, rs);
franta-hg@68
   370
		}
franta-hg@67
   371
	}
franta-hg@67
   372
franta-hg@67
   373
	@Override
franta-hg@67
   374
	public int getLastArticleNumber(Group group) throws StorageBackendException {
franta-hg@68
   375
		PreparedStatement ps = null;
franta-hg@68
   376
		ResultSet rs = null;
franta-hg@68
   377
		try {
franta-hg@68
   378
			ps = conn.prepareStatement("SELECT max(id) FROM nntp_article WHERE group_id = ?");
franta-hg@68
   379
			ps.setLong(1, group.getInternalID());
franta-hg@68
   380
			rs = ps.executeQuery();
franta-hg@68
   381
			rs.next();
franta-hg@68
   382
			return rs.getInt(1);
franta-hg@68
   383
		} catch (Exception e) {
franta-hg@68
   384
			throw new StorageBackendException(e);
franta-hg@68
   385
		} finally {
franta-hg@68
   386
			close(null, ps, rs);
franta-hg@68
   387
		}
franta-hg@67
   388
	}
franta-hg@67
   389
franta-hg@67
   390
	@Override
franta-hg@67
   391
	public boolean isArticleExisting(String messageID) throws StorageBackendException {
franta-hg@68
   392
		Long articleID = parseArticleID(messageID);
franta-hg@68
   393
		Long groupID = parseGroupID(messageID);
franta-hg@68
   394
franta-hg@68
   395
		if (articleID == null || groupID == null) {
franta-hg@68
   396
			return false;
franta-hg@68
   397
		} else {
franta-hg@68
   398
			PreparedStatement ps = null;
franta-hg@68
   399
			ResultSet rs = null;
franta-hg@68
   400
			try {
franta-hg@68
   401
				ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE id = ? AND group_id = ?");
franta-hg@68
   402
				ps.setLong(1, articleID);
franta-hg@68
   403
				ps.setLong(2, groupID);
franta-hg@68
   404
				rs = ps.executeQuery();
franta-hg@68
   405
franta-hg@68
   406
				rs.next();
franta-hg@68
   407
				return rs.getInt(1) == 1;
franta-hg@68
   408
			} catch (Exception e) {
franta-hg@68
   409
				throw new StorageBackendException(e);
franta-hg@68
   410
			} finally {
franta-hg@68
   411
				close(null, ps, rs);
franta-hg@68
   412
			}
franta-hg@68
   413
		}
franta-hg@67
   414
	}
franta-hg@67
   415
franta-hg@67
   416
	//
franta-hg@67
   417
	// --- zatím neimplementovat ---
franta-hg@67
   418
	//
franta-hg@67
   419
	@Override
franta-hg@67
   420
	public void addArticle(Article art) throws StorageBackendException {
franta-hg@67
   421
		log.log(Level.SEVERE, "TODO: addArticle {0}", new Object[]{art});
franta-hg@67
   422
	}
franta-hg@67
   423
franta-hg@67
   424
	@Override
franta-hg@67
   425
	public void addEvent(long timestamp, int type, long groupID) throws StorageBackendException {
franta-hg@67
   426
		log.log(Level.SEVERE, "TODO: addEvent {0} / {1} / {2}", new Object[]{timestamp, type, groupID});
franta-hg@67
   427
	}
franta-hg@67
   428
franta-hg@67
   429
	@Override
franta-hg@67
   430
	public void addGroup(String groupname, int flags) throws StorageBackendException {
franta-hg@67
   431
		log.log(Level.SEVERE, "TODO: addGroup {0} / {1}", new Object[]{groupname, flags});
franta-hg@67
   432
	}
franta-hg@67
   433
franta-hg@67
   434
	@Override
franta-hg@67
   435
	public int countArticles() throws StorageBackendException {
franta-hg@69
   436
		PreparedStatement ps = null;
franta-hg@69
   437
		ResultSet rs = null;
franta-hg@69
   438
		try {
franta-hg@69
   439
			ps = conn.prepareStatement("SELECT count(*) FROM nntp_article");
franta-hg@69
   440
			rs = ps.executeQuery();
franta-hg@69
   441
			rs.next();
franta-hg@69
   442
			return rs.getInt(1);
franta-hg@69
   443
		} catch (Exception e) {
franta-hg@69
   444
			throw new StorageBackendException(e);
franta-hg@69
   445
		} finally {
franta-hg@69
   446
			close(null, ps, rs);
franta-hg@69
   447
		}
franta-hg@67
   448
	}
franta-hg@67
   449
franta-hg@67
   450
	@Override
franta-hg@67
   451
	public int countGroups() throws StorageBackendException {
franta-hg@69
   452
		PreparedStatement ps = null;
franta-hg@69
   453
		ResultSet rs = null;
franta-hg@69
   454
		try {
franta-hg@69
   455
			ps = conn.prepareStatement("SELECT count(*) FROM nntp_group");
franta-hg@69
   456
			rs = ps.executeQuery();
franta-hg@69
   457
			rs.next();
franta-hg@69
   458
			return rs.getInt(1);
franta-hg@69
   459
		} catch (Exception e) {
franta-hg@69
   460
			throw new StorageBackendException(e);
franta-hg@69
   461
		} finally {
franta-hg@69
   462
			close(null, ps, rs);
franta-hg@69
   463
		}
franta-hg@67
   464
	}
franta-hg@67
   465
franta-hg@67
   466
	@Override
franta-hg@67
   467
	public void delete(String messageID) throws StorageBackendException {
franta-hg@67
   468
		log.log(Level.SEVERE, "TODO: delete {0}", new Object[]{messageID});
franta-hg@67
   469
	}
franta-hg@67
   470
franta-hg@67
   471
	@Override
franta-hg@64
   472
	public String getConfigValue(String key) throws StorageBackendException {
franta-hg@69
   473
		//log.log(Level.SEVERE, "TODO: getConfigValue {0}", new Object[]{key});
franta-hg@65
   474
		return null;
franta-hg@64
   475
	}
franta-hg@64
   476
franta-hg@64
   477
	@Override
franta-hg@64
   478
	public int getEventsCount(int eventType, long startTimestamp, long endTimestamp, Group group) throws StorageBackendException {
franta-hg@66
   479
		log.log(Level.SEVERE, "TODO: getEventsCount {0} / {1} / {2} / {3}", new Object[]{eventType, startTimestamp, endTimestamp, group});
franta-hg@65
   480
		return 0;
franta-hg@64
   481
	}
franta-hg@64
   482
franta-hg@64
   483
	@Override
franta-hg@64
   484
	public double getEventsPerHour(int key, long gid) throws StorageBackendException {
franta-hg@66
   485
		log.log(Level.SEVERE, "TODO: getEventsPerHour {0} / {1}", new Object[]{key, gid});
franta-hg@65
   486
		return 0;
franta-hg@64
   487
	}
franta-hg@64
   488
franta-hg@64
   489
	@Override
franta-hg@64
   490
	public List<String> getGroupsForList(String listAddress) throws StorageBackendException {
franta-hg@66
   491
		log.log(Level.SEVERE, "TODO: getGroupsForList {0}", new Object[]{listAddress});
franta-hg@65
   492
		return Collections.emptyList();
franta-hg@64
   493
	}
franta-hg@64
   494
franta-hg@64
   495
	@Override
franta-hg@64
   496
	public List<String> getListsForGroup(String groupname) throws StorageBackendException {
franta-hg@66
   497
		log.log(Level.SEVERE, "TODO: getListsForGroup {0}", new Object[]{groupname});
franta-hg@65
   498
		return Collections.emptyList();
franta-hg@64
   499
	}
franta-hg@64
   500
franta-hg@64
   501
	@Override
franta-hg@64
   502
	public String getOldestArticle() throws StorageBackendException {
franta-hg@66
   503
		log.log(Level.SEVERE, "TODO: getOldestArticle");
franta-hg@65
   504
		return null;
franta-hg@64
   505
	}
franta-hg@64
   506
franta-hg@64
   507
	@Override
franta-hg@64
   508
	public int getPostingsCount(String groupname) throws StorageBackendException {
franta-hg@69
   509
		PreparedStatement ps = null;
franta-hg@69
   510
		ResultSet rs = null;
franta-hg@69
   511
		try {
franta-hg@69
   512
			ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE group_name = ?");
franta-hg@69
   513
			ps.setString(1, groupname);
franta-hg@69
   514
			rs = ps.executeQuery();
franta-hg@69
   515
			rs.next();
franta-hg@69
   516
			return rs.getInt(1);
franta-hg@69
   517
		} catch (Exception e) {
franta-hg@69
   518
			throw new StorageBackendException(e);
franta-hg@69
   519
		} finally {
franta-hg@69
   520
			close(null, ps, rs);
franta-hg@69
   521
		}
franta-hg@64
   522
	}
franta-hg@64
   523
franta-hg@64
   524
	@Override
franta-hg@64
   525
	public List<Subscription> getSubscriptions(int type) throws StorageBackendException {
franta-hg@66
   526
		log.log(Level.SEVERE, "TODO: getSubscriptions {0}", new Object[]{type});
franta-hg@65
   527
		return Collections.emptyList();
franta-hg@64
   528
	}
franta-hg@64
   529
franta-hg@64
   530
	@Override
franta-hg@64
   531
	public void purgeGroup(Group group) throws StorageBackendException {
franta-hg@66
   532
		log.log(Level.SEVERE, "TODO: purgeGroup {0}", new Object[]{group});
franta-hg@64
   533
	}
franta-hg@64
   534
franta-hg@64
   535
	@Override
franta-hg@64
   536
	public void setConfigValue(String key, String value) throws StorageBackendException {
franta-hg@66
   537
		log.log(Level.SEVERE, "TODO: setConfigValue {0} = {1}", new Object[]{key, value});
franta-hg@64
   538
	}
franta-hg@64
   539
franta-hg@64
   540
	@Override
franta-hg@64
   541
	public boolean update(Article article) throws StorageBackendException {
franta-hg@66
   542
		log.log(Level.SEVERE, "TODO: update {0}", new Object[]{article});
franta-hg@65
   543
		throw new StorageBackendException("Not implemented yet.");
franta-hg@64
   544
	}
franta-hg@64
   545
franta-hg@64
   546
	@Override
franta-hg@64
   547
	public boolean update(Group group) throws StorageBackendException {
franta-hg@66
   548
		log.log(Level.SEVERE, "TODO: update {0}", new Object[]{group});
franta-hg@65
   549
		throw new StorageBackendException("Not implemented yet.");
franta-hg@64
   550
	}
franta-hg@63
   551
}