1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.polls.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.annotation.BeanReference;
24  import com.liferay.portal.kernel.cache.CacheRegistry;
25  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
26  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderPath;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.kernel.util.Validator;
40  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
41  import com.liferay.portal.model.ModelListener;
42  import com.liferay.portal.service.persistence.BatchSessionUtil;
43  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44  
45  import com.liferay.portlet.polls.NoSuchQuestionException;
46  import com.liferay.portlet.polls.model.PollsQuestion;
47  import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
48  import com.liferay.portlet.polls.model.impl.PollsQuestionModelImpl;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="PollsQuestionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class PollsQuestionPersistenceImpl extends BasePersistenceImpl
61      implements PollsQuestionPersistence {
62      public static final String FINDER_CLASS_NAME_ENTITY = PollsQuestionImpl.class.getName();
63      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
64          ".List";
65      public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
66              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
67              FINDER_CLASS_NAME_LIST, "findByUuid",
68              new String[] { String.class.getName() });
69      public static final FinderPath FINDER_PATH_FIND_BY_OBC_UUID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
70              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
71              FINDER_CLASS_NAME_LIST, "findByUuid",
72              new String[] {
73                  String.class.getName(),
74                  
75              "java.lang.Integer", "java.lang.Integer",
76                  "com.liferay.portal.kernel.util.OrderByComparator"
77              });
78      public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
79              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
80              FINDER_CLASS_NAME_LIST, "countByUuid",
81              new String[] { String.class.getName() });
82      public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
83              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
84              FINDER_CLASS_NAME_ENTITY, "fetchByUUID_G",
85              new String[] { String.class.getName(), Long.class.getName() });
86      public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
87              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
88              FINDER_CLASS_NAME_LIST, "countByUUID_G",
89              new String[] { String.class.getName(), Long.class.getName() });
90      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
91              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_LIST, "findByGroupId",
93              new String[] { Long.class.getName() });
94      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
95              PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
96              FINDER_CLASS_NAME_LIST, "findByGroupId",
97              new String[] {
98                  Long.class.getName(),
99                  
100             "java.lang.Integer", "java.lang.Integer",
101                 "com.liferay.portal.kernel.util.OrderByComparator"
102             });
103     public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
104             PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
105             FINDER_CLASS_NAME_LIST, "countByGroupId",
106             new String[] { Long.class.getName() });
107     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
108             PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
109             FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
110     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
111             PollsQuestionModelImpl.FINDER_CACHE_ENABLED,
112             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
113 
114     public void cacheResult(PollsQuestion pollsQuestion) {
115         EntityCacheUtil.putResult(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
116             PollsQuestionImpl.class, pollsQuestion.getPrimaryKey(),
117             pollsQuestion);
118 
119         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
120             new Object[] {
121                 pollsQuestion.getUuid(), new Long(pollsQuestion.getGroupId())
122             }, pollsQuestion);
123     }
124 
125     public void cacheResult(List<PollsQuestion> pollsQuestions) {
126         for (PollsQuestion pollsQuestion : pollsQuestions) {
127             if (EntityCacheUtil.getResult(
128                         PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
129                         PollsQuestionImpl.class, pollsQuestion.getPrimaryKey(),
130                         this) == null) {
131                 cacheResult(pollsQuestion);
132             }
133         }
134     }
135 
136     public void clearCache() {
137         CacheRegistry.clear(PollsQuestionImpl.class.getName());
138         EntityCacheUtil.clearCache(PollsQuestionImpl.class.getName());
139         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
140         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
141     }
142 
143     public PollsQuestion create(long questionId) {
144         PollsQuestion pollsQuestion = new PollsQuestionImpl();
145 
146         pollsQuestion.setNew(true);
147         pollsQuestion.setPrimaryKey(questionId);
148 
149         String uuid = PortalUUIDUtil.generate();
150 
151         pollsQuestion.setUuid(uuid);
152 
153         return pollsQuestion;
154     }
155 
156     public PollsQuestion remove(long questionId)
157         throws NoSuchQuestionException, SystemException {
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             PollsQuestion pollsQuestion = (PollsQuestion)session.get(PollsQuestionImpl.class,
164                     new Long(questionId));
165 
166             if (pollsQuestion == null) {
167                 if (_log.isWarnEnabled()) {
168                     _log.warn("No PollsQuestion exists with the primary key " +
169                         questionId);
170                 }
171 
172                 throw new NoSuchQuestionException(
173                     "No PollsQuestion exists with the primary key " +
174                     questionId);
175             }
176 
177             return remove(pollsQuestion);
178         }
179         catch (NoSuchQuestionException nsee) {
180             throw nsee;
181         }
182         catch (Exception e) {
183             throw processException(e);
184         }
185         finally {
186             closeSession(session);
187         }
188     }
189 
190     public PollsQuestion remove(PollsQuestion pollsQuestion)
191         throws SystemException {
192         for (ModelListener<PollsQuestion> listener : listeners) {
193             listener.onBeforeRemove(pollsQuestion);
194         }
195 
196         pollsQuestion = removeImpl(pollsQuestion);
197 
198         for (ModelListener<PollsQuestion> listener : listeners) {
199             listener.onAfterRemove(pollsQuestion);
200         }
201 
202         return pollsQuestion;
203     }
204 
205     protected PollsQuestion removeImpl(PollsQuestion pollsQuestion)
206         throws SystemException {
207         Session session = null;
208 
209         try {
210             session = openSession();
211 
212             if (pollsQuestion.isCachedModel() || BatchSessionUtil.isEnabled()) {
213                 Object staleObject = session.get(PollsQuestionImpl.class,
214                         pollsQuestion.getPrimaryKeyObj());
215 
216                 if (staleObject != null) {
217                     session.evict(staleObject);
218                 }
219             }
220 
221             session.delete(pollsQuestion);
222 
223             session.flush();
224         }
225         catch (Exception e) {
226             throw processException(e);
227         }
228         finally {
229             closeSession(session);
230         }
231 
232         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
233 
234         PollsQuestionModelImpl pollsQuestionModelImpl = (PollsQuestionModelImpl)pollsQuestion;
235 
236         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
237             new Object[] {
238                 pollsQuestionModelImpl.getOriginalUuid(),
239                 new Long(pollsQuestionModelImpl.getOriginalGroupId())
240             });
241 
242         EntityCacheUtil.removeResult(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
243             PollsQuestionImpl.class, pollsQuestion.getPrimaryKey());
244 
245         return pollsQuestion;
246     }
247 
248     /**
249      * @deprecated Use <code>update(PollsQuestion pollsQuestion, boolean merge)</code>.
250      */
251     public PollsQuestion update(PollsQuestion pollsQuestion)
252         throws SystemException {
253         if (_log.isWarnEnabled()) {
254             _log.warn(
255                 "Using the deprecated update(PollsQuestion pollsQuestion) method. Use update(PollsQuestion pollsQuestion, boolean merge) instead.");
256         }
257 
258         return update(pollsQuestion, false);
259     }
260 
261     /**
262      * Add, update, or merge, the entity. This method also calls the model
263      * listeners to trigger the proper events associated with adding, deleting,
264      * or updating an entity.
265      *
266      * @param        pollsQuestion the entity to add, update, or merge
267      * @param        merge boolean value for whether to merge the entity. The
268      *                default value is false. Setting merge to true is more
269      *                expensive and should only be true when pollsQuestion is
270      *                transient. See LEP-5473 for a detailed discussion of this
271      *                method.
272      * @return        true if the portlet can be displayed via Ajax
273      */
274     public PollsQuestion update(PollsQuestion pollsQuestion, boolean merge)
275         throws SystemException {
276         boolean isNew = pollsQuestion.isNew();
277 
278         for (ModelListener<PollsQuestion> listener : listeners) {
279             if (isNew) {
280                 listener.onBeforeCreate(pollsQuestion);
281             }
282             else {
283                 listener.onBeforeUpdate(pollsQuestion);
284             }
285         }
286 
287         pollsQuestion = updateImpl(pollsQuestion, merge);
288 
289         for (ModelListener<PollsQuestion> listener : listeners) {
290             if (isNew) {
291                 listener.onAfterCreate(pollsQuestion);
292             }
293             else {
294                 listener.onAfterUpdate(pollsQuestion);
295             }
296         }
297 
298         return pollsQuestion;
299     }
300 
301     public PollsQuestion updateImpl(
302         com.liferay.portlet.polls.model.PollsQuestion pollsQuestion,
303         boolean merge) throws SystemException {
304         boolean isNew = pollsQuestion.isNew();
305 
306         PollsQuestionModelImpl pollsQuestionModelImpl = (PollsQuestionModelImpl)pollsQuestion;
307 
308         if (Validator.isNull(pollsQuestion.getUuid())) {
309             String uuid = PortalUUIDUtil.generate();
310 
311             pollsQuestion.setUuid(uuid);
312         }
313 
314         Session session = null;
315 
316         try {
317             session = openSession();
318 
319             BatchSessionUtil.update(session, pollsQuestion, merge);
320 
321             pollsQuestion.setNew(false);
322         }
323         catch (Exception e) {
324             throw processException(e);
325         }
326         finally {
327             closeSession(session);
328         }
329 
330         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
331 
332         EntityCacheUtil.putResult(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
333             PollsQuestionImpl.class, pollsQuestion.getPrimaryKey(),
334             pollsQuestion);
335 
336         if (!isNew &&
337                 (!pollsQuestion.getUuid()
338                                    .equals(pollsQuestionModelImpl.getOriginalUuid()) ||
339                 (pollsQuestion.getGroupId() != pollsQuestionModelImpl.getOriginalGroupId()))) {
340             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
341                 new Object[] {
342                     pollsQuestionModelImpl.getOriginalUuid(),
343                     new Long(pollsQuestionModelImpl.getOriginalGroupId())
344                 });
345         }
346 
347         if (isNew ||
348                 (!pollsQuestion.getUuid()
349                                    .equals(pollsQuestionModelImpl.getOriginalUuid()) ||
350                 (pollsQuestion.getGroupId() != pollsQuestionModelImpl.getOriginalGroupId()))) {
351             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
352                 new Object[] {
353                     pollsQuestion.getUuid(),
354                     new Long(pollsQuestion.getGroupId())
355                 }, pollsQuestion);
356         }
357 
358         return pollsQuestion;
359     }
360 
361     public PollsQuestion findByPrimaryKey(long questionId)
362         throws NoSuchQuestionException, SystemException {
363         PollsQuestion pollsQuestion = fetchByPrimaryKey(questionId);
364 
365         if (pollsQuestion == null) {
366             if (_log.isWarnEnabled()) {
367                 _log.warn("No PollsQuestion exists with the primary key " +
368                     questionId);
369             }
370 
371             throw new NoSuchQuestionException(
372                 "No PollsQuestion exists with the primary key " + questionId);
373         }
374 
375         return pollsQuestion;
376     }
377 
378     public PollsQuestion fetchByPrimaryKey(long questionId)
379         throws SystemException {
380         PollsQuestion pollsQuestion = (PollsQuestion)EntityCacheUtil.getResult(PollsQuestionModelImpl.ENTITY_CACHE_ENABLED,
381                 PollsQuestionImpl.class, questionId, this);
382 
383         if (pollsQuestion == null) {
384             Session session = null;
385 
386             try {
387                 session = openSession();
388 
389                 pollsQuestion = (PollsQuestion)session.get(PollsQuestionImpl.class,
390                         new Long(questionId));
391             }
392             catch (Exception e) {
393                 throw processException(e);
394             }
395             finally {
396                 if (pollsQuestion != null) {
397                     cacheResult(pollsQuestion);
398                 }
399 
400                 closeSession(session);
401             }
402         }
403 
404         return pollsQuestion;
405     }
406 
407     public List<PollsQuestion> findByUuid(String uuid)
408         throws SystemException {
409         Object[] finderArgs = new Object[] { uuid };
410 
411         List<PollsQuestion> list = (List<PollsQuestion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
412                 finderArgs, this);
413 
414         if (list == null) {
415             Session session = null;
416 
417             try {
418                 session = openSession();
419 
420                 StringBuilder query = new StringBuilder();
421 
422                 query.append(
423                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
424 
425                 if (uuid == null) {
426                     query.append("uuid_ IS NULL");
427                 }
428                 else {
429                     query.append("uuid_ = ?");
430                 }
431 
432                 query.append(" ");
433 
434                 query.append("ORDER BY ");
435 
436                 query.append("createDate DESC");
437 
438                 Query q = session.createQuery(query.toString());
439 
440                 QueryPos qPos = QueryPos.getInstance(q);
441 
442                 if (uuid != null) {
443                     qPos.add(uuid);
444                 }
445 
446                 list = q.list();
447             }
448             catch (Exception e) {
449                 throw processException(e);
450             }
451             finally {
452                 if (list == null) {
453                     list = new ArrayList<PollsQuestion>();
454                 }
455 
456                 cacheResult(list);
457 
458                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID, finderArgs,
459                     list);
460 
461                 closeSession(session);
462             }
463         }
464 
465         return list;
466     }
467 
468     public List<PollsQuestion> findByUuid(String uuid, int start, int end)
469         throws SystemException {
470         return findByUuid(uuid, start, end, null);
471     }
472 
473     public List<PollsQuestion> findByUuid(String uuid, int start, int end,
474         OrderByComparator obc) throws SystemException {
475         Object[] finderArgs = new Object[] {
476                 uuid,
477                 
478                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
479             };
480 
481         List<PollsQuestion> list = (List<PollsQuestion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_UUID,
482                 finderArgs, this);
483 
484         if (list == null) {
485             Session session = null;
486 
487             try {
488                 session = openSession();
489 
490                 StringBuilder query = new StringBuilder();
491 
492                 query.append(
493                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
494 
495                 if (uuid == null) {
496                     query.append("uuid_ IS NULL");
497                 }
498                 else {
499                     query.append("uuid_ = ?");
500                 }
501 
502                 query.append(" ");
503 
504                 if (obc != null) {
505                     query.append("ORDER BY ");
506                     query.append(obc.getOrderBy());
507                 }
508 
509                 else {
510                     query.append("ORDER BY ");
511 
512                     query.append("createDate DESC");
513                 }
514 
515                 Query q = session.createQuery(query.toString());
516 
517                 QueryPos qPos = QueryPos.getInstance(q);
518 
519                 if (uuid != null) {
520                     qPos.add(uuid);
521                 }
522 
523                 list = (List<PollsQuestion>)QueryUtil.list(q, getDialect(),
524                         start, end);
525             }
526             catch (Exception e) {
527                 throw processException(e);
528             }
529             finally {
530                 if (list == null) {
531                     list = new ArrayList<PollsQuestion>();
532                 }
533 
534                 cacheResult(list);
535 
536                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_UUID,
537                     finderArgs, list);
538 
539                 closeSession(session);
540             }
541         }
542 
543         return list;
544     }
545 
546     public PollsQuestion findByUuid_First(String uuid, OrderByComparator obc)
547         throws NoSuchQuestionException, SystemException {
548         List<PollsQuestion> list = findByUuid(uuid, 0, 1, obc);
549 
550         if (list.isEmpty()) {
551             StringBuilder msg = new StringBuilder();
552 
553             msg.append("No PollsQuestion exists with the key {");
554 
555             msg.append("uuid=" + uuid);
556 
557             msg.append(StringPool.CLOSE_CURLY_BRACE);
558 
559             throw new NoSuchQuestionException(msg.toString());
560         }
561         else {
562             return list.get(0);
563         }
564     }
565 
566     public PollsQuestion findByUuid_Last(String uuid, OrderByComparator obc)
567         throws NoSuchQuestionException, SystemException {
568         int count = countByUuid(uuid);
569 
570         List<PollsQuestion> list = findByUuid(uuid, count - 1, count, obc);
571 
572         if (list.isEmpty()) {
573             StringBuilder msg = new StringBuilder();
574 
575             msg.append("No PollsQuestion exists with the key {");
576 
577             msg.append("uuid=" + uuid);
578 
579             msg.append(StringPool.CLOSE_CURLY_BRACE);
580 
581             throw new NoSuchQuestionException(msg.toString());
582         }
583         else {
584             return list.get(0);
585         }
586     }
587 
588     public PollsQuestion[] findByUuid_PrevAndNext(long questionId, String uuid,
589         OrderByComparator obc) throws NoSuchQuestionException, SystemException {
590         PollsQuestion pollsQuestion = findByPrimaryKey(questionId);
591 
592         int count = countByUuid(uuid);
593 
594         Session session = null;
595 
596         try {
597             session = openSession();
598 
599             StringBuilder query = new StringBuilder();
600 
601             query.append(
602                 "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
603 
604             if (uuid == null) {
605                 query.append("uuid_ IS NULL");
606             }
607             else {
608                 query.append("uuid_ = ?");
609             }
610 
611             query.append(" ");
612 
613             if (obc != null) {
614                 query.append("ORDER BY ");
615                 query.append(obc.getOrderBy());
616             }
617 
618             else {
619                 query.append("ORDER BY ");
620 
621                 query.append("createDate DESC");
622             }
623 
624             Query q = session.createQuery(query.toString());
625 
626             QueryPos qPos = QueryPos.getInstance(q);
627 
628             if (uuid != null) {
629                 qPos.add(uuid);
630             }
631 
632             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
633                     pollsQuestion);
634 
635             PollsQuestion[] array = new PollsQuestionImpl[3];
636 
637             array[0] = (PollsQuestion)objArray[0];
638             array[1] = (PollsQuestion)objArray[1];
639             array[2] = (PollsQuestion)objArray[2];
640 
641             return array;
642         }
643         catch (Exception e) {
644             throw processException(e);
645         }
646         finally {
647             closeSession(session);
648         }
649     }
650 
651     public PollsQuestion findByUUID_G(String uuid, long groupId)
652         throws NoSuchQuestionException, SystemException {
653         PollsQuestion pollsQuestion = fetchByUUID_G(uuid, groupId);
654 
655         if (pollsQuestion == null) {
656             StringBuilder msg = new StringBuilder();
657 
658             msg.append("No PollsQuestion exists with the key {");
659 
660             msg.append("uuid=" + uuid);
661 
662             msg.append(", ");
663             msg.append("groupId=" + groupId);
664 
665             msg.append(StringPool.CLOSE_CURLY_BRACE);
666 
667             if (_log.isWarnEnabled()) {
668                 _log.warn(msg.toString());
669             }
670 
671             throw new NoSuchQuestionException(msg.toString());
672         }
673 
674         return pollsQuestion;
675     }
676 
677     public PollsQuestion fetchByUUID_G(String uuid, long groupId)
678         throws SystemException {
679         return fetchByUUID_G(uuid, groupId, true);
680     }
681 
682     public PollsQuestion fetchByUUID_G(String uuid, long groupId,
683         boolean retrieveFromCache) throws SystemException {
684         Object[] finderArgs = new Object[] { uuid, new Long(groupId) };
685 
686         Object result = null;
687 
688         if (retrieveFromCache) {
689             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G,
690                     finderArgs, this);
691         }
692 
693         if (result == null) {
694             Session session = null;
695 
696             try {
697                 session = openSession();
698 
699                 StringBuilder query = new StringBuilder();
700 
701                 query.append(
702                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
703 
704                 if (uuid == null) {
705                     query.append("uuid_ IS NULL");
706                 }
707                 else {
708                     query.append("uuid_ = ?");
709                 }
710 
711                 query.append(" AND ");
712 
713                 query.append("groupId = ?");
714 
715                 query.append(" ");
716 
717                 query.append("ORDER BY ");
718 
719                 query.append("createDate DESC");
720 
721                 Query q = session.createQuery(query.toString());
722 
723                 QueryPos qPos = QueryPos.getInstance(q);
724 
725                 if (uuid != null) {
726                     qPos.add(uuid);
727                 }
728 
729                 qPos.add(groupId);
730 
731                 List<PollsQuestion> list = q.list();
732 
733                 result = list;
734 
735                 PollsQuestion pollsQuestion = null;
736 
737                 if (list.isEmpty()) {
738                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
739                         finderArgs, list);
740                 }
741                 else {
742                     pollsQuestion = list.get(0);
743 
744                     cacheResult(pollsQuestion);
745 
746                     if ((pollsQuestion.getUuid() == null) ||
747                             !pollsQuestion.getUuid().equals(uuid) ||
748                             (pollsQuestion.getGroupId() != groupId)) {
749                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
750                             finderArgs, list);
751                     }
752                 }
753 
754                 return pollsQuestion;
755             }
756             catch (Exception e) {
757                 throw processException(e);
758             }
759             finally {
760                 if (result == null) {
761                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
762                         finderArgs, new ArrayList<PollsQuestion>());
763                 }
764 
765                 closeSession(session);
766             }
767         }
768         else {
769             if (result instanceof List) {
770                 return null;
771             }
772             else {
773                 return (PollsQuestion)result;
774             }
775         }
776     }
777 
778     public List<PollsQuestion> findByGroupId(long groupId)
779         throws SystemException {
780         Object[] finderArgs = new Object[] { new Long(groupId) };
781 
782         List<PollsQuestion> list = (List<PollsQuestion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
783                 finderArgs, this);
784 
785         if (list == null) {
786             Session session = null;
787 
788             try {
789                 session = openSession();
790 
791                 StringBuilder query = new StringBuilder();
792 
793                 query.append(
794                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
795 
796                 query.append("groupId = ?");
797 
798                 query.append(" ");
799 
800                 query.append("ORDER BY ");
801 
802                 query.append("createDate DESC");
803 
804                 Query q = session.createQuery(query.toString());
805 
806                 QueryPos qPos = QueryPos.getInstance(q);
807 
808                 qPos.add(groupId);
809 
810                 list = q.list();
811             }
812             catch (Exception e) {
813                 throw processException(e);
814             }
815             finally {
816                 if (list == null) {
817                     list = new ArrayList<PollsQuestion>();
818                 }
819 
820                 cacheResult(list);
821 
822                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
823                     finderArgs, list);
824 
825                 closeSession(session);
826             }
827         }
828 
829         return list;
830     }
831 
832     public List<PollsQuestion> findByGroupId(long groupId, int start, int end)
833         throws SystemException {
834         return findByGroupId(groupId, start, end, null);
835     }
836 
837     public List<PollsQuestion> findByGroupId(long groupId, int start, int end,
838         OrderByComparator obc) throws SystemException {
839         Object[] finderArgs = new Object[] {
840                 new Long(groupId),
841                 
842                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
843             };
844 
845         List<PollsQuestion> list = (List<PollsQuestion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
846                 finderArgs, this);
847 
848         if (list == null) {
849             Session session = null;
850 
851             try {
852                 session = openSession();
853 
854                 StringBuilder query = new StringBuilder();
855 
856                 query.append(
857                     "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
858 
859                 query.append("groupId = ?");
860 
861                 query.append(" ");
862 
863                 if (obc != null) {
864                     query.append("ORDER BY ");
865                     query.append(obc.getOrderBy());
866                 }
867 
868                 else {
869                     query.append("ORDER BY ");
870 
871                     query.append("createDate DESC");
872                 }
873 
874                 Query q = session.createQuery(query.toString());
875 
876                 QueryPos qPos = QueryPos.getInstance(q);
877 
878                 qPos.add(groupId);
879 
880                 list = (List<PollsQuestion>)QueryUtil.list(q, getDialect(),
881                         start, end);
882             }
883             catch (Exception e) {
884                 throw processException(e);
885             }
886             finally {
887                 if (list == null) {
888                     list = new ArrayList<PollsQuestion>();
889                 }
890 
891                 cacheResult(list);
892 
893                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
894                     finderArgs, list);
895 
896                 closeSession(session);
897             }
898         }
899 
900         return list;
901     }
902 
903     public PollsQuestion findByGroupId_First(long groupId, OrderByComparator obc)
904         throws NoSuchQuestionException, SystemException {
905         List<PollsQuestion> list = findByGroupId(groupId, 0, 1, obc);
906 
907         if (list.isEmpty()) {
908             StringBuilder msg = new StringBuilder();
909 
910             msg.append("No PollsQuestion exists with the key {");
911 
912             msg.append("groupId=" + groupId);
913 
914             msg.append(StringPool.CLOSE_CURLY_BRACE);
915 
916             throw new NoSuchQuestionException(msg.toString());
917         }
918         else {
919             return list.get(0);
920         }
921     }
922 
923     public PollsQuestion findByGroupId_Last(long groupId, OrderByComparator obc)
924         throws NoSuchQuestionException, SystemException {
925         int count = countByGroupId(groupId);
926 
927         List<PollsQuestion> list = findByGroupId(groupId, count - 1, count, obc);
928 
929         if (list.isEmpty()) {
930             StringBuilder msg = new StringBuilder();
931 
932             msg.append("No PollsQuestion exists with the key {");
933 
934             msg.append("groupId=" + groupId);
935 
936             msg.append(StringPool.CLOSE_CURLY_BRACE);
937 
938             throw new NoSuchQuestionException(msg.toString());
939         }
940         else {
941             return list.get(0);
942         }
943     }
944 
945     public PollsQuestion[] findByGroupId_PrevAndNext(long questionId,
946         long groupId, OrderByComparator obc)
947         throws NoSuchQuestionException, SystemException {
948         PollsQuestion pollsQuestion = findByPrimaryKey(questionId);
949 
950         int count = countByGroupId(groupId);
951 
952         Session session = null;
953 
954         try {
955             session = openSession();
956 
957             StringBuilder query = new StringBuilder();
958 
959             query.append(
960                 "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
961 
962             query.append("groupId = ?");
963 
964             query.append(" ");
965 
966             if (obc != null) {
967                 query.append("ORDER BY ");
968                 query.append(obc.getOrderBy());
969             }
970 
971             else {
972                 query.append("ORDER BY ");
973 
974                 query.append("createDate DESC");
975             }
976 
977             Query q = session.createQuery(query.toString());
978 
979             QueryPos qPos = QueryPos.getInstance(q);
980 
981             qPos.add(groupId);
982 
983             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
984                     pollsQuestion);
985 
986             PollsQuestion[] array = new PollsQuestionImpl[3];
987 
988             array[0] = (PollsQuestion)objArray[0];
989             array[1] = (PollsQuestion)objArray[1];
990             array[2] = (PollsQuestion)objArray[2];
991 
992             return array;
993         }
994         catch (Exception e) {
995             throw processException(e);
996         }
997         finally {
998             closeSession(session);
999         }
1000    }
1001
1002    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1003        throws SystemException {
1004        Session session = null;
1005
1006        try {
1007            session = openSession();
1008
1009            dynamicQuery.compile(session);
1010
1011            return dynamicQuery.list();
1012        }
1013        catch (Exception e) {
1014            throw processException(e);
1015        }
1016        finally {
1017            closeSession(session);
1018        }
1019    }
1020
1021    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1022        int start, int end) throws SystemException {
1023        Session session = null;
1024
1025        try {
1026            session = openSession();
1027
1028            dynamicQuery.setLimit(start, end);
1029
1030            dynamicQuery.compile(session);
1031
1032            return dynamicQuery.list();
1033        }
1034        catch (Exception e) {
1035            throw processException(e);
1036        }
1037        finally {
1038            closeSession(session);
1039        }
1040    }
1041
1042    public List<PollsQuestion> findAll() throws SystemException {
1043        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1044    }
1045
1046    public List<PollsQuestion> findAll(int start, int end)
1047        throws SystemException {
1048        return findAll(start, end, null);
1049    }
1050
1051    public List<PollsQuestion> findAll(int start, int end, OrderByComparator obc)
1052        throws SystemException {
1053        Object[] finderArgs = new Object[] {
1054                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1055            };
1056
1057        List<PollsQuestion> list = (List<PollsQuestion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1058                finderArgs, this);
1059
1060        if (list == null) {
1061            Session session = null;
1062
1063            try {
1064                session = openSession();
1065
1066                StringBuilder query = new StringBuilder();
1067
1068                query.append(
1069                    "FROM com.liferay.portlet.polls.model.PollsQuestion ");
1070
1071                if (obc != null) {
1072                    query.append("ORDER BY ");
1073                    query.append(obc.getOrderBy());
1074                }
1075
1076                else {
1077                    query.append("ORDER BY ");
1078
1079                    query.append("createDate DESC");
1080                }
1081
1082                Query q = session.createQuery(query.toString());
1083
1084                if (obc == null) {
1085                    list = (List<PollsQuestion>)QueryUtil.list(q, getDialect(),
1086                            start, end, false);
1087
1088                    Collections.sort(list);
1089                }
1090                else {
1091                    list = (List<PollsQuestion>)QueryUtil.list(q, getDialect(),
1092                            start, end);
1093                }
1094            }
1095            catch (Exception e) {
1096                throw processException(e);
1097            }
1098            finally {
1099                if (list == null) {
1100                    list = new ArrayList<PollsQuestion>();
1101                }
1102
1103                cacheResult(list);
1104
1105                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1106
1107                closeSession(session);
1108            }
1109        }
1110
1111        return list;
1112    }
1113
1114    public void removeByUuid(String uuid) throws SystemException {
1115        for (PollsQuestion pollsQuestion : findByUuid(uuid)) {
1116            remove(pollsQuestion);
1117        }
1118    }
1119
1120    public void removeByUUID_G(String uuid, long groupId)
1121        throws NoSuchQuestionException, SystemException {
1122        PollsQuestion pollsQuestion = findByUUID_G(uuid, groupId);
1123
1124        remove(pollsQuestion);
1125    }
1126
1127    public void removeByGroupId(long groupId) throws SystemException {
1128        for (PollsQuestion pollsQuestion : findByGroupId(groupId)) {
1129            remove(pollsQuestion);
1130        }
1131    }
1132
1133    public void removeAll() throws SystemException {
1134        for (PollsQuestion pollsQuestion : findAll()) {
1135            remove(pollsQuestion);
1136        }
1137    }
1138
1139    public int countByUuid(String uuid) throws SystemException {
1140        Object[] finderArgs = new Object[] { uuid };
1141
1142        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1143                finderArgs, this);
1144
1145        if (count == null) {
1146            Session session = null;
1147
1148            try {
1149                session = openSession();
1150
1151                StringBuilder query = new StringBuilder();
1152
1153                query.append("SELECT COUNT(*) ");
1154                query.append(
1155                    "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
1156
1157                if (uuid == null) {
1158                    query.append("uuid_ IS NULL");
1159                }
1160                else {
1161                    query.append("uuid_ = ?");
1162                }
1163
1164                query.append(" ");
1165
1166                Query q = session.createQuery(query.toString());
1167
1168                QueryPos qPos = QueryPos.getInstance(q);
1169
1170                if (uuid != null) {
1171                    qPos.add(uuid);
1172                }
1173
1174                count = (Long)q.uniqueResult();
1175            }
1176            catch (Exception e) {
1177                throw processException(e);
1178            }
1179            finally {
1180                if (count == null) {
1181                    count = Long.valueOf(0);
1182                }
1183
1184                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1185                    finderArgs, count);
1186
1187                closeSession(session);
1188            }
1189        }
1190
1191        return count.intValue();
1192    }
1193
1194    public int countByUUID_G(String uuid, long groupId)
1195        throws SystemException {
1196        Object[] finderArgs = new Object[] { uuid, new Long(groupId) };
1197
1198        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID_G,
1199                finderArgs, this);
1200
1201        if (count == null) {
1202            Session session = null;
1203
1204            try {
1205                session = openSession();
1206
1207                StringBuilder query = new StringBuilder();
1208
1209                query.append("SELECT COUNT(*) ");
1210                query.append(
1211                    "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
1212
1213                if (uuid == null) {
1214                    query.append("uuid_ IS NULL");
1215                }
1216                else {
1217                    query.append("uuid_ = ?");
1218                }
1219
1220                query.append(" AND ");
1221
1222                query.append("groupId = ?");
1223
1224                query.append(" ");
1225
1226                Query q = session.createQuery(query.toString());
1227
1228                QueryPos qPos = QueryPos.getInstance(q);
1229
1230                if (uuid != null) {
1231                    qPos.add(uuid);
1232                }
1233
1234                qPos.add(groupId);
1235
1236                count = (Long)q.uniqueResult();
1237            }
1238            catch (Exception e) {
1239                throw processException(e);
1240            }
1241            finally {
1242                if (count == null) {
1243                    count = Long.valueOf(0);
1244                }
1245
1246                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G,
1247                    finderArgs, count);
1248
1249                closeSession(session);
1250            }
1251        }
1252
1253        return count.intValue();
1254    }
1255
1256    public int countByGroupId(long groupId) throws SystemException {
1257        Object[] finderArgs = new Object[] { new Long(groupId) };
1258
1259        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1260                finderArgs, this);
1261
1262        if (count == null) {
1263            Session session = null;
1264
1265            try {
1266                session = openSession();
1267
1268                StringBuilder query = new StringBuilder();
1269
1270                query.append("SELECT COUNT(*) ");
1271                query.append(
1272                    "FROM com.liferay.portlet.polls.model.PollsQuestion WHERE ");
1273
1274                query.append("groupId = ?");
1275
1276                query.append(" ");
1277
1278                Query q = session.createQuery(query.toString());
1279
1280                QueryPos qPos = QueryPos.getInstance(q);
1281
1282                qPos.add(groupId);
1283
1284                count = (Long)q.uniqueResult();
1285            }
1286            catch (Exception e) {
1287                throw processException(e);
1288            }
1289            finally {
1290                if (count == null) {
1291                    count = Long.valueOf(0);
1292                }
1293
1294                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1295                    finderArgs, count);
1296
1297                closeSession(session);
1298            }
1299        }
1300
1301        return count.intValue();
1302    }
1303
1304    public int countAll() throws SystemException {
1305        Object[] finderArgs = new Object[0];
1306
1307        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1308                finderArgs, this);
1309
1310        if (count == null) {
1311            Session session = null;
1312
1313            try {
1314                session = openSession();
1315
1316                Query q = session.createQuery(
1317                        "SELECT COUNT(*) FROM com.liferay.portlet.polls.model.PollsQuestion");
1318
1319                count = (Long)q.uniqueResult();
1320            }
1321            catch (Exception e) {
1322                throw processException(e);
1323            }
1324            finally {
1325                if (count == null) {
1326                    count = Long.valueOf(0);
1327                }
1328
1329                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1330                    count);
1331
1332                closeSession(session);
1333            }
1334        }
1335
1336        return count.intValue();
1337    }
1338
1339    public void afterPropertiesSet() {
1340        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1341                    com.liferay.portal.util.PropsUtil.get(
1342                        "value.object.listener.com.liferay.portlet.polls.model.PollsQuestion")));
1343
1344        if (listenerClassNames.length > 0) {
1345            try {
1346                List<ModelListener<PollsQuestion>> listenersList = new ArrayList<ModelListener<PollsQuestion>>();
1347
1348                for (String listenerClassName : listenerClassNames) {
1349                    listenersList.add((ModelListener<PollsQuestion>)Class.forName(
1350                            listenerClassName).newInstance());
1351                }
1352
1353                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1354            }
1355            catch (Exception e) {
1356                _log.error(e);
1357            }
1358        }
1359    }
1360
1361    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsChoicePersistence.impl")
1362    protected com.liferay.portlet.polls.service.persistence.PollsChoicePersistence pollsChoicePersistence;
1363    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence.impl")
1364    protected com.liferay.portlet.polls.service.persistence.PollsQuestionPersistence pollsQuestionPersistence;
1365    @BeanReference(name = "com.liferay.portlet.polls.service.persistence.PollsVotePersistence.impl")
1366    protected com.liferay.portlet.polls.service.persistence.PollsVotePersistence pollsVotePersistence;
1367    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1368    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1369    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1370    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1371    private static Log _log = LogFactoryUtil.getLog(PollsQuestionPersistenceImpl.class);
1372}