1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchAccountException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
25  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
26  import com.liferay.portal.kernel.dao.orm.Query;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
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.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * <a href="AccountPersistenceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class AccountPersistenceImpl extends BasePersistenceImpl
52      implements AccountPersistence {
53      public Account create(long accountId) {
54          Account account = new AccountImpl();
55  
56          account.setNew(true);
57          account.setPrimaryKey(accountId);
58  
59          return account;
60      }
61  
62      public Account remove(long accountId)
63          throws NoSuchAccountException, SystemException {
64          Session session = null;
65  
66          try {
67              session = openSession();
68  
69              Account account = (Account)session.get(AccountImpl.class,
70                      new Long(accountId));
71  
72              if (account == null) {
73                  if (_log.isWarnEnabled()) {
74                      _log.warn("No Account exists with the primary key " +
75                          accountId);
76                  }
77  
78                  throw new NoSuchAccountException(
79                      "No Account exists with the primary key " + accountId);
80              }
81  
82              return remove(account);
83          }
84          catch (NoSuchAccountException nsee) {
85              throw nsee;
86          }
87          catch (Exception e) {
88              throw processException(e);
89          }
90          finally {
91              closeSession(session);
92          }
93      }
94  
95      public Account remove(Account account) throws SystemException {
96          for (ModelListener listener : listeners) {
97              listener.onBeforeRemove(account);
98          }
99  
100         account = removeImpl(account);
101 
102         for (ModelListener listener : listeners) {
103             listener.onAfterRemove(account);
104         }
105 
106         return account;
107     }
108 
109     protected Account removeImpl(Account account) throws SystemException {
110         Session session = null;
111 
112         try {
113             session = openSession();
114 
115             if (BatchSessionUtil.isEnabled()) {
116                 Object staleObject = session.get(AccountImpl.class,
117                         account.getPrimaryKeyObj());
118 
119                 if (staleObject != null) {
120                     session.evict(staleObject);
121                 }
122             }
123 
124             session.delete(account);
125 
126             session.flush();
127 
128             return account;
129         }
130         catch (Exception e) {
131             throw processException(e);
132         }
133         finally {
134             closeSession(session);
135 
136             FinderCacheUtil.clearCache(Account.class.getName());
137         }
138     }
139 
140     /**
141      * @deprecated Use <code>update(Account account, boolean merge)</code>.
142      */
143     public Account update(Account account) throws SystemException {
144         if (_log.isWarnEnabled()) {
145             _log.warn(
146                 "Using the deprecated update(Account account) method. Use update(Account account, boolean merge) instead.");
147         }
148 
149         return update(account, false);
150     }
151 
152     /**
153      * Add, update, or merge, the entity. This method also calls the model
154      * listeners to trigger the proper events associated with adding, deleting,
155      * or updating an entity.
156      *
157      * @param        account the entity to add, update, or merge
158      * @param        merge boolean value for whether to merge the entity. The
159      *                default value is false. Setting merge to true is more
160      *                expensive and should only be true when account is
161      *                transient. See LEP-5473 for a detailed discussion of this
162      *                method.
163      * @return        true if the portlet can be displayed via Ajax
164      */
165     public Account update(Account account, boolean merge)
166         throws SystemException {
167         boolean isNew = account.isNew();
168 
169         for (ModelListener listener : listeners) {
170             if (isNew) {
171                 listener.onBeforeCreate(account);
172             }
173             else {
174                 listener.onBeforeUpdate(account);
175             }
176         }
177 
178         account = updateImpl(account, merge);
179 
180         for (ModelListener listener : listeners) {
181             if (isNew) {
182                 listener.onAfterCreate(account);
183             }
184             else {
185                 listener.onAfterUpdate(account);
186             }
187         }
188 
189         return account;
190     }
191 
192     public Account updateImpl(com.liferay.portal.model.Account account,
193         boolean merge) throws SystemException {
194         Session session = null;
195 
196         try {
197             session = openSession();
198 
199             BatchSessionUtil.update(session, account, merge);
200 
201             account.setNew(false);
202 
203             return account;
204         }
205         catch (Exception e) {
206             throw processException(e);
207         }
208         finally {
209             closeSession(session);
210 
211             FinderCacheUtil.clearCache(Account.class.getName());
212         }
213     }
214 
215     public Account findByPrimaryKey(long accountId)
216         throws NoSuchAccountException, SystemException {
217         Account account = fetchByPrimaryKey(accountId);
218 
219         if (account == null) {
220             if (_log.isWarnEnabled()) {
221                 _log.warn("No Account exists with the primary key " +
222                     accountId);
223             }
224 
225             throw new NoSuchAccountException(
226                 "No Account exists with the primary key " + accountId);
227         }
228 
229         return account;
230     }
231 
232     public Account fetchByPrimaryKey(long accountId) throws SystemException {
233         Session session = null;
234 
235         try {
236             session = openSession();
237 
238             return (Account)session.get(AccountImpl.class, new Long(accountId));
239         }
240         catch (Exception e) {
241             throw processException(e);
242         }
243         finally {
244             closeSession(session);
245         }
246     }
247 
248     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
249         throws SystemException {
250         Session session = null;
251 
252         try {
253             session = openSession();
254 
255             dynamicQuery.compile(session);
256 
257             return dynamicQuery.list();
258         }
259         catch (Exception e) {
260             throw processException(e);
261         }
262         finally {
263             closeSession(session);
264         }
265     }
266 
267     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
268         int start, int end) throws SystemException {
269         Session session = null;
270 
271         try {
272             session = openSession();
273 
274             dynamicQuery.setLimit(start, end);
275 
276             dynamicQuery.compile(session);
277 
278             return dynamicQuery.list();
279         }
280         catch (Exception e) {
281             throw processException(e);
282         }
283         finally {
284             closeSession(session);
285         }
286     }
287 
288     public List<Account> findAll() throws SystemException {
289         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
290     }
291 
292     public List<Account> findAll(int start, int end) throws SystemException {
293         return findAll(start, end, null);
294     }
295 
296     public List<Account> findAll(int start, int end, OrderByComparator obc)
297         throws SystemException {
298         boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
299         String finderClassName = Account.class.getName();
300         String finderMethodName = "findAll";
301         String[] finderParams = new String[] {
302                 "java.lang.Integer", "java.lang.Integer",
303                 "com.liferay.portal.kernel.util.OrderByComparator"
304             };
305         Object[] finderArgs = new Object[] {
306                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
307             };
308 
309         Object result = null;
310 
311         if (finderClassNameCacheEnabled) {
312             result = FinderCacheUtil.getResult(finderClassName,
313                     finderMethodName, finderParams, finderArgs, this);
314         }
315 
316         if (result == null) {
317             Session session = null;
318 
319             try {
320                 session = openSession();
321 
322                 StringBuilder query = new StringBuilder();
323 
324                 query.append("FROM com.liferay.portal.model.Account ");
325 
326                 if (obc != null) {
327                     query.append("ORDER BY ");
328                     query.append(obc.getOrderBy());
329                 }
330 
331                 Query q = session.createQuery(query.toString());
332 
333                 List<Account> list = null;
334 
335                 if (obc == null) {
336                     list = (List<Account>)QueryUtil.list(q, getDialect(),
337                             start, end, false);
338 
339                     Collections.sort(list);
340                 }
341                 else {
342                     list = (List<Account>)QueryUtil.list(q, getDialect(),
343                             start, end);
344                 }
345 
346                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
347                     finderClassName, finderMethodName, finderParams,
348                     finderArgs, list);
349 
350                 return list;
351             }
352             catch (Exception e) {
353                 throw processException(e);
354             }
355             finally {
356                 closeSession(session);
357             }
358         }
359         else {
360             return (List<Account>)result;
361         }
362     }
363 
364     public void removeAll() throws SystemException {
365         for (Account account : findAll()) {
366             remove(account);
367         }
368     }
369 
370     public int countAll() throws SystemException {
371         boolean finderClassNameCacheEnabled = AccountModelImpl.CACHE_ENABLED;
372         String finderClassName = Account.class.getName();
373         String finderMethodName = "countAll";
374         String[] finderParams = new String[] {  };
375         Object[] finderArgs = new Object[] {  };
376 
377         Object result = null;
378 
379         if (finderClassNameCacheEnabled) {
380             result = FinderCacheUtil.getResult(finderClassName,
381                     finderMethodName, finderParams, finderArgs, this);
382         }
383 
384         if (result == null) {
385             Session session = null;
386 
387             try {
388                 session = openSession();
389 
390                 Query q = session.createQuery(
391                         "SELECT COUNT(*) FROM com.liferay.portal.model.Account");
392 
393                 Long count = null;
394 
395                 Iterator<Long> itr = q.list().iterator();
396 
397                 if (itr.hasNext()) {
398                     count = itr.next();
399                 }
400 
401                 if (count == null) {
402                     count = new Long(0);
403                 }
404 
405                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
406                     finderClassName, finderMethodName, finderParams,
407                     finderArgs, count);
408 
409                 return count.intValue();
410             }
411             catch (Exception e) {
412                 throw processException(e);
413             }
414             finally {
415                 closeSession(session);
416             }
417         }
418         else {
419             return ((Long)result).intValue();
420         }
421     }
422 
423     public void afterPropertiesSet() {
424         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
425                     com.liferay.portal.util.PropsUtil.get(
426                         "value.object.listener.com.liferay.portal.model.Account")));
427 
428         if (listenerClassNames.length > 0) {
429             try {
430                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
431 
432                 for (String listenerClassName : listenerClassNames) {
433                     listenersList.add((ModelListener)Class.forName(
434                             listenerClassName).newInstance());
435                 }
436 
437                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
438             }
439             catch (Exception e) {
440                 _log.error(e);
441             }
442         }
443     }
444 
445     private static Log _log = LogFactoryUtil.getLog(AccountPersistenceImpl.class);
446 }