KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.myvietnam.mvncore.exception.AssertionException;
11 import net.myvietnam.mvncore.exception.CreateException;
12 import net.myvietnam.mvncore.exception.DatabaseException;
13 import net.myvietnam.mvncore.exception.DuplicateKeyException;
14 import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
15 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
16
17 import org.mvnforum.util.DBUtils;
18
19 //import com.mvnforum.db.DAOFactory;
20
import com.mvnforum.db.GroupsBean;
21 import com.mvnforum.db.GroupsDAO;
22
23 public class GroupsDAOImplJDBC implements GroupsDAO {
24
25     public void findByPrimaryKey(int groupID)
26         throws ObjectNotFoundException, DatabaseException {
27
28         Connection JavaDoc connection = null;
29         PreparedStatement JavaDoc statement = null;
30         ResultSet JavaDoc resultSet = null;
31         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
32         sql.append("SELECT GroupName");
33         sql.append(" FROM " + TABLE_NAME);
34         sql.append(" WHERE GroupID = ?");
35         try {
36             connection = DBUtils.getMvnConnection();
37             statement = connection.prepareStatement(sql.toString());
38             statement.setInt(1, groupID);
39             resultSet = statement.executeQuery();
40             if (!resultSet.next()) {
41                 throw new ObjectNotFoundException("Cannot find the primary key (" + groupID + ") in table 'Groups'.");
42             }
43         } catch (SQLException JavaDoc sqle) {
44             throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.findByPrimaryKey.");
45         } finally {
46             DBUtils.closeResultSet(resultSet);
47             DBUtils.closeStatement(statement);
48             DBUtils.closeConnection(connection);
49         }
50     }
51
52     public void findByAlternateKey_GroupName(String JavaDoc groupName)
53         throws ObjectNotFoundException, DatabaseException {
54
55         Connection JavaDoc connection = null;
56         PreparedStatement JavaDoc statement = null;
57         ResultSet JavaDoc resultSet = null;
58         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
59         sql.append("SELECT GroupID");
60         sql.append(" FROM " + TABLE_NAME);
61         sql.append(" WHERE GroupName = ?");
62         try {
63             connection = DBUtils.getMvnConnection();
64             statement = connection.prepareStatement(sql.toString());
65             statement.setString(1, groupName);
66             resultSet = statement.executeQuery();
67             if (!resultSet.next()) {
68                 throw new ObjectNotFoundException("Cannot find the alternate key [GroupName] (" + groupName
69                         + ") in table 'Groups'.");
70             }
71         } catch (SQLException JavaDoc sqle) {
72             sqle.printStackTrace();
73             throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.findByAlternateKey_GroupName.");
74         } finally {
75             DBUtils.closeResultSet(resultSet);
76             DBUtils.closeStatement(statement);
77             DBUtils.closeConnection(connection);
78         }
79     }
80
81     public void create(String JavaDoc groupOwnerName, String JavaDoc groupName, String JavaDoc groupDesc, int groupOption,
82             Timestamp JavaDoc groupCreationDate, Timestamp JavaDoc groupModifiedDate)
83         throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException {
84
85         int groupOwnerID = 0;
86         
87         try {
88             //Check if alternate key already exists
89
findByAlternateKey_GroupName(groupName);
90             //If so, then we have to throw an exception
91
throw new DuplicateKeyException(
92                     "Alternate key already exists. Cannot create new Groups with the same [GroupName] (" + groupName
93                             + ").");
94         } catch (ObjectNotFoundException e) {
95             //Otherwise we can go ahead
96
}
97
98         try {
99             // @todo: modify the parameter list as needed
100
// You may have to regenerate this method if the needed columns dont have attribute 'include'
101
DAOFactory factory = new DAOFactory ();
102             groupOwnerID = 0;
103             if ((groupOwnerName != null) && (groupOwnerName.length() > 0)) {// have group owner
104
try {
105                     groupOwnerID = factory.getMemberDAO().getMemberFromMemberName(groupOwnerName).getMemberID();
106                 } catch (ObjectNotFoundException ex) {
107                     // This exception should never be thrown
108
throw new ObjectNotFoundException("ASSERTION: This should never happen.");
109                 }
110             }
111         } catch (ObjectNotFoundException e) {
112             throw new ForeignKeyNotFoundException(
113                     "Foreign key refers to table 'Member' does not exist. Cannot create new Groups.");
114         }
115
116         Connection JavaDoc connection = null;
117         PreparedStatement JavaDoc statement = null;
118         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
119         sql
120                 .append("INSERT INTO "
121                         + TABLE_NAME
122                         + " (GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate)");
123         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?)");
124         try {
125             connection = DBUtils.getMvnConnection();
126             statement = connection.prepareStatement(sql.toString());
127
128             statement.setInt(1, groupOwnerID);
129             statement.setString(2, groupOwnerName);
130             statement.setString(3, groupName);
131             statement.setString(4, groupDesc);
132             statement.setInt(5, groupOption);
133             statement.setTimestamp(6, groupCreationDate);
134             statement.setTimestamp(7, groupModifiedDate);
135
136             if (statement.executeUpdate() != 1) {
137                 throw new CreateException("Error adding a row into table 'Groups'.");
138             }
139         } catch (SQLException JavaDoc sqle) {
140             throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.create.");
141         } finally {
142             DBUtils.closeStatement(statement);
143             DBUtils.closeConnection(connection);
144         }
145     }
146
147     public void delete(int groupID)
148         throws DatabaseException, ObjectNotFoundException {
149         // TODO Auto-generated method stub
150

151     }
152
153     public void update(int groupID, String JavaDoc groupName, String JavaDoc groupDesc, Timestamp JavaDoc groupModifiedDate)
154         throws ObjectNotFoundException, DatabaseException, DuplicateKeyException {
155         // TODO Auto-generated method stub
156

157     }
158
159     public void updateOwner(int groupID, String JavaDoc groupOwnerName, Timestamp JavaDoc groupModifiedDate)
160         throws ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException {
161         // TODO Auto-generated method stub
162

163     }
164
165     public GroupsBean getGroup(int groupID)
166         throws ObjectNotFoundException, DatabaseException {
167         // TODO Auto-generated method stub
168
return null;
169     }
170
171     public Collection JavaDoc getMyGroups(int memberID)
172         throws DatabaseException {
173         // TODO Auto-generated method stub
174
return null;
175     }
176
177     public Collection JavaDoc getGroups()
178         throws DatabaseException {
179         // TODO Auto-generated method stub
180
return null;
181     }
182
183     public int getNumberOfGroups()
184         throws AssertionException, DatabaseException {
185         // TODO Auto-generated method stub
186
return 0;
187     }
188
189     public int getGroupIDFromGroupName(String JavaDoc groupName)
190         throws ObjectNotFoundException, DatabaseException {
191         // TODO Auto-generated method stub
192
return 0;
193     }
194
195 }
196
Popular Tags