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