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