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