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