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