1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.SystemException;
20  import com.liferay.portal.kernel.annotation.BeanReference;
21  import com.liferay.portal.kernel.cache.CacheRegistry;
22  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
24  import com.liferay.portal.kernel.dao.orm.FinderPath;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
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     /**
190      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
191      */
192     public Account update(Account account) throws SystemException {
193         if (_log.isWarnEnabled()) {
194             _log.warn(
195                 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
196         }
197 
198         return update(account, false);
199     }
200 
201     public Account updateImpl(com.liferay.portal.model.Account account,
202         boolean merge) throws SystemException {
203         account = toUnwrappedModel(account);
204 
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             BatchSessionUtil.update(session, account, merge);
211 
212             account.setNew(false);
213         }
214         catch (Exception e) {
215             throw processException(e);
216         }
217         finally {
218             closeSession(session);
219         }
220 
221         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
222 
223         EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
224             AccountImpl.class, account.getPrimaryKey(), account);
225 
226         return account;
227     }
228 
229     protected Account toUnwrappedModel(Account account) {
230         if (account instanceof AccountImpl) {
231             return account;
232         }
233 
234         AccountImpl accountImpl = new AccountImpl();
235 
236         accountImpl.setNew(account.isNew());
237         accountImpl.setPrimaryKey(account.getPrimaryKey());
238 
239         accountImpl.setAccountId(account.getAccountId());
240         accountImpl.setCompanyId(account.getCompanyId());
241         accountImpl.setUserId(account.getUserId());
242         accountImpl.setUserName(account.getUserName());
243         accountImpl.setCreateDate(account.getCreateDate());
244         accountImpl.setModifiedDate(account.getModifiedDate());
245         accountImpl.setParentAccountId(account.getParentAccountId());
246         accountImpl.setName(account.getName());
247         accountImpl.setLegalName(account.getLegalName());
248         accountImpl.setLegalId(account.getLegalId());
249         accountImpl.setLegalType(account.getLegalType());
250         accountImpl.setSicCode(account.getSicCode());
251         accountImpl.setTickerSymbol(account.getTickerSymbol());
252         accountImpl.setIndustry(account.getIndustry());
253         accountImpl.setType(account.getType());
254         accountImpl.setSize(account.getSize());
255 
256         return accountImpl;
257     }
258 
259     public Account findByPrimaryKey(Serializable primaryKey)
260         throws NoSuchModelException, SystemException {
261         return findByPrimaryKey(((Long)primaryKey).longValue());
262     }
263 
264     public Account findByPrimaryKey(long accountId)
265         throws NoSuchAccountException, SystemException {
266         Account account = fetchByPrimaryKey(accountId);
267 
268         if (account == null) {
269             if (_log.isWarnEnabled()) {
270                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + accountId);
271             }
272 
273             throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
274                 accountId);
275         }
276 
277         return account;
278     }
279 
280     public Account fetchByPrimaryKey(Serializable primaryKey)
281         throws SystemException {
282         return fetchByPrimaryKey(((Long)primaryKey).longValue());
283     }
284 
285     public Account fetchByPrimaryKey(long accountId) throws SystemException {
286         Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
287                 AccountImpl.class, accountId, this);
288 
289         if (account == null) {
290             Session session = null;
291 
292             try {
293                 session = openSession();
294 
295                 account = (Account)session.get(AccountImpl.class,
296                         new Long(accountId));
297             }
298             catch (Exception e) {
299                 throw processException(e);
300             }
301             finally {
302                 if (account != null) {
303                     cacheResult(account);
304                 }
305 
306                 closeSession(session);
307             }
308         }
309 
310         return account;
311     }
312 
313     public List<Account> findAll() throws SystemException {
314         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
315     }
316 
317     public List<Account> findAll(int start, int end) throws SystemException {
318         return findAll(start, end, null);
319     }
320 
321     public List<Account> findAll(int start, int end,
322         OrderByComparator orderByComparator) throws SystemException {
323         Object[] finderArgs = new Object[] {
324                 String.valueOf(start), String.valueOf(end),
325                 String.valueOf(orderByComparator)
326             };
327 
328         List<Account> list = (List<Account>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
329                 finderArgs, this);
330 
331         if (list == null) {
332             Session session = null;
333 
334             try {
335                 session = openSession();
336 
337                 StringBundler query = null;
338                 String sql = null;
339 
340                 if (orderByComparator != null) {
341                     query = new StringBundler(2 +
342                             (orderByComparator.getOrderByFields().length * 3));
343 
344                     query.append(_SQL_SELECT_ACCOUNT);
345 
346                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
347                         orderByComparator);
348 
349                     sql = query.toString();
350                 }
351 
352                 sql = _SQL_SELECT_ACCOUNT;
353 
354                 Query q = session.createQuery(sql);
355 
356                 if (orderByComparator == null) {
357                     list = (List<Account>)QueryUtil.list(q, getDialect(),
358                             start, end, false);
359 
360                     Collections.sort(list);
361                 }
362                 else {
363                     list = (List<Account>)QueryUtil.list(q, getDialect(),
364                             start, end);
365                 }
366             }
367             catch (Exception e) {
368                 throw processException(e);
369             }
370             finally {
371                 if (list == null) {
372                     list = new ArrayList<Account>();
373                 }
374 
375                 cacheResult(list);
376 
377                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
378 
379                 closeSession(session);
380             }
381         }
382 
383         return list;
384     }
385 
386     public void removeAll() throws SystemException {
387         for (Account account : findAll()) {
388             remove(account);
389         }
390     }
391 
392     public int countAll() throws SystemException {
393         Object[] finderArgs = new Object[0];
394 
395         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
396                 finderArgs, this);
397 
398         if (count == null) {
399             Session session = null;
400 
401             try {
402                 session = openSession();
403 
404                 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
405 
406                 count = (Long)q.uniqueResult();
407             }
408             catch (Exception e) {
409                 throw processException(e);
410             }
411             finally {
412                 if (count == null) {
413                     count = Long.valueOf(0);
414                 }
415 
416                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
417                     count);
418 
419                 closeSession(session);
420             }
421         }
422 
423         return count.intValue();
424     }
425 
426     public void afterPropertiesSet() {
427         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
428                     com.liferay.portal.util.PropsUtil.get(
429                         "value.object.listener.com.liferay.portal.model.Account")));
430 
431         if (listenerClassNames.length > 0) {
432             try {
433                 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
434 
435                 for (String listenerClassName : listenerClassNames) {
436                     listenersList.add((ModelListener<Account>)Class.forName(
437                             listenerClassName).newInstance());
438                 }
439 
440                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
441             }
442             catch (Exception e) {
443                 _log.error(e);
444             }
445         }
446     }
447 
448     @BeanReference(type = AccountPersistence.class)
449     protected AccountPersistence accountPersistence;
450     @BeanReference(type = AddressPersistence.class)
451     protected AddressPersistence addressPersistence;
452     @BeanReference(type = BrowserTrackerPersistence.class)
453     protected BrowserTrackerPersistence browserTrackerPersistence;
454     @BeanReference(type = ClassNamePersistence.class)
455     protected ClassNamePersistence classNamePersistence;
456     @BeanReference(type = CompanyPersistence.class)
457     protected CompanyPersistence companyPersistence;
458     @BeanReference(type = ContactPersistence.class)
459     protected ContactPersistence contactPersistence;
460     @BeanReference(type = CountryPersistence.class)
461     protected CountryPersistence countryPersistence;
462     @BeanReference(type = EmailAddressPersistence.class)
463     protected EmailAddressPersistence emailAddressPersistence;
464     @BeanReference(type = GroupPersistence.class)
465     protected GroupPersistence groupPersistence;
466     @BeanReference(type = ImagePersistence.class)
467     protected ImagePersistence imagePersistence;
468     @BeanReference(type = LayoutPersistence.class)
469     protected LayoutPersistence layoutPersistence;
470     @BeanReference(type = LayoutSetPersistence.class)
471     protected LayoutSetPersistence layoutSetPersistence;
472     @BeanReference(type = ListTypePersistence.class)
473     protected ListTypePersistence listTypePersistence;
474     @BeanReference(type = LockPersistence.class)
475     protected LockPersistence lockPersistence;
476     @BeanReference(type = MembershipRequestPersistence.class)
477     protected MembershipRequestPersistence membershipRequestPersistence;
478     @BeanReference(type = OrganizationPersistence.class)
479     protected OrganizationPersistence organizationPersistence;
480     @BeanReference(type = OrgGroupPermissionPersistence.class)
481     protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
482     @BeanReference(type = OrgGroupRolePersistence.class)
483     protected OrgGroupRolePersistence orgGroupRolePersistence;
484     @BeanReference(type = OrgLaborPersistence.class)
485     protected OrgLaborPersistence orgLaborPersistence;
486     @BeanReference(type = PasswordPolicyPersistence.class)
487     protected PasswordPolicyPersistence passwordPolicyPersistence;
488     @BeanReference(type = PasswordPolicyRelPersistence.class)
489     protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
490     @BeanReference(type = PasswordTrackerPersistence.class)
491     protected PasswordTrackerPersistence passwordTrackerPersistence;
492     @BeanReference(type = PermissionPersistence.class)
493     protected PermissionPersistence permissionPersistence;
494     @BeanReference(type = PhonePersistence.class)
495     protected PhonePersistence phonePersistence;
496     @BeanReference(type = PluginSettingPersistence.class)
497     protected PluginSettingPersistence pluginSettingPersistence;
498     @BeanReference(type = PortletPersistence.class)
499     protected PortletPersistence portletPersistence;
500     @BeanReference(type = PortletItemPersistence.class)
501     protected PortletItemPersistence portletItemPersistence;
502     @BeanReference(type = PortletPreferencesPersistence.class)
503     protected PortletPreferencesPersistence portletPreferencesPersistence;
504     @BeanReference(type = RegionPersistence.class)
505     protected RegionPersistence regionPersistence;
506     @BeanReference(type = ReleasePersistence.class)
507     protected ReleasePersistence releasePersistence;
508     @BeanReference(type = ResourcePersistence.class)
509     protected ResourcePersistence resourcePersistence;
510     @BeanReference(type = ResourceActionPersistence.class)
511     protected ResourceActionPersistence resourceActionPersistence;
512     @BeanReference(type = ResourceCodePersistence.class)
513     protected ResourceCodePersistence resourceCodePersistence;
514     @BeanReference(type = ResourcePermissionPersistence.class)
515     protected ResourcePermissionPersistence resourcePermissionPersistence;
516     @BeanReference(type = RolePersistence.class)
517     protected RolePersistence rolePersistence;
518     @BeanReference(type = ServiceComponentPersistence.class)
519     protected ServiceComponentPersistence serviceComponentPersistence;
520     @BeanReference(type = ShardPersistence.class)
521     protected ShardPersistence shardPersistence;
522     @BeanReference(type = SubscriptionPersistence.class)
523     protected SubscriptionPersistence subscriptionPersistence;
524     @BeanReference(type = UserPersistence.class)
525     protected UserPersistence userPersistence;
526     @BeanReference(type = UserGroupPersistence.class)
527     protected UserGroupPersistence userGroupPersistence;
528     @BeanReference(type = UserGroupGroupRolePersistence.class)
529     protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
530     @BeanReference(type = UserGroupRolePersistence.class)
531     protected UserGroupRolePersistence userGroupRolePersistence;
532     @BeanReference(type = UserIdMapperPersistence.class)
533     protected UserIdMapperPersistence userIdMapperPersistence;
534     @BeanReference(type = UserTrackerPersistence.class)
535     protected UserTrackerPersistence userTrackerPersistence;
536     @BeanReference(type = UserTrackerPathPersistence.class)
537     protected UserTrackerPathPersistence userTrackerPathPersistence;
538     @BeanReference(type = WebDAVPropsPersistence.class)
539     protected WebDAVPropsPersistence webDAVPropsPersistence;
540     @BeanReference(type = WebsitePersistence.class)
541     protected WebsitePersistence websitePersistence;
542     private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
543     private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
544     private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
545     private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
546     private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
547 }