View Javadoc

1   package org.paneris.messageboard.model;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   import java.sql.Statement;
6   import java.util.Stack;
7   
8   import org.paneris.jal.model.DBConnectionManager;
9   import org.paneris.jal.model.DDField;
10  
11  /**
12  * <p>A mechanism for accessing a messageboard thread.</p>
13  *
14  **/
15  
16  public class MessageThread extends Stack {
17  
18    private static final long serialVersionUID = 1L;
19      String database;
20      private DBConnectionManager connMgr;
21  
22  
23  /**
24  * construct a stack to store messages that are in this thread
25  **/
26      public MessageThread(String db) {
27          super();
28          database = db;
29      }
30  
31  /**
32    * <p>get the next message after this one.</p>
33    * <p>if we find a follow-up message, return it, and add any other messages
34    * that follow-up this one directly to the stack</p>
35    * <p>if we don't find a follow-up, grab the next message off the stack and return it
36    * (having added any follow-ups to the stack</p>
37    * <p>empty stack - return null</p>
38    *
39    * @return The next message
40    * @exception    java.lang.Exception If SQL fails
41    *
42  **/
43      public Message getNext(Message m) throws Exception {
44          connMgr = DBConnectionManager.getInstance();
45          Connection conn = connMgr.getConnection("MessageThread",database);
46          Statement s = conn.createStatement();
47          DDField idField = (DDField) m.get("id");
48          String sqlString = new String("SELECT * FROM messages WHERE parent = " + (Integer)idField.getValue() + " ORDER BY messagedate DESC");
49          ResultSet rs = s.executeQuery(sqlString);
50          while (rs.next()) {
51              Message n = new Message(database,rs);
52              n.setIndentation(m.getIndentation() + 1);
53              push(n);
54          }
55          connMgr.freeConnection(database, conn);
56          if (empty()) {
57              return null;
58          } else {
59              return (Message) pop();
60          }
61      }
62  }
63