001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.portal.NoSuchAccountException;
020 import com.liferay.portal.kernel.dao.orm.EntityCache;
021 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderCache;
023 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024 import com.liferay.portal.kernel.dao.orm.FinderPath;
025 import com.liferay.portal.kernel.dao.orm.Query;
026 import com.liferay.portal.kernel.dao.orm.QueryUtil;
027 import com.liferay.portal.kernel.dao.orm.Session;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.util.OrderByComparator;
031 import com.liferay.portal.kernel.util.SetUtil;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.model.Account;
035 import com.liferay.portal.model.CacheModel;
036 import com.liferay.portal.model.MVCCModel;
037 import com.liferay.portal.model.impl.AccountImpl;
038 import com.liferay.portal.model.impl.AccountModelImpl;
039 import com.liferay.portal.service.ServiceContext;
040 import com.liferay.portal.service.ServiceContextThreadLocal;
041 import com.liferay.portal.service.persistence.AccountPersistence;
042
043 import java.io.Serializable;
044
045 import java.util.Collections;
046 import java.util.Date;
047 import java.util.HashMap;
048 import java.util.HashSet;
049 import java.util.Iterator;
050 import java.util.List;
051 import java.util.Map;
052 import java.util.Set;
053
054
066 @ProviderType
067 public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
068 implements AccountPersistence {
069
074 public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
075 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076 ".List1";
077 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
078 ".List2";
079 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
080 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
081 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
082 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
083 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
084 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
085 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
086 AccountModelImpl.FINDER_CACHE_ENABLED, Long.class,
087 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
088
089 public AccountPersistenceImpl() {
090 setModelClass(Account.class);
091 }
092
093
098 @Override
099 public void cacheResult(Account account) {
100 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
101 AccountImpl.class, account.getPrimaryKey(), account);
102
103 account.resetOriginalValues();
104 }
105
106
111 @Override
112 public void cacheResult(List<Account> accounts) {
113 for (Account account : accounts) {
114 if (entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
115 AccountImpl.class, account.getPrimaryKey()) == null) {
116 cacheResult(account);
117 }
118 else {
119 account.resetOriginalValues();
120 }
121 }
122 }
123
124
131 @Override
132 public void clearCache() {
133 entityCache.clearCache(AccountImpl.class);
134
135 finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
136 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
137 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
138 }
139
140
147 @Override
148 public void clearCache(Account account) {
149 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
150 AccountImpl.class, account.getPrimaryKey());
151
152 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
153 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
154 }
155
156 @Override
157 public void clearCache(List<Account> accounts) {
158 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
159 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
160
161 for (Account account : accounts) {
162 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
163 AccountImpl.class, account.getPrimaryKey());
164 }
165 }
166
167
173 @Override
174 public Account create(long accountId) {
175 Account account = new AccountImpl();
176
177 account.setNew(true);
178 account.setPrimaryKey(accountId);
179
180 return account;
181 }
182
183
190 @Override
191 public Account remove(long accountId) throws NoSuchAccountException {
192 return remove((Serializable)accountId);
193 }
194
195
202 @Override
203 public Account remove(Serializable primaryKey)
204 throws NoSuchAccountException {
205 Session session = null;
206
207 try {
208 session = openSession();
209
210 Account account = (Account)session.get(AccountImpl.class, primaryKey);
211
212 if (account == null) {
213 if (_log.isWarnEnabled()) {
214 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
215 }
216
217 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
218 primaryKey);
219 }
220
221 return remove(account);
222 }
223 catch (NoSuchAccountException nsee) {
224 throw nsee;
225 }
226 catch (Exception e) {
227 throw processException(e);
228 }
229 finally {
230 closeSession(session);
231 }
232 }
233
234 @Override
235 protected Account removeImpl(Account account) {
236 account = toUnwrappedModel(account);
237
238 Session session = null;
239
240 try {
241 session = openSession();
242
243 if (!session.contains(account)) {
244 account = (Account)session.get(AccountImpl.class,
245 account.getPrimaryKeyObj());
246 }
247
248 if (account != null) {
249 session.delete(account);
250 }
251 }
252 catch (Exception e) {
253 throw processException(e);
254 }
255 finally {
256 closeSession(session);
257 }
258
259 if (account != null) {
260 clearCache(account);
261 }
262
263 return account;
264 }
265
266 @Override
267 public Account updateImpl(Account account) {
268 account = toUnwrappedModel(account);
269
270 boolean isNew = account.isNew();
271
272 AccountModelImpl accountModelImpl = (AccountModelImpl)account;
273
274 ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
275
276 Date now = new Date();
277
278 if (isNew && (account.getCreateDate() == null)) {
279 if (serviceContext == null) {
280 account.setCreateDate(now);
281 }
282 else {
283 account.setCreateDate(serviceContext.getCreateDate(now));
284 }
285 }
286
287 if (!accountModelImpl.hasSetModifiedDate()) {
288 if (serviceContext == null) {
289 account.setModifiedDate(now);
290 }
291 else {
292 account.setModifiedDate(serviceContext.getModifiedDate(now));
293 }
294 }
295
296 Session session = null;
297
298 try {
299 session = openSession();
300
301 if (account.isNew()) {
302 session.save(account);
303
304 account.setNew(false);
305 }
306 else {
307 account = (Account)session.merge(account);
308 }
309 }
310 catch (Exception e) {
311 throw processException(e);
312 }
313 finally {
314 closeSession(session);
315 }
316
317 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
318
319 if (isNew) {
320 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
321 }
322
323 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
324 AccountImpl.class, account.getPrimaryKey(), account, false);
325
326 account.resetOriginalValues();
327
328 return account;
329 }
330
331 protected Account toUnwrappedModel(Account account) {
332 if (account instanceof AccountImpl) {
333 return account;
334 }
335
336 AccountImpl accountImpl = new AccountImpl();
337
338 accountImpl.setNew(account.isNew());
339 accountImpl.setPrimaryKey(account.getPrimaryKey());
340
341 accountImpl.setMvccVersion(account.getMvccVersion());
342 accountImpl.setAccountId(account.getAccountId());
343 accountImpl.setCompanyId(account.getCompanyId());
344 accountImpl.setUserId(account.getUserId());
345 accountImpl.setUserName(account.getUserName());
346 accountImpl.setCreateDate(account.getCreateDate());
347 accountImpl.setModifiedDate(account.getModifiedDate());
348 accountImpl.setParentAccountId(account.getParentAccountId());
349 accountImpl.setName(account.getName());
350 accountImpl.setLegalName(account.getLegalName());
351 accountImpl.setLegalId(account.getLegalId());
352 accountImpl.setLegalType(account.getLegalType());
353 accountImpl.setSicCode(account.getSicCode());
354 accountImpl.setTickerSymbol(account.getTickerSymbol());
355 accountImpl.setIndustry(account.getIndustry());
356 accountImpl.setType(account.getType());
357 accountImpl.setSize(account.getSize());
358
359 return accountImpl;
360 }
361
362
369 @Override
370 public Account findByPrimaryKey(Serializable primaryKey)
371 throws NoSuchAccountException {
372 Account account = fetchByPrimaryKey(primaryKey);
373
374 if (account == null) {
375 if (_log.isWarnEnabled()) {
376 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
377 }
378
379 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
380 primaryKey);
381 }
382
383 return account;
384 }
385
386
393 @Override
394 public Account findByPrimaryKey(long accountId)
395 throws NoSuchAccountException {
396 return findByPrimaryKey((Serializable)accountId);
397 }
398
399
405 @Override
406 public Account fetchByPrimaryKey(Serializable primaryKey) {
407 Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
408 AccountImpl.class, primaryKey);
409
410 if (account == _nullAccount) {
411 return null;
412 }
413
414 if (account == null) {
415 Session session = null;
416
417 try {
418 session = openSession();
419
420 account = (Account)session.get(AccountImpl.class, primaryKey);
421
422 if (account != null) {
423 cacheResult(account);
424 }
425 else {
426 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
427 AccountImpl.class, primaryKey, _nullAccount);
428 }
429 }
430 catch (Exception e) {
431 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
432 AccountImpl.class, primaryKey);
433
434 throw processException(e);
435 }
436 finally {
437 closeSession(session);
438 }
439 }
440
441 return account;
442 }
443
444
450 @Override
451 public Account fetchByPrimaryKey(long accountId) {
452 return fetchByPrimaryKey((Serializable)accountId);
453 }
454
455 @Override
456 public Map<Serializable, Account> fetchByPrimaryKeys(
457 Set<Serializable> primaryKeys) {
458 if (primaryKeys.isEmpty()) {
459 return Collections.emptyMap();
460 }
461
462 Map<Serializable, Account> map = new HashMap<Serializable, Account>();
463
464 if (primaryKeys.size() == 1) {
465 Iterator<Serializable> iterator = primaryKeys.iterator();
466
467 Serializable primaryKey = iterator.next();
468
469 Account account = fetchByPrimaryKey(primaryKey);
470
471 if (account != null) {
472 map.put(primaryKey, account);
473 }
474
475 return map;
476 }
477
478 Set<Serializable> uncachedPrimaryKeys = null;
479
480 for (Serializable primaryKey : primaryKeys) {
481 Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
482 AccountImpl.class, primaryKey);
483
484 if (account == null) {
485 if (uncachedPrimaryKeys == null) {
486 uncachedPrimaryKeys = new HashSet<Serializable>();
487 }
488
489 uncachedPrimaryKeys.add(primaryKey);
490 }
491 else {
492 map.put(primaryKey, account);
493 }
494 }
495
496 if (uncachedPrimaryKeys == null) {
497 return map;
498 }
499
500 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
501 1);
502
503 query.append(_SQL_SELECT_ACCOUNT_WHERE_PKS_IN);
504
505 for (Serializable primaryKey : uncachedPrimaryKeys) {
506 query.append(String.valueOf(primaryKey));
507
508 query.append(StringPool.COMMA);
509 }
510
511 query.setIndex(query.index() - 1);
512
513 query.append(StringPool.CLOSE_PARENTHESIS);
514
515 String sql = query.toString();
516
517 Session session = null;
518
519 try {
520 session = openSession();
521
522 Query q = session.createQuery(sql);
523
524 for (Account account : (List<Account>)q.list()) {
525 map.put(account.getPrimaryKeyObj(), account);
526
527 cacheResult(account);
528
529 uncachedPrimaryKeys.remove(account.getPrimaryKeyObj());
530 }
531
532 for (Serializable primaryKey : uncachedPrimaryKeys) {
533 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
534 AccountImpl.class, primaryKey, _nullAccount);
535 }
536 }
537 catch (Exception e) {
538 throw processException(e);
539 }
540 finally {
541 closeSession(session);
542 }
543
544 return map;
545 }
546
547
552 @Override
553 public List<Account> findAll() {
554 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
555 }
556
557
568 @Override
569 public List<Account> findAll(int start, int end) {
570 return findAll(start, end, null);
571 }
572
573
585 @Override
586 public List<Account> findAll(int start, int end,
587 OrderByComparator<Account> orderByComparator) {
588 return findAll(start, end, orderByComparator, true);
589 }
590
591
604 @Override
605 public List<Account> findAll(int start, int end,
606 OrderByComparator<Account> orderByComparator, boolean retrieveFromCache) {
607 boolean pagination = true;
608 FinderPath finderPath = null;
609 Object[] finderArgs = null;
610
611 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
612 (orderByComparator == null)) {
613 pagination = false;
614 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
615 finderArgs = FINDER_ARGS_EMPTY;
616 }
617 else {
618 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
619 finderArgs = new Object[] { start, end, orderByComparator };
620 }
621
622 List<Account> list = null;
623
624 if (retrieveFromCache) {
625 list = (List<Account>)finderCache.getResult(finderPath, finderArgs,
626 this);
627 }
628
629 if (list == null) {
630 StringBundler query = null;
631 String sql = null;
632
633 if (orderByComparator != null) {
634 query = new StringBundler(2 +
635 (orderByComparator.getOrderByFields().length * 3));
636
637 query.append(_SQL_SELECT_ACCOUNT);
638
639 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
640 orderByComparator);
641
642 sql = query.toString();
643 }
644 else {
645 sql = _SQL_SELECT_ACCOUNT;
646
647 if (pagination) {
648 sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
649 }
650 }
651
652 Session session = null;
653
654 try {
655 session = openSession();
656
657 Query q = session.createQuery(sql);
658
659 if (!pagination) {
660 list = (List<Account>)QueryUtil.list(q, getDialect(),
661 start, end, false);
662
663 Collections.sort(list);
664
665 list = Collections.unmodifiableList(list);
666 }
667 else {
668 list = (List<Account>)QueryUtil.list(q, getDialect(),
669 start, end);
670 }
671
672 cacheResult(list);
673
674 finderCache.putResult(finderPath, finderArgs, list);
675 }
676 catch (Exception e) {
677 finderCache.removeResult(finderPath, finderArgs);
678
679 throw processException(e);
680 }
681 finally {
682 closeSession(session);
683 }
684 }
685
686 return list;
687 }
688
689
693 @Override
694 public void removeAll() {
695 for (Account account : findAll()) {
696 remove(account);
697 }
698 }
699
700
705 @Override
706 public int countAll() {
707 Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
708 FINDER_ARGS_EMPTY, this);
709
710 if (count == null) {
711 Session session = null;
712
713 try {
714 session = openSession();
715
716 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
717
718 count = (Long)q.uniqueResult();
719
720 finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
721 count);
722 }
723 catch (Exception e) {
724 finderCache.removeResult(FINDER_PATH_COUNT_ALL,
725 FINDER_ARGS_EMPTY);
726
727 throw processException(e);
728 }
729 finally {
730 closeSession(session);
731 }
732 }
733
734 return count.intValue();
735 }
736
737 @Override
738 public Set<String> getBadColumnNames() {
739 return _badColumnNames;
740 }
741
742 @Override
743 protected Map<String, Integer> getTableColumnsMap() {
744 return AccountModelImpl.TABLE_COLUMNS_MAP;
745 }
746
747
750 public void afterPropertiesSet() {
751 }
752
753 public void destroy() {
754 entityCache.removeCache(AccountImpl.class.getName());
755 finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
756 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
757 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
758 }
759
760 protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
761 protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
762 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
763 private static final String _SQL_SELECT_ACCOUNT_WHERE_PKS_IN = "SELECT account FROM Account account WHERE accountId IN (";
764 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
765 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
766 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
767 private static final Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
768 private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
769 "type", "size"
770 });
771 private static final Account _nullAccount = new AccountImpl() {
772 @Override
773 public Object clone() {
774 return this;
775 }
776
777 @Override
778 public CacheModel<Account> toCacheModel() {
779 return _nullAccountCacheModel;
780 }
781 };
782
783 private static final CacheModel<Account> _nullAccountCacheModel = new NullCacheModel();
784
785 private static class NullCacheModel implements CacheModel<Account>,
786 MVCCModel {
787 @Override
788 public long getMvccVersion() {
789 return -1;
790 }
791
792 @Override
793 public void setMvccVersion(long mvccVersion) {
794 }
795
796 @Override
797 public Account toEntityModel() {
798 return _nullAccount;
799 }
800 }
801 }