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 if (BatchSessionUtil.isEnabled()) {
125 Object staleObject = session.get(AccountImpl.class,
126 account.getPrimaryKeyObj());
127
128 if (staleObject != null) {
129 session.evict(staleObject);
130 }
131 }
132
133 session.delete(account);
134
135 session.flush();
136
137 return account;
138 }
139 catch (Exception e) {
140 throw processException(e);
141 }
142 finally {
143 closeSession(session);
144
145 FinderCacheUtil.clearCache(Account.class.getName());
146 }
147 }
148
149
152 public Account update(Account account) throws SystemException {
153 if (_log.isWarnEnabled()) {
154 _log.warn(
155 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
156 }
157
158 return update(account, false);
159 }
160
161
174 public Account update(Account account, boolean merge)
175 throws SystemException {
176 boolean isNew = account.isNew();
177
178 if (_listeners.length > 0) {
179 for (ModelListener listener : _listeners) {
180 if (isNew) {
181 listener.onBeforeCreate(account);
182 }
183 else {
184 listener.onBeforeUpdate(account);
185 }
186 }
187 }
188
189 account = updateImpl(account, merge);
190
191 if (_listeners.length > 0) {
192 for (ModelListener listener : _listeners) {
193 if (isNew) {
194 listener.onAfterCreate(account);
195 }
196 else {
197 listener.onAfterUpdate(account);
198 }
199 }
200 }
201
202 return account;
203 }
204
205 public Account updateImpl(com.liferay.portal.model.Account account,
206 boolean merge) throws SystemException {
207 Session session = null;
208
209 try {
210 session = openSession();
211
212 BatchSessionUtil.update(session, account, merge);
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 = null;
347
348 if (obc == null) {
349 list = (List<Account>)QueryUtil.list(q, getDialect(),
350 start, end, false);
351
352 Collections.sort(list);
353 }
354 else {
355 list = (List<Account>)QueryUtil.list(q, getDialect(),
356 start, end);
357 }
358
359 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
360 finderClassName, finderMethodName, finderParams,
361 finderArgs, list);
362
363 return list;
364 }
365 catch (Exception e) {
366 throw processException(e);
367 }
368 finally {
369 closeSession(session);
370 }
371 }
372 else {
373 return (List<Account>)result;
374 }
375 }
376
377 public void removeAll() throws SystemException {
378 for (Account account : findAll()) {
379 remove(account);
380 }
381 }
382
383 public int countAll() throws SystemException {
384 boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
385 String finderClassName = Account.class.getName();
386 String finderMethodName = "countAll";
387 String[] finderParams = new String[] { };
388 Object[] finderArgs = new Object[] { };
389
390 Object result = null;
391
392 if (finderClassNameCacheEnabled) {
393 result = FinderCacheUtil.getResult(finderClassName,
394 finderMethodName, finderParams, finderArgs, this);
395 }
396
397 if (result == null) {
398 Session session = null;
399
400 try {
401 session = openSession();
402
403 Query q = session.createQuery(
404 "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
405
406 Long count = null;
407
408 Iterator<Long> itr = q.list().iterator();
409
410 if (itr.hasNext()) {
411 count = itr.next();
412 }
413
414 if (count == null) {
415 count = new Long(0);
416 }
417
418 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
419 finderClassName, finderMethodName, finderParams,
420 finderArgs, count);
421
422 return count.intValue();
423 }
424 catch (Exception e) {
425 throw processException(e);
426 }
427 finally {
428 closeSession(session);
429 }
430 }
431 else {
432 return ((Long)result).intValue();
433 }
434 }
435
436 public void registerListener(ModelListener listener) {
437 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
438
439 listeners.add(listener);
440
441 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
442 }
443
444 public void unregisterListener(ModelListener listener) {
445 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
446
447 listeners.remove(listener);
448
449 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
450 }
451
452 public void afterPropertiesSet() {
453 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
454 com.liferay.portal.util.PropsUtil.get(
455 "value.object.listener.com.liferay.portal.model.Account")));
456
457 if (listenerClassNames.length > 0) {
458 try {
459 List<ModelListener> listeners = new ArrayList<ModelListener>();
460
461 for (String listenerClassName : listenerClassNames) {
462 listeners.add((ModelListener)Class.forName(
463 listenerClassName).newInstance());
464 }
465
466 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
467 }
468 catch (Exception e) {
469 _log.error(e);
470 }
471 }
472 }
473
474 private static Log _log = LogFactory.getLog(AccountPersistenceImpl.class);
475 private ModelListener[] _listeners = new ModelListener[0];
476 }