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.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  /**
48   * <a href="AccountPersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * <p>
51   * ServiceBuilder generated this class. Modifications in this class will be
52   * overwritten the next time is generated.
53   * </p>
54   *
55   * @author    Brian Wing Shun Chan
56   * @see       AccountPersistence
57   * @see       AccountUtil
58   * @generated
59   */
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     protected Account removeImpl(Account account) throws SystemException {
146         account = toUnwrappedModel(account);
147 
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             BatchSessionUtil.delete(session, account);
154         }
155         catch (Exception e) {
156             throw processException(e);
157         }
158         finally {
159             closeSession(session);
160         }
161 
162         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
163 
164         EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
165             AccountImpl.class, account.getPrimaryKey());
166 
167         return account;
168     }
169 
170     /**
171      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
172      */
173     public Account update(Account account) throws SystemException {
174         if (_log.isWarnEnabled()) {
175             _log.warn(
176                 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
177         }
178 
179         return update(account, false);
180     }
181 
182     public Account updateImpl(com.liferay.portal.model.Account account,
183         boolean merge) throws SystemException {
184         account = toUnwrappedModel(account);
185 
186         Session session = null;
187 
188         try {
189             session = openSession();
190 
191             BatchSessionUtil.update(session, account, merge);
192 
193             account.setNew(false);
194         }
195         catch (Exception e) {
196             throw processException(e);
197         }
198         finally {
199             closeSession(session);
200         }
201 
202         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
203 
204         EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
205             AccountImpl.class, account.getPrimaryKey(), account);
206 
207         return account;
208     }
209 
210     protected Account toUnwrappedModel(Account account) {
211         if (account instanceof AccountImpl) {
212             return account;
213         }
214 
215         AccountImpl accountImpl = new AccountImpl();
216 
217         accountImpl.setNew(account.isNew());
218         accountImpl.setPrimaryKey(account.getPrimaryKey());
219 
220         accountImpl.setAccountId(account.getAccountId());
221         accountImpl.setCompanyId(account.getCompanyId());
222         accountImpl.setUserId(account.getUserId());
223         accountImpl.setUserName(account.getUserName());
224         accountImpl.setCreateDate(account.getCreateDate());
225         accountImpl.setModifiedDate(account.getModifiedDate());
226         accountImpl.setParentAccountId(account.getParentAccountId());
227         accountImpl.setName(account.getName());
228         accountImpl.setLegalName(account.getLegalName());
229         accountImpl.setLegalId(account.getLegalId());
230         accountImpl.setLegalType(account.getLegalType());
231         accountImpl.setSicCode(account.getSicCode());
232         accountImpl.setTickerSymbol(account.getTickerSymbol());
233         accountImpl.setIndustry(account.getIndustry());
234         accountImpl.setType(account.getType());
235         accountImpl.setSize(account.getSize());
236 
237         return accountImpl;
238     }
239 
240     public Account findByPrimaryKey(Serializable primaryKey)
241         throws NoSuchModelException, SystemException {
242         return findByPrimaryKey(((Long)primaryKey).longValue());
243     }
244 
245     public Account findByPrimaryKey(long accountId)
246         throws NoSuchAccountException, SystemException {
247         Account account = fetchByPrimaryKey(accountId);
248 
249         if (account == null) {
250             if (_log.isWarnEnabled()) {
251                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + accountId);
252             }
253 
254             throw new NoSuchAccountException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
255                 accountId);
256         }
257 
258         return account;
259     }
260 
261     public Account fetchByPrimaryKey(Serializable primaryKey)
262         throws SystemException {
263         return fetchByPrimaryKey(((Long)primaryKey).longValue());
264     }
265 
266     public Account fetchByPrimaryKey(long accountId) throws SystemException {
267         Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
268                 AccountImpl.class, accountId, this);
269 
270         if (account == null) {
271             Session session = null;
272 
273             try {
274                 session = openSession();
275 
276                 account = (Account)session.get(AccountImpl.class,
277                         new Long(accountId));
278             }
279             catch (Exception e) {
280                 throw processException(e);
281             }
282             finally {
283                 if (account != null) {
284                     cacheResult(account);
285                 }
286 
287                 closeSession(session);
288             }
289         }
290 
291         return account;
292     }
293 
294     public List<Account> findAll() throws SystemException {
295         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
296     }
297 
298     public List<Account> findAll(int start, int end) throws SystemException {
299         return findAll(start, end, null);
300     }
301 
302     public List<Account> findAll(int start, int end,
303         OrderByComparator orderByComparator) throws SystemException {
304         Object[] finderArgs = new Object[] {
305                 String.valueOf(start), String.valueOf(end),
306                 String.valueOf(orderByComparator)
307             };
308 
309         List<Account> list = (List<Account>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
310                 finderArgs, this);
311 
312         if (list == null) {
313             StringBundler query = null;
314             String sql = null;
315 
316             if (orderByComparator != null) {
317                 query = new StringBundler(2 +
318                         (orderByComparator.getOrderByFields().length * 3));
319 
320                 query.append(_SQL_SELECT_ACCOUNT);
321 
322                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
323                     orderByComparator);
324 
325                 sql = query.toString();
326             }
327             else {
328                 sql = _SQL_SELECT_ACCOUNT;
329             }
330 
331             Session session = null;
332 
333             try {
334                 session = openSession();
335 
336                 Query q = session.createQuery(sql);
337 
338                 if (orderByComparator == null) {
339                     list = (List<Account>)QueryUtil.list(q, getDialect(),
340                             start, end, false);
341 
342                     Collections.sort(list);
343                 }
344                 else {
345                     list = (List<Account>)QueryUtil.list(q, getDialect(),
346                             start, end);
347                 }
348             }
349             catch (Exception e) {
350                 throw processException(e);
351             }
352             finally {
353                 if (list == null) {
354                     list = new ArrayList<Account>();
355                 }
356 
357                 cacheResult(list);
358 
359                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
360 
361                 closeSession(session);
362             }
363         }
364 
365         return list;
366     }
367 
368     public void removeAll() throws SystemException {
369         for (Account account : findAll()) {
370             remove(account);
371         }
372     }
373 
374     public int countAll() throws SystemException {
375         Object[] finderArgs = new Object[0];
376 
377         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
378                 finderArgs, this);
379 
380         if (count == null) {
381             Session session = null;
382 
383             try {
384                 session = openSession();
385 
386                 Query q = session.createQuery(_SQL_COUNT_ACCOUNT);
387 
388                 count = (Long)q.uniqueResult();
389             }
390             catch (Exception e) {
391                 throw processException(e);
392             }
393             finally {
394                 if (count == null) {
395                     count = Long.valueOf(0);
396                 }
397 
398                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
399                     count);
400 
401                 closeSession(session);
402             }
403         }
404 
405         return count.intValue();
406     }
407 
408     public void afterPropertiesSet() {
409         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
410                     com.liferay.portal.util.PropsUtil.get(
411                         "value.object.listener.com.liferay.portal.model.Account")));
412 
413         if (listenerClassNames.length > 0) {
414             try {
415                 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
416 
417                 for (String listenerClassName : listenerClassNames) {
418                     listenersList.add((ModelListener<Account>)InstanceFactory.newInstance(
419                             listenerClassName));
420                 }
421 
422                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
423             }
424             catch (Exception e) {
425                 _log.error(e);
426             }
427         }
428     }
429 
430     public void destroy() {
431         EntityCacheUtil.removeCache(AccountImpl.class.getName());
432         FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
433         FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
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 = LayoutSetPersistence.class)
459     protected LayoutSetPersistence layoutSetPersistence;
460     @BeanReference(type = ListTypePersistence.class)
461     protected ListTypePersistence listTypePersistence;
462     @BeanReference(type = LockPersistence.class)
463     protected LockPersistence lockPersistence;
464     @BeanReference(type = MembershipRequestPersistence.class)
465     protected MembershipRequestPersistence membershipRequestPersistence;
466     @BeanReference(type = OrganizationPersistence.class)
467     protected OrganizationPersistence organizationPersistence;
468     @BeanReference(type = OrgGroupPermissionPersistence.class)
469     protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
470     @BeanReference(type = OrgGroupRolePersistence.class)
471     protected OrgGroupRolePersistence orgGroupRolePersistence;
472     @BeanReference(type = OrgLaborPersistence.class)
473     protected OrgLaborPersistence orgLaborPersistence;
474     @BeanReference(type = PasswordPolicyPersistence.class)
475     protected PasswordPolicyPersistence passwordPolicyPersistence;
476     @BeanReference(type = PasswordPolicyRelPersistence.class)
477     protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
478     @BeanReference(type = PasswordTrackerPersistence.class)
479     protected PasswordTrackerPersistence passwordTrackerPersistence;
480     @BeanReference(type = PermissionPersistence.class)
481     protected PermissionPersistence permissionPersistence;
482     @BeanReference(type = PhonePersistence.class)
483     protected PhonePersistence phonePersistence;
484     @BeanReference(type = PluginSettingPersistence.class)
485     protected PluginSettingPersistence pluginSettingPersistence;
486     @BeanReference(type = PortletPersistence.class)
487     protected PortletPersistence portletPersistence;
488     @BeanReference(type = PortletItemPersistence.class)
489     protected PortletItemPersistence portletItemPersistence;
490     @BeanReference(type = PortletPreferencesPersistence.class)
491     protected PortletPreferencesPersistence portletPreferencesPersistence;
492     @BeanReference(type = RegionPersistence.class)
493     protected RegionPersistence regionPersistence;
494     @BeanReference(type = ReleasePersistence.class)
495     protected ReleasePersistence releasePersistence;
496     @BeanReference(type = ResourcePersistence.class)
497     protected ResourcePersistence resourcePersistence;
498     @BeanReference(type = ResourceActionPersistence.class)
499     protected ResourceActionPersistence resourceActionPersistence;
500     @BeanReference(type = ResourceCodePersistence.class)
501     protected ResourceCodePersistence resourceCodePersistence;
502     @BeanReference(type = ResourcePermissionPersistence.class)
503     protected ResourcePermissionPersistence resourcePermissionPersistence;
504     @BeanReference(type = RolePersistence.class)
505     protected RolePersistence rolePersistence;
506     @BeanReference(type = ServiceComponentPersistence.class)
507     protected ServiceComponentPersistence serviceComponentPersistence;
508     @BeanReference(type = ShardPersistence.class)
509     protected ShardPersistence shardPersistence;
510     @BeanReference(type = SubscriptionPersistence.class)
511     protected SubscriptionPersistence subscriptionPersistence;
512     @BeanReference(type = UserPersistence.class)
513     protected UserPersistence userPersistence;
514     @BeanReference(type = UserGroupPersistence.class)
515     protected UserGroupPersistence userGroupPersistence;
516     @BeanReference(type = UserGroupGroupRolePersistence.class)
517     protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
518     @BeanReference(type = UserGroupRolePersistence.class)
519     protected UserGroupRolePersistence userGroupRolePersistence;
520     @BeanReference(type = UserIdMapperPersistence.class)
521     protected UserIdMapperPersistence userIdMapperPersistence;
522     @BeanReference(type = UserTrackerPersistence.class)
523     protected UserTrackerPersistence userTrackerPersistence;
524     @BeanReference(type = UserTrackerPathPersistence.class)
525     protected UserTrackerPathPersistence userTrackerPathPersistence;
526     @BeanReference(type = WebDAVPropsPersistence.class)
527     protected WebDAVPropsPersistence webDAVPropsPersistence;
528     @BeanReference(type = WebsitePersistence.class)
529     protected WebsitePersistence websitePersistence;
530     private static final String _SQL_SELECT_ACCOUNT = "SELECT account FROM Account account";
531     private static final String _SQL_COUNT_ACCOUNT = "SELECT COUNT(account) FROM Account account";
532     private static final String _ORDER_BY_ENTITY_ALIAS = "account.";
533     private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Account exists with the primary key ";
534     private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
535 }