001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.portal.kernel.bean.BeanReference;
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.exception.NoSuchAccountException;
029 import com.liferay.portal.kernel.log.Log;
030 import com.liferay.portal.kernel.log.LogFactoryUtil;
031 import com.liferay.portal.kernel.model.Account;
032 import com.liferay.portal.kernel.model.CacheModel;
033 import com.liferay.portal.kernel.model.MVCCModel;
034 import com.liferay.portal.kernel.service.ServiceContext;
035 import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
036 import com.liferay.portal.kernel.service.persistence.AccountPersistence;
037 import com.liferay.portal.kernel.service.persistence.CompanyProvider;
038 import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
039 import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
040 import com.liferay.portal.kernel.util.OrderByComparator;
041 import com.liferay.portal.kernel.util.SetUtil;
042 import com.liferay.portal.kernel.util.StringBundler;
043 import com.liferay.portal.kernel.util.StringPool;
044 import com.liferay.portal.model.impl.AccountImpl;
045 import com.liferay.portal.model.impl.AccountModelImpl;
046
047 import java.io.Serializable;
048
049 import java.util.Collections;
050 import java.util.Date;
051 import java.util.HashMap;
052 import java.util.HashSet;
053 import java.util.Iterator;
054 import java.util.List;
055 import java.util.Map;
056 import java.util.Set;
057
058
070 @ProviderType
071 public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
072 implements AccountPersistence {
073
078 public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
079 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
080 ".List1";
081 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
082 ".List2";
083 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
084 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
085 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
086 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
087 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
088 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
089 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
090 AccountModelImpl.FINDER_CACHE_ENABLED, Long.class,
091 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
092
093 public AccountPersistenceImpl() {
094 setModelClass(Account.class);
095 }
096
097
102 @Override
103 public void cacheResult(Account account) {
104 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
105 AccountImpl.class, account.getPrimaryKey(), account);
106
107 account.resetOriginalValues();
108 }
109
110
115 @Override
116 public void cacheResult(List<Account> accounts) {
117 for (Account account : accounts) {
118 if (entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
119 AccountImpl.class, account.getPrimaryKey()) == null) {
120 cacheResult(account);
121 }
122 else {
123 account.resetOriginalValues();
124 }
125 }
126 }
127
128
135 @Override
136 public void clearCache() {
137 entityCache.clearCache(AccountImpl.class);
138
139 finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
140 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
141 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
142 }
143
144
151 @Override
152 public void clearCache(Account account) {
153 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
154 AccountImpl.class, account.getPrimaryKey());
155
156 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
157 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
158 }
159
160 @Override
161 public void clearCache(List<Account> accounts) {
162 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
163 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
164
165 for (Account account : accounts) {
166 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
167 AccountImpl.class, account.getPrimaryKey());
168 }
169 }
170
171
177 @Override
178 public Account create(long accountId) {
179 Account account = new AccountImpl();
180
181 account.setNew(true);
182 account.setPrimaryKey(accountId);
183
184 account.setCompanyId(companyProvider.getCompanyId());
185
186 return account;
187 }
188
189
196 @Override
197 public Account remove(long accountId) throws NoSuchAccountException {
198 return remove((Serializable)accountId);
199 }
200
201
208 @Override
209 public Account remove(Serializable primaryKey)
210 throws NoSuchAccountException {
211 Session session = null;
212
213 try {
214 session = openSession();
215
216 Account account = (Account)session.get(AccountImpl.class, primaryKey);
217
218 if (account == null) {
219 if (_log.isDebugEnabled()) {
220 _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
221 }
222
223 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
224 primaryKey);
225 }
226
227 return remove(account);
228 }
229 catch (NoSuchAccountException nsee) {
230 throw nsee;
231 }
232 catch (Exception e) {
233 throw processException(e);
234 }
235 finally {
236 closeSession(session);
237 }
238 }
239
240 @Override
241 protected Account removeImpl(Account account) {
242 account = toUnwrappedModel(account);
243
244 Session session = null;
245
246 try {
247 session = openSession();
248
249 if (!session.contains(account)) {
250 account = (Account)session.get(AccountImpl.class,
251 account.getPrimaryKeyObj());
252 }
253
254 if (account != null) {
255 session.delete(account);
256 }
257 }
258 catch (Exception e) {
259 throw processException(e);
260 }
261 finally {
262 closeSession(session);
263 }
264
265 if (account != null) {
266 clearCache(account);
267 }
268
269 return account;
270 }
271
272 @Override
273 public Account updateImpl(Account account) {
274 account = toUnwrappedModel(account);
275
276 boolean isNew = account.isNew();
277
278 AccountModelImpl accountModelImpl = (AccountModelImpl)account;
279
280 ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
281
282 Date now = new Date();
283
284 if (isNew && (account.getCreateDate() == null)) {
285 if (serviceContext == null) {
286 account.setCreateDate(now);
287 }
288 else {
289 account.setCreateDate(serviceContext.getCreateDate(now));
290 }
291 }
292
293 if (!accountModelImpl.hasSetModifiedDate()) {
294 if (serviceContext == null) {
295 account.setModifiedDate(now);
296 }
297 else {
298 account.setModifiedDate(serviceContext.getModifiedDate(now));
299 }
300 }
301
302 Session session = null;
303
304 try {
305 session = openSession();
306
307 if (account.isNew()) {
308 session.save(account);
309
310 account.setNew(false);
311 }
312 else {
313 account = (Account)session.merge(account);
314 }
315 }
316 catch (Exception e) {
317 throw processException(e);
318 }
319 finally {
320 closeSession(session);
321 }
322
323 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
324
325 if (isNew) {
326 finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
327 }
328
329 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
330 AccountImpl.class, account.getPrimaryKey(), account, false);
331
332 account.resetOriginalValues();
333
334 return account;
335 }
336
337 protected Account toUnwrappedModel(Account account) {
338 if (account instanceof AccountImpl) {
339 return account;
340 }
341
342 AccountImpl accountImpl = new AccountImpl();
343
344 accountImpl.setNew(account.isNew());
345 accountImpl.setPrimaryKey(account.getPrimaryKey());
346
347 accountImpl.setMvccVersion(account.getMvccVersion());
348 accountImpl.setAccountId(account.getAccountId());
349 accountImpl.setCompanyId(account.getCompanyId());
350 accountImpl.setUserId(account.getUserId());
351 accountImpl.setUserName(account.getUserName());
352 accountImpl.setCreateDate(account.getCreateDate());
353 accountImpl.setModifiedDate(account.getModifiedDate());
354 accountImpl.setParentAccountId(account.getParentAccountId());
355 accountImpl.setName(account.getName());
356 accountImpl.setLegalName(account.getLegalName());
357 accountImpl.setLegalId(account.getLegalId());
358 accountImpl.setLegalType(account.getLegalType());
359 accountImpl.setSicCode(account.getSicCode());
360 accountImpl.setTickerSymbol(account.getTickerSymbol());
361 accountImpl.setIndustry(account.getIndustry());
362 accountImpl.setType(account.getType());
363 accountImpl.setSize(account.getSize());
364
365 return accountImpl;
366 }
367
368
375 @Override
376 public Account findByPrimaryKey(Serializable primaryKey)
377 throws NoSuchAccountException {
378 Account account = fetchByPrimaryKey(primaryKey);
379
380 if (account == null) {
381 if (_log.isDebugEnabled()) {
382 _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
383 }
384
385 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
386 primaryKey);
387 }
388
389 return account;
390 }
391
392
399 @Override
400 public Account findByPrimaryKey(long accountId)
401 throws NoSuchAccountException {
402 return findByPrimaryKey((Serializable)accountId);
403 }
404
405
411 @Override
412 public Account fetchByPrimaryKey(Serializable primaryKey) {
413 Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
414 AccountImpl.class, primaryKey);
415
416 if (account == _nullAccount) {
417 return null;
418 }
419
420 if (account == null) {
421 Session session = null;
422
423 try {
424 session = openSession();
425
426 account = (Account)session.get(AccountImpl.class, primaryKey);
427
428 if (account != null) {
429 cacheResult(account);
430 }
431 else {
432 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
433 AccountImpl.class, primaryKey, _nullAccount);
434 }
435 }
436 catch (Exception e) {
437 entityCache.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
438 AccountImpl.class, primaryKey);
439
440 throw processException(e);
441 }
442 finally {
443 closeSession(session);
444 }
445 }
446
447 return account;
448 }
449
450
456 @Override
457 public Account fetchByPrimaryKey(long accountId) {
458 return fetchByPrimaryKey((Serializable)accountId);
459 }
460
461 @Override
462 public Map<Serializable, Account> fetchByPrimaryKeys(
463 Set<Serializable> primaryKeys) {
464 if (primaryKeys.isEmpty()) {
465 return Collections.emptyMap();
466 }
467
468 Map<Serializable, Account> map = new HashMap<Serializable, Account>();
469
470 if (primaryKeys.size() == 1) {
471 Iterator<Serializable> iterator = primaryKeys.iterator();
472
473 Serializable primaryKey = iterator.next();
474
475 Account account = fetchByPrimaryKey(primaryKey);
476
477 if (account != null) {
478 map.put(primaryKey, account);
479 }
480
481 return map;
482 }
483
484 Set<Serializable> uncachedPrimaryKeys = null;
485
486 for (Serializable primaryKey : primaryKeys) {
487 Account account = (Account)entityCache.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
488 AccountImpl.class, primaryKey);
489
490 if (account == null) {
491 if (uncachedPrimaryKeys == null) {
492 uncachedPrimaryKeys = new HashSet<Serializable>();
493 }
494
495 uncachedPrimaryKeys.add(primaryKey);
496 }
497 else {
498 map.put(primaryKey, account);
499 }
500 }
501
502 if (uncachedPrimaryKeys == null) {
503 return map;
504 }
505
506 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
507 1);
508
509 query.append(_SQL_SELECT_ACCOUNT_WHERE_PKS_IN);
510
511 for (Serializable primaryKey : uncachedPrimaryKeys) {
512 query.append(String.valueOf(primaryKey));
513
514 query.append(StringPool.COMMA);
515 }
516
517 query.setIndex(query.index() - 1);
518
519 query.append(StringPool.CLOSE_PARENTHESIS);
520
521 String sql = query.toString();
522
523 Session session = null;
524
525 try {
526 session = openSession();
527
528 Query q = session.createQuery(sql);
529
530 for (Account account : (List<Account>)q.list()) {
531 map.put(account.getPrimaryKeyObj(), account);
532
533 cacheResult(account);
534
535 uncachedPrimaryKeys.remove(account.getPrimaryKeyObj());
536 }
537
538 for (Serializable primaryKey : uncachedPrimaryKeys) {
539 entityCache.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
540 AccountImpl.class, primaryKey, _nullAccount);
541 }
542 }
543 catch (Exception e) {
544 throw processException(e);
545 }
546 finally {
547 closeSession(session);
548 }
549
550 return map;
551 }
552
553
558 @Override
559 public List<Account> findAll() {
560 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
561 }
562
563
574 @Override
575 public List<Account> findAll(int start, int end) {
576 return findAll(start, end, null);
577 }
578
579
591 @Override
592 public List<Account> findAll(int start, int end,
593 OrderByComparator<Account> orderByComparator) {
594 return findAll(start, end, orderByComparator, true);
595 }
596
597
610 @Override
611 public List<Account> findAll(int start, int end,
612 OrderByComparator<Account> orderByComparator, boolean retrieveFromCache) {
613 boolean pagination = true;
614 FinderPath finderPath = null;
615 Object[] finderArgs = null;
616
617 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
618 (orderByComparator == null)) {
619 pagination = false;
620 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
621 finderArgs = FINDER_ARGS_EMPTY;
622 }
623 else {
624 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
625 finderArgs = new Object[] { start, end, orderByComparator };
626 }
627
628 List<Account> list = null;
629
630 if (retrieveFromCache) {
631 list = (List<Account>)finderCache.getResult(finderPath, finderArgs,
632 this);
633 }
634
635 if (list == null) {
636 StringBundler query = null;
637 String sql = null;
638
639 if (orderByComparator != null) {
640 query = new StringBundler(2 +
641 (orderByComparator.getOrderByFields().length * 2));
642
643 query.append(_SQL_SELECT_ACCOUNT);
644
645 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
646 orderByComparator);
647
648 sql = query.toString();
649 }
650 else {
651 sql = _SQL_SELECT_ACCOUNT;
652
653 if (pagination) {
654 sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
655 }
656 }
657
658 Session session = null;
659
660 try {
661 session = openSession();
662
663 Query q = session.createQuery(sql);
664
665 if (!pagination) {
666 list = (List<Account>)QueryUtil.list(q, getDialect(),
667 start, end, false);
668
669 Collections.sort(list);
670
671 list = Collections.unmodifiableList(list);
672 }
673 else {
674 list = (List<Account>)QueryUtil.list(q, getDialect(),
675 start, end);
676 }
677
678 cacheResult(list);
679
680 finderCache.putResult(finderPath, finderArgs, list);
681 }
682 catch (Exception e) {
683 finderCache.removeResult(finderPath, finderArgs);
684
685 throw processException(e);
686 }
687 finally {
688 closeSession(session);
689 }
690 }
691
692 return list;
693 }
694
695
699 @Override
700 public void removeAll() {
701 for (Account account : findAll()) {
702 remove(account);
703 }
704 }
705
706
711 @Override
712 public int countAll() {
713 Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
714 FINDER_ARGS_EMPTY, this);
715
716 if (count == null) {
717 Session session = null;
718
719 try {
720 session = openSession();
721
722 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
723
724 count = (Long)q.uniqueResult();
725
726 finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
727 count);
728 }
729 catch (Exception e) {
730 finderCache.removeResult(FINDER_PATH_COUNT_ALL,
731 FINDER_ARGS_EMPTY);
732
733 throw processException(e);
734 }
735 finally {
736 closeSession(session);
737 }
738 }
739
740 return count.intValue();
741 }
742
743 @Override
744 public Set<String> getBadColumnNames() {
745 return _badColumnNames;
746 }
747
748 @Override
749 protected Map<String, Integer> getTableColumnsMap() {
750 return AccountModelImpl.TABLE_COLUMNS_MAP;
751 }
752
753
756 public void afterPropertiesSet() {
757 }
758
759 public void destroy() {
760 entityCache.removeCache(AccountImpl.class.getName());
761 finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
762 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
763 finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
764 }
765
766 @BeanReference(type = CompanyProviderWrapper.class)
767 protected CompanyProvider companyProvider;
768 protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
769 protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
770 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
771 private static final String _SQL_SELECT_ACCOUNT_WHERE_PKS_IN = "SELECT account FROM Account account WHERE accountId IN (";
772 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
773 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
774 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
775 private static final Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
776 private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
777 "type", "size"
778 });
779 private static final Account _nullAccount = new AccountImpl() {
780 @Override
781 public Object clone() {
782 return this;
783 }
784
785 @Override
786 public CacheModel<Account> toCacheModel() {
787 return _nullAccountCacheModel;
788 }
789 };
790
791 private static final CacheModel<Account> _nullAccountCacheModel = new NullCacheModel();
792
793 private static class NullCacheModel implements CacheModel<Account>,
794 MVCCModel {
795 @Override
796 public long getMvccVersion() {
797 return -1;
798 }
799
800 @Override
801 public void setMvccVersion(long mvccVersion) {
802 }
803
804 @Override
805 public Account toEntityModel() {
806 return _nullAccount;
807 }
808 }
809 }