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             if (BatchSessionUtil.isEnabled()) {
125                 Object staleObject = session.get(ReleaseImpl.class,
126                         release.getPrimaryKeyObj());
127 
128                 if (staleObject != null) {
129                     session.evict(staleObject);
130                 }
131             }
132 
133             session.delete(release);
134 
135             session.flush();
136 
137             return release;
138         }
139         catch (Exception e) {
140             throw processException(e);
141         }
142         finally {
143             closeSession(session);
144 
145             FinderCacheUtil.clearCache(Release.class.getName());
146         }
147     }
148 
149     /**
150      * @deprecated Use <code>update(Release release, boolean merge)</code>.
151      */
152     public Release update(Release release) throws SystemException {
153         if (_log.isWarnEnabled()) {
154             _log.warn(
155                 "Using the deprecated update(Release release) method. Use update(Release release, boolean merge) instead.");
156         }
157 
158         return update(release, false);
159     }
160 
161     /**
162      * Add, update, or merge, the entity. This method also calls the model
163      * listeners to trigger the proper events associated with adding, deleting,
164      * or updating an entity.
165      *
166      * @param        release the entity to add, update, or merge
167      * @param        merge boolean value for whether to merge the entity. The
168      *                default value is false. Setting merge to true is more
169      *                expensive and should only be true when release is
170      *                transient. See LEP-5473 for a detailed discussion of this
171      *                method.
172      * @return        true if the portlet can be displayed via Ajax
173      */
174     public Release update(Release release, boolean merge)
175         throws SystemException {
176         boolean isNew = release.isNew();
177 
178         if (_listeners.length > 0) {
179             for (ModelListener listener : _listeners) {
180                 if (isNew) {
181                     listener.onBeforeCreate(release);
182                 }
183                 else {
184                     listener.onBeforeUpdate(release);
185                 }
186             }
187         }
188 
189         release = updateImpl(release, merge);
190 
191         if (_listeners.length > 0) {
192             for (ModelListener listener : _listeners) {
193                 if (isNew) {
194                     listener.onAfterCreate(release);
195                 }
196                 else {
197                     listener.onAfterUpdate(release);
198                 }
199             }
200         }
201 
202         return release;
203     }
204 
205     public Release updateImpl(com.liferay.portal.model.Release release,
206         boolean merge) throws SystemException {
207         Session session = null;
208 
209         try {
210             session = openSession();
211 
212             BatchSessionUtil.update(session, release, merge);
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 = null;
347 
348                 if (obc == null) {
349                     list = (List<Release>)QueryUtil.list(q, getDialect(),
350                             start, end, false);
351 
352                     Collections.sort(list);
353                 }
354                 else {
355                     list = (List<Release>)QueryUtil.list(q, getDialect(),
356                             start, end);
357                 }
358 
359                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
360                     finderClassName, finderMethodName, finderParams,
361                     finderArgs, list);
362 
363                 return list;
364             }
365             catch (Exception e) {
366                 throw processException(e);
367             }
368             finally {
369                 closeSession(session);
370             }
371         }
372         else {
373             return (List<Release>)result;
374         }
375     }
376 
377     public void removeAll() throws SystemException {
378         for (Release release : findAll()) {
379             remove(release);
380         }
381     }
382 
383     public int countAll() throws SystemException {
384         boolean finderClassNameCacheEnabled = ReleaseModelImpl.CACHE_ENABLED;
385         String finderClassName = Release.class.getName();
386         String finderMethodName = "countAll";
387         String[] finderParams = new String[] {  };
388         Object[] finderArgs = new Object[] {  };
389 
390         Object result = null;
391 
392         if (finderClassNameCacheEnabled) {
393             result = FinderCacheUtil.getResult(finderClassName,
394                     finderMethodName, finderParams, finderArgs, this);
395         }
396 
397         if (result == null) {
398             Session session = null;
399 
400             try {
401                 session = openSession();
402 
403                 Query q = session.createQuery(
404                         "SELECT COUNT(*) FROM com.liferay.portal.model.Release");
405 
406                 Long count = null;
407 
408                 Iterator<Long> itr = q.list().iterator();
409 
410                 if (itr.hasNext()) {
411                     count = itr.next();
412                 }
413 
414                 if (count == null) {
415                     count = new Long(0);
416                 }
417 
418                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
419                     finderClassName, finderMethodName, finderParams,
420                     finderArgs, count);
421 
422                 return count.intValue();
423             }
424             catch (Exception e) {
425                 throw processException(e);
426             }
427             finally {
428                 closeSession(session);
429             }
430         }
431         else {
432             return ((Long)result).intValue();
433         }
434     }
435 
436     public void registerListener(ModelListener listener) {
437         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
438 
439         listeners.add(listener);
440 
441         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
442     }
443 
444     public void unregisterListener(ModelListener listener) {
445         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
446 
447         listeners.remove(listener);
448 
449         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
450     }
451 
452     public void afterPropertiesSet() {
453         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
454                     com.liferay.portal.util.PropsUtil.get(
455                         "value.object.listener.com.liferay.portal.model.Release")));
456 
457         if (listenerClassNames.length > 0) {
458             try {
459                 List<ModelListener> listeners = new ArrayList<ModelListener>();
460 
461                 for (String listenerClassName : listenerClassNames) {
462                     listeners.add((ModelListener)Class.forName(
463                             listenerClassName).newInstance());
464                 }
465 
466                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
467             }
468             catch (Exception e) {
469                 _log.error(e);
470             }
471         }
472     }
473 
474     private static Log _log = LogFactory.getLog(ReleasePersistenceImpl.class);
475     private ModelListener[] _listeners = new ModelListener[0];
476 }