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             if (BatchSessionUtil.isEnabled()) {
127                 Object staleObject = session.get(ListTypeImpl.class,
128                         listType.getPrimaryKeyObj());
129 
130                 if (staleObject != null) {
131                     session.evict(staleObject);
132                 }
133             }
134 
135             session.delete(listType);
136 
137             session.flush();
138 
139             return listType;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(ListType.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(ListType listType, boolean merge)</code>.
153      */
154     public ListType update(ListType listType) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(ListType listType) method. Use update(ListType listType, boolean merge) instead.");
158         }
159 
160         return update(listType, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        listType the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when listType is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public ListType update(ListType listType, boolean merge)
177         throws SystemException {
178         boolean isNew = listType.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(listType);
184                 }
185                 else {
186                     listener.onBeforeUpdate(listType);
187                 }
188             }
189         }
190 
191         listType = updateImpl(listType, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(listType);
197                 }
198                 else {
199                     listener.onAfterUpdate(listType);
200                 }
201             }
202         }
203 
204         return listType;
205     }
206 
207     public ListType updateImpl(com.liferay.portal.model.ListType listType,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             BatchSessionUtil.update(session, listType, merge);
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 = null;
612 
613                 if (obc == null) {
614                     list = (List<ListType>)QueryUtil.list(q, getDialect(),
615                             start, end, false);
616 
617                     Collections.sort(list);
618                 }
619                 else {
620                     list = (List<ListType>)QueryUtil.list(q, getDialect(),
621                             start, end);
622                 }
623 
624                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
625                     finderClassName, finderMethodName, finderParams,
626                     finderArgs, list);
627 
628                 return list;
629             }
630             catch (Exception e) {
631                 throw processException(e);
632             }
633             finally {
634                 closeSession(session);
635             }
636         }
637         else {
638             return (List<ListType>)result;
639         }
640     }
641 
642     public void removeByType(String type) throws SystemException {
643         for (ListType listType : findByType(type)) {
644             remove(listType);
645         }
646     }
647 
648     public void removeAll() throws SystemException {
649         for (ListType listType : findAll()) {
650             remove(listType);
651         }
652     }
653 
654     public int countByType(String type) throws SystemException {
655         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
656         String finderClassName = ListType.class.getName();
657         String finderMethodName = "countByType";
658         String[] finderParams = new String[] { String.class.getName() };
659         Object[] finderArgs = new Object[] { type };
660 
661         Object result = null;
662 
663         if (finderClassNameCacheEnabled) {
664             result = FinderCacheUtil.getResult(finderClassName,
665                     finderMethodName, finderParams, finderArgs, this);
666         }
667 
668         if (result == null) {
669             Session session = null;
670 
671             try {
672                 session = openSession();
673 
674                 StringBuilder query = new StringBuilder();
675 
676                 query.append("SELECT COUNT(*) ");
677                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
678 
679                 if (type == null) {
680                     query.append("type_ IS NULL");
681                 }
682                 else {
683                     query.append("type_ = ?");
684                 }
685 
686                 query.append(" ");
687 
688                 Query q = session.createQuery(query.toString());
689 
690                 QueryPos qPos = QueryPos.getInstance(q);
691 
692                 if (type != null) {
693                     qPos.add(type);
694                 }
695 
696                 Long count = null;
697 
698                 Iterator<Long> itr = q.list().iterator();
699 
700                 if (itr.hasNext()) {
701                     count = itr.next();
702                 }
703 
704                 if (count == null) {
705                     count = new Long(0);
706                 }
707 
708                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
709                     finderClassName, finderMethodName, finderParams,
710                     finderArgs, count);
711 
712                 return count.intValue();
713             }
714             catch (Exception e) {
715                 throw processException(e);
716             }
717             finally {
718                 closeSession(session);
719             }
720         }
721         else {
722             return ((Long)result).intValue();
723         }
724     }
725 
726     public int countAll() throws SystemException {
727         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
728         String finderClassName = ListType.class.getName();
729         String finderMethodName = "countAll";
730         String[] finderParams = new String[] {  };
731         Object[] finderArgs = new Object[] {  };
732 
733         Object result = null;
734 
735         if (finderClassNameCacheEnabled) {
736             result = FinderCacheUtil.getResult(finderClassName,
737                     finderMethodName, finderParams, finderArgs, this);
738         }
739 
740         if (result == null) {
741             Session session = null;
742 
743             try {
744                 session = openSession();
745 
746                 Query q = session.createQuery(
747                         "SELECT COUNT(*) FROM com.liferay.portal.model.ListType");
748 
749                 Long count = null;
750 
751                 Iterator<Long> itr = q.list().iterator();
752 
753                 if (itr.hasNext()) {
754                     count = itr.next();
755                 }
756 
757                 if (count == null) {
758                     count = new Long(0);
759                 }
760 
761                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
762                     finderClassName, finderMethodName, finderParams,
763                     finderArgs, count);
764 
765                 return count.intValue();
766             }
767             catch (Exception e) {
768                 throw processException(e);
769             }
770             finally {
771                 closeSession(session);
772             }
773         }
774         else {
775             return ((Long)result).intValue();
776         }
777     }
778 
779     public void registerListener(ModelListener listener) {
780         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
781 
782         listeners.add(listener);
783 
784         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
785     }
786 
787     public void unregisterListener(ModelListener listener) {
788         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
789 
790         listeners.remove(listener);
791 
792         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
793     }
794 
795     public void afterPropertiesSet() {
796         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
797                     com.liferay.portal.util.PropsUtil.get(
798                         "value.object.listener.com.liferay.portal.model.ListType")));
799 
800         if (listenerClassNames.length > 0) {
801             try {
802                 List<ModelListener> listeners = new ArrayList<ModelListener>();
803 
804                 for (String listenerClassName : listenerClassNames) {
805                     listeners.add((ModelListener)Class.forName(
806                             listenerClassName).newInstance());
807                 }
808 
809                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
810             }
811             catch (Exception e) {
812                 _log.error(e);
813             }
814         }
815     }
816 
817     private static Log _log = LogFactory.getLog(ListTypePersistenceImpl.class);
818     private ModelListener[] _listeners = new ModelListener[0];
819 }