1
14
15 package com.liferay.portal.service.persistence;
16
17 import com.liferay.portal.NoSuchAccountException;
18 import com.liferay.portal.NoSuchModelException;
19 import com.liferay.portal.kernel.annotation.BeanReference;
20 import com.liferay.portal.kernel.cache.CacheRegistry;
21 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23 import com.liferay.portal.kernel.dao.orm.FinderPath;
24 import com.liferay.portal.kernel.dao.orm.Query;
25 import com.liferay.portal.kernel.dao.orm.QueryUtil;
26 import com.liferay.portal.kernel.dao.orm.Session;
27 import com.liferay.portal.kernel.exception.SystemException;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.InstanceFactory;
32 import com.liferay.portal.kernel.util.OrderByComparator;
33 import com.liferay.portal.kernel.util.StringBundler;
34 import com.liferay.portal.kernel.util.StringUtil;
35 import com.liferay.portal.model.Account;
36 import com.liferay.portal.model.ModelListener;
37 import com.liferay.portal.model.impl.AccountImpl;
38 import com.liferay.portal.model.impl.AccountModelImpl;
39 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40
41 import java.io.Serializable;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46
47
60 public class AccountPersistenceImpl extends BasePersistenceImpl<Account>
61 implements AccountPersistence {
62 public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
63 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
64 ".List";
65 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
66 AccountModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
67 "findAll", new String[0]);
68 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
69 AccountModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
70 "countAll", new String[0]);
71
72 public void cacheResult(Account account) {
73 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
74 AccountImpl.class, account.getPrimaryKey(), account);
75 }
76
77 public void cacheResult(List<Account> accounts) {
78 for (Account account : accounts) {
79 if (EntityCacheUtil.getResult(
80 AccountModelImpl.ENTITY_CACHE_ENABLED,
81 AccountImpl.class, account.getPrimaryKey(), this) == null) {
82 cacheResult(account);
83 }
84 }
85 }
86
87 public void clearCache() {
88 CacheRegistry.clear(AccountImpl.class.getName());
89 EntityCacheUtil.clearCache(AccountImpl.class.getName());
90 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
91 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
92 }
93
94 public void clearCache(Account account) {
95 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
96 AccountImpl.class, account.getPrimaryKey());
97 }
98
99 public Account create(long accountId) {
100 Account account = new AccountImpl();
101
102 account.setNew(true);
103 account.setPrimaryKey(accountId);
104
105 return account;
106 }
107
108 public Account remove(Serializable primaryKey)
109 throws NoSuchModelException, SystemException {
110 return remove(((Long)primaryKey).longValue());
111 }
112
113 public Account remove(long accountId)
114 throws NoSuchAccountException, SystemException {
115 Session session = null;
116
117 try {
118 session = openSession();
119
120 Account account = (Account)session.get(AccountImpl.class,
121 new Long(accountId));
122
123 if (account == null) {
124 if (_log.isWarnEnabled()) {
125 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + accountId);
126 }
127
128 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
129 accountId);
130 }
131
132 return remove(account);
133 }
134 catch (NoSuchAccountException nsee) {
135 throw nsee;
136 }
137 catch (Exception e) {
138 throw processException(e);
139 }
140 finally {
141 closeSession(session);
142 }
143 }
144
145 public Account remove(Account account) throws SystemException {
146 for (ModelListener<Account> listener : listeners) {
147 listener.onBeforeRemove(account);
148 }
149
150 account = removeImpl(account);
151
152 for (ModelListener<Account> listener : listeners) {
153 listener.onAfterRemove(account);
154 }
155
156 return account;
157 }
158
159 protected Account removeImpl(Account account) throws SystemException {
160 account = toUnwrappedModel(account);
161
162 Session session = null;
163
164 try {
165 session = openSession();
166
167 if (account.isCachedModel() || BatchSessionUtil.isEnabled()) {
168 Object staleObject = session.get(AccountImpl.class,
169 account.getPrimaryKeyObj());
170
171 if (staleObject != null) {
172 session.evict(staleObject);
173 }
174 }
175
176 session.delete(account);
177
178 session.flush();
179 }
180 catch (Exception e) {
181 throw processException(e);
182 }
183 finally {
184 closeSession(session);
185 }
186
187 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
188
189 EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
190 AccountImpl.class, account.getPrimaryKey());
191
192 return account;
193 }
194
195 public Account updateImpl(com.liferay.portal.model.Account account,
196 boolean merge) throws SystemException {
197 account = toUnwrappedModel(account);
198
199 Session session = null;
200
201 try {
202 session = openSession();
203
204 BatchSessionUtil.update(session, account, merge);
205
206 account.setNew(false);
207 }
208 catch (Exception e) {
209 throw processException(e);
210 }
211 finally {
212 closeSession(session);
213 }
214
215 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
216
217 EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
218 AccountImpl.class, account.getPrimaryKey(), account);
219
220 return account;
221 }
222
223 protected Account toUnwrappedModel(Account account) {
224 if (account instanceof AccountImpl) {
225 return account;
226 }
227
228 AccountImpl accountImpl = new AccountImpl();
229
230 accountImpl.setNew(account.isNew());
231 accountImpl.setPrimaryKey(account.getPrimaryKey());
232
233 accountImpl.setAccountId(account.getAccountId());
234 accountImpl.setCompanyId(account.getCompanyId());
235 accountImpl.setUserId(account.getUserId());
236 accountImpl.setUserName(account.getUserName());
237 accountImpl.setCreateDate(account.getCreateDate());
238 accountImpl.setModifiedDate(account.getModifiedDate());
239 accountImpl.setParentAccountId(account.getParentAccountId());
240 accountImpl.setName(account.getName());
241 accountImpl.setLegalName(account.getLegalName());
242 accountImpl.setLegalId(account.getLegalId());
243 accountImpl.setLegalType(account.getLegalType());
244 accountImpl.setSicCode(account.getSicCode());
245 accountImpl.setTickerSymbol(account.getTickerSymbol());
246 accountImpl.setIndustry(account.getIndustry());
247 accountImpl.setType(account.getType());
248 accountImpl.setSize(account.getSize());
249
250 return accountImpl;
251 }
252
253 public Account findByPrimaryKey(Serializable primaryKey)
254 throws NoSuchModelException, SystemException {
255 return findByPrimaryKey(((Long)primaryKey).longValue());
256 }
257
258 public Account findByPrimaryKey(long accountId)
259 throws NoSuchAccountException, SystemException {
260 Account account = fetchByPrimaryKey(accountId);
261
262 if (account == null) {
263 if (_log.isWarnEnabled()) {
264 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + accountId);
265 }
266
267 throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
268 accountId);
269 }
270
271 return account;
272 }
273
274 public Account fetchByPrimaryKey(Serializable primaryKey)
275 throws SystemException {
276 return fetchByPrimaryKey(((Long)primaryKey).longValue());
277 }
278
279 public Account fetchByPrimaryKey(long accountId) throws SystemException {
280 Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
281 AccountImpl.class, accountId, this);
282
283 if (account == null) {
284 Session session = null;
285
286 try {
287 session = openSession();
288
289 account = (Account)session.get(AccountImpl.class,
290 new Long(accountId));
291 }
292 catch (Exception e) {
293 throw processException(e);
294 }
295 finally {
296 if (account != null) {
297 cacheResult(account);
298 }
299
300 closeSession(session);
301 }
302 }
303
304 return account;
305 }
306
307 public List<Account> findAll() throws SystemException {
308 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
309 }
310
311 public List<Account> findAll(int start, int end) throws SystemException {
312 return findAll(start, end, null);
313 }
314
315 public List<Account> findAll(int start, int end,
316 OrderByComparator orderByComparator) throws SystemException {
317 Object[] finderArgs = new Object[] {
318 String.valueOf(start), String.valueOf(end),
319 String.valueOf(orderByComparator)
320 };
321
322 List<Account> list = (List<Account>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
323 finderArgs, this);
324
325 if (list == null) {
326 Session session = null;
327
328 try {
329 session = openSession();
330
331 StringBundler query = null;
332 String sql = null;
333
334 if (orderByComparator != null) {
335 query = new StringBundler(2 +
336 (orderByComparator.getOrderByFields().length * 3));
337
338 query.append(_SQL_SELECT_ACCOUNT);
339
340 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
341 orderByComparator);
342
343 sql = query.toString();
344 }
345
346 sql = _SQL_SELECT_ACCOUNT;
347
348 Query q = session.createQuery(sql);
349
350 if (orderByComparator == null) {
351 list = (List<Account>)QueryUtil.list(q, getDialect(),
352 start, end, false);
353
354 Collections.sort(list);
355 }
356 else {
357 list = (List<Account>)QueryUtil.list(q, getDialect(),
358 start, end);
359 }
360 }
361 catch (Exception e) {
362 throw processException(e);
363 }
364 finally {
365 if (list == null) {
366 list = new ArrayList<Account>();
367 }
368
369 cacheResult(list);
370
371 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
372
373 closeSession(session);
374 }
375 }
376
377 return list;
378 }
379
380 public void removeAll() throws SystemException {
381 for (Account account : findAll()) {
382 remove(account);
383 }
384 }
385
386 public int countAll() throws SystemException {
387 Object[] finderArgs = new Object[0];
388
389 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
390 finderArgs, this);
391
392 if (count == null) {
393 Session session = null;
394
395 try {
396 session = openSession();
397
398 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
399
400 count = (Long)q.uniqueResult();
401 }
402 catch (Exception e) {
403 throw processException(e);
404 }
405 finally {
406 if (count == null) {
407 count = Long.valueOf(0);
408 }
409
410 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
411 count);
412
413 closeSession(session);
414 }
415 }
416
417 return count.intValue();
418 }
419
420 public void afterPropertiesSet() {
421 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
422 com.liferay.portal.util.PropsUtil.get(
423 "value.object.listener.com.liferay.portal.model.Account")));
424
425 if (listenerClassNames.length > 0) {
426 try {
427 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
428
429 for (String listenerClassName : listenerClassNames) {
430 listenersList.add((ModelListener<Account>)InstanceFactory.newInstance(
431 listenerClassName));
432 }
433
434 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
435 }
436 catch (Exception e) {
437 _log.error(e);
438 }
439 }
440 }
441
442 @BeanReference(type = AccountPersistence.class)
443 protected AccountPersistence accountPersistence;
444 @BeanReference(type = AddressPersistence.class)
445 protected AddressPersistence addressPersistence;
446 @BeanReference(type = BrowserTrackerPersistence.class)
447 protected BrowserTrackerPersistence browserTrackerPersistence;
448 @BeanReference(type = ClassNamePersistence.class)
449 protected ClassNamePersistence classNamePersistence;
450 @BeanReference(type = CompanyPersistence.class)
451 protected CompanyPersistence companyPersistence;
452 @BeanReference(type = ContactPersistence.class)
453 protected ContactPersistence contactPersistence;
454 @BeanReference(type = CountryPersistence.class)
455 protected CountryPersistence countryPersistence;
456 @BeanReference(type = EmailAddressPersistence.class)
457 protected EmailAddressPersistence emailAddressPersistence;
458 @BeanReference(type = GroupPersistence.class)
459 protected GroupPersistence groupPersistence;
460 @BeanReference(type = ImagePersistence.class)
461 protected ImagePersistence imagePersistence;
462 @BeanReference(type = LayoutPersistence.class)
463 protected LayoutPersistence layoutPersistence;
464 @BeanReference(type = LayoutPrototypePersistence.class)
465 protected LayoutPrototypePersistence layoutPrototypePersistence;
466 @BeanReference(type = LayoutSetPersistence.class)
467 protected LayoutSetPersistence layoutSetPersistence;
468 @BeanReference(type = LayoutSetPrototypePersistence.class)
469 protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
470 @BeanReference(type = ListTypePersistence.class)
471 protected ListTypePersistence listTypePersistence;
472 @BeanReference(type = LockPersistence.class)
473 protected LockPersistence lockPersistence;
474 @BeanReference(type = MembershipRequestPersistence.class)
475 protected MembershipRequestPersistence membershipRequestPersistence;
476 @BeanReference(type = OrganizationPersistence.class)
477 protected OrganizationPersistence organizationPersistence;
478 @BeanReference(type = OrgGroupPermissionPersistence.class)
479 protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
480 @BeanReference(type = OrgGroupRolePersistence.class)
481 protected OrgGroupRolePersistence orgGroupRolePersistence;
482 @BeanReference(type = OrgLaborPersistence.class)
483 protected OrgLaborPersistence orgLaborPersistence;
484 @BeanReference(type = PasswordPolicyPersistence.class)
485 protected PasswordPolicyPersistence passwordPolicyPersistence;
486 @BeanReference(type = PasswordPolicyRelPersistence.class)
487 protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
488 @BeanReference(type = PasswordTrackerPersistence.class)
489 protected PasswordTrackerPersistence passwordTrackerPersistence;
490 @BeanReference(type = PermissionPersistence.class)
491 protected PermissionPersistence permissionPersistence;
492 @BeanReference(type = PhonePersistence.class)
493 protected PhonePersistence phonePersistence;
494 @BeanReference(type = PluginSettingPersistence.class)
495 protected PluginSettingPersistence pluginSettingPersistence;
496 @BeanReference(type = PortletPersistence.class)
497 protected PortletPersistence portletPersistence;
498 @BeanReference(type = PortletItemPersistence.class)
499 protected PortletItemPersistence portletItemPersistence;
500 @BeanReference(type = PortletPreferencesPersistence.class)
501 protected PortletPreferencesPersistence portletPreferencesPersistence;
502 @BeanReference(type = RegionPersistence.class)
503 protected RegionPersistence regionPersistence;
504 @BeanReference(type = ReleasePersistence.class)
505 protected ReleasePersistence releasePersistence;
506 @BeanReference(type = ResourcePersistence.class)
507 protected ResourcePersistence resourcePersistence;
508 @BeanReference(type = ResourceActionPersistence.class)
509 protected ResourceActionPersistence resourceActionPersistence;
510 @BeanReference(type = ResourceCodePersistence.class)
511 protected ResourceCodePersistence resourceCodePersistence;
512 @BeanReference(type = ResourcePermissionPersistence.class)
513 protected ResourcePermissionPersistence resourcePermissionPersistence;
514 @BeanReference(type = RolePersistence.class)
515 protected RolePersistence rolePersistence;
516 @BeanReference(type = ServiceComponentPersistence.class)
517 protected ServiceComponentPersistence serviceComponentPersistence;
518 @BeanReference(type = ShardPersistence.class)
519 protected ShardPersistence shardPersistence;
520 @BeanReference(type = SubscriptionPersistence.class)
521 protected SubscriptionPersistence subscriptionPersistence;
522 @BeanReference(type = TicketPersistence.class)
523 protected TicketPersistence ticketPersistence;
524 @BeanReference(type = TeamPersistence.class)
525 protected TeamPersistence teamPersistence;
526 @BeanReference(type = UserPersistence.class)
527 protected UserPersistence userPersistence;
528 @BeanReference(type = UserGroupPersistence.class)
529 protected UserGroupPersistence userGroupPersistence;
530 @BeanReference(type = UserGroupGroupRolePersistence.class)
531 protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
532 @BeanReference(type = UserGroupRolePersistence.class)
533 protected UserGroupRolePersistence userGroupRolePersistence;
534 @BeanReference(type = UserIdMapperPersistence.class)
535 protected UserIdMapperPersistence userIdMapperPersistence;
536 @BeanReference(type = UserTrackerPersistence.class)
537 protected UserTrackerPersistence userTrackerPersistence;
538 @BeanReference(type = UserTrackerPathPersistence.class)
539 protected UserTrackerPathPersistence userTrackerPathPersistence;
540 @BeanReference(type = WebDAVPropsPersistence.class)
541 protected WebDAVPropsPersistence webDAVPropsPersistence;
542 @BeanReference(type = WebsitePersistence.class)
543 protected WebsitePersistence websitePersistence;
544 @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
545 protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
546 @BeanReference(type = WorkflowInstanceLinkPersistence.class)
547 protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
548 private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
549 private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
550 private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
551 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
552 private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
553 }