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.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  public class CyrusVirtualPersistence extends BasePersistenceImpl {
42  
43      public static String FIND_BY_USER_ID =
44          "SELECT cyrusVirtual FROM CyrusVirtual cyrusVirtual WHERE userId = ?";
45  
46      public void remove(String emailAddress)
47          throws NoSuchCyrusVirtualException, SystemException {
48  
49          Session session = null;
50  
51          try {
52              session = openSession();
53  
54              CyrusVirtual virtual = (CyrusVirtual)session.load(
55                  CyrusVirtual.class, emailAddress);
56  
57              session.delete(virtual);
58  
59              session.flush();
60          }
61          catch (ObjectNotFoundException onfe) {
62              throw new NoSuchCyrusVirtualException();
63          }
64          catch (Exception e) {
65              throw processException(e);
66          }
67          finally {
68              closeSession(session);
69          }
70      }
71  
72      public void update(CyrusVirtual virtual) throws SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              try {
79                  CyrusVirtual virtualModel = (CyrusVirtual)session.load(
80                      CyrusVirtual.class, virtual.getEmailAddress());
81  
82                  virtualModel.setUserId(virtual.getUserId());
83  
84                  session.flush();
85              }
86              catch (ObjectNotFoundException onfe) {
87                  CyrusVirtual virtualModel = new CyrusVirtual(
88                      virtual.getEmailAddress(), virtual.getUserId());
89  
90                  session.save(virtualModel);
91  
92                  session.flush();
93              }
94          }
95          catch (Exception e) {
96              throw processException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public CyrusVirtual findByPrimaryKey(String emailAddress)
104         throws NoSuchCyrusVirtualException, SystemException {
105 
106         Session session = null;
107 
108         try {
109             session = openSession();
110 
111             return (CyrusVirtual)session.load(CyrusVirtual.class, emailAddress);
112         }
113         catch (ObjectNotFoundException onfe) {
114             throw new NoSuchCyrusVirtualException();
115         }
116         catch (Exception e) {
117             throw processException(e);
118         }
119         finally {
120             closeSession(session);
121         }
122     }
123 
124     public List<CyrusVirtual> findByUserId(long userId) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             Query q = session.createQuery(FIND_BY_USER_ID);
131 
132             q.setString(0, String.valueOf(userId));
133 
134             return q.list();
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             closeSession(session);
141         }
142     }
143 
144     public void removeByUserId(long userId) throws SystemException {
145         Session session = null;
146 
147         try {
148             session = openSession();
149 
150             Query q = session.createQuery(FIND_BY_USER_ID);
151 
152             q.setString(0, String.valueOf(userId));
153 
154             Iterator<CyrusVirtual> itr = q.iterate();
155 
156             while (itr.hasNext()) {
157                 CyrusVirtual virtual = itr.next();
158 
159                 session.delete(virtual);
160             }
161 
162             closeSession(session);
163         }
164         catch (Exception e) {
165             throw processException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172 }