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