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