Implement JDBCDatabase.update(Article) method to fix issue #7.
authorcli
Sun Aug 29 23:23:15 2010 +0200 (2010-08-29)
changeset 38fdfc7225f799
parent 37 74139325d305
child 39 73b21e9f3958
Implement JDBCDatabase.update(Article) method to fix issue #7.
src/org/sonews/storage/impl/JDBCDatabase.java
     1.1 --- a/src/org/sonews/storage/impl/JDBCDatabase.java	Sun Aug 29 18:17:37 2010 +0200
     1.2 +++ b/src/org/sonews/storage/impl/JDBCDatabase.java	Sun Aug 29 23:23:15 2010 +0200
     1.3 @@ -305,39 +305,7 @@
     1.4  			this.conn.setAutoCommit(false);
     1.5  
     1.6  			int newArticleID = getMaxArticleID() + 1;
     1.7 -
     1.8 -			// Fill prepared statement with values;
     1.9 -			// writes body to article table
    1.10 -			pstmtAddArticle1.setInt(1, newArticleID);
    1.11 -			pstmtAddArticle1.setBytes(2, article.getBody());
    1.12 -			pstmtAddArticle1.execute();
    1.13 -
    1.14 -			// Add headers
    1.15 -			Enumeration headers = article.getAllHeaders();
    1.16 -			for (int n = 0; headers.hasMoreElements(); n++) {
    1.17 -				Header header = (Header) headers.nextElement();
    1.18 -				pstmtAddArticle2.setInt(1, newArticleID);
    1.19 -				pstmtAddArticle2.setString(2, header.getName().toLowerCase());
    1.20 -				pstmtAddArticle2.setString(3,
    1.21 -					header.getValue().replaceAll("[\r\n]", ""));
    1.22 -				pstmtAddArticle2.setInt(4, n);
    1.23 -				pstmtAddArticle2.execute();
    1.24 -			}
    1.25 -
    1.26 -			// For each newsgroup add a reference
    1.27 -			List<Group> groups = article.getGroups();
    1.28 -			for (Group group : groups) {
    1.29 -				pstmtAddArticle3.setLong(1, group.getInternalID());
    1.30 -				pstmtAddArticle3.setInt(2, newArticleID);
    1.31 -				pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
    1.32 -				pstmtAddArticle3.execute();
    1.33 -			}
    1.34 -
    1.35 -			// Write message-id to article_ids table
    1.36 -			this.pstmtAddArticle4.setInt(1, newArticleID);
    1.37 -			this.pstmtAddArticle4.setString(2, article.getMessageID());
    1.38 -			this.pstmtAddArticle4.execute();
    1.39 -
    1.40 +			addArticle(article, newArticleID);
    1.41  			this.conn.commit();
    1.42  			this.conn.setAutoCommit(true);
    1.43  
    1.44 @@ -361,6 +329,48 @@
    1.45  	}
    1.46  
    1.47  	/**
    1.48 +	 * Adds an article to the database.
    1.49 +	 * @param article
    1.50 +	 * @return
    1.51 +	 * @throws java.sql.SQLException
    1.52 +	 */
    1.53 +	void addArticle(final Article article, final int newArticleID)
    1.54 +		throws SQLException, StorageBackendException
    1.55 +	{
    1.56 +		// Fill prepared statement with values;
    1.57 +		// writes body to article table
    1.58 +		pstmtAddArticle1.setInt(1, newArticleID);
    1.59 +		pstmtAddArticle1.setBytes(2, article.getBody());
    1.60 +		pstmtAddArticle1.execute();
    1.61 +
    1.62 +		// Add headers
    1.63 +		Enumeration headers = article.getAllHeaders();
    1.64 +		for (int n = 0; headers.hasMoreElements(); n++) {
    1.65 +			Header header = (Header) headers.nextElement();
    1.66 +			pstmtAddArticle2.setInt(1, newArticleID);
    1.67 +			pstmtAddArticle2.setString(2, header.getName().toLowerCase());
    1.68 +			pstmtAddArticle2.setString(3,
    1.69 +				header.getValue().replaceAll("[\r\n]", ""));
    1.70 +			pstmtAddArticle2.setInt(4, n);
    1.71 +			pstmtAddArticle2.execute();
    1.72 +		}
    1.73 +
    1.74 +		// For each newsgroup add a reference
    1.75 +		List<Group> groups = article.getGroups();
    1.76 +		for (Group group : groups) {
    1.77 +			pstmtAddArticle3.setLong(1, group.getInternalID());
    1.78 +			pstmtAddArticle3.setInt(2, newArticleID);
    1.79 +			pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
    1.80 +			pstmtAddArticle3.execute();
    1.81 +		}
    1.82 +
    1.83 +		// Write message-id to article_ids table
    1.84 +		this.pstmtAddArticle4.setInt(1, newArticleID);
    1.85 +		this.pstmtAddArticle4.setString(2, article.getMessageID());
    1.86 +		this.pstmtAddArticle4.execute();
    1.87 +	}
    1.88 +
    1.89 +	/**
    1.90  	 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
    1.91  	 * @param name
    1.92  	 * @throws java.sql.SQLException
    1.93 @@ -1398,12 +1408,29 @@
    1.94  	public boolean update(Article article)
    1.95  		throws StorageBackendException
    1.96  	{
    1.97 -		// DELETE FROM headers WHERE article_id = ?
    1.98 +		ResultSet rs = null;
    1.99 +		try {
   1.100 +			// Retrieve internal article_id
   1.101 +			this.pstmtGetArticle0.setString(1, article.getMessageID());
   1.102 +			rs = this.pstmtGetArticle0.executeQuery();
   1.103 +			int articleID = rs.getInt("article_id");
   1.104  
   1.105 -		// INSERT INTO headers ...
   1.106 +			delete(article.getMessageID());
   1.107  
   1.108 -		// SELECT * FROM postings WHERE article_id = ? AND group_id = ?
   1.109 -		return false;
   1.110 +			this.conn.setAutoCommit(false);
   1.111 +			addArticle(article, articleID);
   1.112 +			this.conn.commit();
   1.113 +			this.conn.setAutoCommit(true);
   1.114 +			return true;
   1.115 +		} catch (SQLException ex) {
   1.116 +			try {
   1.117 +				this.conn.rollback();
   1.118 +			} catch(SQLException ex2) {
   1.119 +				Log.get().severe("Rollback failed: " + ex2.getMessage());
   1.120 +			}
   1.121 +			restartConnection(ex);
   1.122 +			return update(article);
   1.123 +		}
   1.124  	}
   1.125  
   1.126  	/**