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