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