Drupal: XSLT odstavce – vnořující se.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package org.sonews.storage.impl;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.Statement;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.sonews.config.Config;
31 import org.sonews.feed.Subscription;
32 import org.sonews.storage.Article;
33 import org.sonews.storage.ArticleHead;
34 import org.sonews.storage.DrupalArticle;
35 import org.sonews.storage.DrupalMessage;
36 import org.sonews.storage.Group;
37 import org.sonews.storage.Storage;
38 import org.sonews.storage.StorageBackendException;
39 import org.sonews.util.Pair;
43 * @author František Kučera (frantovo.cz)
45 public class DrupalDatabase implements Storage {
47 private static final Logger log = Logger.getLogger(DrupalDatabase.class.getName());
48 public static final String CHARSET = "UTF-8";
49 public static final String CRLF = "\r\n";
50 protected Connection conn = null;
51 // TODO: správná doména
52 private String myDomain = "nntp.i1984.cz";
54 public DrupalDatabase() throws StorageBackendException {
58 private void connectDatabase() throws StorageBackendException {
60 // Load database driver
61 String driverClass = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object");
62 Class.forName(driverClass);
64 // Establish database connection
65 String url = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>");
66 String username = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root");
67 String password = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, "");
68 conn = DriverManager.getConnection(url, username, password);
70 conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
71 if (conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE) {
72 log.warning("Database is NOT fully serializable!");
74 } catch (Exception e) {
75 throw new StorageBackendException(e);
79 protected static void close(Connection connection, Statement statement, ResultSet resultSet) {
80 if (resultSet != null) {
83 } catch (Exception e) {
86 if (statement != null) {
89 } catch (Exception e) {
92 if (connection != null) {
95 } catch (Exception e) {
102 * @param messageID <{0}-{1}-{2}@domain.tld> where {0} is nntp_id and {1} is group_id and {2} is group_name
103 * @return array where [0] = nntp_id and [1] = group_id and [2] = group_name or returns null if messageID is invalid
105 private static String[] parseMessageID(String messageID) {
106 if (messageID.matches("<[0-9]+\\-[0-9]+\\-[a-z0-9\\.]+@.+>")) {
107 return messageID.substring(1).split("@")[0].split("\\-");
113 private static Long parseArticleID(String messageID) {
114 String[] localPart = parseMessageID(messageID);
115 if (localPart == null) {
118 return Long.parseLong(localPart[0]);
122 private static Long parseGroupID(String messageID) {
123 String[] localPart = parseMessageID(messageID);
124 if (localPart == null) {
127 return Long.parseLong(localPart[1]);
129 // parseGroupName() will be same as this method, just with:
130 // return localPart[2];
135 public List<Group> getGroups() throws StorageBackendException {
136 PreparedStatement ps = null;
139 ps = conn.prepareStatement("SELECT * FROM nntp_group");
140 rs = ps.executeQuery();
141 List<Group> skupiny = new ArrayList<Group>();
144 skupiny.add(new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY));
148 } catch (Exception e) {
149 throw new StorageBackendException(e);
156 public Group getGroup(String name) throws StorageBackendException {
157 PreparedStatement ps = null;
160 ps = conn.prepareStatement("SELECT * FROM nntp_group WHERE name = ?");
161 ps.setString(1, name);
162 rs = ps.executeQuery();
165 return new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY);
169 } catch (Exception e) {
170 throw new StorageBackendException(e);
177 public boolean isGroupExisting(String groupname) throws StorageBackendException {
178 return getGroup(groupname) != null;
182 public Article getArticle(String messageID) throws StorageBackendException {
183 Long articleID = parseArticleID(messageID);
184 Long groupID = parseGroupID(messageID);
186 if (articleID == null || groupID == null) {
187 log.log(Level.SEVERE, "Invalid messageID: {0}", new Object[]{messageID});
190 return getArticle(articleID, groupID);
195 public Article getArticle(long articleID, long groupID) throws StorageBackendException {
196 PreparedStatement ps = null;
199 ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE id = ? AND group_id = ?");
200 ps.setLong(1, articleID);
201 ps.setLong(2, groupID);
202 rs = ps.executeQuery();
205 DrupalMessage m = new DrupalMessage(rs, myDomain, true);
206 return new DrupalArticle(m);
210 } catch (Exception e) {
211 throw new StorageBackendException(e);
218 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first, long last) throws StorageBackendException {
219 PreparedStatement ps = null;
222 ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE group_id = ? AND id >= ? AND id <= ? ORDER BY id");
223 ps.setLong(1, group.getInternalID());
224 ps.setLong(2, first);
226 rs = ps.executeQuery();
228 List<Pair<Long, ArticleHead>> heads = new ArrayList<Pair<Long, ArticleHead>>();
231 DrupalMessage m = new DrupalMessage(rs, myDomain, false);
232 String headers = m.getHeaders();
233 heads.add(new Pair<Long, ArticleHead>(rs.getLong("id"), new ArticleHead(headers)));
237 } catch (Exception e) {
238 throw new StorageBackendException(e);
245 public long getArticleIndex(Article article, Group group) throws StorageBackendException {
246 Long id = parseArticleID(article.getMessageID());
248 throw new StorageBackendException("Invalid messageID: " + article.getMessageID());
255 public List<Long> getArticleNumbers(long groupID) throws StorageBackendException {
256 PreparedStatement ps = null;
259 ps = conn.prepareStatement("SELECT id FROM nntp_article WHERE group_id = ?");
260 ps.setLong(1, groupID);
261 rs = ps.executeQuery();
262 List<Long> articleNumbers = new ArrayList<Long>();
264 articleNumbers.add(rs.getLong(1));
266 return articleNumbers;
267 } catch (Exception e) {
268 throw new StorageBackendException(e);
275 public int getFirstArticleNumber(Group group) throws StorageBackendException {
276 PreparedStatement ps = null;
279 ps = conn.prepareStatement("SELECT min(id) FROM nntp_article WHERE group_id = ?");
280 ps.setLong(1, group.getInternalID());
281 rs = ps.executeQuery();
284 } catch (Exception e) {
285 throw new StorageBackendException(e);
292 public int getLastArticleNumber(Group group) throws StorageBackendException {
293 PreparedStatement ps = null;
296 ps = conn.prepareStatement("SELECT max(id) FROM nntp_article WHERE group_id = ?");
297 ps.setLong(1, group.getInternalID());
298 rs = ps.executeQuery();
301 } catch (Exception e) {
302 throw new StorageBackendException(e);
309 public boolean isArticleExisting(String messageID) throws StorageBackendException {
310 Long articleID = parseArticleID(messageID);
311 Long groupID = parseGroupID(messageID);
313 if (articleID == null || groupID == null) {
316 PreparedStatement ps = null;
319 ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE id = ? AND group_id = ?");
320 ps.setLong(1, articleID);
321 ps.setLong(2, groupID);
322 rs = ps.executeQuery();
325 return rs.getInt(1) == 1;
326 } catch (Exception e) {
327 throw new StorageBackendException(e);
335 public int countArticles() throws StorageBackendException {
336 PreparedStatement ps = null;
339 ps = conn.prepareStatement("SELECT count(*) FROM nntp_article");
340 rs = ps.executeQuery();
343 } catch (Exception e) {
344 throw new StorageBackendException(e);
351 public int countGroups() throws StorageBackendException {
352 PreparedStatement ps = null;
355 ps = conn.prepareStatement("SELECT count(*) FROM nntp_group");
356 rs = ps.executeQuery();
359 } catch (Exception e) {
360 throw new StorageBackendException(e);
367 public int getPostingsCount(String groupname) throws StorageBackendException {
368 PreparedStatement ps = null;
371 ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE group_name = ?");
372 ps.setString(1, groupname);
373 rs = ps.executeQuery();
376 } catch (Exception e) {
377 throw new StorageBackendException(e);
384 public List<Pair<Long, String>> getArticleHeaders(Group group, long start, long end, String header, String pattern) throws StorageBackendException {
385 log.log(Level.SEVERE, "TODO: getArticleHeaders {0} / {1} / {2} / {3} / {4}", new Object[]{group, start, end, header, pattern});
387 return Collections.emptyList();
391 public void addArticle(Article art) throws StorageBackendException {
392 log.log(Level.SEVERE, "TODO: addArticle {0}", new Object[]{art});
396 public void addEvent(long timestamp, int type, long groupID) throws StorageBackendException {
397 log.log(Level.SEVERE, "TODO: addEvent {0} / {1} / {2}", new Object[]{timestamp, type, groupID});
401 public void addGroup(String groupname, int flags) throws StorageBackendException {
402 log.log(Level.SEVERE, "TODO: addGroup {0} / {1}", new Object[]{groupname, flags});
406 public void delete(String messageID) throws StorageBackendException {
407 log.log(Level.SEVERE, "TODO: delete {0}", new Object[]{messageID});
411 public String getConfigValue(String key) throws StorageBackendException {
412 //log.log(Level.SEVERE, "TODO: getConfigValue {0}", new Object[]{key});
417 public void setConfigValue(String key, String value) throws StorageBackendException {
418 log.log(Level.SEVERE, "TODO: setConfigValue {0} = {1}", new Object[]{key, value});
422 public int getEventsCount(int eventType, long startTimestamp, long endTimestamp, Group group) throws StorageBackendException {
423 log.log(Level.SEVERE, "TODO: getEventsCount {0} / {1} / {2} / {3}", new Object[]{eventType, startTimestamp, endTimestamp, group});
428 public double getEventsPerHour(int key, long gid) throws StorageBackendException {
429 log.log(Level.SEVERE, "TODO: getEventsPerHour {0} / {1}", new Object[]{key, gid});
434 public List<String> getGroupsForList(String listAddress) throws StorageBackendException {
435 log.log(Level.SEVERE, "TODO: getGroupsForList {0}", new Object[]{listAddress});
436 return Collections.emptyList();
440 public List<String> getListsForGroup(String groupname) throws StorageBackendException {
441 log.log(Level.SEVERE, "TODO: getListsForGroup {0}", new Object[]{groupname});
442 return Collections.emptyList();
446 public String getOldestArticle() throws StorageBackendException {
447 log.log(Level.SEVERE, "TODO: getOldestArticle");
452 public List<Subscription> getSubscriptions(int type) throws StorageBackendException {
453 log.log(Level.SEVERE, "TODO: getSubscriptions {0}", new Object[]{type});
454 return Collections.emptyList();
458 public void purgeGroup(Group group) throws StorageBackendException {
459 log.log(Level.SEVERE, "TODO: purgeGroup {0}", new Object[]{group});
463 public boolean update(Article article) throws StorageBackendException {
464 log.log(Level.SEVERE, "TODO: update {0}", new Object[]{article});
465 throw new StorageBackendException("Not implemented yet.");
469 public boolean update(Group group) throws StorageBackendException {
470 log.log(Level.SEVERE, "TODO: update {0}", new Object[]{group});
471 throw new StorageBackendException("Not implemented yet.");