001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/GroupsDAOImplJDBC.java,v 1.6 2007/01/15 10:27:12 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.6 $
005: * $Date: 2007/01/15 10:27:12 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.phpbb2mvnforum.db.jdbc;
041:
042: import java.sql.Connection;
043: import java.sql.PreparedStatement;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.sql.Timestamp;
047: import java.util.Collection;
048:
049: import net.myvietnam.mvncore.exception.AssertionException;
050: import net.myvietnam.mvncore.exception.CreateException;
051: import net.myvietnam.mvncore.exception.DatabaseException;
052: import net.myvietnam.mvncore.exception.DuplicateKeyException;
053: import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
054: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
055:
056: import org.mvnforum.util.DBUtils;
057:
058: import com.mvnforum.db.GroupsBean;
059: import com.mvnforum.db.GroupsDAO;
060:
061: public class GroupsDAOImplJDBC implements GroupsDAO {
062:
063: public void findByPrimaryKey(int groupID)
064: throws ObjectNotFoundException, DatabaseException {
065:
066: Connection connection = null;
067: PreparedStatement statement = null;
068: ResultSet resultSet = null;
069: StringBuffer sql = new StringBuffer(512);
070: sql.append("SELECT GroupName");
071: sql.append(" FROM " + TABLE_NAME);
072: sql.append(" WHERE GroupID = ?");
073: try {
074: connection = DBUtils.getMvnConnection();
075: statement = connection.prepareStatement(sql.toString());
076: statement.setInt(1, groupID);
077: resultSet = statement.executeQuery();
078: if (!resultSet.next()) {
079: throw new ObjectNotFoundException(
080: "Cannot find the primary key (" + groupID
081: + ") in table 'Groups'.");
082: }
083: } catch (SQLException sqle) {
084: throw new DatabaseException(
085: "Error executing SQL in GroupsDAOImplJDBC.findByPrimaryKey.");
086: } finally {
087: DBUtils.closeResultSet(resultSet);
088: DBUtils.closeStatement(statement);
089: DBUtils.closeConnection(connection);
090: }
091: }
092:
093: public void findByAlternateKey_GroupName(String groupName)
094: throws ObjectNotFoundException, DatabaseException {
095:
096: Connection connection = null;
097: PreparedStatement statement = null;
098: ResultSet resultSet = null;
099: StringBuffer sql = new StringBuffer(512);
100: sql.append("SELECT GroupID");
101: sql.append(" FROM " + TABLE_NAME);
102: sql.append(" WHERE GroupName = ?");
103: try {
104: connection = DBUtils.getMvnConnection();
105: statement = connection.prepareStatement(sql.toString());
106: statement.setString(1, groupName);
107: resultSet = statement.executeQuery();
108: if (!resultSet.next()) {
109: throw new ObjectNotFoundException(
110: "Cannot find the alternate key [GroupName] ("
111: + groupName + ") in table 'Groups'.");
112: }
113: } catch (SQLException sqle) {
114: sqle.printStackTrace();
115: throw new DatabaseException(
116: "Error executing SQL in GroupsDAOImplJDBC.findByAlternateKey_GroupName.");
117: } finally {
118: DBUtils.closeResultSet(resultSet);
119: DBUtils.closeStatement(statement);
120: DBUtils.closeConnection(connection);
121: }
122: }
123:
124: public void create(String groupOwnerName, String groupName,
125: String groupDesc, int groupOption,
126: Timestamp groupCreationDate, Timestamp groupModifiedDate)
127: throws CreateException, DatabaseException,
128: DuplicateKeyException, ForeignKeyNotFoundException {
129:
130: int groupOwnerID = 0;
131:
132: // Kiem tra xem GroupName co bi trung ko?
133: // Neu ko thi nhay den ObjectNotFoundException, OK!
134: try {
135: //Check if alternate key already exists
136: findByAlternateKey_GroupName(groupName);
137: //If so, then we have to throw an exception
138: throw new DuplicateKeyException(
139: "Alternate key already exists. Cannot create new Groups with the same [GroupName] ("
140: + groupName + ").");
141: } catch (ObjectNotFoundException e) {
142: //Otherwise we can go ahead
143: }
144:
145: try {
146: // @todo: modify the parameter list as needed
147: // You may have to regenerate this method if the needed columns dont have attribute 'include'
148: DAOFactory factory = new DAOFactory();
149: groupOwnerID = 0;
150: if ((groupOwnerName != null)
151: && (groupOwnerName.length() > 0)) {// have group owner
152: try {
153: groupOwnerID = factory.getMemberDAO()
154: .getMemberFromMemberName(groupOwnerName)
155: .getMemberID();
156: } catch (ObjectNotFoundException ex) {
157: // This exception should never be thrown
158: throw new ObjectNotFoundException(
159: "ASSERTION: This should never happen.");
160: }
161: }
162: } catch (ObjectNotFoundException e) {
163: throw new ForeignKeyNotFoundException(
164: "Foreign key refers to table 'Member' does not exist. Cannot create new Groups.");
165: }
166:
167: Connection connection = null;
168: PreparedStatement statement = null;
169: StringBuffer sql = new StringBuffer(512);
170: sql.append("INSERT INTO " + TABLE_NAME);
171: sql
172: .append(" (GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate)");
173: sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?)");
174: try {
175: connection = DBUtils.getMvnConnection();
176: statement = connection.prepareStatement(sql.toString());
177:
178: statement.setInt(1, groupOwnerID);
179: statement.setString(2, groupOwnerName);
180: statement.setString(3, groupName);
181: statement.setString(4, groupDesc);
182: statement.setInt(5, groupOption);
183: statement.setTimestamp(6, groupCreationDate);
184: statement.setTimestamp(7, groupModifiedDate);
185:
186: if (statement.executeUpdate() != 1) {
187: throw new CreateException(
188: "Error adding a row into table 'Groups'.");
189: }
190: } catch (SQLException sqle) {
191: throw new DatabaseException(
192: "Error executing SQL in GroupsDAOImplJDBC.create.");
193: } finally {
194: DBUtils.closeStatement(statement);
195: DBUtils.closeConnection(connection);
196: }
197: }
198:
199: public void delete(int groupID) throws DatabaseException,
200: ObjectNotFoundException {
201: // TODO Auto-generated method stub
202:
203: }
204:
205: public void update(int groupID, String groupName, String groupDesc,
206: Timestamp groupModifiedDate)
207: throws ObjectNotFoundException, DatabaseException,
208: DuplicateKeyException {
209: // TODO Auto-generated method stub
210:
211: }
212:
213: public void updateOwner(int groupID, String groupOwnerName,
214: Timestamp groupModifiedDate)
215: throws ObjectNotFoundException, DatabaseException,
216: ForeignKeyNotFoundException {
217: // TODO Auto-generated method stub
218:
219: }
220:
221: public GroupsBean getGroup(int groupID)
222: throws ObjectNotFoundException, DatabaseException {
223: // TODO Auto-generated method stub
224: return null;
225: }
226:
227: public Collection getMyGroups(int memberID)
228: throws DatabaseException {
229: // TODO Auto-generated method stub
230: return null;
231: }
232:
233: public Collection getGroups() throws DatabaseException {
234: // TODO Auto-generated method stub
235: return null;
236: }
237:
238: public int getNumberOfGroups() throws AssertionException,
239: DatabaseException {
240: // TODO Auto-generated method stub
241: return 0;
242: }
243:
244: public int getGroupIDFromGroupName(String groupName)
245: throws ObjectNotFoundException, DatabaseException {
246: // TODO Auto-generated method stub
247: return 0;
248: }
249:
250: }
|