1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.service.persistence.impl.BasePersistenceImpl;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="CyrusVirtualPersistence.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class CyrusVirtualPersistence extends BasePersistenceImpl {
43  
44      public static String FIND_BY_USER_ID =
45          "FROM " + CyrusVirtual.class.getName() + " WHERE userId = ?";
46  
47      public void remove(String emailAddress)
48          throws NoSuchCyrusVirtualException, SystemException {
49  
50          Session session = null;
51  
52          try {
53              session = openSession();
54  
55              CyrusVirtual virtual = (CyrusVirtual)session.load(
56                  CyrusVirtual.class, emailAddress);
57  
58              session.delete(virtual);
59  
60              session.flush();
61          }
62          catch (ObjectNotFoundException onfe) {
63              throw new NoSuchCyrusVirtualException();
64          }
65          catch (Exception e) {
66              throw processException(e);
67          }
68          finally {
69              closeSession(session);
70          }
71      }
72  
73      public void update(CyrusVirtual virtual) throws SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              try {
80                  CyrusVirtual virtualModel = (CyrusVirtual)session.load(
81                      CyrusVirtual.class, virtual.getEmailAddress());
82  
83                  virtualModel.setUserId(virtual.getUserId());
84  
85                  session.flush();
86              }
87              catch (ObjectNotFoundException onfe) {
88                  CyrusVirtual virtualModel = new CyrusVirtual(
89                      virtual.getEmailAddress(), virtual.getUserId());
90  
91                  session.save(virtualModel);
92  
93                  session.flush();
94              }
95          }
96          catch (Exception e) {
97              throw processException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public CyrusVirtual findByPrimaryKey(String emailAddress)
105         throws NoSuchCyrusVirtualException, SystemException {
106 
107         Session session = null;
108 
109         try {
110             session = openSession();
111 
112             return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
113         }
114         catch (ObjectNotFoundException onfe) {
115             throw new NoSuchCyrusVirtualException();
116         }
117         catch (Exception e) {
118             throw processException(e);
119         }
120         finally {
121             closeSession(session);
122         }
123     }
124 
125     public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             Query q = session.createQuery(FIND_BY_USER_ID);
132 
133             q.setString(0, String.valueOf(userId));
134 
135             return q.list();
136         }
137         catch (Exception e) {
138             throw processException(e);
139         }
140         finally {
141             closeSession(session);
142         }
143     }
144 
145     public void removeByUserId(long userId) throws SystemException {
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             Query q = session.createQuery(FIND_BY_USER_ID);
152 
153             q.setString(0, String.valueOf(userId));
154 
155             Iterator<CyrusVirtual> itr = q.iterate();
156 
157             while (itr.hasNext()) {
158                 CyrusVirtual virtual = itr.next();
159 
160                 session.delete(virtual);
161             }
162 
163             closeSession(session);
164         }
165         catch (Exception e) {
166             throw processException(e);
167         }
168         finally {
169             closeSession(session);
170         }
171     }
172 
173 }