001    /**
002     * Copyright (c) 2000-present 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.model.Dummy;
023    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
024    
025    import java.util.Iterator;
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class CyrusVirtualPersistenceImpl
032            extends BasePersistenceImpl<Dummy> implements CyrusVirtualPersistence {
033    
034            public static final String FIND_BY_USER_ID =
035                    "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
036    
037            @Override
038            public CyrusVirtual findByPrimaryKey(String emailAddress)
039                    throws NoSuchCyrusVirtualException {
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                                    "{emailAddress=" + emailAddress + "}");
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) {
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) throws NoSuchCyrusVirtualException {
083                    Session session = null;
084    
085                    try {
086                            session = openSession();
087    
088                            CyrusVirtual virtual = (CyrusVirtual)session.load(
089                                    CyrusVirtual.class, emailAddress);
090    
091                            session.delete(virtual);
092    
093                            session.flush();
094                    }
095                    catch (ObjectNotFoundException onfe) {
096                            throw new NoSuchCyrusVirtualException(
097                                    "{emailAddress=" + emailAddress + "}");
098                    }
099                    catch (Exception e) {
100                            throw processException(e);
101                    }
102                    finally {
103                            closeSession(session);
104                    }
105            }
106    
107            @Override
108            public void removeByUserId(long userId) {
109                    Session session = null;
110    
111                    try {
112                            session = openSession();
113    
114                            Query q = session.createQuery(FIND_BY_USER_ID);
115    
116                            q.setString(0, String.valueOf(userId));
117    
118                            Iterator<CyrusVirtual> itr = q.iterate();
119    
120                            while (itr.hasNext()) {
121                                    CyrusVirtual virtual = itr.next();
122    
123                                    session.delete(virtual);
124                            }
125    
126                            closeSession(session);
127                    }
128                    catch (Exception e) {
129                            throw processException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136            @Override
137            public void update(CyrusVirtual virtual) {
138                    Session session = null;
139    
140                    try {
141                            session = openSession();
142    
143                            try {
144                                    CyrusVirtual virtualModel = (CyrusVirtual)session.load(
145                                            CyrusVirtual.class, virtual.getEmailAddress());
146    
147                                    virtualModel.setUserId(virtual.getUserId());
148    
149                                    session.flush();
150                            }
151                            catch (ObjectNotFoundException onfe) {
152                                    CyrusVirtual virtualModel = new CyrusVirtual(
153                                            virtual.getEmailAddress(), virtual.getUserId());
154    
155                                    session.save(virtualModel);
156    
157                                    session.flush();
158                            }
159                    }
160                    catch (Exception e) {
161                            throw processException(e);
162                    }
163                    finally {
164                            closeSession(session);
165                    }
166            }
167    
168    }