1   /**
2    * Copyright (c) 2000-2007 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.service.persistence.BasePersistence;
29  import com.liferay.portal.spring.hibernate.HibernateUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.hibernate.ObjectNotFoundException;
35  import org.hibernate.Query;
36  import org.hibernate.Session;
37  
38  /**
39   * <a href="CyrusVirtualPersistence.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class CyrusVirtualPersistence extends BasePersistence {
45  
46      public static String FIND_BY_USER_ID =
47          "FROM " + CyrusVirtual.class.getName() + " WHERE userId = ?";
48  
49      public void remove(String emailAddress)
50          throws NoSuchCyrusVirtualException, SystemException {
51  
52          Session session = null;
53  
54          try {
55              session = getSessionFactory().openSession();
56  
57              CyrusVirtual virtual = (CyrusVirtual)session.load(
58                  CyrusVirtual.class, emailAddress);
59  
60              session.delete(virtual);
61  
62              session.flush();
63          }
64          catch (ObjectNotFoundException onfe) {
65              throw new NoSuchCyrusVirtualException();
66          }
67          catch (Exception e) {
68              throw HibernateUtil.processException(e);
69          }
70          finally {
71              session.close();
72          }
73      }
74  
75      public void update(CyrusVirtual virtual) throws SystemException {
76          Session session = null;
77  
78          try {
79              session = getSessionFactory().openSession();
80  
81              try {
82                  CyrusVirtual virtualModel = (CyrusVirtual)session.load(
83                      CyrusVirtual.class, virtual.getEmailAddress());
84  
85                  virtualModel.setUserId(virtual.getUserId());
86  
87                  session.flush();
88              }
89              catch (ObjectNotFoundException onfe) {
90                  CyrusVirtual virtualModel = new CyrusVirtual(
91                      virtual.getEmailAddress(), virtual.getUserId());
92  
93                  session.save(virtualModel);
94  
95                  session.flush();
96              }
97          }
98          catch (Exception e) {
99              throw HibernateUtil.processException(e);
100         }
101         finally {
102             session.close();
103         }
104     }
105 
106     public CyrusVirtual findByPrimaryKey(String emailAddress)
107         throws NoSuchCyrusVirtualException, SystemException {
108 
109         Session session = null;
110 
111         try {
112             session = getSessionFactory().openSession();
113 
114             return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
115         }
116         catch (ObjectNotFoundException onfe) {
117             throw new NoSuchCyrusVirtualException();
118         }
119         catch (Exception e) {
120             throw HibernateUtil.processException(e);
121         }
122         finally {
123             session.close();
124         }
125     }
126 
127     public List findByUserId(long userId) throws SystemException {
128         Session session = null;
129 
130         try {
131             session = getSessionFactory().openSession();
132 
133             Query q = session.createQuery(FIND_BY_USER_ID);
134 
135             q.setString(0, String.valueOf(userId));
136 
137             return q.list();
138         }
139         catch (Exception e) {
140             throw HibernateUtil.processException(e);
141         }
142         finally {
143             session.close();
144         }
145     }
146 
147     public void removeByUserId(long userId) throws SystemException {
148         Session session = null;
149 
150         try {
151             session = getSessionFactory().openSession();
152 
153             Query q = session.createQuery(FIND_BY_USER_ID);
154 
155             q.setString(0, String.valueOf(userId));
156 
157             Iterator itr = q.iterate();
158 
159             while (itr.hasNext()) {
160                 CyrusVirtual virtual = (CyrusVirtual)itr.next();
161 
162                 session.delete(virtual);
163             }
164 
165             session.flush();
166         }
167         catch (Exception e) {
168             throw HibernateUtil.processException(e);
169         }
170         finally {
171             session.close();
172         }
173     }
174 
175 }