001: package org.contineo.core.communication.dao;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.contineo.core.communication.EMail;
009: import org.contineo.core.communication.EMailAccount;
010: import org.hibernate.criterion.DetachedCriteria;
011: import org.hibernate.criterion.Property;
012: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
013:
014: /**
015: * Hibernate implementation of <code>EMailAccount</code>
016: *
017: * @author Alessandro Gasparini
018: * @version $Id: HibernateEMailAccountDAO.java,v 1.1 2007/06/29 06:28:30 marco Exp $
019: * @since 3.0
020: */
021: public class HibernateEMailAccountDAO extends HibernateDaoSupport
022: implements EMailAccountDAO {
023:
024: protected static Log log = LogFactory
025: .getLog(HibernateEMailAccountDAO.class);
026:
027: private HibernateEMailAccountDAO() {
028: }
029:
030: /**
031: * @see org.contineo.core.communication.dao.EMailAccountDAO#store(org.contineo.core.communication.EMailAccount)
032: */
033: public boolean store(EMailAccount account) {
034: boolean result = true;
035:
036: try {
037: getHibernateTemplate().saveOrUpdate(account);
038: } catch (Exception e) {
039: if (log.isErrorEnabled())
040: log.error(e.getMessage());
041: result = false;
042: }
043:
044: return result;
045: }
046:
047: /**
048: * @see org.contineo.core.communication.dao.EMailAccountDAO#delete(int)
049: */
050: public boolean delete(int accountId) {
051: boolean result = true;
052:
053: try {
054: DetachedCriteria dt = DetachedCriteria
055: .forClass(EMail.class);
056: dt.add(Property.forName("accountId").eq(
057: new Integer(accountId)));
058: getHibernateTemplate().deleteAll(
059: getHibernateTemplate().findByCriteria(dt));
060:
061: EMailAccount emAccount = (EMailAccount) getHibernateTemplate()
062: .get(EMailAccount.class, accountId);
063: if (emAccount != null)
064: getHibernateTemplate().delete(emAccount);
065: } catch (Exception e) {
066: if (log.isErrorEnabled())
067: log.error(e.getMessage(), e);
068: result = false;
069: }
070:
071: return result;
072: }
073:
074: /**
075: * @see org.contineo.core.communication.dao.EMailAccountDAO#findByPrimaryKey(int)
076: */
077: public EMailAccount findByPrimaryKey(int accountId) {
078: EMailAccount emAccount = null;
079:
080: try {
081: emAccount = (EMailAccount) getHibernateTemplate().get(
082: EMailAccount.class, new Integer(accountId));
083: } catch (Exception e) {
084: if (log.isErrorEnabled())
085: log.error(e.getMessage(), e);
086: }
087:
088: return emAccount;
089: }
090:
091: /**
092: * @see org.contineo.core.communication.dao.EMailAccountDAO#findAll()
093: */
094: @SuppressWarnings("unchecked")
095: public Collection<EMailAccount> findAll() {
096: Collection<EMailAccount> result = new ArrayList<EMailAccount>();
097:
098: try {
099: DetachedCriteria dt = DetachedCriteria
100: .forClass(EMailAccount.class);
101: result = (Collection<EMailAccount>) getHibernateTemplate()
102: .findByCriteria(dt);
103: } catch (Exception e) {
104: if (log.isErrorEnabled())
105: log.error(e.getMessage(), e);
106: }
107:
108: return result;
109: }
110:
111: /**
112: * @see org.contineo.core.communication.dao.EMailAccountDAO#deleteByUsername(java.lang.String)
113: */
114: public boolean deleteByUsername(String username) {
115: boolean result = true;
116:
117: try {
118: Collection<EMailAccount> coll = findByUserName(username);
119: for (EMailAccount account : coll) {
120: delete(account.getAccountId());
121: }
122: } catch (Exception e) {
123: if (log.isErrorEnabled())
124: log.error(e.getMessage());
125: result = false;
126: }
127:
128: return result;
129: }
130:
131: /**
132: * @see org.contineo.core.communication.dao.EMailAccountDAO#findByUserName(java.lang.String)
133: */
134: @SuppressWarnings("unchecked")
135: public Collection<EMailAccount> findByUserName(String username) {
136: Collection<EMailAccount> result = new ArrayList<EMailAccount>();
137:
138: try {
139: DetachedCriteria dt = DetachedCriteria
140: .forClass(EMailAccount.class);
141: dt.add(Property.forName("userName").eq(username));
142:
143: result = (Collection<EMailAccount>) getHibernateTemplate()
144: .findByCriteria(dt);
145: } catch (Exception e) {
146: if (log.isErrorEnabled())
147: log.error(e.getMessage(), e);
148: }
149:
150: return result;
151: }
152: }
|