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