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.orm.DynamicQuery;
28 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29 import com.liferay.portal.kernel.dao.orm.Query;
30 import com.liferay.portal.kernel.dao.orm.QueryUtil;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.util.GetterUtil;
33 import com.liferay.portal.kernel.util.ListUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringUtil;
36 import com.liferay.portal.model.Account;
37 import com.liferay.portal.model.ModelListener;
38 import com.liferay.portal.model.impl.AccountImpl;
39 import com.liferay.portal.model.impl.AccountModelImpl;
40 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.List;
49
50
56 public class AccountPersistenceImpl extends BasePersistenceImpl
57 implements AccountPersistence {
58 public Account create(long accountId) {
59 Account account = new AccountImpl();
60
61 account.setNew(true);
62 account.setPrimaryKey(accountId);
63
64 return account;
65 }
66
67 public Account remove(long accountId)
68 throws NoSuchAccountException, SystemException {
69 Session session = null;
70
71 try {
72 session = openSession();
73
74 Account account = (Account)session.get(AccountImpl.class,
75 new Long(accountId));
76
77 if (account == null) {
78 if (_log.isWarnEnabled()) {
79 _log.warn("No Account exists with the primary key " +
80 accountId);
81 }
82
83 throw new NoSuchAccountException(
84 "No Account exists with the primary key " + accountId);
85 }
86
87 return remove(account);
88 }
89 catch (NoSuchAccountException nsee) {
90 throw nsee;
91 }
92 catch (Exception e) {
93 throw processException(e);
94 }
95 finally {
96 closeSession(session);
97 }
98 }
99
100 public Account remove(Account account) throws SystemException {
101 if (_listeners.length > 0) {
102 for (ModelListener listener : _listeners) {
103 listener.onBeforeRemove(account);
104 }
105 }
106
107 account = removeImpl(account);
108
109 if (_listeners.length > 0) {
110 for (ModelListener listener : _listeners) {
111 listener.onAfterRemove(account);
112 }
113 }
114
115 return account;
116 }
117
118 protected Account removeImpl(Account account) throws SystemException {
119 Session session = null;
120
121 try {
122 session = openSession();
123
124 session.delete(account);
125
126 session.flush();
127
128 return account;
129 }
130 catch (Exception e) {
131 throw processException(e);
132 }
133 finally {
134 closeSession(session);
135
136 FinderCacheUtil.clearCache(Account.class.getName());
137 }
138 }
139
140
143 public Account update(Account account) throws SystemException {
144 if (_log.isWarnEnabled()) {
145 _log.warn(
146 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
147 }
148
149 return update(account, false);
150 }
151
152
165 public Account update(Account account, boolean merge)
166 throws SystemException {
167 boolean isNew = account.isNew();
168
169 if (_listeners.length > 0) {
170 for (ModelListener listener : _listeners) {
171 if (isNew) {
172 listener.onBeforeCreate(account);
173 }
174 else {
175 listener.onBeforeUpdate(account);
176 }
177 }
178 }
179
180 account = updateImpl(account, merge);
181
182 if (_listeners.length > 0) {
183 for (ModelListener listener : _listeners) {
184 if (isNew) {
185 listener.onAfterCreate(account);
186 }
187 else {
188 listener.onAfterUpdate(account);
189 }
190 }
191 }
192
193 return account;
194 }
195
196 public Account updateImpl(com.liferay.portal.model.Account account,
197 boolean merge) throws SystemException {
198 Session session = null;
199
200 try {
201 session = openSession();
202
203 if (merge) {
204 session.merge(account);
205 }
206 else {
207 if (account.isNew()) {
208 session.save(account);
209 }
210 }
211
212 session.flush();
213
214 account.setNew(false);
215
216 return account;
217 }
218 catch (Exception e) {
219 throw processException(e);
220 }
221 finally {
222 closeSession(session);
223
224 FinderCacheUtil.clearCache(Account.class.getName());
225 }
226 }
227
228 public Account findByPrimaryKey(long accountId)
229 throws NoSuchAccountException, SystemException {
230 Account account = fetchByPrimaryKey(accountId);
231
232 if (account == null) {
233 if (_log.isWarnEnabled()) {
234 _log.warn("No Account exists with the primary key " +
235 accountId);
236 }
237
238 throw new NoSuchAccountException(
239 "No Account exists with the primary key " + accountId);
240 }
241
242 return account;
243 }
244
245 public Account fetchByPrimaryKey(long accountId) throws SystemException {
246 Session session = null;
247
248 try {
249 session = openSession();
250
251 return (Account)session.get(AccountImpl.class, new Long(accountId));
252 }
253 catch (Exception e) {
254 throw processException(e);
255 }
256 finally {
257 closeSession(session);
258 }
259 }
260
261 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
262 throws SystemException {
263 Session session = null;
264
265 try {
266 session = openSession();
267
268 dynamicQuery.compile(session);
269
270 return dynamicQuery.list();
271 }
272 catch (Exception e) {
273 throw processException(e);
274 }
275 finally {
276 closeSession(session);
277 }
278 }
279
280 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
281 int start, int end) throws SystemException {
282 Session session = null;
283
284 try {
285 session = openSession();
286
287 dynamicQuery.setLimit(start, end);
288
289 dynamicQuery.compile(session);
290
291 return dynamicQuery.list();
292 }
293 catch (Exception e) {
294 throw processException(e);
295 }
296 finally {
297 closeSession(session);
298 }
299 }
300
301 public List<Account> findAll() throws SystemException {
302 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
303 }
304
305 public List<Account> findAll(int start, int end) throws SystemException {
306 return findAll(start, end, null);
307 }
308
309 public List<Account> findAll(int start, int end, OrderByComparator obc)
310 throws SystemException {
311 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
312 String finderClassName = Account.class.getName();
313 String finderMethodName = "findAll";
314 String[] finderParams = new String[] {
315 "java.lang.Integer", "java.lang.Integer",
316 "com.liferay.portal.kernel.util.OrderByComparator"
317 };
318 Object[] finderArgs = new Object[] {
319 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
320 };
321
322 Object result = null;
323
324 if (finderClassNameCacheEnabled) {
325 result = FinderCacheUtil.getResult(finderClassName,
326 finderMethodName, finderParams, finderArgs, this);
327 }
328
329 if (result == null) {
330 Session session = null;
331
332 try {
333 session = openSession();
334
335 StringBuilder query = new StringBuilder();
336
337 query.append("FROM com.liferay.portal.model.Account ");
338
339 if (obc != null) {
340 query.append("ORDER BY ");
341 query.append(obc.getOrderBy());
342 }
343
344 Query q = session.createQuery(query.toString());
345
346 List<Account> list = (List<Account>)QueryUtil.list(q,
347 getDialect(), start, end);
348
349 if (obc == null) {
350 Collections.sort(list);
351 }
352
353 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
354 finderClassName, finderMethodName, finderParams,
355 finderArgs, list);
356
357 return list;
358 }
359 catch (Exception e) {
360 throw processException(e);
361 }
362 finally {
363 closeSession(session);
364 }
365 }
366 else {
367 return (List<Account>)result;
368 }
369 }
370
371 public void removeAll() throws SystemException {
372 for (Account account : findAll()) {
373 remove(account);
374 }
375 }
376
377 public int countAll() throws SystemException {
378 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
379 String finderClassName = Account.class.getName();
380 String finderMethodName = "countAll";
381 String[] finderParams = new String[] { };
382 Object[] finderArgs = new Object[] { };
383
384 Object result = null;
385
386 if (finderClassNameCacheEnabled) {
387 result = FinderCacheUtil.getResult(finderClassName,
388 finderMethodName, finderParams, finderArgs, this);
389 }
390
391 if (result == null) {
392 Session session = null;
393
394 try {
395 session = openSession();
396
397 Query q = session.createQuery(
398 "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
399
400 Long count = null;
401
402 Iterator<Long> itr = q.list().iterator();
403
404 if (itr.hasNext()) {
405 count = itr.next();
406 }
407
408 if (count == null) {
409 count = new Long(0);
410 }
411
412 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
413 finderClassName, finderMethodName, finderParams,
414 finderArgs, count);
415
416 return count.intValue();
417 }
418 catch (Exception e) {
419 throw processException(e);
420 }
421 finally {
422 closeSession(session);
423 }
424 }
425 else {
426 return ((Long)result).intValue();
427 }
428 }
429
430 public void registerListener(ModelListener listener) {
431 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
432
433 listeners.add(listener);
434
435 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
436 }
437
438 public void unregisterListener(ModelListener listener) {
439 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
440
441 listeners.remove(listener);
442
443 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
444 }
445
446 public void afterPropertiesSet() {
447 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
448 com.liferay.portal.util.PropsUtil.get(
449 "value.object.listener.com.liferay.portal.model.Account")));
450
451 if (listenerClassNames.length > 0) {
452 try {
453 List<ModelListener> listeners = new ArrayList<ModelListener>();
454
455 for (String listenerClassName : listenerClassNames) {
456 listeners.add((ModelListener)Class.forName(
457 listenerClassName).newInstance());
458 }
459
460 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
461 }
462 catch (Exception e) {
463 _log.error(e);
464 }
465 }
466 }
467
468 private static Log _log = LogFactory.getLog(AccountPersistenceImpl.class);
469 private ModelListener[] _listeners = new ModelListener[0];
470 }