001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.mail.service.persistence;
022:
023: import com.liferay.mail.NoSuchCyrusVirtualException;
024: import com.liferay.mail.model.CyrusVirtual;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.service.persistence.BasePersistence;
027: import com.liferay.portal.spring.hibernate.HibernateUtil;
028:
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import org.hibernate.ObjectNotFoundException;
033: import org.hibernate.Query;
034: import org.hibernate.Session;
035:
036: /**
037: * <a href="CyrusVirtualPersistence.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class CyrusVirtualPersistence extends BasePersistence {
043:
044: public static String FIND_BY_USER_ID = "FROM "
045: + CyrusVirtual.class.getName() + " WHERE userId = ?";
046:
047: public void remove(String emailAddress)
048: throws NoSuchCyrusVirtualException, SystemException {
049:
050: Session session = null;
051:
052: try {
053: session = getSessionFactory().openSession();
054:
055: CyrusVirtual virtual = (CyrusVirtual) session.load(
056: CyrusVirtual.class, emailAddress);
057:
058: session.delete(virtual);
059:
060: session.flush();
061: } catch (ObjectNotFoundException onfe) {
062: throw new NoSuchCyrusVirtualException();
063: } catch (Exception e) {
064: throw HibernateUtil.processException(e);
065: } finally {
066: session.close();
067: }
068: }
069:
070: public void update(CyrusVirtual virtual) throws SystemException {
071: Session session = null;
072:
073: try {
074: session = getSessionFactory().openSession();
075:
076: try {
077: CyrusVirtual virtualModel = (CyrusVirtual) session
078: .load(CyrusVirtual.class, virtual
079: .getEmailAddress());
080:
081: virtualModel.setUserId(virtual.getUserId());
082:
083: session.flush();
084: } catch (ObjectNotFoundException onfe) {
085: CyrusVirtual virtualModel = new CyrusVirtual(virtual
086: .getEmailAddress(), virtual.getUserId());
087:
088: session.save(virtualModel);
089:
090: session.flush();
091: }
092: } catch (Exception e) {
093: throw HibernateUtil.processException(e);
094: } finally {
095: session.close();
096: }
097: }
098:
099: public CyrusVirtual findByPrimaryKey(String emailAddress)
100: throws NoSuchCyrusVirtualException, SystemException {
101:
102: Session session = null;
103:
104: try {
105: session = getSessionFactory().openSession();
106:
107: return (CyrusVirtual) session.load(CyrusVirtual.class,
108: emailAddress);
109: } catch (ObjectNotFoundException onfe) {
110: throw new NoSuchCyrusVirtualException();
111: } catch (Exception e) {
112: throw HibernateUtil.processException(e);
113: } finally {
114: session.close();
115: }
116: }
117:
118: public List findByUserId(long userId) throws SystemException {
119: Session session = null;
120:
121: try {
122: session = getSessionFactory().openSession();
123:
124: Query q = session.createQuery(FIND_BY_USER_ID);
125:
126: q.setString(0, String.valueOf(userId));
127:
128: return q.list();
129: } catch (Exception e) {
130: throw HibernateUtil.processException(e);
131: } finally {
132: session.close();
133: }
134: }
135:
136: public void removeByUserId(long userId) throws SystemException {
137: Session session = null;
138:
139: try {
140: session = getSessionFactory().openSession();
141:
142: Query q = session.createQuery(FIND_BY_USER_ID);
143:
144: q.setString(0, String.valueOf(userId));
145:
146: Iterator itr = q.iterate();
147:
148: while (itr.hasNext()) {
149: CyrusVirtual virtual = (CyrusVirtual) itr.next();
150:
151: session.delete(virtual);
152: }
153:
154: session.flush();
155: } catch (Exception e) {
156: throw HibernateUtil.processException(e);
157: } finally {
158: session.close();
159: }
160: }
161:
162: }
|