001: /*
002: * Created on 28/08/2006 23:12:09
003: */
004: package net.jforum.dao.generic;
005:
006: import java.sql.PreparedStatement;
007: import java.sql.ResultSet;
008: import java.sql.SQLException;
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import net.jforum.JForumExecutionContext;
013: import net.jforum.dao.MailIntegrationDAO;
014: import net.jforum.entities.MailIntegration;
015: import net.jforum.exceptions.DatabaseException;
016: import net.jforum.util.DbUtils;
017: import net.jforum.util.preferences.SystemGlobals;
018:
019: /**
020: * @author Rafael Steil
021: * @version $Id: GenericMailIntegrationDAO.java,v 1.3 2006/10/10 00:19:09 rafaelsteil Exp $
022: */
023: public class GenericMailIntegrationDAO implements MailIntegrationDAO {
024: /**
025: * @see net.jforum.dao.MailIntegrationDAO#add(net.jforum.entities.MailIntegration)
026: */
027: public void add(MailIntegration integration) {
028: PreparedStatement p = null;
029:
030: try {
031: p = JForumExecutionContext
032: .getConnection()
033: .prepareStatement(
034: SystemGlobals.getSql("MailIntegration.add"));
035: this .prepareForSave(integration, p);
036: p.executeUpdate();
037: } catch (SQLException e) {
038: throw new DatabaseException(e);
039: } finally {
040: DbUtils.close(p);
041: }
042: }
043:
044: /**
045: * @see net.jforum.dao.MailIntegrationDAO#delete(int)
046: */
047: public void delete(int forumId) {
048: PreparedStatement p = null;
049:
050: try {
051: p = JForumExecutionContext.getConnection()
052: .prepareStatement(
053: SystemGlobals
054: .getSql("MailIntegration.delete"));
055: p.setInt(1, forumId);
056: p.executeUpdate();
057: } catch (SQLException e) {
058: throw new DatabaseException(e);
059: } finally {
060: DbUtils.close(p);
061: }
062: }
063:
064: /**
065: * @see net.jforum.dao.MailIntegrationDAO#find(int)
066: */
067: public MailIntegration find(int forumId) {
068: MailIntegration m = null;
069:
070: PreparedStatement p = null;
071: ResultSet rs = null;
072:
073: try {
074: p = JForumExecutionContext.getConnection()
075: .prepareStatement(
076: SystemGlobals
077: .getSql("MailIntegration.find"));
078: p.setInt(1, forumId);
079: rs = p.executeQuery();
080:
081: if (rs.next()) {
082: m = this .buildMailIntegration(rs);
083: }
084: } catch (SQLException e) {
085: throw new DatabaseException(e);
086: } finally {
087: DbUtils.close(rs, p);
088: }
089:
090: return m;
091: }
092:
093: /**
094: * @see net.jforum.dao.MailIntegrationDAO#findAll()
095: */
096: public List findAll() {
097: List l = new ArrayList();
098:
099: PreparedStatement p = null;
100: ResultSet rs = null;
101:
102: try {
103: p = JForumExecutionContext.getConnection()
104: .prepareStatement(
105: SystemGlobals
106: .getSql("MailIntegration.findAll"));
107: rs = p.executeQuery();
108:
109: while (rs.next()) {
110: l.add(this .buildMailIntegration(rs));
111: }
112: } catch (SQLException e) {
113: throw new DatabaseException(e);
114: } finally {
115: DbUtils.close(rs, p);
116: }
117:
118: return l;
119: }
120:
121: /**
122: * @see net.jforum.dao.MailIntegrationDAO#update(net.jforum.entities.MailIntegration)
123: */
124: public void update(MailIntegration integration) {
125: PreparedStatement p = null;
126:
127: try {
128: p = JForumExecutionContext.getConnection()
129: .prepareStatement(
130: SystemGlobals
131: .getSql("MailIntegration.update"));
132:
133: this .prepareForSave(integration, p);
134: p.setInt(8, integration.getForumId());
135:
136: p.executeUpdate();
137: } catch (SQLException e) {
138: throw new DatabaseException(e);
139: } finally {
140: DbUtils.close(p);
141: }
142: }
143:
144: private MailIntegration buildMailIntegration(ResultSet rs)
145: throws SQLException {
146: MailIntegration mi = new MailIntegration();
147:
148: mi.setForumId(rs.getInt("forum_id"));
149: mi.setForumEmail(rs.getString("forum_email"));
150: mi.setPopHost(rs.getString("pop_host"));
151: mi.setPopPassword(rs.getString("pop_password"));
152: mi.setPopPort(rs.getInt("pop_port"));
153: mi.setPopUsername(rs.getString("pop_username"));
154: mi.setSSL(rs.getInt("pop_ssl") == 1);
155:
156: return mi;
157: }
158:
159: /**
160: * Given a PreparedStatement, fill its values with the data of a MailIntegration instance
161: * @param integration the data to fill the statement
162: * @param p the statement to be filled
163: * @throws SQLException
164: */
165: private void prepareForSave(MailIntegration integration,
166: PreparedStatement p) throws SQLException {
167: p.setInt(1, integration.getForumId());
168: p.setString(2, integration.getForumEmail());
169: p.setString(3, integration.getPopHost());
170: p.setString(4, integration.getPopUsername());
171: p.setString(5, integration.getPopPassword());
172: p.setInt(6, integration.getPopPort());
173: p.setInt(7, integration.isSSL() ? 1 : 0);
174: }
175: }
|