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