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