1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.mail.service.persistence;
16  
17  import com.liferay.mail.NoSuchCyrusVirtualException;
18  import com.liferay.mail.model.CyrusVirtual;
19  import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
20  import com.liferay.portal.kernel.dao.orm.Query;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.model.Dummy;
24  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * <a href="CyrusVirtualPersistenceImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class CyrusVirtualPersistenceImpl
35      extends BasePersistenceImpl<Dummy> implements CyrusVirtualPersistence {
36  
37      public static String FIND_BY_USER_ID =
38          "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
39  
40      public CyrusVirtual findByPrimaryKey(String emailAddress)
41          throws NoSuchCyrusVirtualException, SystemException {
42  
43          Session session = null;
44  
45          try {
46              session = openSession();
47  
48              return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
49          }
50          catch (ObjectNotFoundException onfe) {
51              throw new NoSuchCyrusVirtualException();
52          }
53          catch (Exception e) {
54              throw processException(e);
55          }
56          finally {
57              closeSession(session);
58          }
59      }
60  
61      public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
62          Session session = null;
63  
64          try {
65              session = openSession();
66  
67              Query q = session.createQuery(FIND_BY_USER_ID);
68  
69              q.setString(0, String.valueOf(userId));
70  
71              return q.list();
72          }
73          catch (Exception e) {
74              throw processException(e);
75          }
76          finally {
77              closeSession(session);
78          }
79      }
80  
81      public void remove(String emailAddress)
82          throws NoSuchCyrusVirtualException, SystemException {
83  
84          Session session = null;
85  
86          try {
87              session = openSession();
88  
89              CyrusVirtual virtual = (CyrusVirtual)session.load(
90                  CyrusVirtual.class, emailAddress);
91  
92              session.delete(virtual);
93  
94              session.flush();
95          }
96          catch (ObjectNotFoundException onfe) {
97              throw new NoSuchCyrusVirtualException();
98          }
99          catch (Exception e) {
100             throw processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public void removeByUserId(long userId) throws SystemException {
108         Session session = null;
109 
110         try {
111             session = openSession();
112 
113             Query q = session.createQuery(FIND_BY_USER_ID);
114 
115             q.setString(0, String.valueOf(userId));
116 
117             Iterator<CyrusVirtual> itr = q.iterate();
118 
119             while (itr.hasNext()) {
120                 CyrusVirtual virtual = itr.next();
121 
122                 session.delete(virtual);
123             }
124 
125             closeSession(session);
126         }
127         catch (Exception e) {
128             throw processException(e);
129         }
130         finally {
131             closeSession(session);
132         }
133     }
134 
135     public void update(CyrusVirtual virtual) throws SystemException {
136         Session session = null;
137 
138         try {
139             session = openSession();
140 
141             try {
142                 CyrusVirtual virtualModel = (CyrusVirtual)session.load(
143                     CyrusVirtual.class, virtual.getEmailAddress());
144 
145                 virtualModel.setUserId(virtual.getUserId());
146 
147                 session.flush();
148             }
149             catch (ObjectNotFoundException onfe) {
150                 CyrusVirtual virtualModel = new CyrusVirtual(
151                     virtual.getEmailAddress(), virtual.getUserId());
152 
153                 session.save(virtualModel);
154 
155                 session.flush();
156             }
157         }
158         catch (Exception e) {
159             throw processException(e);
160         }
161         finally {
162             closeSession(session);
163         }
164     }
165 
166 }