1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.mail.service.persistence;
24  
25  import com.liferay.mail.NoSuchCyrusVirtualException;
26  import com.liferay.mail.model.CyrusVirtual;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.dao.orm.ObjectNotFoundException;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.Session;
31  import com.liferay.portal.model.Dummy;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="CyrusVirtualPersistenceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class CyrusVirtualPersistenceImpl
43      extends BasePersistenceImpl<Dummy> implements CyrusVirtualPersistence {
44  
45      public static String FIND_BY_USER_ID =
46          "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
47  
48      public CyrusVirtual findByPrimaryKey(String emailAddress)
49          throws NoSuchCyrusVirtualException, SystemException {
50  
51          Session session = null;
52  
53          try {
54              session = openSession();
55  
56              return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
57          }
58          catch (ObjectNotFoundException onfe) {
59              throw new NoSuchCyrusVirtualException();
60          }
61          catch (Exception e) {
62              throw processException(e);
63          }
64          finally {
65              closeSession(session);
66          }
67      }
68  
69      public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
70          Session session = null;
71  
72          try {
73              session = openSession();
74  
75              Query q = session.createQuery(FIND_BY_USER_ID);
76  
77              q.setString(0, String.valueOf(userId));
78  
79              return q.list();
80          }
81          catch (Exception e) {
82              throw processException(e);
83          }
84          finally {
85              closeSession(session);
86          }
87      }
88  
89      public void remove(String emailAddress)
90          throws NoSuchCyrusVirtualException, SystemException {
91  
92          Session session = null;
93  
94          try {
95              session = openSession();
96  
97              CyrusVirtual virtual = (CyrusVirtual)session.load(
98                  CyrusVirtual.class, emailAddress);
99  
100             session.delete(virtual);
101 
102             session.flush();
103         }
104         catch (ObjectNotFoundException onfe) {
105             throw new NoSuchCyrusVirtualException();
106         }
107         catch (Exception e) {
108             throw processException(e);
109         }
110         finally {
111             closeSession(session);
112         }
113     }
114 
115     public void removeByUserId(long userId) throws SystemException {
116         Session session = null;
117 
118         try {
119             session = openSession();
120 
121             Query q = session.createQuery(FIND_BY_USER_ID);
122 
123             q.setString(0, String.valueOf(userId));
124 
125             Iterator<CyrusVirtual> itr = q.iterate();
126 
127             while (itr.hasNext()) {
128                 CyrusVirtual virtual = itr.next();
129 
130                 session.delete(virtual);
131             }
132 
133             closeSession(session);
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             closeSession(session);
140         }
141     }
142 
143     public void update(CyrusVirtual virtual) throws SystemException {
144         Session session = null;
145 
146         try {
147             session = openSession();
148 
149             try {
150                 CyrusVirtual virtualModel = (CyrusVirtual)session.load(
151                     CyrusVirtual.class, virtual.getEmailAddress());
152 
153                 virtualModel.setUserId(virtual.getUserId());
154 
155                 session.flush();
156             }
157             catch (ObjectNotFoundException onfe) {
158                 CyrusVirtual virtualModel = new CyrusVirtual(
159                     virtual.getEmailAddress(), virtual.getUserId());
160 
161                 session.save(virtualModel);
162 
163                 session.flush();
164             }
165         }
166         catch (Exception e) {
167             throw processException(e);
168         }
169         finally {
170             closeSession(session);
171         }
172     }
173 
174 }