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.portlet.messageboards.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
36  import com.liferay.portlet.messageboards.model.MBDiscussion;
37  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class MBDiscussionPersistenceImpl extends BasePersistence
58      implements MBDiscussionPersistence {
59      public MBDiscussion create(long discussionId) {
60          MBDiscussion mbDiscussion = new MBDiscussionImpl();
61          mbDiscussion.setNew(true);
62          mbDiscussion.setPrimaryKey(discussionId);
63  
64          return mbDiscussion;
65      }
66  
67      public MBDiscussion remove(long discussionId)
68          throws NoSuchDiscussionException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
75                      new Long(discussionId));
76  
77              if (mbDiscussion == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No MBDiscussion exists with the primary key " +
80                          discussionId);
81                  }
82  
83                  throw new NoSuchDiscussionException(
84                      "No MBDiscussion exists with the primary key " +
85                      discussionId);
86              }
87  
88              return remove(mbDiscussion);
89          }
90          catch (NoSuchDiscussionException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public MBDiscussion remove(MBDiscussion mbDiscussion)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(mbDiscussion);
108             session.flush();
109 
110             return mbDiscussion;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(MBDiscussion.class.getName());
118         }
119     }
120 
121     public MBDiscussion update(
122         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion)
123         throws SystemException {
124         return update(mbDiscussion, false);
125     }
126 
127     public MBDiscussion update(
128         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(mbDiscussion);
137             }
138             else {
139                 if (mbDiscussion.isNew()) {
140                     session.save(mbDiscussion);
141                 }
142             }
143 
144             session.flush();
145             mbDiscussion.setNew(false);
146 
147             return mbDiscussion;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(MBDiscussion.class.getName());
155         }
156     }
157 
158     public MBDiscussion findByPrimaryKey(long discussionId)
159         throws NoSuchDiscussionException, SystemException {
160         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
161 
162         if (mbDiscussion == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No MBDiscussion exists with the primary key " +
165                     discussionId);
166             }
167 
168             throw new NoSuchDiscussionException(
169                 "No MBDiscussion exists with the primary key " + discussionId);
170         }
171 
172         return mbDiscussion;
173     }
174 
175     public MBDiscussion fetchByPrimaryKey(long discussionId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (MBDiscussion)session.get(MBDiscussionImpl.class,
183                 new Long(discussionId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public MBDiscussion findByC_C(long classNameId, long classPK)
194         throws NoSuchDiscussionException, SystemException {
195         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
196 
197         if (mbDiscussion == null) {
198             StringMaker msg = new StringMaker();
199             msg.append("No MBDiscussion exists with the key ");
200             msg.append(StringPool.OPEN_CURLY_BRACE);
201             msg.append("classNameId=");
202             msg.append(classNameId);
203             msg.append(", ");
204             msg.append("classPK=");
205             msg.append(classPK);
206             msg.append(StringPool.CLOSE_CURLY_BRACE);
207 
208             if (_log.isWarnEnabled()) {
209                 _log.warn(msg.toString());
210             }
211 
212             throw new NoSuchDiscussionException(msg.toString());
213         }
214 
215         return mbDiscussion;
216     }
217 
218     public MBDiscussion fetchByC_C(long classNameId, long classPK)
219         throws SystemException {
220         String finderClassName = MBDiscussion.class.getName();
221         String finderMethodName = "fetchByC_C";
222         String[] finderParams = new String[] {
223                 Long.class.getName(), Long.class.getName()
224             };
225         Object[] finderArgs = new Object[] {
226                 new Long(classNameId), new Long(classPK)
227             };
228         Object result = FinderCache.getResult(finderClassName,
229                 finderMethodName, finderParams, finderArgs, getSessionFactory());
230 
231         if (result == null) {
232             Session session = null;
233 
234             try {
235                 session = openSession();
236 
237                 StringMaker query = new StringMaker();
238                 query.append(
239                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
240                 query.append("classNameId = ?");
241                 query.append(" AND ");
242                 query.append("classPK = ?");
243                 query.append(" ");
244 
245                 Query q = session.createQuery(query.toString());
246                 int queryPos = 0;
247                 q.setLong(queryPos++, classNameId);
248                 q.setLong(queryPos++, classPK);
249 
250                 List list = q.list();
251                 FinderCache.putResult(finderClassName, finderMethodName,
252                     finderParams, finderArgs, list);
253 
254                 if (list.size() == 0) {
255                     return null;
256                 }
257                 else {
258                     return (MBDiscussion)list.get(0);
259                 }
260             }
261             catch (Exception e) {
262                 throw HibernateUtil.processException(e);
263             }
264             finally {
265                 closeSession(session);
266             }
267         }
268         else {
269             List list = (List)result;
270 
271             if (list.size() == 0) {
272                 return null;
273             }
274             else {
275                 return (MBDiscussion)list.get(0);
276             }
277         }
278     }
279 
280     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
281         throws SystemException {
282         Session session = null;
283 
284         try {
285             session = openSession();
286 
287             DynamicQuery query = queryInitializer.initialize(session);
288 
289             return query.list();
290         }
291         catch (Exception e) {
292             throw HibernateUtil.processException(e);
293         }
294         finally {
295             closeSession(session);
296         }
297     }
298 
299     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
300         int begin, int end) throws SystemException {
301         Session session = null;
302 
303         try {
304             session = openSession();
305 
306             DynamicQuery query = queryInitializer.initialize(session);
307             query.setLimit(begin, end);
308 
309             return query.list();
310         }
311         catch (Exception e) {
312             throw HibernateUtil.processException(e);
313         }
314         finally {
315             closeSession(session);
316         }
317     }
318 
319     public List findAll() throws SystemException {
320         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
321     }
322 
323     public List findAll(int begin, int end) throws SystemException {
324         return findAll(begin, end, null);
325     }
326 
327     public List findAll(int begin, int end, OrderByComparator obc)
328         throws SystemException {
329         String finderClassName = MBDiscussion.class.getName();
330         String finderMethodName = "findAll";
331         String[] finderParams = new String[] {
332                 "java.lang.Integer", "java.lang.Integer",
333                 "com.liferay.portal.kernel.util.OrderByComparator"
334             };
335         Object[] finderArgs = new Object[] {
336                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
337             };
338         Object result = FinderCache.getResult(finderClassName,
339                 finderMethodName, finderParams, finderArgs, getSessionFactory());
340 
341         if (result == null) {
342             Session session = null;
343 
344             try {
345                 session = openSession();
346 
347                 StringMaker query = new StringMaker();
348                 query.append(
349                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
350 
351                 if (obc != null) {
352                     query.append("ORDER BY ");
353                     query.append(obc.getOrderBy());
354                 }
355 
356                 Query q = session.createQuery(query.toString());
357                 List list = QueryUtil.list(q, getDialect(), begin, end);
358 
359                 if (obc == null) {
360                     Collections.sort(list);
361                 }
362 
363                 FinderCache.putResult(finderClassName, finderMethodName,
364                     finderParams, finderArgs, list);
365 
366                 return list;
367             }
368             catch (Exception e) {
369                 throw HibernateUtil.processException(e);
370             }
371             finally {
372                 closeSession(session);
373             }
374         }
375         else {
376             return (List)result;
377         }
378     }
379 
380     public void removeByC_C(long classNameId, long classPK)
381         throws NoSuchDiscussionException, SystemException {
382         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
383         remove(mbDiscussion);
384     }
385 
386     public void removeAll() throws SystemException {
387         Iterator itr = findAll().iterator();
388 
389         while (itr.hasNext()) {
390             remove((MBDiscussion)itr.next());
391         }
392     }
393 
394     public int countByC_C(long classNameId, long classPK)
395         throws SystemException {
396         String finderClassName = MBDiscussion.class.getName();
397         String finderMethodName = "countByC_C";
398         String[] finderParams = new String[] {
399                 Long.class.getName(), Long.class.getName()
400             };
401         Object[] finderArgs = new Object[] {
402                 new Long(classNameId), new Long(classPK)
403             };
404         Object result = FinderCache.getResult(finderClassName,
405                 finderMethodName, finderParams, finderArgs, getSessionFactory());
406 
407         if (result == null) {
408             Session session = null;
409 
410             try {
411                 session = openSession();
412 
413                 StringMaker query = new StringMaker();
414                 query.append("SELECT COUNT(*) ");
415                 query.append(
416                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
417                 query.append("classNameId = ?");
418                 query.append(" AND ");
419                 query.append("classPK = ?");
420                 query.append(" ");
421 
422                 Query q = session.createQuery(query.toString());
423                 int queryPos = 0;
424                 q.setLong(queryPos++, classNameId);
425                 q.setLong(queryPos++, classPK);
426 
427                 Long count = null;
428                 Iterator itr = q.list().iterator();
429 
430                 if (itr.hasNext()) {
431                     count = (Long)itr.next();
432                 }
433 
434                 if (count == null) {
435                     count = new Long(0);
436                 }
437 
438                 FinderCache.putResult(finderClassName, finderMethodName,
439                     finderParams, finderArgs, count);
440 
441                 return count.intValue();
442             }
443             catch (Exception e) {
444                 throw HibernateUtil.processException(e);
445             }
446             finally {
447                 closeSession(session);
448             }
449         }
450         else {
451             return ((Long)result).intValue();
452         }
453     }
454 
455     public int countAll() throws SystemException {
456         String finderClassName = MBDiscussion.class.getName();
457         String finderMethodName = "countAll";
458         String[] finderParams = new String[] {  };
459         Object[] finderArgs = new Object[] {  };
460         Object result = FinderCache.getResult(finderClassName,
461                 finderMethodName, finderParams, finderArgs, getSessionFactory());
462 
463         if (result == null) {
464             Session session = null;
465 
466             try {
467                 session = openSession();
468 
469                 StringMaker query = new StringMaker();
470                 query.append("SELECT COUNT(*) ");
471                 query.append(
472                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion");
473 
474                 Query q = session.createQuery(query.toString());
475                 Long count = null;
476                 Iterator itr = q.list().iterator();
477 
478                 if (itr.hasNext()) {
479                     count = (Long)itr.next();
480                 }
481 
482                 if (count == null) {
483                     count = new Long(0);
484                 }
485 
486                 FinderCache.putResult(finderClassName, finderMethodName,
487                     finderParams, finderArgs, count);
488 
489                 return count.intValue();
490             }
491             catch (Exception e) {
492                 throw HibernateUtil.processException(e);
493             }
494             finally {
495                 closeSession(session);
496             }
497         }
498         else {
499             return ((Long)result).intValue();
500         }
501     }
502 
503     protected void initDao() {
504     }
505 
506     private static Log _log = LogFactory.getLog(MBDiscussionPersistenceImpl.class);
507 }