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
088 public void cacheResult(Account account) {
089 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
090 AccountImpl.class, account.getPrimaryKey(), account);
091
092 account.resetOriginalValues();
093 }
094
095
100 public void cacheResult(List<Account> accounts) {
101 for (Account account : accounts) {
102 if (EntityCacheUtil.getResult(
103 AccountModelImpl.ENTITY_CACHE_ENABLED,
104 AccountImpl.class, account.getPrimaryKey()) == null) {
105 cacheResult(account);
106 }
107 else {
108 account.resetOriginalValues();
109 }
110 }
111 }
112
113
120 @Override
121 public void clearCache() {
122 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
123 CacheRegistryUtil.clear(AccountImpl.class.getName());
124 }
125
126 EntityCacheUtil.clearCache(AccountImpl.class.getName());
127
128 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
129 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
130 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
131 }
132
133
140 @Override
141 public void clearCache(Account account) {
142 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
143 AccountImpl.class, account.getPrimaryKey());
144
145 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
146 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
147 }
148
149 @Override
150 public void clearCache(List<Account> accounts) {
151 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
152 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
153
154 for (Account account : accounts) {
155 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
156 AccountImpl.class, account.getPrimaryKey());
157 }
158 }
159
160
166 public Account create(long accountId) {
167 Account account = new AccountImpl();
168
169 account.setNew(true);
170 account.setPrimaryKey(accountId);
171
172 return account;
173 }
174
175
183 public Account remove(long accountId)
184 throws NoSuchAccountException, SystemException {
185 return remove((Serializable)accountId);
186 }
187
188
196 @Override
197 public Account remove(Serializable primaryKey)
198 throws NoSuchAccountException, SystemException {
199 Session session = null;
200
201 try {
202 session = openSession();
203
204 Account account = (Account)session.get(AccountImpl.class, primaryKey);
205
206 if (account == null) {
207 if (_log.isWarnEnabled()) {
208 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
209 }
210
211 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
212 primaryKey);
213 }
214
215 return remove(account);
216 }
217 catch (NoSuchAccountException nsee) {
218 throw nsee;
219 }
220 catch (Exception e) {
221 throw processException(e);
222 }
223 finally {
224 closeSession(session);
225 }
226 }
227
228 @Override
229 protected Account removeImpl(Account account) throws SystemException {
230 account = toUnwrappedModel(account);
231
232 Session session = null;
233
234 try {
235 session = openSession();
236
237 if (!session.contains(account)) {
238 account = (Account)session.get(AccountImpl.class,
239 account.getPrimaryKeyObj());
240 }
241
242 if (account != null) {
243 session.delete(account);
244 }
245 }
246 catch (Exception e) {
247 throw processException(e);
248 }
249 finally {
250 closeSession(session);
251 }
252
253 if (account != null) {
254 clearCache(account);
255 }
256
257 return account;
258 }
259
260 @Override
261 public Account updateImpl(com.liferay.portal.model.Account account)
262 throws SystemException {
263 account = toUnwrappedModel(account);
264
265 boolean isNew = account.isNew();
266
267 Session session = null;
268
269 try {
270 session = openSession();
271
272 if (account.isNew()) {
273 session.save(account);
274
275 account.setNew(false);
276 }
277 else {
278 session.merge(account);
279 }
280 }
281 catch (Exception e) {
282 throw processException(e);
283 }
284 finally {
285 closeSession(session);
286 }
287
288 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
289
290 if (isNew) {
291 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
292 }
293
294 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
295 AccountImpl.class, account.getPrimaryKey(), account);
296
297 return account;
298 }
299
300 protected Account toUnwrappedModel(Account account) {
301 if (account instanceof AccountImpl) {
302 return account;
303 }
304
305 AccountImpl accountImpl = new AccountImpl();
306
307 accountImpl.setNew(account.isNew());
308 accountImpl.setPrimaryKey(account.getPrimaryKey());
309
310 accountImpl.setAccountId(account.getAccountId());
311 accountImpl.setCompanyId(account.getCompanyId());
312 accountImpl.setUserId(account.getUserId());
313 accountImpl.setUserName(account.getUserName());
314 accountImpl.setCreateDate(account.getCreateDate());
315 accountImpl.setModifiedDate(account.getModifiedDate());
316 accountImpl.setParentAccountId(account.getParentAccountId());
317 accountImpl.setName(account.getName());
318 accountImpl.setLegalName(account.getLegalName());
319 accountImpl.setLegalId(account.getLegalId());
320 accountImpl.setLegalType(account.getLegalType());
321 accountImpl.setSicCode(account.getSicCode());
322 accountImpl.setTickerSymbol(account.getTickerSymbol());
323 accountImpl.setIndustry(account.getIndustry());
324 accountImpl.setType(account.getType());
325 accountImpl.setSize(account.getSize());
326
327 return accountImpl;
328 }
329
330
338 @Override
339 public Account findByPrimaryKey(Serializable primaryKey)
340 throws NoSuchAccountException, SystemException {
341 Account account = fetchByPrimaryKey(primaryKey);
342
343 if (account == null) {
344 if (_log.isWarnEnabled()) {
345 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
346 }
347
348 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
349 primaryKey);
350 }
351
352 return account;
353 }
354
355
363 public Account findByPrimaryKey(long accountId)
364 throws NoSuchAccountException, SystemException {
365 return findByPrimaryKey((Serializable)accountId);
366 }
367
368
375 @Override
376 public Account fetchByPrimaryKey(Serializable primaryKey)
377 throws SystemException {
378 Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
379 AccountImpl.class, primaryKey);
380
381 if (account == _nullAccount) {
382 return null;
383 }
384
385 if (account == null) {
386 Session session = null;
387
388 try {
389 session = openSession();
390
391 account = (Account)session.get(AccountImpl.class, primaryKey);
392
393 if (account != null) {
394 cacheResult(account);
395 }
396 else {
397 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
398 AccountImpl.class, primaryKey, _nullAccount);
399 }
400 }
401 catch (Exception e) {
402 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
403 AccountImpl.class, primaryKey);
404
405 throw processException(e);
406 }
407 finally {
408 closeSession(session);
409 }
410 }
411
412 return account;
413 }
414
415
422 public Account fetchByPrimaryKey(long accountId) throws SystemException {
423 return fetchByPrimaryKey((Serializable)accountId);
424 }
425
426
432 public List<Account> findAll() throws SystemException {
433 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
434 }
435
436
448 public List<Account> findAll(int start, int end) throws SystemException {
449 return findAll(start, end, null);
450 }
451
452
465 public List<Account> findAll(int start, int end,
466 OrderByComparator orderByComparator) throws SystemException {
467 boolean pagination = true;
468 FinderPath finderPath = null;
469 Object[] finderArgs = null;
470
471 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
472 (orderByComparator == null)) {
473 pagination = false;
474 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
475 finderArgs = FINDER_ARGS_EMPTY;
476 }
477 else {
478 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
479 finderArgs = new Object[] { start, end, orderByComparator };
480 }
481
482 List<Account> list = (List<Account>)FinderCacheUtil.getResult(finderPath,
483 finderArgs, this);
484
485 if (list == null) {
486 StringBundler query = null;
487 String sql = null;
488
489 if (orderByComparator != null) {
490 query = new StringBundler(2 +
491 (orderByComparator.getOrderByFields().length * 3));
492
493 query.append(_SQL_SELECT_ACCOUNT);
494
495 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
496 orderByComparator);
497
498 sql = query.toString();
499 }
500 else {
501 sql = _SQL_SELECT_ACCOUNT;
502
503 if (pagination) {
504 sql = sql.concat(AccountModelImpl.ORDER_BY_JPQL);
505 }
506 }
507
508 Session session = null;
509
510 try {
511 session = openSession();
512
513 Query q = session.createQuery(sql);
514
515 if (!pagination) {
516 list = (List<Account>)QueryUtil.list(q, getDialect(),
517 start, end, false);
518
519 Collections.sort(list);
520
521 list = new UnmodifiableList<Account>(list);
522 }
523 else {
524 list = (List<Account>)QueryUtil.list(q, getDialect(),
525 start, end);
526 }
527
528 cacheResult(list);
529
530 FinderCacheUtil.putResult(finderPath, finderArgs, list);
531 }
532 catch (Exception e) {
533 FinderCacheUtil.removeResult(finderPath, finderArgs);
534
535 throw processException(e);
536 }
537 finally {
538 closeSession(session);
539 }
540 }
541
542 return list;
543 }
544
545
550 public void removeAll() throws SystemException {
551 for (Account account : findAll()) {
552 remove(account);
553 }
554 }
555
556
562 public int countAll() throws SystemException {
563 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
564 FINDER_ARGS_EMPTY, this);
565
566 if (count == null) {
567 Session session = null;
568
569 try {
570 session = openSession();
571
572 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
573
574 count = (Long)q.uniqueResult();
575
576 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
577 FINDER_ARGS_EMPTY, count);
578 }
579 catch (Exception e) {
580 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
581 FINDER_ARGS_EMPTY);
582
583 throw processException(e);
584 }
585 finally {
586 closeSession(session);
587 }
588 }
589
590 return count.intValue();
591 }
592
593 @Override
594 protected Set<String> getBadColumnNames() {
595 return _badColumnNames;
596 }
597
598
601 public void afterPropertiesSet() {
602 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
603 com.liferay.portal.util.PropsUtil.get(
604 "value.object.listener.com.liferay.portal.model.Account")));
605
606 if (listenerClassNames.length > 0) {
607 try {
608 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
609
610 for (String listenerClassName : listenerClassNames) {
611 listenersList.add((ModelListener<Account>)InstanceFactory.newInstance(
612 getClassLoader(), listenerClassName));
613 }
614
615 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
616 }
617 catch (Exception e) {
618 _log.error(e);
619 }
620 }
621 }
622
623 public void destroy() {
624 EntityCacheUtil.removeCache(AccountImpl.class.getName());
625 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
626 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
627 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
628 }
629
630 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
631 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
632 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
633 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
634 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
635 private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
636 private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
637 "type", "size"
638 });
639 private static Account _nullAccount = new AccountImpl() {
640 @Override
641 public Object clone() {
642 return this;
643 }
644
645 @Override
646 public CacheModel<Account> toCacheModel() {
647 return _nullAccountCacheModel;
648 }
649 };
650
651 private static CacheModel<Account> _nullAccountCacheModel = new CacheModel<Account>() {
652 public Account toEntityModel() {
653 return _nullAccount;
654 }
655 };
656 }