1   /**
2    * Copyright (c) 2000-2007 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.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.model.Release;
32  import com.liferay.portal.model.impl.ReleaseImpl;
33  import com.liferay.portal.service.persistence.BasePersistence;
34  import com.liferay.portal.spring.hibernate.FinderCache;
35  import com.liferay.portal.spring.hibernate.HibernateUtil;
36  
37  import com.liferay.util.dao.hibernate.QueryUtil;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  import org.hibernate.Query;
43  import org.hibernate.Session;
44  
45  import java.util.Collections;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  /**
50   * <a href="ReleasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class ReleasePersistenceImpl extends BasePersistence
56      implements ReleasePersistence {
57      public Release create(long releaseId) {
58          Release release = new ReleaseImpl();
59          release.setNew(true);
60          release.setPrimaryKey(releaseId);
61  
62          return release;
63      }
64  
65      public Release remove(long releaseId)
66          throws NoSuchReleaseException, SystemException {
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              Release release = (Release)session.get(ReleaseImpl.class,
73                      new Long(releaseId));
74  
75              if (release == null) {
76                  if (_log.isWarnEnabled()) {
77                      _log.warn("No Release exists with the primary key " +
78                          releaseId);
79                  }
80  
81                  throw new NoSuchReleaseException(
82                      "No Release exists with the primary key " + releaseId);
83              }
84  
85              return remove(release);
86          }
87          catch (NoSuchReleaseException nsee) {
88              throw nsee;
89          }
90          catch (Exception e) {
91              throw HibernateUtil.processException(e);
92          }
93          finally {
94              closeSession(session);
95          }
96      }
97  
98      public Release remove(Release release) throws SystemException {
99          Session session = null;
100 
101         try {
102             session = openSession();
103             session.delete(release);
104             session.flush();
105 
106             return release;
107         }
108         catch (Exception e) {
109             throw HibernateUtil.processException(e);
110         }
111         finally {
112             closeSession(session);
113             FinderCache.clearCache(Release.class.getName());
114         }
115     }
116 
117     public Release update(com.liferay.portal.model.Release release)
118         throws SystemException {
119         return update(release, false);
120     }
121 
122     public Release update(com.liferay.portal.model.Release release,
123         boolean merge) throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             if (merge) {
130                 session.merge(release);
131             }
132             else {
133                 if (release.isNew()) {
134                     session.save(release);
135                 }
136             }
137 
138             session.flush();
139             release.setNew(false);
140 
141             return release;
142         }
143         catch (Exception e) {
144             throw HibernateUtil.processException(e);
145         }
146         finally {
147             closeSession(session);
148             FinderCache.clearCache(Release.class.getName());
149         }
150     }
151 
152     public Release findByPrimaryKey(long releaseId)
153         throws NoSuchReleaseException, SystemException {
154         Release release = fetchByPrimaryKey(releaseId);
155 
156         if (release == null) {
157             if (_log.isWarnEnabled()) {
158                 _log.warn("No Release exists with the primary key " +
159                     releaseId);
160             }
161 
162             throw new NoSuchReleaseException(
163                 "No Release exists with the primary key " + releaseId);
164         }
165 
166         return release;
167     }
168 
169     public Release fetchByPrimaryKey(long releaseId) throws SystemException {
170         Session session = null;
171 
172         try {
173             session = openSession();
174 
175             return (Release)session.get(ReleaseImpl.class, new Long(releaseId));
176         }
177         catch (Exception e) {
178             throw HibernateUtil.processException(e);
179         }
180         finally {
181             closeSession(session);
182         }
183     }
184 
185     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
186         throws SystemException {
187         Session session = null;
188 
189         try {
190             session = openSession();
191 
192             DynamicQuery query = queryInitializer.initialize(session);
193 
194             return query.list();
195         }
196         catch (Exception e) {
197             throw HibernateUtil.processException(e);
198         }
199         finally {
200             closeSession(session);
201         }
202     }
203 
204     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
205         int begin, int end) throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             DynamicQuery query = queryInitializer.initialize(session);
212             query.setLimit(begin, end);
213 
214             return query.list();
215         }
216         catch (Exception e) {
217             throw HibernateUtil.processException(e);
218         }
219         finally {
220             closeSession(session);
221         }
222     }
223 
224     public List findAll() throws SystemException {
225         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
226     }
227 
228     public List findAll(int begin, int end) throws SystemException {
229         return findAll(begin, end, null);
230     }
231 
232     public List findAll(int begin, int end, OrderByComparator obc)
233         throws SystemException {
234         String finderClassName = Release.class.getName();
235         String finderMethodName = "findAll";
236         String[] finderParams = new String[] {
237                 "java.lang.Integer", "java.lang.Integer",
238                 "com.liferay.portal.kernel.util.OrderByComparator"
239             };
240         Object[] finderArgs = new Object[] {
241                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
242             };
243         Object result = FinderCache.getResult(finderClassName,
244                 finderMethodName, finderParams, finderArgs, getSessionFactory());
245 
246         if (result == null) {
247             Session session = null;
248 
249             try {
250                 session = openSession();
251 
252                 StringMaker query = new StringMaker();
253                 query.append("FROM com.liferay.portal.model.Release ");
254 
255                 if (obc != null) {
256                     query.append("ORDER BY ");
257                     query.append(obc.getOrderBy());
258                 }
259 
260                 Query q = session.createQuery(query.toString());
261                 List list = QueryUtil.list(q, getDialect(), begin, end);
262 
263                 if (obc == null) {
264                     Collections.sort(list);
265                 }
266 
267                 FinderCache.putResult(finderClassName, finderMethodName,
268                     finderParams, finderArgs, list);
269 
270                 return list;
271             }
272             catch (Exception e) {
273                 throw HibernateUtil.processException(e);
274             }
275             finally {
276                 closeSession(session);
277             }
278         }
279         else {
280             return (List)result;
281         }
282     }
283 
284     public void removeAll() throws SystemException {
285         Iterator itr = findAll().iterator();
286 
287         while (itr.hasNext()) {
288             remove((Release)itr.next());
289         }
290     }
291 
292     public int countAll() throws SystemException {
293         String finderClassName = Release.class.getName();
294         String finderMethodName = "countAll";
295         String[] finderParams = new String[] {  };
296         Object[] finderArgs = new Object[] {  };
297         Object result = FinderCache.getResult(finderClassName,
298                 finderMethodName, finderParams, finderArgs, getSessionFactory());
299 
300         if (result == null) {
301             Session session = null;
302 
303             try {
304                 session = openSession();
305 
306                 StringMaker query = new StringMaker();
307                 query.append("SELECT COUNT(*) ");
308                 query.append("FROM com.liferay.portal.model.Release");
309 
310                 Query q = session.createQuery(query.toString());
311                 Long count = null;
312                 Iterator itr = q.list().iterator();
313 
314                 if (itr.hasNext()) {
315                     count = (Long)itr.next();
316                 }
317 
318                 if (count == null) {
319                     count = new Long(0);
320                 }
321 
322                 FinderCache.putResult(finderClassName, finderMethodName,
323                     finderParams, finderArgs, count);
324 
325                 return count.intValue();
326             }
327             catch (Exception e) {
328                 throw HibernateUtil.processException(e);
329             }
330             finally {
331                 closeSession(session);
332             }
333         }
334         else {
335             return ((Long)result).intValue();
336         }
337     }
338 
339     protected void initDao() {
340     }
341 
342     private static Log _log = LogFactory.getLog(ReleasePersistenceImpl.class);
343 }