Added a method for getting one contact's private messages from the DB.

This commit is contained in:
akwizgran
2013-03-02 03:31:39 +00:00
parent a5da3694f9
commit 882420ebc2
8 changed files with 83 additions and 11 deletions

View File

@@ -278,6 +278,15 @@ interface Database<T> {
Collection<PrivateMessageHeader> getPrivateMessageHeaders(T txn)
throws DbException;
/**
* Returns the headers of all private messages to or from the given
* contact.
* <p>
* Locking: message read.
*/
Collection<PrivateMessageHeader> getPrivateMessageHeaders(T txn,
ContactId c) throws DbException;
/**
* Returns the IDs of all messages signed by the given author.
* <p>

View File

@@ -946,6 +946,25 @@ DatabaseCleaner.Callback {
}
}
public Collection<PrivateMessageHeader> getPrivateMessageHeaders(
ContactId c) throws DbException {
messageLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Collection<PrivateMessageHeader> headers =
db.getPrivateMessageHeaders(txn, c);
db.commitTransaction(txn);
return headers;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
messageLock.readLock().unlock();
}
}
public Rating getRating(AuthorId a) throws DbException {
ratingLock.readLock().lock();
try {

View File

@@ -1302,6 +1302,45 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<PrivateMessageHeader> getPrivateMessageHeaders(
Connection txn, ContactId c) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT m.messageId, parentId, subject, timestamp,"
+ " read, starred, seen"
+ " FROM messages AS m"
+ " JOIN statuses AS s"
+ " ON m.messageId = s.messageId"
+ " AND m.contactId = s.contactId"
+ " WHERE m.contactId = ? AND groupId IS NULL";
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
rs = ps.executeQuery();
List<PrivateMessageHeader> headers =
new ArrayList<PrivateMessageHeader>();
while(rs.next()) {
MessageId id = new MessageId(rs.getBytes(1));
byte[] b = rs.getBytes(2);
MessageId parent = b == null ? null : new MessageId(b);
String subject = rs.getString(3);
long timestamp = rs.getLong(4);
boolean read = rs.getBoolean(5);
boolean starred = rs.getBoolean(6);
boolean seen = rs.getBoolean(7);
headers.add(new PrivateMessageHeader(id, parent, subject,
timestamp, read, starred, c, !seen));
}
rs.close();
ps.close();
return Collections.unmodifiableList(headers);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public Collection<MessageId> getMessagesByAuthor(Connection txn, AuthorId a)
throws DbException {
PreparedStatement ps = null;