001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchAccountException;
018 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderPath;
022 import com.liferay.portal.kernel.dao.orm.Query;
023 import com.liferay.portal.kernel.dao.orm.QueryUtil;
024 import com.liferay.portal.kernel.dao.orm.Session;
025 import com.liferay.portal.kernel.exception.SystemException;
026 import com.liferay.portal.kernel.log.Log;
027 import com.liferay.portal.kernel.log.LogFactoryUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.InstanceFactory;
030 import com.liferay.portal.kernel.util.OrderByComparator;
031 import com.liferay.portal.kernel.util.SetUtil;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringUtil;
034 import com.liferay.portal.kernel.util.UnmodifiableList;
035 import com.liferay.portal.model.Account;
036 import com.liferay.portal.model.CacheModel;
037 import com.liferay.portal.model.ModelListener;
038 import com.liferay.portal.model.impl.AccountImpl;
039 import com.liferay.portal.model.impl.AccountModelImpl;
040 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041
042 import java.io.Serializable;
043
044 import java.util.ArrayList;
045 import java.util.Collections;
046 import java.util.List;
047 import java.util.Set;
048
049
061 public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
062 implements AccountPersistence {
063
068 public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
069 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
070 ".List1";
071 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072 ".List2";
073 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
074 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
075 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
076 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
077 AccountModelImpl.FINDER_CACHE_ENABLED, AccountImpl.class,
078 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
079 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
080 AccountModelImpl.FINDER_CACHE_ENABLED, Long.class,
081 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
082
083 public AccountPersistenceImpl() {
084 setModelClass(Account.class);
085 }
086
087
092 @Override
093 public void cacheResult(Account account) {
094 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
095 AccountImpl.class, account.getPrimaryKey(), account);
096
097 account.resetOriginalValues();
098 }
099
100
105 @Override
106 public void cacheResult(List<Account> accounts) {
107 for (Account account : accounts) {
108 if (EntityCacheUtil.getResult(
109 AccountModelImpl.ENTITY_CACHE_ENABLED,
110 AccountImpl.class, account.getPrimaryKey()) == null) {
111 cacheResult(account);
112 }
113 else {
114 account.resetOriginalValues();
115 }
116 }
117 }
118
119
126 @Override
127 public void clearCache() {
128 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
129 CacheRegistryUtil.clear(AccountImpl.class.getName());
130 }
131
132 EntityCacheUtil.clearCache(AccountImpl.class.getName());
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
190 @Override
191 public Account remove(long accountId)
192 throws NoSuchAccountException, SystemException {
193 return remove((Serializable)accountId);
194 }
195
196
204 @Override
205 public Account remove(Serializable primaryKey)
206 throws NoSuchAccountException, SystemException {
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) throws SystemException {
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(com.liferay.portal.model.Account account)
270 throws SystemException {
271 account = toUnwrappedModel(account);
272
273 boolean isNew = account.isNew();
274
275 Session session = null;
276
277 try {
278 session = openSession();
279
280 if (account.isNew()) {
281 session.save(account);
282
283 account.setNew(false);
284 }
285 else {
286 session.merge(account);
287 }
288 }
289 catch (Exception e) {
290 throw processException(e);
291 }
292 finally {
293 closeSession(session);
294 }
295
296 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
297
298 if (isNew) {
299 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
300 }
301
302 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
303 AccountImpl.class, account.getPrimaryKey(), account);
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.setAccountId(account.getAccountId());
319 accountImpl.setCompanyId(account.getCompanyId());
320 accountImpl.setUserId(account.getUserId());
321 accountImpl.setUserName(account.getUserName());
322 accountImpl.setCreateDate(account.getCreateDate());
323 accountImpl.setModifiedDate(account.getModifiedDate());
324 accountImpl.setParentAccountId(account.getParentAccountId());
325 accountImpl.setName(account.getName());
326 accountImpl.setLegalName(account.getLegalName());
327 accountImpl.setLegalId(account.getLegalId());
328 accountImpl.setLegalType(account.getLegalType());
329 accountImpl.setSicCode(account.getSicCode());
330 accountImpl.setTickerSymbol(account.getTickerSymbol());
331 accountImpl.setIndustry(account.getIndustry());
332 accountImpl.setType(account.getType());
333 accountImpl.setSize(account.getSize());
334
335 return accountImpl;
336 }
337
338
346 @Override
347 public Account findByPrimaryKey(Serializable primaryKey)
348 throws NoSuchAccountException, SystemException {
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
371 @Override
372 public Account findByPrimaryKey(long accountId)
373 throws NoSuchAccountException, SystemException {
374 return findByPrimaryKey((Serializable)accountId);
375 }
376
377
384 @Override
385 public Account fetchByPrimaryKey(Serializable primaryKey)
386 throws SystemException {
387 Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
388 AccountImpl.class, primaryKey);
389
390 if (account == _nullAccount) {
391 return null;
392 }
393
394 if (account == null) {
395 Session session = null;
396
397 try {
398 session = openSession();
399
400 account = (Account)session.get(AccountImpl.class, primaryKey);
401
402 if (account != null) {
403 cacheResult(account);
404 }
405 else {
406 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
407 AccountImpl.class, primaryKey, _nullAccount);
408 }
409 }
410 catch (Exception e) {
411 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
412 AccountImpl.class, primaryKey);
413
414 throw processException(e);
415 }
416 finally {
417 closeSession(session);
418 }
419 }
420
421 return account;
422 }
423
424
431 @Override
432 public Account fetchByPrimaryKey(long accountId) throws SystemException {
433 return fetchByPrimaryKey((Serializable)accountId);
434 }
435
436
442 @Override
443 public List<Account> findAll() throws SystemException {
444 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
445 }
446
447
459 @Override
460 public List<Account> findAll(int start, int end) throws SystemException {
461 return findAll(start, end, null);
462 }
463
464
477 @Override
478 public List<Account> findAll(int start, int end,
479 OrderByComparator orderByComparator) throws SystemException {
480 boolean pagination = true;
481 FinderPath finderPath = null;
482 Object[] finderArgs = null;
483
484 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
485 (orderByComparator == null)) {
486 pagination = false;
487 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
488 finderArgs = FINDER_ARGS_EMPTY;
489 }
490 else {
491 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
492 finderArgs = new Object[] { start, end, orderByComparator };
493 }
494
495 List<Account> list = (List<Account>)FinderCacheUtil.getResult(finderPath,
496 finderArgs, this);
497
498 if (list == null) {
499 StringBundler query = null;
500 String sql = null;
501
502 if (orderByComparator != null) {
503 query = new StringBundler(2 +
504 (orderByComparator.getOrderByFields().length * 3));
505
506 query.append(_SQL_SELECT_ACCOUNT);
507
508 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
509 orderByComparator);
510
511 sql = query.toString();
512 }
513 else {
514 sql = _SQL_SELECT_ACCOUNT;
515
516 if (pagination) {
517 sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
518 }
519 }
520
521 Session session = null;
522
523 try {
524 session = openSession();
525
526 Query q = session.createQuery(sql);
527
528 if (!pagination) {
529 list = (List<Account>)QueryUtil.list(q, getDialect(),
530 start, end, false);
531
532 Collections.sort(list);
533
534 list = new UnmodifiableList<Account>(list);
535 }
536 else {
537 list = (List<Account>)QueryUtil.list(q, getDialect(),
538 start, end);
539 }
540
541 cacheResult(list);
542
543 FinderCacheUtil.putResult(finderPath, finderArgs, list);
544 }
545 catch (Exception e) {
546 FinderCacheUtil.removeResult(finderPath, finderArgs);
547
548 throw processException(e);
549 }
550 finally {
551 closeSession(session);
552 }
553 }
554
555 return list;
556 }
557
558
563 @Override
564 public void removeAll() throws SystemException {
565 for (Account account : findAll()) {
566 remove(account);
567 }
568 }
569
570
576 @Override
577 public int countAll() throws SystemException {
578 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
579 FINDER_ARGS_EMPTY, this);
580
581 if (count == null) {
582 Session session = null;
583
584 try {
585 session = openSession();
586
587 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
588
589 count = (Long)q.uniqueResult();
590
591 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
592 FINDER_ARGS_EMPTY, count);
593 }
594 catch (Exception e) {
595 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
596 FINDER_ARGS_EMPTY);
597
598 throw processException(e);
599 }
600 finally {
601 closeSession(session);
602 }
603 }
604
605 return count.intValue();
606 }
607
608 @Override
609 protected Set<String> getBadColumnNames() {
610 return _badColumnNames;
611 }
612
613
616 public void afterPropertiesSet() {
617 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
618 com.liferay.portal.util.PropsUtil.get(
619 "value.object.listener.com.liferay.portal.model.Account")));
620
621 if (listenerClassNames.length > 0) {
622 try {
623 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
624
625 for (String listenerClassName : listenerClassNames) {
626 listenersList.add((ModelListener<Account>)InstanceFactory.newInstance(
627 getClassLoader(), listenerClassName));
628 }
629
630 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
631 }
632 catch (Exception e) {
633 _log.error(e);
634 }
635 }
636 }
637
638 public void destroy() {
639 EntityCacheUtil.removeCache(AccountImpl.class.getName());
640 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
641 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
642 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
643 }
644
645 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
646 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
647 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
648 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
649 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
650 private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
651 private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
652 "type", "size"
653 });
654 private static Account _nullAccount = new AccountImpl() {
655 @Override
656 public Object clone() {
657 return this;
658 }
659
660 @Override
661 public CacheModel<Account> toCacheModel() {
662 return _nullAccountCacheModel;
663 }
664 };
665
666 private static CacheModel<Account> _nullAccountCacheModel = new CacheModel<Account>() {
667 @Override
668 public Account toEntityModel() {
669 return _nullAccount;
670 }
671 };
672 }