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