View Javadoc

1   package org.paneris.messageboard.model;
2   
3   import org.paneris.jal.model.DDField;
4   import org.paneris.jal.model.DDRecord;
5   import org.paneris.jal.model.SystemProperties;
6   import org.paneris.jal.model.Email;
7   import org.paneris.jal.model.DBConnectionManager;
8   import org.paneris.user.model.User;
9   import java.sql.Connection;
10  import java.sql.Statement;
11  import java.sql.ResultSet;
12  import java.util.Vector;
13  
14  /**
15  * <p>An item on the messageboard.</p>
16  *
17  **/
18  
19  public class Message extends DDRecord {
20  
21    private static final long serialVersionUID = 1L;
22      int indentation = 0;
23      int attachments = 0;
24  
25  
26  /**
27   * Constructor to build a DDRecord given a string describing the table and
28   * a java.sql.resultSet.
29   *
30   * @exception    java.lang.Exception If somthing fails
31  **/
32      public Message(String db, ResultSet rs) throws Exception {
33          super(db,"messages",rs);
34          setAttachments();
35      }
36  
37  /**
38   * Constructor to build an empty DDRecord
39   *
40   * @exception    java.lang.Exception If somthing fails
41  **/
42      public Message(String db) throws Exception {
43          super(db,"messages");
44      }
45  
46  /**
47   * Constructor to build a DDRecord given a string or int describing the table and
48   * an record id int.  if you don't have a record id, send it in as null
49   *
50   * @exception    java.lang.Exception If somthing fails
51  **/
52      public Message(String db, Integer record) throws Exception {
53          super(db,"messages",record);
54          setAttachments();
55      }
56  
57  /**
58   *
59   * @exception    java.lang.Exception If somthing fails
60  **/
61      public void setAttachments() throws Exception {
62          DDField idField = (DDField) get("id");
63          Object idValue = idField.getValue();
64          int id;
65          if (idValue == null) {
66              id = 0;
67          } else {
68              id = ((Integer)idValue).intValue();
69          }
70          if (id != 0) {
71              DBConnectionManager connMgr = DBConnectionManager.getInstance();
72              Connection conn = connMgr.getConnection("Message",database);
73              Statement s = conn.createStatement();
74              String sqlString = "SELECT COUNT(*) FROM attachments WHERE message = " + id;
75              ResultSet rs = s.executeQuery(sqlString);
76              if (rs.next()) {
77                  attachments = rs.getInt(1);
78              }
79              connMgr.freeConnection(database, conn);
80          }
81      }
82  
83  
84  /**
85   get the level of indentation for displaying this message
86   we should not need to do this, as there should be a <loop n> construct in webmacro
87  **/
88      public Vector getIndentationVector() {
89          Vector v = new Vector();
90          for (int i=0; i<indentation; i++) {
91              v.addElement("bollocks code");
92          }
93          return v;
94      }
95  
96  /**
97   * get the parent message to this one
98   *
99   * @exception    java.lang.Exception If somthing fails
100 **/
101     public Message getParent() throws Exception {
102         DDField id = (DDField) get("parent");
103         if (id != null) {
104             Integer idInteger = (Integer)id.getValue();
105             if (!idInteger.equals(new Integer(0))) {
106                 return new Message(database,idInteger);
107             } else {
108                 return null;
109             }
110         } else {
111             return null;
112         }
113     }
114 
115 /**
116  * get the top parent message to this one
117  *
118  * @exception    java.lang.Exception If somthing fails
119 **/
120     public Message getTopParent() throws Exception {
121         int parentInt = 0;
122         Message m = getParent();
123         if (m == null) {
124             return this;
125         } else {
126             while (m != null) {
127                 DDField parent = (DDField) m.get("parent");
128                 if (parent == null) {
129                     return m;
130                 } else {
131                     parentInt = ((Integer)parent.getValue()).intValue();
132                     if (parentInt == 0) {
133                         return m;
134                     }
135                 }
136                 m = m.getParent();
137             }
138         }
139         return this;
140     }
141 
142 /**
143  set the level of indentation for displaying this message
144 **/
145     public void setIndentation(int i) {
146         indentation = i;
147     }
148 
149 /**
150  get the level of indentation for displaying this message
151 **/
152     public int getIndentation() {
153         return indentation;
154     }
155 
156 /**
157  get the level of indentation for displaying this message
158 **/
159     public int getAttachments() {
160         return attachments;
161     }
162     public boolean hasAttachments() {
163         if (attachments > 0) {
164             return true;
165         } else {
166             return false;
167         }
168     }
169 
170 /**
171 * set the timesamp if we are writing a message
172  *
173  * @exception    java.lang.Exception If somthing fails
174 **/
175     public void write() throws Exception {
176         DDField idField = (DDField) get("id");
177         Object idValue = idField.getValue();
178         int id;
179         if (idValue == null) {
180             id = 0;
181         } else {
182             id = ((Integer)idValue).intValue();
183         }
184         if (id == 0) {
185             DDField field = (DDField) get("messagedate");
186             java.util.Date today = new java.util.Date();
187             java.sql.Timestamp sqltoday = new java.sql.Timestamp(today.getTime());
188             field.setValue(sqltoday);
189         }
190         // ensure we have a subject
191         if (getFieldValue("subject").equals("")) {
192             setFieldValue("subject","no subject");
193         }
194         super.write();
195     }
196 
197 
198 /**
199  *
200  * @exception    java.lang.Exception If somthing fails
201  *
202  * FIXME: String toString --> addresseeList
203  */
204     public void distribute() throws Exception {
205 
206         User author = new User(database,(Integer) ((DDField) get("author")).getValue());
207         DDField fromName = (DDField)author.get("username");
208         DDField fromEmail = (DDField)author.get("email");
209         String fromEmailS =  fromEmail.getValue().toString().trim();
210         String fromNameS = fromName.getValue().toString().trim();
211         DBConnectionManager connMgr = DBConnectionManager.getInstance();
212         Connection conn = connMgr.getConnection("Message",database);
213         Statement s = conn.createStatement();
214         DDField boardId = (DDField) get("board");
215         String sqlString = new String("SELECT users.* FROM usersboards, users WHERE usersboards.username = users.id AND usersboards.board = " + (Integer)boardId.getValue());
216         ResultSet rs = s.executeQuery(sqlString);
217         String toString = "";
218         while (rs.next()) {
219             User user = new User(database,rs);
220             DDField email = (DDField) user.get("email");
221             DDField name = (DDField) user.get("username");
222             toString += name.getValue().toString().trim();
223             toString += " <" + email.getValue().toString().trim() + ">, ";
224         }
225         if (!toString.equals("")) {
226             toString = toString.substring(0,toString.lastIndexOf(","));
227             DDField subject = (DDField) get("subject");
228             Board b = new Board(database,(Integer)boardId.getValue());
229             String MessageID = getFieldValue("id").trim();
230             String reply = b.getMessageboardEmail();
231             if ((!MessageID.equals("")) && (!MessageID.equals("0"))) {
232                 reply = MessageID + "." + reply;
233             } 
234             String messageString = "This message has been sent to " + b.getSubscribers();
235             messageString += " subscribers to the " + b.getFieldValue("description").trim() + " Messageboard (";
236             messageString += b.getMessageboardURL() + ")\n";
237             if (hasAttachments()) {
238                 messageString += "and has " + attachments + " attachment";
239                 if (attachments > 1) {
240                     messageString += "s";
241                 }
242                 messageString += "\n(" + b.getMessageURL(MessageID) + ")\n";
243             }
244             messageString += "\n" + getFieldValue("message").toString();
245             messageString += "\n\nTo read this message online, click below:\n" + b.getMessageURL(MessageID);
246             messageString += "\n\nTo unsubscribe yourself from the " + b.getFieldValue("description").trim() + " Messageboard:\n" + b.getUnsubscribeURL() + "\n";
247             Email.send(database,
248                        fromNameS,
249                        fromEmailS,
250                        toString,
251                        reply,
252                        subject.getValue().toString().trim(),
253                        messageString
254                       );
255         }
256         connMgr.freeConnection(database, conn);
257     }
258 
259 /**
260  *
261  * @exception    java.lang.Exception If somthing fails
262 **/
263     public void send(String addresseeList) throws Exception {
264 
265         User author = new User(database,(Integer) ((DDField) get("author")).getValue());
266         DDField fromEmail = (DDField) author.get("email");
267         DBConnectionManager connMgr = DBConnectionManager.getInstance();
268         Connection conn = connMgr.getConnection("Message",database);
269         DDField boardId = (DDField) get("board");
270         if (!addresseeList.equals("")) {
271             DDField subject = (DDField) get("subject");
272             Board b = new Board(database,(Integer)boardId.getValue());
273             String boardName = b.getFieldValue("name").trim();
274             String MessageID = getFieldValue("id").trim();
275 
276             String murl = "";
277             try {
278                 SystemProperties sp = new SystemProperties(database);
279                 murl = sp.getProperty("messageboardurl");
280             } catch (Exception e) {
281                 murl = "messageboards.paneris.org";
282             }
283             String reply = boardName + "@" + murl;
284             if ((!MessageID.equals("")) && (!MessageID.equals("0"))) {
285                 reply = MessageID + "." + reply;
286             } 
287 
288             String messageString = getFieldValue("message").toString();
289 
290             Email.send(database,
291                        fromEmail.getValue().toString().trim(),
292                        addresseeList,
293                        reply,
294                        subject.getValue().toString().trim(),
295                        messageString
296                       );
297         }
298         connMgr.freeConnection(database, conn);
299     }
300 
301 }