1   /**
2    * Copyright (c) 2000-2008 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.NoSuchReleaseException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.model.Release;
38  import com.liferay.portal.model.impl.ReleaseImpl;
39  import com.liferay.portal.model.impl.ReleaseModelImpl;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="ReleasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ReleasePersistenceImpl extends BasePersistenceImpl
57      implements ReleasePersistence {
58      public Release create(long releaseId) {
59          Release release = new ReleaseImpl();
60  
61          release.setNew(true);
62          release.setPrimaryKey(releaseId);
63  
64          return release;
65      }
66  
67      public Release remove(long releaseId)
68          throws NoSuchReleaseException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              Release release = (Release)session.get(ReleaseImpl.class,
75                      new Long(releaseId));
76  
77              if (release == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No Release exists with the primary key " +
80                          releaseId);
81                  }
82  
83                  throw new NoSuchReleaseException(
84                      "No Release exists with the primary key " + releaseId);
85              }
86  
87              return remove(release);
88          }
89          catch (NoSuchReleaseException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public Release remove(Release release) throws SystemException {
101         if (_listeners.length > 0) {
102             for (ModelListener listener : _listeners) {
103                 listener.onBeforeRemove(release);
104             }
105         }
106 
107         release = removeImpl(release);
108 
109         if (_listeners.length > 0) {
110             for (ModelListener listener : _listeners) {
111                 listener.onAfterRemove(release);
112             }
113         }
114 
115         return release;
116     }
117 
118     protected Release removeImpl(Release release) throws SystemException {
119         Session session = null;
120 
121         try {
122             session = openSession();
123 
124             session.delete(release);
125 
126             session.flush();
127 
128             return release;
129         }
130         catch (Exception e) {
131             throw processException(e);
132         }
133         finally {
134             closeSession(session);
135 
136             FinderCacheUtil.clearCache(Release.class.getName());
137         }
138     }
139 
140     /**
141      * @deprecated Use <code>update(Release release, boolean merge)</code>.
142      */
143     public Release update(Release release) throws SystemException {
144         if (_log.isWarnEnabled()) {
145             _log.warn(
146                 "Using the deprecated update(Release release) method. Use update(Release release, boolean merge) instead.");
147         }
148 
149         return update(release, 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        release 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 release 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 Release update(Release release, boolean merge)
166         throws SystemException {
167         boolean isNew = release.isNew();
168 
169         if (_listeners.length > 0) {
170             for (ModelListener listener : _listeners) {
171                 if (isNew) {
172                     listener.onBeforeCreate(release);
173                 }
174                 else {
175                     listener.onBeforeUpdate(release);
176                 }
177             }
178         }
179 
180         release = updateImpl(release, merge);
181 
182         if (_listeners.length > 0) {
183             for (ModelListener listener : _listeners) {
184                 if (isNew) {
185                     listener.onAfterCreate(release);
186                 }
187                 else {
188                     listener.onAfterUpdate(release);
189                 }
190             }
191         }
192 
193         return release;
194     }
195 
196     public Release updateImpl(com.liferay.portal.model.Release release,
197         boolean merge) throws SystemException {
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             if (merge) {
204                 session.merge(release);
205             }
206             else {
207                 if (release.isNew()) {
208                     session.save(release);
209                 }
210             }
211 
212             session.flush();
213 
214             release.setNew(false);
215 
216             return release;
217         }
218         catch (Exception e) {
219             throw processException(e);
220         }
221         finally {
222             closeSession(session);
223 
224             FinderCacheUtil.clearCache(Release.class.getName());
225         }
226     }
227 
228     public Release findByPrimaryKey(long releaseId)
229         throws NoSuchReleaseException, SystemException {
230         Release release = fetchByPrimaryKey(releaseId);
231 
232         if (release == null) {
233             if (_log.isWarnEnabled()) {
234                 _log.warn("No Release exists with the primary key " +
235                     releaseId);
236             }
237 
238             throw new NoSuchReleaseException(
239                 "No Release exists with the primary key " + releaseId);
240         }
241 
242         return release;
243     }
244 
245     public Release fetchByPrimaryKey(long releaseId) throws SystemException {
246         Session session = null;
247 
248         try {
249             session = openSession();
250 
251             return (Release)session.get(ReleaseImpl.class, new Long(releaseId));
252         }
253         catch (Exception e) {
254             throw processException(e);
255         }
256         finally {
257             closeSession(session);
258         }
259     }
260 
261     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
262         throws SystemException {
263         Session session = null;
264 
265         try {
266             session = openSession();
267 
268             dynamicQuery.compile(session);
269 
270             return dynamicQuery.list();
271         }
272         catch (Exception e) {
273             throw processException(e);
274         }
275         finally {
276             closeSession(session);
277         }
278     }
279 
280     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
281         int start, int end) throws SystemException {
282         Session session = null;
283 
284         try {
285             session = openSession();
286 
287             dynamicQuery.setLimit(start, end);
288 
289             dynamicQuery.compile(session);
290 
291             return dynamicQuery.list();
292         }
293         catch (Exception e) {
294             throw processException(e);
295         }
296         finally {
297             closeSession(session);
298         }
299     }
300 
301     public List<Release> findAll() throws SystemException {
302         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
303     }
304 
305     public List<Release> findAll(int start, int end) throws SystemException {
306         return findAll(start, end, null);
307     }
308 
309     public List<Release> findAll(int start, int end, OrderByComparator obc)
310         throws SystemException {
311         boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
312         String finderClassName = Release.class.getName();
313         String finderMethodName = "findAll";
314         String[] finderParams = new String[] {
315                 "java.lang.Integer", "java.lang.Integer",
316                 "com.liferay.portal.kernel.util.OrderByComparator"
317             };
318         Object[] finderArgs = new Object[] {
319                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
320             };
321 
322         Object result = null;
323 
324         if (finderClassNameCacheEnabled) {
325             result = FinderCacheUtil.getResult(finderClassName,
326                     finderMethodName, finderParams, finderArgs, this);
327         }
328 
329         if (result == null) {
330             Session session = null;
331 
332             try {
333                 session = openSession();
334 
335                 StringBuilder query = new StringBuilder();
336 
337                 query.append("FROM com.liferay.portal.model.Release ");
338 
339                 if (obc != null) {
340                     query.append("ORDER BY ");
341                     query.append(obc.getOrderBy());
342                 }
343 
344                 Query q = session.createQuery(query.toString());
345 
346                 List<Release> list = (List<Release>)QueryUtil.list(q,
347                         getDialect(), start, end);
348 
349                 if (obc == null) {
350                     Collections.sort(list);
351                 }
352 
353                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
354                     finderClassName, finderMethodName, finderParams,
355                     finderArgs, list);
356 
357                 return list;
358             }
359             catch (Exception e) {
360                 throw processException(e);
361             }
362             finally {
363                 closeSession(session);
364             }
365         }
366         else {
367             return (List<Release>)result;
368         }
369     }
370 
371     public void removeAll() throws SystemException {
372         for (Release release : findAll()) {
373             remove(release);
374         }
375     }
376 
377     public int countAll() throws SystemException {
378         boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
379         String finderClassName = Release.class.getName();
380         String finderMethodName = "countAll";
381         String[] finderParams = new String[] {  };
382         Object[] finderArgs = new Object[] {  };
383 
384         Object result = null;
385 
386         if (finderClassNameCacheEnabled) {
387             result = FinderCacheUtil.getResult(finderClassName,
388                     finderMethodName, finderParams, finderArgs, this);
389         }
390 
391         if (result == null) {
392             Session session = null;
393 
394             try {
395                 session = openSession();
396 
397                 Query q = session.createQuery(
398                         "SELECT COUNT(*) FROM com.liferay.portal.model.Release");
399 
400                 Long count = null;
401 
402                 Iterator<Long> itr = q.list().iterator();
403 
404                 if (itr.hasNext()) {
405                     count = itr.next();
406                 }
407 
408                 if (count == null) {
409                     count = new Long(0);
410                 }
411 
412                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
413                     finderClassName, finderMethodName, finderParams,
414                     finderArgs, count);
415 
416                 return count.intValue();
417             }
418             catch (Exception e) {
419                 throw processException(e);
420             }
421             finally {
422                 closeSession(session);
423             }
424         }
425         else {
426             return ((Long)result).intValue();
427         }
428     }
429 
430     public void registerListener(ModelListener listener) {
431         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
432 
433         listeners.add(listener);
434 
435         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
436     }
437 
438     public void unregisterListener(ModelListener listener) {
439         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
440 
441         listeners.remove(listener);
442 
443         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
444     }
445 
446     public void afterPropertiesSet() {
447         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
448                     com.liferay.portal.util.PropsUtil.get(
449                         "value.object.listener.com.liferay.portal.model.Release")));
450 
451         if (listenerClassNames.length > 0) {
452             try {
453                 List<ModelListener> listeners = new ArrayList<ModelListener>();
454 
455                 for (String listenerClassName : listenerClassNames) {
456                     listeners.add((ModelListener)Class.forName(
457                             listenerClassName).newInstance());
458                 }
459 
460                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
461             }
462             catch (Exception e) {
463                 _log.error(e);
464             }
465         }
466     }
467 
468     private static Log _log = LogFactory.getLog(ReleasePersistenceImpl.class);
469     private ModelListener[] _listeners = new ModelListener[0];
470 }