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.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.Account;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.impl.AccountImpl;
41  import com.liferay.portal.model.impl.AccountModelImpl;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  /**
50   * <a href="AccountPersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class AccountPersistenceImpl extends BasePersistenceImpl
56      implements AccountPersistence {
57      public Account create(long accountId) {
58          Account account = new AccountImpl();
59  
60          account.setNew(true);
61          account.setPrimaryKey(accountId);
62  
63          return account;
64      }
65  
66      public Account remove(long accountId)
67          throws NoSuchAccountException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              Account account = (Account)session.get(AccountImpl.class,
74                      new Long(accountId));
75  
76              if (account == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No Account exists with the primary key " +
79                          accountId);
80                  }
81  
82                  throw new NoSuchAccountException(
83                      "No Account exists with the primary key " + accountId);
84              }
85  
86              return remove(account);
87          }
88          catch (NoSuchAccountException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public Account remove(Account account) throws SystemException {
100         for (ModelListener listener : listeners) {
101             listener.onBeforeRemove(account);
102         }
103 
104         account = removeImpl(account);
105 
106         for (ModelListener listener : listeners) {
107             listener.onAfterRemove(account);
108         }
109 
110         return account;
111     }
112 
113     protected Account removeImpl(Account account) throws SystemException {
114         Session session = null;
115 
116         try {
117             session = openSession();
118 
119             if (BatchSessionUtil.isEnabled()) {
120                 Object staleObject = session.get(AccountImpl.class,
121                         account.getPrimaryKeyObj());
122 
123                 if (staleObject != null) {
124                     session.evict(staleObject);
125                 }
126             }
127 
128             session.delete(account);
129 
130             session.flush();
131 
132             return account;
133         }
134         catch (Exception e) {
135             throw processException(e);
136         }
137         finally {
138             closeSession(session);
139 
140             FinderCacheUtil.clearCache(Account.class.getName());
141         }
142     }
143 
144     /**
145      * @deprecated Use <code>update(Account account, boolean merge)</code>.
146      */
147     public Account update(Account account) throws SystemException {
148         if (_log.isWarnEnabled()) {
149             _log.warn(
150                 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
151         }
152 
153         return update(account, false);
154     }
155 
156     /**
157      * Add, update, or merge, the entity. This method also calls the model
158      * listeners to trigger the proper events associated with adding, deleting,
159      * or updating an entity.
160      *
161      * @param        account the entity to add, update, or merge
162      * @param        merge boolean value for whether to merge the entity. The
163      *                default value is false. Setting merge to true is more
164      *                expensive and should only be true when account is
165      *                transient. See LEP-5473 for a detailed discussion of this
166      *                method.
167      * @return        true if the portlet can be displayed via Ajax
168      */
169     public Account update(Account account, boolean merge)
170         throws SystemException {
171         boolean isNew = account.isNew();
172 
173         for (ModelListener listener : listeners) {
174             if (isNew) {
175                 listener.onBeforeCreate(account);
176             }
177             else {
178                 listener.onBeforeUpdate(account);
179             }
180         }
181 
182         account = updateImpl(account, merge);
183 
184         for (ModelListener listener : listeners) {
185             if (isNew) {
186                 listener.onAfterCreate(account);
187             }
188             else {
189                 listener.onAfterUpdate(account);
190             }
191         }
192 
193         return account;
194     }
195 
196     public Account updateImpl(com.liferay.portal.model.Account account,
197         boolean merge) throws SystemException {
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             BatchSessionUtil.update(session, account, merge);
204 
205             account.setNew(false);
206 
207             return account;
208         }
209         catch (Exception e) {
210             throw processException(e);
211         }
212         finally {
213             closeSession(session);
214 
215             FinderCacheUtil.clearCache(Account.class.getName());
216         }
217     }
218 
219     public Account findByPrimaryKey(long accountId)
220         throws NoSuchAccountException, SystemException {
221         Account account = fetchByPrimaryKey(accountId);
222 
223         if (account == null) {
224             if (_log.isWarnEnabled()) {
225                 _log.warn("No Account exists with the primary key " +
226                     accountId);
227             }
228 
229             throw new NoSuchAccountException(
230                 "No Account exists with the primary key " + accountId);
231         }
232 
233         return account;
234     }
235 
236     public Account fetchByPrimaryKey(long accountId) throws SystemException {
237         Session session = null;
238 
239         try {
240             session = openSession();
241 
242             return (Account)session.get(AccountImpl.class, new Long(accountId));
243         }
244         catch (Exception e) {
245             throw processException(e);
246         }
247         finally {
248             closeSession(session);
249         }
250     }
251 
252     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
253         throws SystemException {
254         Session session = null;
255 
256         try {
257             session = openSession();
258 
259             dynamicQuery.compile(session);
260 
261             return dynamicQuery.list();
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
272         int start, int end) throws SystemException {
273         Session session = null;
274 
275         try {
276             session = openSession();
277 
278             dynamicQuery.setLimit(start, end);
279 
280             dynamicQuery.compile(session);
281 
282             return dynamicQuery.list();
283         }
284         catch (Exception e) {
285             throw processException(e);
286         }
287         finally {
288             closeSession(session);
289         }
290     }
291 
292     public List<Account> findAll() throws SystemException {
293         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
294     }
295 
296     public List<Account> findAll(int start, int end) throws SystemException {
297         return findAll(start, end, null);
298     }
299 
300     public List<Account> findAll(int start, int end, OrderByComparator obc)
301         throws SystemException {
302         boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
303         String finderClassName = Account.class.getName();
304         String finderMethodName = "findAll";
305         String[] finderParams = new String[] {
306                 "java.lang.Integer", "java.lang.Integer",
307                 "com.liferay.portal.kernel.util.OrderByComparator"
308             };
309         Object[] finderArgs = new Object[] {
310                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
311             };
312 
313         Object result = null;
314 
315         if (finderClassNameCacheEnabled) {
316             result = FinderCacheUtil.getResult(finderClassName,
317                     finderMethodName, finderParams, finderArgs, this);
318         }
319 
320         if (result == null) {
321             Session session = null;
322 
323             try {
324                 session = openSession();
325 
326                 StringBuilder query = new StringBuilder();
327 
328                 query.append("FROM com.liferay.portal.model.Account ");
329 
330                 if (obc != null) {
331                     query.append("ORDER BY ");
332                     query.append(obc.getOrderBy());
333                 }
334 
335                 Query q = session.createQuery(query.toString());
336 
337                 List<Account> list = null;
338 
339                 if (obc == null) {
340                     list = (List<Account>)QueryUtil.list(q, getDialect(),
341                             start, end, false);
342 
343                     Collections.sort(list);
344                 }
345                 else {
346                     list = (List<Account>)QueryUtil.list(q, getDialect(),
347                             start, end);
348                 }
349 
350                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
351                     finderClassName, finderMethodName, finderParams,
352                     finderArgs, list);
353 
354                 return list;
355             }
356             catch (Exception e) {
357                 throw processException(e);
358             }
359             finally {
360                 closeSession(session);
361             }
362         }
363         else {
364             return (List<Account>)result;
365         }
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         boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
376         String finderClassName = Account.class.getName();
377         String finderMethodName = "countAll";
378         String[] finderParams = new String[] {  };
379         Object[] finderArgs = new Object[] {  };
380 
381         Object result = null;
382 
383         if (finderClassNameCacheEnabled) {
384             result = FinderCacheUtil.getResult(finderClassName,
385                     finderMethodName, finderParams, finderArgs, this);
386         }
387 
388         if (result == null) {
389             Session session = null;
390 
391             try {
392                 session = openSession();
393 
394                 Query q = session.createQuery(
395                         "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
396 
397                 Long count = null;
398 
399                 Iterator<Long> itr = q.list().iterator();
400 
401                 if (itr.hasNext()) {
402                     count = itr.next();
403                 }
404 
405                 if (count == null) {
406                     count = new Long(0);
407                 }
408 
409                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
410                     finderClassName, finderMethodName, finderParams,
411                     finderArgs, count);
412 
413                 return count.intValue();
414             }
415             catch (Exception e) {
416                 throw processException(e);
417             }
418             finally {
419                 closeSession(session);
420             }
421         }
422         else {
423             return ((Long)result).intValue();
424         }
425     }
426 
427     public void afterPropertiesSet() {
428         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
429                     com.liferay.portal.util.PropsUtil.get(
430                         "value.object.listener.com.liferay.portal.model.Account")));
431 
432         if (listenerClassNames.length > 0) {
433             try {
434                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
435 
436                 for (String listenerClassName : listenerClassNames) {
437                     listenersList.add((ModelListener)Class.forName(
438                             listenerClassName).newInstance());
439                 }
440 
441                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
442             }
443             catch (Exception e) {
444                 _log.error(e);
445             }
446         }
447     }
448 
449     @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
450     protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
451     @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
452     protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
453     @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
454     protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
455     @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
456     protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
457     @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
458     protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
459     @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
460     protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
461     @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
462     protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
463     @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
464     protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
465     @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
466     protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
467     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
468     protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
469     @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
470     protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
471     @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
472     protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
473     @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
474     protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
475     @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
476     protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
477     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
478     protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
479     @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
480     protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
481     @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
482     protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
483     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
484     protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
485     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
486     protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
487     @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
488     protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
489     @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
490     protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
491     @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
492     protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
493     @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
494     protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
495     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
496     protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
497     @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
498     protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
499     @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
500     protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
501     @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
502     protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
503     @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
504     protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
505     @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
506     protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
507     @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
508     protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
509     @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
510     protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
511     @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
512     protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
513     @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
514     protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
515     @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
516     protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
517     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
518     protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
519     @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
520     protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
521     @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
522     protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
523     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
524     protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
525     @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
526     protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
527     @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
528     protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
529     @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
530     protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
531     private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
532 }