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