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