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.polls.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.polls.NoSuchQuestionException;
36  import com.liferay.portlet.polls.model.PollsQuestion;
37  import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
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="PollsQuestionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class PollsQuestionPersistenceImpl extends BasePersistence
58      implements PollsQuestionPersistence {
59      public PollsQuestion create(long questionId) {
60          PollsQuestion pollsQuestion = new PollsQuestionImpl();
61          pollsQuestion.setNew(true);
62          pollsQuestion.setPrimaryKey(questionId);
63  
64          return pollsQuestion;
65      }
66  
67      public PollsQuestion remove(long questionId)
68          throws NoSuchQuestionException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              PollsQuestion pollsQuestion = (PollsQuestion)session.get(PollsQuestionImpl.class,
75                      new Long(questionId));
76  
77              if (pollsQuestion == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No PollsQuestion exists with the primary key " +
80                          questionId);
81                  }
82  
83                  throw new NoSuchQuestionException(
84                      "No PollsQuestion exists with the primary key " +
85                      questionId);
86              }
87  
88              return remove(pollsQuestion);
89          }
90          catch (NoSuchQuestionException 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 PollsQuestion remove(PollsQuestion pollsQuestion)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(pollsQuestion);
108             session.flush();
109 
110             return pollsQuestion;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(PollsQuestion.class.getName());
118         }
119     }
120 
121     public PollsQuestion update(
122         com.liferay.portlet.polls.model.PollsQuestion pollsQuestion)
123         throws SystemException {
124         return update(pollsQuestion, false);
125     }
126 
127     public PollsQuestion update(
128         com.liferay.portlet.polls.model.PollsQuestion pollsQuestion,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(pollsQuestion);
137             }
138             else {
139                 if (pollsQuestion.isNew()) {
140                     session.save(pollsQuestion);
141                 }
142             }
143 
144             session.flush();
145             pollsQuestion.setNew(false);
146 
147             return pollsQuestion;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(PollsQuestion.class.getName());
155         }
156     }
157 
158     public PollsQuestion findByPrimaryKey(long questionId)
159         throws NoSuchQuestionException, SystemException {
160         PollsQuestion pollsQuestion = fetchByPrimaryKey(questionId);
161 
162         if (pollsQuestion == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No PollsQuestion exists with the primary key " +
165                     questionId);
166             }
167 
168             throw new NoSuchQuestionException(
169                 "No PollsQuestion exists with the primary key " + questionId);
170         }
171 
172         return pollsQuestion;
173     }
174 
175     public PollsQuestion fetchByPrimaryKey(long questionId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (PollsQuestion)session.get(PollsQuestionImpl.class,
183                 new Long(questionId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByGroupId(long groupId) throws SystemException {
194         String finderClassName = PollsQuestion.class.getName();
195         String finderMethodName = "findByGroupId";
196         String[] finderParams = new String[] { Long.class.getName() };
197         Object[] finderArgs = new Object[] { new Long(groupId) };
198         Object result = FinderCache.getResult(finderClassName,
199                 finderMethodName, finderParams, finderArgs, getSessionFactory());
200 
201         if (result == null) {
202             Session session = null;
203 
204             try {
205                 session = openSession();
206 
207                 StringMaker query = new StringMaker();
208                 query.append(
209                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
210                 query.append("groupId = ?");
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("createDate DESC");
214 
215                 Query q = session.createQuery(query.toString());
216                 int queryPos = 0;
217                 q.setLong(queryPos++, groupId);
218 
219                 List list = q.list();
220                 FinderCache.putResult(finderClassName, finderMethodName,
221                     finderParams, finderArgs, list);
222 
223                 return list;
224             }
225             catch (Exception e) {
226                 throw HibernateUtil.processException(e);
227             }
228             finally {
229                 closeSession(session);
230             }
231         }
232         else {
233             return (List)result;
234         }
235     }
236 
237     public List findByGroupId(long groupId, int begin, int end)
238         throws SystemException {
239         return findByGroupId(groupId, begin, end, null);
240     }
241 
242     public List findByGroupId(long groupId, int begin, int end,
243         OrderByComparator obc) throws SystemException {
244         String finderClassName = PollsQuestion.class.getName();
245         String finderMethodName = "findByGroupId";
246         String[] finderParams = new String[] {
247                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
248                 "com.liferay.portal.kernel.util.OrderByComparator"
249             };
250         Object[] finderArgs = new Object[] {
251                 new Long(groupId), String.valueOf(begin), String.valueOf(end),
252                 String.valueOf(obc)
253             };
254         Object result = FinderCache.getResult(finderClassName,
255                 finderMethodName, finderParams, finderArgs, getSessionFactory());
256 
257         if (result == null) {
258             Session session = null;
259 
260             try {
261                 session = openSession();
262 
263                 StringMaker query = new StringMaker();
264                 query.append(
265                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
266                 query.append("groupId = ?");
267                 query.append(" ");
268 
269                 if (obc != null) {
270                     query.append("ORDER BY ");
271                     query.append(obc.getOrderBy());
272                 }
273                 else {
274                     query.append("ORDER BY ");
275                     query.append("createDate DESC");
276                 }
277 
278                 Query q = session.createQuery(query.toString());
279                 int queryPos = 0;
280                 q.setLong(queryPos++, groupId);
281 
282                 List list = QueryUtil.list(q, getDialect(), begin, end);
283                 FinderCache.putResult(finderClassName, finderMethodName,
284                     finderParams, finderArgs, list);
285 
286                 return list;
287             }
288             catch (Exception e) {
289                 throw HibernateUtil.processException(e);
290             }
291             finally {
292                 closeSession(session);
293             }
294         }
295         else {
296             return (List)result;
297         }
298     }
299 
300     public PollsQuestion findByGroupId_First(long groupId, OrderByComparator obc)
301         throws NoSuchQuestionException, SystemException {
302         List list = findByGroupId(groupId, 0, 1, obc);
303 
304         if (list.size() == 0) {
305             StringMaker msg = new StringMaker();
306             msg.append("No PollsQuestion exists with the key ");
307             msg.append(StringPool.OPEN_CURLY_BRACE);
308             msg.append("groupId=");
309             msg.append(groupId);
310             msg.append(StringPool.CLOSE_CURLY_BRACE);
311             throw new NoSuchQuestionException(msg.toString());
312         }
313         else {
314             return (PollsQuestion)list.get(0);
315         }
316     }
317 
318     public PollsQuestion findByGroupId_Last(long groupId, OrderByComparator obc)
319         throws NoSuchQuestionException, SystemException {
320         int count = countByGroupId(groupId);
321         List list = findByGroupId(groupId, count - 1, count, obc);
322 
323         if (list.size() == 0) {
324             StringMaker msg = new StringMaker();
325             msg.append("No PollsQuestion exists with the key ");
326             msg.append(StringPool.OPEN_CURLY_BRACE);
327             msg.append("groupId=");
328             msg.append(groupId);
329             msg.append(StringPool.CLOSE_CURLY_BRACE);
330             throw new NoSuchQuestionException(msg.toString());
331         }
332         else {
333             return (PollsQuestion)list.get(0);
334         }
335     }
336 
337     public PollsQuestion[] findByGroupId_PrevAndNext(long questionId,
338         long groupId, OrderByComparator obc)
339         throws NoSuchQuestionException, SystemException {
340         PollsQuestion pollsQuestion = findByPrimaryKey(questionId);
341         int count = countByGroupId(groupId);
342         Session session = null;
343 
344         try {
345             session = openSession();
346 
347             StringMaker query = new StringMaker();
348             query.append(
349                 "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
350             query.append("groupId = ?");
351             query.append(" ");
352 
353             if (obc != null) {
354                 query.append("ORDER BY ");
355                 query.append(obc.getOrderBy());
356             }
357             else {
358                 query.append("ORDER BY ");
359                 query.append("createDate DESC");
360             }
361 
362             Query q = session.createQuery(query.toString());
363             int queryPos = 0;
364             q.setLong(queryPos++, groupId);
365 
366             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
367                     pollsQuestion);
368             PollsQuestion[] array = new PollsQuestionImpl[3];
369             array[0] = (PollsQuestion)objArray[0];
370             array[1] = (PollsQuestion)objArray[1];
371             array[2] = (PollsQuestion)objArray[2];
372 
373             return array;
374         }
375         catch (Exception e) {
376             throw HibernateUtil.processException(e);
377         }
378         finally {
379             closeSession(session);
380         }
381     }
382 
383     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
384         throws SystemException {
385         Session session = null;
386 
387         try {
388             session = openSession();
389 
390             DynamicQuery query = queryInitializer.initialize(session);
391 
392             return query.list();
393         }
394         catch (Exception e) {
395             throw HibernateUtil.processException(e);
396         }
397         finally {
398             closeSession(session);
399         }
400     }
401 
402     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
403         int begin, int end) throws SystemException {
404         Session session = null;
405 
406         try {
407             session = openSession();
408 
409             DynamicQuery query = queryInitializer.initialize(session);
410             query.setLimit(begin, end);
411 
412             return query.list();
413         }
414         catch (Exception e) {
415             throw HibernateUtil.processException(e);
416         }
417         finally {
418             closeSession(session);
419         }
420     }
421 
422     public List findAll() throws SystemException {
423         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
424     }
425 
426     public List findAll(int begin, int end) throws SystemException {
427         return findAll(begin, end, null);
428     }
429 
430     public List findAll(int begin, int end, OrderByComparator obc)
431         throws SystemException {
432         String finderClassName = PollsQuestion.class.getName();
433         String finderMethodName = "findAll";
434         String[] finderParams = new String[] {
435                 "java.lang.Integer", "java.lang.Integer",
436                 "com.liferay.portal.kernel.util.OrderByComparator"
437             };
438         Object[] finderArgs = new Object[] {
439                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
440             };
441         Object result = FinderCache.getResult(finderClassName,
442                 finderMethodName, finderParams, finderArgs, getSessionFactory());
443 
444         if (result == null) {
445             Session session = null;
446 
447             try {
448                 session = openSession();
449 
450                 StringMaker query = new StringMaker();
451                 query.append(
452                     "FROM com.liferay.portlet.polls.model.PollsQuestion ");
453 
454                 if (obc != null) {
455                     query.append("ORDER BY ");
456                     query.append(obc.getOrderBy());
457                 }
458                 else {
459                     query.append("ORDER BY ");
460                     query.append("createDate DESC");
461                 }
462 
463                 Query q = session.createQuery(query.toString());
464                 List list = QueryUtil.list(q, getDialect(), begin, end);
465 
466                 if (obc == null) {
467                     Collections.sort(list);
468                 }
469 
470                 FinderCache.putResult(finderClassName, finderMethodName,
471                     finderParams, finderArgs, list);
472 
473                 return list;
474             }
475             catch (Exception e) {
476                 throw HibernateUtil.processException(e);
477             }
478             finally {
479                 closeSession(session);
480             }
481         }
482         else {
483             return (List)result;
484         }
485     }
486 
487     public void removeByGroupId(long groupId) throws SystemException {
488         Iterator itr = findByGroupId(groupId).iterator();
489 
490         while (itr.hasNext()) {
491             PollsQuestion pollsQuestion = (PollsQuestion)itr.next();
492             remove(pollsQuestion);
493         }
494     }
495 
496     public void removeAll() throws SystemException {
497         Iterator itr = findAll().iterator();
498 
499         while (itr.hasNext()) {
500             remove((PollsQuestion)itr.next());
501         }
502     }
503 
504     public int countByGroupId(long groupId) throws SystemException {
505         String finderClassName = PollsQuestion.class.getName();
506         String finderMethodName = "countByGroupId";
507         String[] finderParams = new String[] { Long.class.getName() };
508         Object[] finderArgs = new Object[] { new Long(groupId) };
509         Object result = FinderCache.getResult(finderClassName,
510                 finderMethodName, finderParams, finderArgs, getSessionFactory());
511 
512         if (result == null) {
513             Session session = null;
514 
515             try {
516                 session = openSession();
517 
518                 StringMaker query = new StringMaker();
519                 query.append("SELECT COUNT(*) ");
520                 query.append(
521                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
522                 query.append("groupId = ?");
523                 query.append(" ");
524 
525                 Query q = session.createQuery(query.toString());
526                 int queryPos = 0;
527                 q.setLong(queryPos++, groupId);
528 
529                 Long count = null;
530                 Iterator itr = q.list().iterator();
531 
532                 if (itr.hasNext()) {
533                     count = (Long)itr.next();
534                 }
535 
536                 if (count == null) {
537                     count = new Long(0);
538                 }
539 
540                 FinderCache.putResult(finderClassName, finderMethodName,
541                     finderParams, finderArgs, count);
542 
543                 return count.intValue();
544             }
545             catch (Exception e) {
546                 throw HibernateUtil.processException(e);
547             }
548             finally {
549                 closeSession(session);
550             }
551         }
552         else {
553             return ((Long)result).intValue();
554         }
555     }
556 
557     public int countAll() throws SystemException {
558         String finderClassName = PollsQuestion.class.getName();
559         String finderMethodName = "countAll";
560         String[] finderParams = new String[] {  };
561         Object[] finderArgs = new Object[] {  };
562         Object result = FinderCache.getResult(finderClassName,
563                 finderMethodName, finderParams, finderArgs, getSessionFactory());
564 
565         if (result == null) {
566             Session session = null;
567 
568             try {
569                 session = openSession();
570 
571                 StringMaker query = new StringMaker();
572                 query.append("SELECT COUNT(*) ");
573                 query.append(
574                     "FROM com.liferay.portlet.polls.model.PollsQuestion");
575 
576                 Query q = session.createQuery(query.toString());
577                 Long count = null;
578                 Iterator itr = q.list().iterator();
579 
580                 if (itr.hasNext()) {
581                     count = (Long)itr.next();
582                 }
583 
584                 if (count == null) {
585                     count = new Long(0);
586                 }
587 
588                 FinderCache.putResult(finderClassName, finderMethodName,
589                     finderParams, finderArgs, count);
590 
591                 return count.intValue();
592             }
593             catch (Exception e) {
594                 throw HibernateUtil.processException(e);
595             }
596             finally {
597                 closeSession(session);
598             }
599         }
600         else {
601             return ((Long)result).intValue();
602         }
603     }
604 
605     protected void initDao() {
606     }
607 
608     private static Log _log = LogFactory.getLog(PollsQuestionPersistenceImpl.class);
609 }