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