1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchAccountException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.dao.DynamicQuery;
28 import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29 import com.liferay.portal.kernel.util.OrderByComparator;
30 import com.liferay.portal.kernel.util.StringMaker;
31 import com.liferay.portal.model.Account;
32 import com.liferay.portal.model.impl.AccountImpl;
33 import com.liferay.portal.service.persistence.BasePersistence;
34 import com.liferay.portal.spring.hibernate.FinderCache;
35 import com.liferay.portal.spring.hibernate.HibernateUtil;
36
37 import com.liferay.util.dao.hibernate.QueryUtil;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 import org.hibernate.Query;
43 import org.hibernate.Session;
44
45 import java.util.Collections;
46 import java.util.Iterator;
47 import java.util.List;
48
49
55 public class AccountPersistenceImpl extends BasePersistence
56 implements AccountPersistence {
57 public Account create(long accountId) {
58 Account account = new AccountImpl();
59 account.setNew(true);
60 account.setPrimaryKey(accountId);
61
62 return account;
63 }
64
65 public Account remove(long accountId)
66 throws NoSuchAccountException, SystemException {
67 Session session = null;
68
69 try {
70 session = openSession();
71
72 Account account = (Account)session.get(AccountImpl.class,
73 new Long(accountId));
74
75 if (account == null) {
76 if (_log.isWarnEnabled()) {
77 _log.warn("No Account exists with the primary key " +
78 accountId);
79 }
80
81 throw new NoSuchAccountException(
82 "No Account exists with the primary key " + accountId);
83 }
84
85 return remove(account);
86 }
87 catch (NoSuchAccountException nsee) {
88 throw nsee;
89 }
90 catch (Exception e) {
91 throw HibernateUtil.processException(e);
92 }
93 finally {
94 closeSession(session);
95 }
96 }
97
98 public Account remove(Account account) throws SystemException {
99 Session session = null;
100
101 try {
102 session = openSession();
103 session.delete(account);
104 session.flush();
105
106 return account;
107 }
108 catch (Exception e) {
109 throw HibernateUtil.processException(e);
110 }
111 finally {
112 closeSession(session);
113 FinderCache.clearCache(Account.class.getName());
114 }
115 }
116
117 public Account update(com.liferay.portal.model.Account account)
118 throws SystemException {
119 return update(account, false);
120 }
121
122 public Account update(com.liferay.portal.model.Account account,
123 boolean merge) throws SystemException {
124 Session session = null;
125
126 try {
127 session = openSession();
128
129 if (merge) {
130 session.merge(account);
131 }
132 else {
133 if (account.isNew()) {
134 session.save(account);
135 }
136 }
137
138 session.flush();
139 account.setNew(false);
140
141 return account;
142 }
143 catch (Exception e) {
144 throw HibernateUtil.processException(e);
145 }
146 finally {
147 closeSession(session);
148 FinderCache.clearCache(Account.class.getName());
149 }
150 }
151
152 public Account findByPrimaryKey(long accountId)
153 throws NoSuchAccountException, SystemException {
154 Account account = fetchByPrimaryKey(accountId);
155
156 if (account == null) {
157 if (_log.isWarnEnabled()) {
158 _log.warn("No Account exists with the primary key " +
159 accountId);
160 }
161
162 throw new NoSuchAccountException(
163 "No Account exists with the primary key " + accountId);
164 }
165
166 return account;
167 }
168
169 public Account fetchByPrimaryKey(long accountId) throws SystemException {
170 Session session = null;
171
172 try {
173 session = openSession();
174
175 return (Account)session.get(AccountImpl.class, new Long(accountId));
176 }
177 catch (Exception e) {
178 throw HibernateUtil.processException(e);
179 }
180 finally {
181 closeSession(session);
182 }
183 }
184
185 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
186 throws SystemException {
187 Session session = null;
188
189 try {
190 session = openSession();
191
192 DynamicQuery query = queryInitializer.initialize(session);
193
194 return query.list();
195 }
196 catch (Exception e) {
197 throw HibernateUtil.processException(e);
198 }
199 finally {
200 closeSession(session);
201 }
202 }
203
204 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
205 int begin, int end) throws SystemException {
206 Session session = null;
207
208 try {
209 session = openSession();
210
211 DynamicQuery query = queryInitializer.initialize(session);
212 query.setLimit(begin, end);
213
214 return query.list();
215 }
216 catch (Exception e) {
217 throw HibernateUtil.processException(e);
218 }
219 finally {
220 closeSession(session);
221 }
222 }
223
224 public List findAll() throws SystemException {
225 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
226 }
227
228 public List findAll(int begin, int end) throws SystemException {
229 return findAll(begin, end, null);
230 }
231
232 public List findAll(int begin, int end, OrderByComparator obc)
233 throws SystemException {
234 String finderClassName = Account.class.getName();
235 String finderMethodName = "findAll";
236 String[] finderParams = new String[] {
237 "java.lang.Integer", "java.lang.Integer",
238 "com.liferay.portal.kernel.util.OrderByComparator"
239 };
240 Object[] finderArgs = new Object[] {
241 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
242 };
243 Object result = FinderCache.getResult(finderClassName,
244 finderMethodName, finderParams, finderArgs, getSessionFactory());
245
246 if (result == null) {
247 Session session = null;
248
249 try {
250 session = openSession();
251
252 StringMaker query = new StringMaker();
253 query.append("FROM com.liferay.portal.model.Account ");
254
255 if (obc != null) {
256 query.append("ORDER BY ");
257 query.append(obc.getOrderBy());
258 }
259
260 Query q = session.createQuery(query.toString());
261 List list = QueryUtil.list(q, getDialect(), begin, end);
262
263 if (obc == null) {
264 Collections.sort(list);
265 }
266
267 FinderCache.putResult(finderClassName, finderMethodName,
268 finderParams, finderArgs, list);
269
270 return list;
271 }
272 catch (Exception e) {
273 throw HibernateUtil.processException(e);
274 }
275 finally {
276 closeSession(session);
277 }
278 }
279 else {
280 return (List)result;
281 }
282 }
283
284 public void removeAll() throws SystemException {
285 Iterator itr = findAll().iterator();
286
287 while (itr.hasNext()) {
288 remove((Account)itr.next());
289 }
290 }
291
292 public int countAll() throws SystemException {
293 String finderClassName = Account.class.getName();
294 String finderMethodName = "countAll";
295 String[] finderParams = new String[] { };
296 Object[] finderArgs = new Object[] { };
297 Object result = FinderCache.getResult(finderClassName,
298 finderMethodName, finderParams, finderArgs, getSessionFactory());
299
300 if (result == null) {
301 Session session = null;
302
303 try {
304 session = openSession();
305
306 StringMaker query = new StringMaker();
307 query.append("SELECT COUNT(*) ");
308 query.append("FROM com.liferay.portal.model.Account");
309
310 Query q = session.createQuery(query.toString());
311 Long count = null;
312 Iterator itr = q.list().iterator();
313
314 if (itr.hasNext()) {
315 count = (Long)itr.next();
316 }
317
318 if (count == null) {
319 count = new Long(0);
320 }
321
322 FinderCache.putResult(finderClassName, finderMethodName,
323 finderParams, finderArgs, count);
324
325 return count.intValue();
326 }
327 catch (Exception e) {
328 throw HibernateUtil.processException(e);
329 }
330 finally {
331 closeSession(session);
332 }
333 }
334 else {
335 return ((Long)result).intValue();
336 }
337 }
338
339 protected void initDao() {
340 }
341
342 private static Log _log = LogFactory.getLog(AccountPersistenceImpl.class);
343 }