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