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