KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mvnforum > phpbb2mvnforum > db > jdbc > WatchDAOImplJDBC


1 package org.mvnforum.phpbb2mvnforum.db.jdbc;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Timestamp JavaDoc;
8 import java.util.Collection JavaDoc;
9
10 import org.mvnforum.util.DBUtils;
11
12 import net.myvietnam.mvncore.exception.AssertionException;
13 import net.myvietnam.mvncore.exception.CreateException;
14 import net.myvietnam.mvncore.exception.DatabaseException;
15 import net.myvietnam.mvncore.exception.DuplicateKeyException;
16 import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
17 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
18
19 import com.mvnforum.MVNForumConstant;
20 import com.mvnforum.db.DAOFactory;
21 import com.mvnforum.db.WatchBean;
22 import com.mvnforum.db.WatchDAO;
23
24 public class WatchDAOImplJDBC implements WatchDAO {
25
26     public void findByPrimaryKey(int watchID)
27         throws ObjectNotFoundException, DatabaseException {
28         // TODO Auto-generated method stub
29
Connection JavaDoc connection = null;
30         PreparedStatement JavaDoc statement = null;
31         ResultSet JavaDoc resultSet = null;
32         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
33         sql.append("SELECT WatchType");
34         sql.append(" FROM " + TABLE_NAME);
35         sql.append(" WHERE WatchID = ?");
36         try {
37             connection = DBUtils.getMvnConnection();
38             statement = connection.prepareStatement(sql.toString());
39             statement.setInt(1, watchID);
40             resultSet = statement.executeQuery();
41             if (!resultSet.next()) {
42                 throw new ObjectNotFoundException("Cannot find the primary key (" + watchID + ") in table 'Watch'.");
43             }
44         } catch(SQLException JavaDoc sqle) {
45             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByPrimaryKey.");
46         } finally {
47             DBUtils.closeResultSet(resultSet);
48             DBUtils.closeStatement(statement);
49             DBUtils.closeConnection(connection);
50         }
51     }
52
53     public void findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID, int forumID,
54             int threadID)
55         throws ObjectNotFoundException, DatabaseException {
56         Connection JavaDoc connection = null;
57         PreparedStatement JavaDoc statement = null;
58         ResultSet JavaDoc resultSet = null;
59         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
60         sql.append("SELECT WatchType");
61         sql.append(" FROM " + TABLE_NAME);
62         sql.append(" WHERE MemberID = ? AND CategoryID = ? AND ForumID = ? AND ThreadID = ?");
63         try {
64             connection = DBUtils.getMvnConnection();
65             statement = connection.prepareStatement(sql.toString());
66             statement.setInt(1, memberID);
67             statement.setInt(2, categoryID);
68             statement.setInt(3, forumID);
69             statement.setInt(4, threadID);
70             resultSet = statement.executeQuery();
71             if (!resultSet.next()) {
72                 throw new ObjectNotFoundException("Cannot find the alternate key [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ") in table 'Watch'.");
73             }
74         } catch(SQLException JavaDoc sqle) {
75             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID.");
76         } finally {
77             DBUtils.closeResultSet(resultSet);
78             DBUtils.closeStatement(statement);
79             DBUtils.closeConnection(connection);
80         }
81
82     }
83
84     public void create(int memberID, int categoryID, int forumID, int threadID, int watchType, int watchOption,
85             int watchStatus, Timestamp JavaDoc watchCreationDate, Timestamp JavaDoc watchLastSentDate, Timestamp JavaDoc watchEndDate)
86         throws IllegalArgumentException JavaDoc, CreateException, DatabaseException, DuplicateKeyException,
87         ForeignKeyNotFoundException {
88         if ((memberID == 0) || (memberID == MVNForumConstant.MEMBER_ID_OF_GUEST)) {
89             throw new IllegalArgumentException JavaDoc("Cannot add a new watch for Guest (id = " + memberID + ")");
90         }
91         int notZeroCount = 0;
92         if (categoryID != 0) {
93             notZeroCount++;
94         }
95         if (forumID != 0) {
96             notZeroCount++;
97         }
98         if (threadID != 0) {
99             notZeroCount++;
100         }
101         if (notZeroCount > 1) {
102             throw new IllegalArgumentException JavaDoc("Cannot add watch with more than 1 element.");
103         }
104
105         // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
106
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
107
try {
108             //Check if alternate key already exists
109
findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(memberID, categoryID, forumID, threadID);
110             //If so, then we have to throw an exception
111
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Watch with the same [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ").");
112         } catch(ObjectNotFoundException e) {
113             //Otherwise we can go ahead
114
}
115
116         try {
117             // @todo: modify the parameter list as needed
118
// You may have to regenerate this method if the needed columns dont have attribute 'include'
119
DAOFactory.getMemberDAO().findByPrimaryKey(memberID);
120         } catch(ObjectNotFoundException e) {
121             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Member' does not exist. Cannot create new Watch.");
122         }
123
124         try {
125             // @todo: modify the parameter list as needed
126
// You may have to regenerate this method if the needed columns dont have attribute 'include'
127
if (categoryID != 0) {
128                 DAOFactory.getCategoryDAO().findByPrimaryKey(categoryID);
129             }
130         } catch(ObjectNotFoundException e) {
131             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Category' does not exist. Cannot create new Watch.");
132         }
133
134         try {
135             // @todo: modify the parameter list as needed
136
// You may have to regenerate this method if the needed columns dont have attribute 'include'
137
if (forumID != 0) {
138                 DAOFactory.getForumDAO().findByPrimaryKey(forumID);
139             }
140         } catch(ObjectNotFoundException e) {
141             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Forum' does not exist. Cannot create new Watch.");
142         }
143
144         try {
145             // @todo: modify the parameter list as needed
146
// You may have to regenerate this method if the needed columns dont have attribute 'include'
147
if (threadID != 0) {
148                 DAOFactory.getThreadDAO().findByPrimaryKey(threadID);
149             }
150         } catch(ObjectNotFoundException e) {
151             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Thread' does not exist. Cannot create new Watch.");
152         }
153
154         Connection JavaDoc connection = null;
155         PreparedStatement JavaDoc statement = null;
156         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
157         sql.append("INSERT INTO " + TABLE_NAME + " (MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate)");
158         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
159         try {
160             connection = DBUtils.getMvnConnection();
161             statement = connection.prepareStatement(sql.toString());
162
163             statement.setInt(1, memberID);
164             statement.setInt(2, categoryID);
165             statement.setInt(3, forumID);
166             statement.setInt(4, threadID);
167             statement.setInt(5, watchType);
168             statement.setInt(6, watchOption);
169             statement.setInt(7, watchStatus);
170             statement.setTimestamp(8, watchCreationDate);
171             statement.setTimestamp(9, watchLastSentDate);
172             statement.setTimestamp(10, watchEndDate);
173
174             if (statement.executeUpdate() != 1) {
175                 throw new CreateException("Error adding a row into table 'Watch'.");
176             }
177         } catch(SQLException JavaDoc sqle) {
178             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.create.");
179         } finally {
180             DBUtils.closeStatement(statement);
181             DBUtils.closeConnection(connection);
182         }
183
184     }
185
186     public void delete(int watchID)
187         throws DatabaseException, ObjectNotFoundException {
188         // TODO Auto-generated method stub
189

190     }
191
192     public void delete_inMember(int memberID)
193         throws DatabaseException {
194         // TODO Auto-generated method stub
195

196     }
197
198     public void delete_inCategory(int categoryID)
199         throws DatabaseException {
200         // TODO Auto-generated method stub
201

202     }
203
204     public void delete_inForum(int forumID)
205         throws DatabaseException {
206         // TODO Auto-generated method stub
207

208     }
209
210     public void delete_inThread(int threadID)
211         throws DatabaseException {
212         // TODO Auto-generated method stub
213

214     }
215
216     public void updateLastSentDate(int watchID, Timestamp JavaDoc watchLastSentDate)
217         throws ObjectNotFoundException, DatabaseException {
218         // TODO Auto-generated method stub
219

220     }
221
222     public WatchBean getWatch(int watchID)
223         throws ObjectNotFoundException, DatabaseException {
224         // TODO Auto-generated method stub
225
return null;
226     }
227
228     public WatchBean getWatch_byAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID,
229             int forumID, int threadID)
230         throws ObjectNotFoundException, DatabaseException {
231         // TODO Auto-generated method stub
232
return null;
233     }
234
235     public Collection JavaDoc getWatches()
236         throws DatabaseException {
237         // TODO Auto-generated method stub
238
return null;
239     }
240
241     public int getNumberOfWatches()
242         throws AssertionException, DatabaseException {
243         // TODO Auto-generated method stub
244
return 0;
245     }
246
247     public int getNumberOfWatches_forMember(int memberID)
248         throws AssertionException, DatabaseException {
249         // TODO Auto-generated method stub
250
return 0;
251     }
252
253     public Collection JavaDoc getMemberBeans()
254         throws DatabaseException {
255         // TODO Auto-generated method stub
256
return null;
257     }
258
259     public Collection JavaDoc getWatches_forMember(int memberID)
260         throws DatabaseException {
261         // TODO Auto-generated method stub
262
return null;
263     }
264
265     public void updateLastSentDate_forMember(int memberID, Timestamp JavaDoc watchLastSentDate)
266         throws ObjectNotFoundException, DatabaseException {
267         // TODO Auto-generated method stub
268

269     }
270
271 }
272
Popular Tags