1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchAccountException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.annotation.BeanReference;
28  import com.liferay.portal.kernel.cache.CacheRegistry;
29  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32  import com.liferay.portal.kernel.dao.orm.FinderPath;
33  import com.liferay.portal.kernel.dao.orm.Query;
34  import com.liferay.portal.kernel.dao.orm.QueryUtil;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.OrderByComparator;
40  import com.liferay.portal.kernel.util.StringUtil;
41  import com.liferay.portal.model.Account;
42  import com.liferay.portal.model.ModelListener;
43  import com.liferay.portal.model.impl.AccountImpl;
44  import com.liferay.portal.model.impl.AccountModelImpl;
45  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  /**
52   * <a href="AccountPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class AccountPersistenceImpl extends BasePersistenceImpl
58      implements AccountPersistence {
59      public static final String FINDER_CLASS_NAME_ENTITY = AccountImpl.class.getName();
60      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
61          ".List";
62      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
63              AccountModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
64              "findAll", new String[0]);
65      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AccountModelImpl.ENTITY_CACHE_ENABLED,
66              AccountModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
67              "countAll", new String[0]);
68  
69      public void cacheResult(Account account) {
70          EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
71              AccountImpl.class, account.getPrimaryKey(), account);
72      }
73  
74      public void cacheResult(List<Account> accounts) {
75          for (Account account : accounts) {
76              if (EntityCacheUtil.getResult(
77                          AccountModelImpl.ENTITY_CACHE_ENABLED,
78                          AccountImpl.class, account.getPrimaryKey(), this) == null) {
79                  cacheResult(account);
80              }
81          }
82      }
83  
84      public void clearCache() {
85          CacheRegistry.clear(AccountImpl.class.getName());
86          EntityCacheUtil.clearCache(AccountImpl.class.getName());
87          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
88          FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
89      }
90  
91      public Account create(long accountId) {
92          Account account = new AccountImpl();
93  
94          account.setNew(true);
95          account.setPrimaryKey(accountId);
96  
97          return account;
98      }
99  
100     public Account remove(long accountId)
101         throws NoSuchAccountException, SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106 
107             Account account = (Account)session.get(AccountImpl.class,
108                     new Long(accountId));
109 
110             if (account == null) {
111                 if (_log.isWarnEnabled()) {
112                     _log.warn("No Account exists with the primary key " +
113                         accountId);
114                 }
115 
116                 throw new NoSuchAccountException(
117                     "No Account exists with the primary key " + accountId);
118             }
119 
120             return remove(account);
121         }
122         catch (NoSuchAccountException nsee) {
123             throw nsee;
124         }
125         catch (Exception e) {
126             throw processException(e);
127         }
128         finally {
129             closeSession(session);
130         }
131     }
132 
133     public Account remove(Account account) throws SystemException {
134         for (ModelListener<Account> listener : listeners) {
135             listener.onBeforeRemove(account);
136         }
137 
138         account = removeImpl(account);
139 
140         for (ModelListener<Account> listener : listeners) {
141             listener.onAfterRemove(account);
142         }
143 
144         return account;
145     }
146 
147     protected Account removeImpl(Account account) throws SystemException {
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             if (account.isCachedModel() || BatchSessionUtil.isEnabled()) {
154                 Object staleObject = session.get(AccountImpl.class,
155                         account.getPrimaryKeyObj());
156 
157                 if (staleObject != null) {
158                     session.evict(staleObject);
159                 }
160             }
161 
162             session.delete(account);
163 
164             session.flush();
165         }
166         catch (Exception e) {
167             throw processException(e);
168         }
169         finally {
170             closeSession(session);
171         }
172 
173         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
174 
175         EntityCacheUtil.removeResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
176             AccountImpl.class, account.getPrimaryKey());
177 
178         return account;
179     }
180 
181     /**
182      * @deprecated Use <code>update(Account account, boolean merge)</code>.
183      */
184     public Account update(Account account) throws SystemException {
185         if (_log.isWarnEnabled()) {
186             _log.warn(
187                 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
188         }
189 
190         return update(account, false);
191     }
192 
193     /**
194      * Add, update, or merge, the entity. This method also calls the model
195      * listeners to trigger the proper events associated with adding, deleting,
196      * or updating an entity.
197      *
198      * @param        account the entity to add, update, or merge
199      * @param        merge boolean value for whether to merge the entity. The
200      *                default value is false. Setting merge to true is more
201      *                expensive and should only be true when account is
202      *                transient. See LEP-5473 for a detailed discussion of this
203      *                method.
204      * @return        true if the portlet can be displayed via Ajax
205      */
206     public Account update(Account account, boolean merge)
207         throws SystemException {
208         boolean isNew = account.isNew();
209 
210         for (ModelListener<Account> listener : listeners) {
211             if (isNew) {
212                 listener.onBeforeCreate(account);
213             }
214             else {
215                 listener.onBeforeUpdate(account);
216             }
217         }
218 
219         account = updateImpl(account, merge);
220 
221         for (ModelListener<Account> listener : listeners) {
222             if (isNew) {
223                 listener.onAfterCreate(account);
224             }
225             else {
226                 listener.onAfterUpdate(account);
227             }
228         }
229 
230         return account;
231     }
232 
233     public Account updateImpl(com.liferay.portal.model.Account account,
234         boolean merge) throws SystemException {
235         Session session = null;
236 
237         try {
238             session = openSession();
239 
240             BatchSessionUtil.update(session, account, merge);
241 
242             account.setNew(false);
243         }
244         catch (Exception e) {
245             throw processException(e);
246         }
247         finally {
248             closeSession(session);
249         }
250 
251         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
252 
253         EntityCacheUtil.putResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
254             AccountImpl.class, account.getPrimaryKey(), account);
255 
256         return account;
257     }
258 
259     public Account findByPrimaryKey(long accountId)
260         throws NoSuchAccountException, SystemException {
261         Account account = fetchByPrimaryKey(accountId);
262 
263         if (account == null) {
264             if (_log.isWarnEnabled()) {
265                 _log.warn("No Account exists with the primary key " +
266                     accountId);
267             }
268 
269             throw new NoSuchAccountException(
270                 "No Account exists with the primary key " + accountId);
271         }
272 
273         return account;
274     }
275 
276     public Account fetchByPrimaryKey(long accountId) throws SystemException {
277         Account account = (Account)EntityCacheUtil.getResult(AccountModelImpl.ENTITY_CACHE_ENABLED,
278                 AccountImpl.class, accountId, this);
279 
280         if (account == null) {
281             Session session = null;
282 
283             try {
284                 session = openSession();
285 
286                 account = (Account)session.get(AccountImpl.class,
287                         new Long(accountId));
288             }
289             catch (Exception e) {
290                 throw processException(e);
291             }
292             finally {
293                 if (account != null) {
294                     cacheResult(account);
295                 }
296 
297                 closeSession(session);
298             }
299         }
300 
301         return account;
302     }
303 
304     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
305         throws SystemException {
306         Session session = null;
307 
308         try {
309             session = openSession();
310 
311             dynamicQuery.compile(session);
312 
313             return dynamicQuery.list();
314         }
315         catch (Exception e) {
316             throw processException(e);
317         }
318         finally {
319             closeSession(session);
320         }
321     }
322 
323     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
324         int start, int end) throws SystemException {
325         Session session = null;
326 
327         try {
328             session = openSession();
329 
330             dynamicQuery.setLimit(start, end);
331 
332             dynamicQuery.compile(session);
333 
334             return dynamicQuery.list();
335         }
336         catch (Exception e) {
337             throw processException(e);
338         }
339         finally {
340             closeSession(session);
341         }
342     }
343 
344     public List<Account> findAll() throws SystemException {
345         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
346     }
347 
348     public List<Account> findAll(int start, int end) throws SystemException {
349         return findAll(start, end, null);
350     }
351 
352     public List<Account> findAll(int start, int end, OrderByComparator obc)
353         throws SystemException {
354         Object[] finderArgs = new Object[] {
355                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
356             };
357 
358         List<Account> list = (List<Account>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
359                 finderArgs, this);
360 
361         if (list == null) {
362             Session session = null;
363 
364             try {
365                 session = openSession();
366 
367                 StringBuilder query = new StringBuilder();
368 
369                 query.append("FROM com.liferay.portal.model.Account ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373                     query.append(obc.getOrderBy());
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 if (obc == null) {
379                     list = (List<Account>)QueryUtil.list(q, getDialect(),
380                             start, end, false);
381 
382                     Collections.sort(list);
383                 }
384                 else {
385                     list = (List<Account>)QueryUtil.list(q, getDialect(),
386                             start, end);
387                 }
388             }
389             catch (Exception e) {
390                 throw processException(e);
391             }
392             finally {
393                 if (list == null) {
394                     list = new ArrayList<Account>();
395                 }
396 
397                 cacheResult(list);
398 
399                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
400 
401                 closeSession(session);
402             }
403         }
404 
405         return list;
406     }
407 
408     public void removeAll() throws SystemException {
409         for (Account account : findAll()) {
410             remove(account);
411         }
412     }
413 
414     public int countAll() throws SystemException {
415         Object[] finderArgs = new Object[0];
416 
417         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
418                 finderArgs, this);
419 
420         if (count == null) {
421             Session session = null;
422 
423             try {
424                 session = openSession();
425 
426                 Query q = session.createQuery(
427                         "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
428 
429                 count = (Long)q.uniqueResult();
430             }
431             catch (Exception e) {
432                 throw processException(e);
433             }
434             finally {
435                 if (count == null) {
436                     count = Long.valueOf(0);
437                 }
438 
439                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
440                     count);
441 
442                 closeSession(session);
443             }
444         }
445 
446         return count.intValue();
447     }
448 
449     public void afterPropertiesSet() {
450         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
451                     com.liferay.portal.util.PropsUtil.get(
452                         "value.object.listener.com.liferay.portal.model.Account")));
453 
454         if (listenerClassNames.length > 0) {
455             try {
456                 List<ModelListener<Account>> listenersList = new ArrayList<ModelListener<Account>>();
457 
458                 for (String listenerClassName : listenerClassNames) {
459                     listenersList.add((ModelListener<Account>)Class.forName(
460                             listenerClassName).newInstance());
461                 }
462 
463                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
464             }
465             catch (Exception e) {
466                 _log.error(e);
467             }
468         }
469     }
470 
471     @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
472     protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
473     @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
474     protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
475     @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
476     protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
477     @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
478     protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
479     @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
480     protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
481     @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
482     protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
483     @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
484     protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
485     @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
486     protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
487     @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
488     protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
489     @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
490     protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
491     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
492     protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
493     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
494     protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
495     @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
496     protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
497     @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
498     protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
499     @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
500     protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
501     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
502     protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
503     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
504     protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
505     @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
506     protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
507     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
508     protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
509     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
510     protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
511     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
512     protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
513     @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
514     protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
515     @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
516     protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
517     @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
518     protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
519     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
520     protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
521     @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
522     protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
523     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
524     protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
525     @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
526     protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
527     @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
528     protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
529     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
530     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
531     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
532     protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
533     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
534     protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
535     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
536     protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
537     @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
538     protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
539     @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
540     protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
541     @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
542     protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
543     @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
544     protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
545     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
546     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
547     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
548     protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
549     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
550     protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
551     @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
552     protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
553     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
554     protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
555     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
556     protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
557     @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
558     protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
559     @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
560     protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
561     private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
562 }