001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchAccountException;
018 import com.liferay.portal.NoSuchModelException;
019 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderPath;
023 import com.liferay.portal.kernel.dao.orm.Query;
024 import com.liferay.portal.kernel.dao.orm.QueryUtil;
025 import com.liferay.portal.kernel.dao.orm.Session;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.log.Log;
028 import com.liferay.portal.kernel.log.LogFactoryUtil;
029 import com.liferay.portal.kernel.util.GetterUtil;
030 import com.liferay.portal.kernel.util.InstanceFactory;
031 import com.liferay.portal.kernel.util.OrderByComparator;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringUtil;
034 import com.liferay.portal.kernel.util.UnmodifiableList;
035 import com.liferay.portal.model.Account;
036 import com.liferay.portal.model.CacheModel;
037 import com.liferay.portal.model.ModelListener;
038 import com.liferay.portal.model.impl.AccountImpl;
039 import com.liferay.portal.model.impl.AccountModelImpl;
040 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041
042 import java.io.Serializable;
043
044 import java.util.ArrayList;
045 import java.util.Collections;
046 import java.util.List;
047
048
060 public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
061 implements AccountPersistence {
062
067 public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
068 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
069 ".List1";
070 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071 ".List2";
072 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
073 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
074 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
075 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
076 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
077 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
078 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
079 AccountModelImpl.FINDER_CACHE_ENABLED, Long.class,
080 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
081
082
087 public void cacheResult(Account account) {
088 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
089 AccountImpl.class, account.getPrimaryKey(), account);
090
091 account.resetOriginalValues();
092 }
093
094
099 public void cacheResult(List<Account> accounts) {
100 for (Account account : accounts) {
101 if (EntityCacheUtil.getResult(
102 AccountModelImpl.ENTITY_CACHE_ENABLED,
103 AccountImpl.class, account.getPrimaryKey()) == null) {
104 cacheResult(account);
105 }
106 else {
107 account.resetOriginalValues();
108 }
109 }
110 }
111
112
119 @Override
120 public void clearCache() {
121 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
122 CacheRegistryUtil.clear(AccountImpl.class.getName());
123 }
124
125 EntityCacheUtil.clearCache(AccountImpl.class.getName());
126
127 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
128 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
129 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
130 }
131
132
139 @Override
140 public void clearCache(Account account) {
141 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
142 AccountImpl.class, account.getPrimaryKey());
143
144 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
145 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
146 }
147
148 @Override
149 public void clearCache(List<Account> accounts) {
150 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
151 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
152
153 for (Account account : accounts) {
154 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
155 AccountImpl.class, account.getPrimaryKey());
156 }
157 }
158
159
165 public Account create(long accountId) {
166 Account account = new AccountImpl();
167
168 account.setNew(true);
169 account.setPrimaryKey(accountId);
170
171 return account;
172 }
173
174
182 public Account remove(long accountId)
183 throws NoSuchAccountException, SystemException {
184 return remove(Long.valueOf(accountId));
185 }
186
187
195 @Override
196 public Account remove(Serializable primaryKey)
197 throws NoSuchAccountException, SystemException {
198 Session session = null;
199
200 try {
201 session = openSession();
202
203 Account account = (Account)session.get(AccountImpl.class, primaryKey);
204
205 if (account == null) {
206 if (_log.isWarnEnabled()) {
207 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
208 }
209
210 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
211 primaryKey);
212 }
213
214 return remove(account);
215 }
216 catch (NoSuchAccountException nsee) {
217 throw nsee;
218 }
219 catch (Exception e) {
220 throw processException(e);
221 }
222 finally {
223 closeSession(session);
224 }
225 }
226
227 @Override
228 protected Account removeImpl(Account account) throws SystemException {
229 account = toUnwrappedModel(account);
230
231 Session session = null;
232
233 try {
234 session = openSession();
235
236 if (!session.contains(account)) {
237 account = (Account)session.get(AccountImpl.class,
238 account.getPrimaryKeyObj());
239 }
240
241 if (account != null) {
242 session.delete(account);
243 }
244 }
245 catch (Exception e) {
246 throw processException(e);
247 }
248 finally {
249 closeSession(session);
250 }
251
252 if (account != null) {
253 clearCache(account);
254 }
255
256 return account;
257 }
258
259 @Override
260 public Account updateImpl(com.liferay.portal.model.Account account)
261 throws SystemException {
262 account = toUnwrappedModel(account);
263
264 boolean isNew = account.isNew();
265
266 Session session = null;
267
268 try {
269 session = openSession();
270
271 if (account.isNew()) {
272 session.save(account);
273
274 account.setNew(false);
275 }
276 else {
277 session.merge(account);
278 }
279 }
280 catch (Exception e) {
281 throw processException(e);
282 }
283 finally {
284 closeSession(session);
285 }
286
287 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
288
289 if (isNew) {
290 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
291 }
292
293 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
294 AccountImpl.class, account.getPrimaryKey(), account);
295
296 return account;
297 }
298
299 protected Account toUnwrappedModel(Account account) {
300 if (account instanceof AccountImpl) {
301 return account;
302 }
303
304 AccountImpl accountImpl = new AccountImpl();
305
306 accountImpl.setNew(account.isNew());
307 accountImpl.setPrimaryKey(account.getPrimaryKey());
308
309 accountImpl.setAccountId(account.getAccountId());
310 accountImpl.setCompanyId(account.getCompanyId());
311 accountImpl.setUserId(account.getUserId());
312 accountImpl.setUserName(account.getUserName());
313 accountImpl.setCreateDate(account.getCreateDate());
314 accountImpl.setModifiedDate(account.getModifiedDate());
315 accountImpl.setParentAccountId(account.getParentAccountId());
316 accountImpl.setName(account.getName());
317 accountImpl.setLegalName(account.getLegalName());
318 accountImpl.setLegalId(account.getLegalId());
319 accountImpl.setLegalType(account.getLegalType());
320 accountImpl.setSicCode(account.getSicCode());
321 accountImpl.setTickerSymbol(account.getTickerSymbol());
322 accountImpl.setIndustry(account.getIndustry());
323 accountImpl.setType(account.getType());
324 accountImpl.setSize(account.getSize());
325
326 return accountImpl;
327 }
328
329
337 @Override
338 public Account findByPrimaryKey(Serializable primaryKey)
339 throws NoSuchModelException, SystemException {
340 return findByPrimaryKey(((Long)primaryKey).longValue());
341 }
342
343
351 public Account findByPrimaryKey(long accountId)
352 throws NoSuchAccountException, SystemException {
353 Account account = fetchByPrimaryKey(accountId);
354
355 if (account == null) {
356 if (_log.isWarnEnabled()) {
357 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + accountId);
358 }
359
360 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
361 accountId);
362 }
363
364 return account;
365 }
366
367
374 @Override
375 public Account fetchByPrimaryKey(Serializable primaryKey)
376 throws SystemException {
377 return fetchByPrimaryKey(((Long)primaryKey).longValue());
378 }
379
380
387 public Account fetchByPrimaryKey(long accountId) throws SystemException {
388 Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
389 AccountImpl.class, accountId);
390
391 if (account == _nullAccount) {
392 return null;
393 }
394
395 if (account == null) {
396 Session session = null;
397
398 try {
399 session = openSession();
400
401 account = (Account)session.get(AccountImpl.class,
402 Long.valueOf(accountId));
403
404 if (account != null) {
405 cacheResult(account);
406 }
407 else {
408 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
409 AccountImpl.class, accountId, _nullAccount);
410 }
411 }
412 catch (Exception e) {
413 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
414 AccountImpl.class, accountId);
415
416 throw processException(e);
417 }
418 finally {
419 closeSession(session);
420 }
421 }
422
423 return account;
424 }
425
426
432 public List<Account> findAll() throws SystemException {
433 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
434 }
435
436
448 public List<Account> findAll(int start, int end) throws SystemException {
449 return findAll(start, end, null);
450 }
451
452
465 public List<Account> findAll(int start, int end,
466 OrderByComparator orderByComparator) throws SystemException {
467 boolean pagination = true;
468 FinderPath finderPath = null;
469 Object[] finderArgs = null;
470
471 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
472 (orderByComparator == null)) {
473 pagination = false;
474 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
475 finderArgs = FINDER_ARGS_EMPTY;
476 }
477 else {
478 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
479 finderArgs = new Object[] { start, end, orderByComparator };
480 }
481
482 List<Account> list = (List<Account>)FinderCacheUtil.getResult(finderPath,
483 finderArgs, this);
484
485 if (list == null) {
486 StringBundler query = null;
487 String sql = null;
488
489 if (orderByComparator != null) {
490 query = new StringBundler(2 +
491 (orderByComparator.getOrderByFields().length * 3));
492
493 query.append(_SQL_SELECT_ACCOUNT);
494
495 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
496 orderByComparator);
497
498 sql = query.toString();
499 }
500 else {
501 sql = _SQL_SELECT_ACCOUNT;
502
503 if (pagination) {
504 sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
505 }
506 }
507
508 Session session = null;
509
510 try {
511 session = openSession();
512
513 Query q = session.createQuery(sql);
514
515 if (!pagination) {
516 list = (List<Account>)QueryUtil.list(q, getDialect(),
517 start, end, false);
518
519 Collections.sort(list);
520
521 list = new UnmodifiableList<Account>(list);
522 }
523 else {
524 list = (List<Account>)QueryUtil.list(q, getDialect(),
525 start, end);
526 }
527
528 cacheResult(list);
529
530 FinderCacheUtil.putResult(finderPath, finderArgs, list);
531 }
532 catch (Exception e) {
533 FinderCacheUtil.removeResult(finderPath, finderArgs);
534
535 throw processException(e);
536 }
537 finally {
538 closeSession(session);
539 }
540 }
541
542 return list;
543 }
544
545
550 public void removeAll() throws SystemException {
551 for (Account account : findAll()) {
552 remove(account);
553 }
554 }
555
556
562 public int countAll() throws SystemException {
563 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
564 FINDER_ARGS_EMPTY, this);
565
566 if (count == null) {
567 Session session = null;
568
569 try {
570 session = openSession();
571
572 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
573
574 count = (Long)q.uniqueResult();
575
576 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
577 FINDER_ARGS_EMPTY, count);
578 }
579 catch (Exception e) {
580 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
581 FINDER_ARGS_EMPTY);
582
583 throw processException(e);
584 }
585 finally {
586 closeSession(session);
587 }
588 }
589
590 return count.intValue();
591 }
592
593
596 public void afterPropertiesSet() {
597 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
598 com.liferay.portal.util.PropsUtil.get(
599 "value.object.listener.com.liferay.portal.model.Account")));
600
601 if (listenerClassNames.length > 0) {
602 try {
603 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
604
605 for (String listenerClassName : listenerClassNames) {
606 listenersList.add((ModelListener<Account>)InstanceFactory.newInstance(
607 listenerClassName));
608 }
609
610 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
611 }
612 catch (Exception e) {
613 _log.error(e);
614 }
615 }
616 }
617
618 public void destroy() {
619 EntityCacheUtil.removeCache(AccountImpl.class.getName());
620 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
621 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
622 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
623 }
624
625 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
626 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
627 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
628 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
629 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
630 private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
631 private static Account _nullAccount = new AccountImpl() {
632 @Override
633 public Object clone() {
634 return this;
635 }
636
637 @Override
638 public CacheModel<Account> toCacheModel() {
639 return _nullAccountCacheModel;
640 }
641 };
642
643 private static CacheModel<Account> _nullAccountCacheModel = new CacheModel<Account>() {
644 public Account toEntityModel() {
645 return _nullAccount;
646 }
647 };
648 }