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.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchListTypeException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.ListType;
33  import com.liferay.portal.model.impl.ListTypeImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="ListTypePersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ListTypePersistenceImpl extends BasePersistence
57      implements ListTypePersistence {
58      public ListType create(int listTypeId) {
59          ListType listType = new ListTypeImpl();
60          listType.setNew(true);
61          listType.setPrimaryKey(listTypeId);
62  
63          return listType;
64      }
65  
66      public ListType remove(int listTypeId)
67          throws NoSuchListTypeException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              ListType listType = (ListType)session.get(ListTypeImpl.class,
74                      new Integer(listTypeId));
75  
76              if (listType == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No ListType exists with the primary key " +
79                          listTypeId);
80                  }
81  
82                  throw new NoSuchListTypeException(
83                      "No ListType exists with the primary key " + listTypeId);
84              }
85  
86              return remove(listType);
87          }
88          catch (NoSuchListTypeException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw HibernateUtil.processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public ListType remove(ListType listType) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(listType);
105             session.flush();
106 
107             return listType;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(ListType.class.getName());
115         }
116     }
117 
118     public ListType update(com.liferay.portal.model.ListType listType)
119         throws SystemException {
120         return update(listType, false);
121     }
122 
123     public ListType update(com.liferay.portal.model.ListType listType,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(listType);
132             }
133             else {
134                 if (listType.isNew()) {
135                     session.save(listType);
136                 }
137             }
138 
139             session.flush();
140             listType.setNew(false);
141 
142             return listType;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(ListType.class.getName());
150         }
151     }
152 
153     public ListType findByPrimaryKey(int listTypeId)
154         throws NoSuchListTypeException, SystemException {
155         ListType listType = fetchByPrimaryKey(listTypeId);
156 
157         if (listType == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No ListType exists with the primary key " +
160                     listTypeId);
161             }
162 
163             throw new NoSuchListTypeException(
164                 "No ListType exists with the primary key " + listTypeId);
165         }
166 
167         return listType;
168     }
169 
170     public ListType fetchByPrimaryKey(int listTypeId) throws SystemException {
171         Session session = null;
172 
173         try {
174             session = openSession();
175 
176             return (ListType)session.get(ListTypeImpl.class,
177                 new Integer(listTypeId));
178         }
179         catch (Exception e) {
180             throw HibernateUtil.processException(e);
181         }
182         finally {
183             closeSession(session);
184         }
185     }
186 
187     public List findByType(String type) throws SystemException {
188         String finderClassName = ListType.class.getName();
189         String finderMethodName = "findByType";
190         String[] finderParams = new String[] { String.class.getName() };
191         Object[] finderArgs = new Object[] { type };
192         Object result = FinderCache.getResult(finderClassName,
193                 finderMethodName, finderParams, finderArgs, getSessionFactory());
194 
195         if (result == null) {
196             Session session = null;
197 
198             try {
199                 session = openSession();
200 
201                 StringMaker query = new StringMaker();
202                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
203 
204                 if (type == null) {
205                     query.append("type_ IS NULL");
206                 }
207                 else {
208                     query.append("type_ = ?");
209                 }
210 
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("name ASC");
214 
215                 Query q = session.createQuery(query.toString());
216                 int queryPos = 0;
217 
218                 if (type != null) {
219                     q.setString(queryPos++, type);
220                 }
221 
222                 List list = q.list();
223                 FinderCache.putResult(finderClassName, finderMethodName,
224                     finderParams, finderArgs, list);
225 
226                 return list;
227             }
228             catch (Exception e) {
229                 throw HibernateUtil.processException(e);
230             }
231             finally {
232                 closeSession(session);
233             }
234         }
235         else {
236             return (List)result;
237         }
238     }
239 
240     public List findByType(String type, int begin, int end)
241         throws SystemException {
242         return findByType(type, begin, end, null);
243     }
244 
245     public List findByType(String type, int begin, int end,
246         OrderByComparator obc) throws SystemException {
247         String finderClassName = ListType.class.getName();
248         String finderMethodName = "findByType";
249         String[] finderParams = new String[] {
250                 String.class.getName(), "java.lang.Integer", "java.lang.Integer",
251                 "com.liferay.portal.kernel.util.OrderByComparator"
252             };
253         Object[] finderArgs = new Object[] {
254                 type, String.valueOf(begin), String.valueOf(end),
255                 String.valueOf(obc)
256             };
257         Object result = FinderCache.getResult(finderClassName,
258                 finderMethodName, finderParams, finderArgs, getSessionFactory());
259 
260         if (result == null) {
261             Session session = null;
262 
263             try {
264                 session = openSession();
265 
266                 StringMaker query = new StringMaker();
267                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
268 
269                 if (type == null) {
270                     query.append("type_ IS NULL");
271                 }
272                 else {
273                     query.append("type_ = ?");
274                 }
275 
276                 query.append(" ");
277 
278                 if (obc != null) {
279                     query.append("ORDER BY ");
280                     query.append(obc.getOrderBy());
281                 }
282                 else {
283                     query.append("ORDER BY ");
284                     query.append("name ASC");
285                 }
286 
287                 Query q = session.createQuery(query.toString());
288                 int queryPos = 0;
289 
290                 if (type != null) {
291                     q.setString(queryPos++, type);
292                 }
293 
294                 List list = QueryUtil.list(q, getDialect(), begin, end);
295                 FinderCache.putResult(finderClassName, finderMethodName,
296                     finderParams, finderArgs, list);
297 
298                 return list;
299             }
300             catch (Exception e) {
301                 throw HibernateUtil.processException(e);
302             }
303             finally {
304                 closeSession(session);
305             }
306         }
307         else {
308             return (List)result;
309         }
310     }
311 
312     public ListType findByType_First(String type, OrderByComparator obc)
313         throws NoSuchListTypeException, SystemException {
314         List list = findByType(type, 0, 1, obc);
315 
316         if (list.size() == 0) {
317             StringMaker msg = new StringMaker();
318             msg.append("No ListType exists with the key ");
319             msg.append(StringPool.OPEN_CURLY_BRACE);
320             msg.append("type=");
321             msg.append(type);
322             msg.append(StringPool.CLOSE_CURLY_BRACE);
323             throw new NoSuchListTypeException(msg.toString());
324         }
325         else {
326             return (ListType)list.get(0);
327         }
328     }
329 
330     public ListType findByType_Last(String type, OrderByComparator obc)
331         throws NoSuchListTypeException, SystemException {
332         int count = countByType(type);
333         List list = findByType(type, count - 1, count, obc);
334 
335         if (list.size() == 0) {
336             StringMaker msg = new StringMaker();
337             msg.append("No ListType exists with the key ");
338             msg.append(StringPool.OPEN_CURLY_BRACE);
339             msg.append("type=");
340             msg.append(type);
341             msg.append(StringPool.CLOSE_CURLY_BRACE);
342             throw new NoSuchListTypeException(msg.toString());
343         }
344         else {
345             return (ListType)list.get(0);
346         }
347     }
348 
349     public ListType[] findByType_PrevAndNext(int listTypeId, String type,
350         OrderByComparator obc) throws NoSuchListTypeException, SystemException {
351         ListType listType = findByPrimaryKey(listTypeId);
352         int count = countByType(type);
353         Session session = null;
354 
355         try {
356             session = openSession();
357 
358             StringMaker query = new StringMaker();
359             query.append("FROM com.liferay.portal.model.ListType WHERE ");
360 
361             if (type == null) {
362                 query.append("type_ IS NULL");
363             }
364             else {
365                 query.append("type_ = ?");
366             }
367 
368             query.append(" ");
369 
370             if (obc != null) {
371                 query.append("ORDER BY ");
372                 query.append(obc.getOrderBy());
373             }
374             else {
375                 query.append("ORDER BY ");
376                 query.append("name ASC");
377             }
378 
379             Query q = session.createQuery(query.toString());
380             int queryPos = 0;
381 
382             if (type != null) {
383                 q.setString(queryPos++, type);
384             }
385 
386             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, listType);
387             ListType[] array = new ListTypeImpl[3];
388             array[0] = (ListType)objArray[0];
389             array[1] = (ListType)objArray[1];
390             array[2] = (ListType)objArray[2];
391 
392             return array;
393         }
394         catch (Exception e) {
395             throw HibernateUtil.processException(e);
396         }
397         finally {
398             closeSession(session);
399         }
400     }
401 
402     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
403         throws SystemException {
404         Session session = null;
405 
406         try {
407             session = openSession();
408 
409             DynamicQuery query = queryInitializer.initialize(session);
410 
411             return query.list();
412         }
413         catch (Exception e) {
414             throw HibernateUtil.processException(e);
415         }
416         finally {
417             closeSession(session);
418         }
419     }
420 
421     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
422         int begin, int end) throws SystemException {
423         Session session = null;
424 
425         try {
426             session = openSession();
427 
428             DynamicQuery query = queryInitializer.initialize(session);
429             query.setLimit(begin, end);
430 
431             return query.list();
432         }
433         catch (Exception e) {
434             throw HibernateUtil.processException(e);
435         }
436         finally {
437             closeSession(session);
438         }
439     }
440 
441     public List findAll() throws SystemException {
442         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
443     }
444 
445     public List findAll(int begin, int end) throws SystemException {
446         return findAll(begin, end, null);
447     }
448 
449     public List findAll(int begin, int end, OrderByComparator obc)
450         throws SystemException {
451         String finderClassName = ListType.class.getName();
452         String finderMethodName = "findAll";
453         String[] finderParams = new String[] {
454                 "java.lang.Integer", "java.lang.Integer",
455                 "com.liferay.portal.kernel.util.OrderByComparator"
456             };
457         Object[] finderArgs = new Object[] {
458                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
459             };
460         Object result = FinderCache.getResult(finderClassName,
461                 finderMethodName, finderParams, finderArgs, getSessionFactory());
462 
463         if (result == null) {
464             Session session = null;
465 
466             try {
467                 session = openSession();
468 
469                 StringMaker query = new StringMaker();
470                 query.append("FROM com.liferay.portal.model.ListType ");
471 
472                 if (obc != null) {
473                     query.append("ORDER BY ");
474                     query.append(obc.getOrderBy());
475                 }
476                 else {
477                     query.append("ORDER BY ");
478                     query.append("name ASC");
479                 }
480 
481                 Query q = session.createQuery(query.toString());
482                 List list = QueryUtil.list(q, getDialect(), begin, end);
483 
484                 if (obc == null) {
485                     Collections.sort(list);
486                 }
487 
488                 FinderCache.putResult(finderClassName, finderMethodName,
489                     finderParams, finderArgs, list);
490 
491                 return list;
492             }
493             catch (Exception e) {
494                 throw HibernateUtil.processException(e);
495             }
496             finally {
497                 closeSession(session);
498             }
499         }
500         else {
501             return (List)result;
502         }
503     }
504 
505     public void removeByType(String type) throws SystemException {
506         Iterator itr = findByType(type).iterator();
507 
508         while (itr.hasNext()) {
509             ListType listType = (ListType)itr.next();
510             remove(listType);
511         }
512     }
513 
514     public void removeAll() throws SystemException {
515         Iterator itr = findAll().iterator();
516 
517         while (itr.hasNext()) {
518             remove((ListType)itr.next());
519         }
520     }
521 
522     public int countByType(String type) throws SystemException {
523         String finderClassName = ListType.class.getName();
524         String finderMethodName = "countByType";
525         String[] finderParams = new String[] { String.class.getName() };
526         Object[] finderArgs = new Object[] { type };
527         Object result = FinderCache.getResult(finderClassName,
528                 finderMethodName, finderParams, finderArgs, getSessionFactory());
529 
530         if (result == null) {
531             Session session = null;
532 
533             try {
534                 session = openSession();
535 
536                 StringMaker query = new StringMaker();
537                 query.append("SELECT COUNT(*) ");
538                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
539 
540                 if (type == null) {
541                     query.append("type_ IS NULL");
542                 }
543                 else {
544                     query.append("type_ = ?");
545                 }
546 
547                 query.append(" ");
548 
549                 Query q = session.createQuery(query.toString());
550                 int queryPos = 0;
551 
552                 if (type != null) {
553                     q.setString(queryPos++, type);
554                 }
555 
556                 Long count = null;
557                 Iterator itr = q.list().iterator();
558 
559                 if (itr.hasNext()) {
560                     count = (Long)itr.next();
561                 }
562 
563                 if (count == null) {
564                     count = new Long(0);
565                 }
566 
567                 FinderCache.putResult(finderClassName, finderMethodName,
568                     finderParams, finderArgs, count);
569 
570                 return count.intValue();
571             }
572             catch (Exception e) {
573                 throw HibernateUtil.processException(e);
574             }
575             finally {
576                 closeSession(session);
577             }
578         }
579         else {
580             return ((Long)result).intValue();
581         }
582     }
583 
584     public int countAll() throws SystemException {
585         String finderClassName = ListType.class.getName();
586         String finderMethodName = "countAll";
587         String[] finderParams = new String[] {  };
588         Object[] finderArgs = new Object[] {  };
589         Object result = FinderCache.getResult(finderClassName,
590                 finderMethodName, finderParams, finderArgs, getSessionFactory());
591 
592         if (result == null) {
593             Session session = null;
594 
595             try {
596                 session = openSession();
597 
598                 StringMaker query = new StringMaker();
599                 query.append("SELECT COUNT(*) ");
600                 query.append("FROM com.liferay.portal.model.ListType");
601 
602                 Query q = session.createQuery(query.toString());
603                 Long count = null;
604                 Iterator itr = q.list().iterator();
605 
606                 if (itr.hasNext()) {
607                     count = (Long)itr.next();
608                 }
609 
610                 if (count == null) {
611                     count = new Long(0);
612                 }
613 
614                 FinderCache.putResult(finderClassName, finderMethodName,
615                     finderParams, finderArgs, count);
616 
617                 return count.intValue();
618             }
619             catch (Exception e) {
620                 throw HibernateUtil.processException(e);
621             }
622             finally {
623                 closeSession(session);
624             }
625         }
626         else {
627             return ((Long)result).intValue();
628         }
629     }
630 
631     protected void initDao() {
632     }
633 
634     private static Log _log = LogFactory.getLog(ListTypePersistenceImpl.class);
635 }