001    /**
002     * Copyright (c) 2000-2013 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 final String FIND_BY_USER_ID =
036                    "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
037    
038            @Override
039            public CyrusVirtual findByPrimaryKey(String emailAddress)
040                    throws NoSuchCyrusVirtualException, SystemException {
041    
042                    Session session = null;
043    
044                    try {
045                            session = openSession();
046    
047                            return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
048                    }
049                    catch (ObjectNotFoundException onfe) {
050                            throw new NoSuchCyrusVirtualException(
051                                    "{emailAddress=" + emailAddress + "}");
052                    }
053                    catch (Exception e) {
054                            throw processException(e);
055                    }
056                    finally {
057                            closeSession(session);
058                    }
059            }
060    
061            @Override
062            public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
063                    Session session = null;
064    
065                    try {
066                            session = openSession();
067    
068                            Query q = session.createQuery(FIND_BY_USER_ID);
069    
070                            q.setString(0, String.valueOf(userId));
071    
072                            return q.list();
073                    }
074                    catch (Exception e) {
075                            throw processException(e);
076                    }
077                    finally {
078                            closeSession(session);
079                    }
080            }
081    
082            @Override
083            public void remove(String emailAddress)
084                    throws NoSuchCyrusVirtualException, SystemException {
085    
086                    Session session = null;
087    
088                    try {
089                            session = openSession();
090    
091                            CyrusVirtual virtual = (CyrusVirtual)session.load(
092                                    CyrusVirtual.class, emailAddress);
093    
094                            session.delete(virtual);
095    
096                            session.flush();
097                    }
098                    catch (ObjectNotFoundException onfe) {
099                            throw new NoSuchCyrusVirtualException(
100                                    "{emailAddress=" + emailAddress + "}");
101                    }
102                    catch (Exception e) {
103                            throw processException(e);
104                    }
105                    finally {
106                            closeSession(session);
107                    }
108            }
109    
110            @Override
111            public void removeByUserId(long userId) throws SystemException {
112                    Session session = null;
113    
114                    try {
115                            session = openSession();
116    
117                            Query q = session.createQuery(FIND_BY_USER_ID);
118    
119                            q.setString(0, String.valueOf(userId));
120    
121                            Iterator<CyrusVirtual> itr = q.iterate();
122    
123                            while (itr.hasNext()) {
124                                    CyrusVirtual virtual = itr.next();
125    
126                                    session.delete(virtual);
127                            }
128    
129                            closeSession(session);
130                    }
131                    catch (Exception e) {
132                            throw processException(e);
133                    }
134                    finally {
135                            closeSession(session);
136                    }
137            }
138    
139            @Override
140            public void update(CyrusVirtual virtual) throws SystemException {
141                    Session session = null;
142    
143                    try {
144                            session = openSession();
145    
146                            try {
147                                    CyrusVirtual virtualModel = (CyrusVirtual)session.load(
148                                            CyrusVirtual.class, virtual.getEmailAddress());
149    
150                                    virtualModel.setUserId(virtual.getUserId());
151    
152                                    session.flush();
153                            }
154                            catch (ObjectNotFoundException onfe) {
155                                    CyrusVirtual virtualModel = new CyrusVirtual(
156                                            virtual.getEmailAddress(), virtual.getUserId());
157    
158                                    session.save(virtualModel);
159    
160                                    session.flush();
161                            }
162                    }
163                    catch (Exception e) {
164                            throw processException(e);
165                    }
166                    finally {
167                            closeSession(session);
168                    }
169            }
170    
171    }