1
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
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<CyrusVirtual> 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<CyrusVirtual> itr = q.iterate();
158
159 while (itr.hasNext()) {
160 CyrusVirtual virtual = 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 }