001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.mail.service.persistence;
016    
017    import com.liferay.mail.NoSuchCyrusVirtualException;
018    import com.liferay.mail.model.CyrusVirtual;
019    import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
020    import com.liferay.portal.kernel.dao.orm.Query;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.model.Dummy;
024    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
025    
026    import java.util.Iterator;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class CyrusVirtualPersistenceImpl
033            extends BasePersistenceImpl<Dummy> implements CyrusVirtualPersistence {
034    
035            public static String FIND_BY_USER_ID =
036                    "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
037    
038            public CyrusVirtual findByPrimaryKey(String emailAddress)
039                    throws NoSuchCyrusVirtualException, SystemException {
040    
041                    Session session = null;
042    
043                    try {
044                            session = openSession();
045    
046                            return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
047                    }
048                    catch (ObjectNotFoundException onfe) {
049                            throw new NoSuchCyrusVirtualException();
050                    }
051                    catch (Exception e) {
052                            throw processException(e);
053                    }
054                    finally {
055                            closeSession(session);
056                    }
057            }
058    
059            public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
060                    Session session = null;
061    
062                    try {
063                            session = openSession();
064    
065                            Query q = session.createQuery(FIND_BY_USER_ID);
066    
067                            q.setString(0, String.valueOf(userId));
068    
069                            return q.list();
070                    }
071                    catch (Exception e) {
072                            throw processException(e);
073                    }
074                    finally {
075                            closeSession(session);
076                    }
077            }
078    
079            public void remove(String emailAddress)
080                    throws NoSuchCyrusVirtualException, SystemException {
081    
082                    Session session = null;
083    
084                    try {
085                            session = openSession();
086    
087                            CyrusVirtual virtual = (CyrusVirtual)session.load(
088                                    CyrusVirtual.class, emailAddress);
089    
090                            session.delete(virtual);
091    
092                            session.flush();
093                    }
094                    catch (ObjectNotFoundException onfe) {
095                            throw new NoSuchCyrusVirtualException();
096                    }
097                    catch (Exception e) {
098                            throw processException(e);
099                    }
100                    finally {
101                            closeSession(session);
102                    }
103            }
104    
105            public void removeByUserId(long userId) throws SystemException {
106                    Session session = null;
107    
108                    try {
109                            session = openSession();
110    
111                            Query q = session.createQuery(FIND_BY_USER_ID);
112    
113                            q.setString(0, String.valueOf(userId));
114    
115                            Iterator<CyrusVirtual> itr = q.iterate();
116    
117                            while (itr.hasNext()) {
118                                    CyrusVirtual virtual = itr.next();
119    
120                                    session.delete(virtual);
121                            }
122    
123                            closeSession(session);
124                    }
125                    catch (Exception e) {
126                            throw processException(e);
127                    }
128                    finally {
129                            closeSession(session);
130                    }
131            }
132    
133            public void update(CyrusVirtual virtual) throws SystemException {
134                    Session session = null;
135    
136                    try {
137                            session = openSession();
138    
139                            try {
140                                    CyrusVirtual virtualModel = (CyrusVirtual)session.load(
141                                            CyrusVirtual.class, virtual.getEmailAddress());
142    
143                                    virtualModel.setUserId(virtual.getUserId());
144    
145                                    session.flush();
146                            }
147                            catch (ObjectNotFoundException onfe) {
148                                    CyrusVirtual virtualModel = new CyrusVirtual(
149                                            virtual.getEmailAddress(), virtual.getUserId());
150    
151                                    session.save(virtualModel);
152    
153                                    session.flush();
154                            }
155                    }
156                    catch (Exception e) {
157                            throw processException(e);
158                    }
159                    finally {
160                            closeSession(session);
161                    }
162            }
163    
164    }