1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchListTypeException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
25  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
26  import com.liferay.portal.kernel.dao.orm.Query;
27  import com.liferay.portal.kernel.dao.orm.QueryPos;
28  import com.liferay.portal.kernel.dao.orm.QueryUtil;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ListType;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.model.impl.ListTypeImpl;
39  import com.liferay.portal.model.impl.ListTypeModelImpl;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   * <a href="ListTypePersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ListTypePersistenceImpl extends BasePersistenceImpl
54      implements ListTypePersistence {
55      public ListType create(int listTypeId) {
56          ListType listType = new ListTypeImpl();
57  
58          listType.setNew(true);
59          listType.setPrimaryKey(listTypeId);
60  
61          return listType;
62      }
63  
64      public ListType remove(int listTypeId)
65          throws NoSuchListTypeException, SystemException {
66          Session session = null;
67  
68          try {
69              session = openSession();
70  
71              ListType listType = (ListType)session.get(ListTypeImpl.class,
72                      new Integer(listTypeId));
73  
74              if (listType == null) {
75                  if (_log.isWarnEnabled()) {
76                      _log.warn("No ListType exists with the primary key " +
77                          listTypeId);
78                  }
79  
80                  throw new NoSuchListTypeException(
81                      "No ListType exists with the primary key " + listTypeId);
82              }
83  
84              return remove(listType);
85          }
86          catch (NoSuchListTypeException nsee) {
87              throw nsee;
88          }
89          catch (Exception e) {
90              throw processException(e);
91          }
92          finally {
93              closeSession(session);
94          }
95      }
96  
97      public ListType remove(ListType listType) throws SystemException {
98          for (ModelListener listener : listeners) {
99              listener.onBeforeRemove(listType);
100         }
101 
102         listType = removeImpl(listType);
103 
104         for (ModelListener listener : listeners) {
105             listener.onAfterRemove(listType);
106         }
107 
108         return listType;
109     }
110 
111     protected ListType removeImpl(ListType listType) throws SystemException {
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             if (BatchSessionUtil.isEnabled()) {
118                 Object staleObject = session.get(ListTypeImpl.class,
119                         listType.getPrimaryKeyObj());
120 
121                 if (staleObject != null) {
122                     session.evict(staleObject);
123                 }
124             }
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         for (ModelListener listener : listeners) {
172             if (isNew) {
173                 listener.onBeforeCreate(listType);
174             }
175             else {
176                 listener.onBeforeUpdate(listType);
177             }
178         }
179 
180         listType = updateImpl(listType, merge);
181 
182         for (ModelListener listener : listeners) {
183             if (isNew) {
184                 listener.onAfterCreate(listType);
185             }
186             else {
187                 listener.onAfterUpdate(listType);
188             }
189         }
190 
191         return listType;
192     }
193 
194     public ListType updateImpl(com.liferay.portal.model.ListType listType,
195         boolean merge) throws SystemException {
196         Session session = null;
197 
198         try {
199             session = openSession();
200 
201             BatchSessionUtil.update(session, listType, merge);
202 
203             listType.setNew(false);
204 
205             return listType;
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212 
213             FinderCacheUtil.clearCache(ListType.class.getName());
214         }
215     }
216 
217     public ListType findByPrimaryKey(int listTypeId)
218         throws NoSuchListTypeException, SystemException {
219         ListType listType = fetchByPrimaryKey(listTypeId);
220 
221         if (listType == null) {
222             if (_log.isWarnEnabled()) {
223                 _log.warn("No ListType exists with the primary key " +
224                     listTypeId);
225             }
226 
227             throw new NoSuchListTypeException(
228                 "No ListType exists with the primary key " + listTypeId);
229         }
230 
231         return listType;
232     }
233 
234     public ListType fetchByPrimaryKey(int listTypeId) throws SystemException {
235         Session session = null;
236 
237         try {
238             session = openSession();
239 
240             return (ListType)session.get(ListTypeImpl.class,
241                 new Integer(listTypeId));
242         }
243         catch (Exception e) {
244             throw processException(e);
245         }
246         finally {
247             closeSession(session);
248         }
249     }
250 
251     public List<ListType> findByType(String type) throws SystemException {
252         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
253         String finderClassName = ListType.class.getName();
254         String finderMethodName = "findByType";
255         String[] finderParams = new String[] { String.class.getName() };
256         Object[] finderArgs = new Object[] { type };
257 
258         Object result = null;
259 
260         if (finderClassNameCacheEnabled) {
261             result = FinderCacheUtil.getResult(finderClassName,
262                     finderMethodName, finderParams, finderArgs, this);
263         }
264 
265         if (result == null) {
266             Session session = null;
267 
268             try {
269                 session = openSession();
270 
271                 StringBuilder query = new StringBuilder();
272 
273                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
274 
275                 if (type == null) {
276                     query.append("type_ IS NULL");
277                 }
278                 else {
279                     query.append("type_ = ?");
280                 }
281 
282                 query.append(" ");
283 
284                 query.append("ORDER BY ");
285 
286                 query.append("name ASC");
287 
288                 Query q = session.createQuery(query.toString());
289 
290                 QueryPos qPos = QueryPos.getInstance(q);
291 
292                 if (type != null) {
293                     qPos.add(type);
294                 }
295 
296                 List<ListType> list = q.list();
297 
298                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
299                     finderClassName, finderMethodName, finderParams,
300                     finderArgs, list);
301 
302                 return list;
303             }
304             catch (Exception e) {
305                 throw processException(e);
306             }
307             finally {
308                 closeSession(session);
309             }
310         }
311         else {
312             return (List<ListType>)result;
313         }
314     }
315 
316     public List<ListType> findByType(String type, int start, int end)
317         throws SystemException {
318         return findByType(type, start, end, null);
319     }
320 
321     public List<ListType> findByType(String type, int start, int end,
322         OrderByComparator obc) throws SystemException {
323         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
324         String finderClassName = ListType.class.getName();
325         String finderMethodName = "findByType";
326         String[] finderParams = new String[] {
327                 String.class.getName(),
328                 
329                 "java.lang.Integer", "java.lang.Integer",
330                 "com.liferay.portal.kernel.util.OrderByComparator"
331             };
332         Object[] finderArgs = new Object[] {
333                 type,
334                 
335                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
336             };
337 
338         Object result = null;
339 
340         if (finderClassNameCacheEnabled) {
341             result = FinderCacheUtil.getResult(finderClassName,
342                     finderMethodName, finderParams, finderArgs, this);
343         }
344 
345         if (result == null) {
346             Session session = null;
347 
348             try {
349                 session = openSession();
350 
351                 StringBuilder query = new StringBuilder();
352 
353                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
354 
355                 if (type == null) {
356                     query.append("type_ IS NULL");
357                 }
358                 else {
359                     query.append("type_ = ?");
360                 }
361 
362                 query.append(" ");
363 
364                 if (obc != null) {
365                     query.append("ORDER BY ");
366                     query.append(obc.getOrderBy());
367                 }
368 
369                 else {
370                     query.append("ORDER BY ");
371 
372                     query.append("name ASC");
373                 }
374 
375                 Query q = session.createQuery(query.toString());
376 
377                 QueryPos qPos = QueryPos.getInstance(q);
378 
379                 if (type != null) {
380                     qPos.add(type);
381                 }
382 
383                 List<ListType> list = (List<ListType>)QueryUtil.list(q,
384                         getDialect(), start, end);
385 
386                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
387                     finderClassName, finderMethodName, finderParams,
388                     finderArgs, list);
389 
390                 return list;
391             }
392             catch (Exception e) {
393                 throw processException(e);
394             }
395             finally {
396                 closeSession(session);
397             }
398         }
399         else {
400             return (List<ListType>)result;
401         }
402     }
403 
404     public ListType findByType_First(String type, OrderByComparator obc)
405         throws NoSuchListTypeException, SystemException {
406         List<ListType> list = findByType(type, 0, 1, obc);
407 
408         if (list.size() == 0) {
409             StringBuilder msg = new StringBuilder();
410 
411             msg.append("No ListType exists with the key {");
412 
413             msg.append("type=" + type);
414 
415             msg.append(StringPool.CLOSE_CURLY_BRACE);
416 
417             throw new NoSuchListTypeException(msg.toString());
418         }
419         else {
420             return list.get(0);
421         }
422     }
423 
424     public ListType findByType_Last(String type, OrderByComparator obc)
425         throws NoSuchListTypeException, SystemException {
426         int count = countByType(type);
427 
428         List<ListType> list = findByType(type, count - 1, count, obc);
429 
430         if (list.size() == 0) {
431             StringBuilder msg = new StringBuilder();
432 
433             msg.append("No ListType exists with the key {");
434 
435             msg.append("type=" + type);
436 
437             msg.append(StringPool.CLOSE_CURLY_BRACE);
438 
439             throw new NoSuchListTypeException(msg.toString());
440         }
441         else {
442             return list.get(0);
443         }
444     }
445 
446     public ListType[] findByType_PrevAndNext(int listTypeId, String type,
447         OrderByComparator obc) throws NoSuchListTypeException, SystemException {
448         ListType listType = findByPrimaryKey(listTypeId);
449 
450         int count = countByType(type);
451 
452         Session session = null;
453 
454         try {
455             session = openSession();
456 
457             StringBuilder query = new StringBuilder();
458 
459             query.append("FROM com.liferay.portal.model.ListType WHERE ");
460 
461             if (type == null) {
462                 query.append("type_ IS NULL");
463             }
464             else {
465                 query.append("type_ = ?");
466             }
467 
468             query.append(" ");
469 
470             if (obc != null) {
471                 query.append("ORDER BY ");
472                 query.append(obc.getOrderBy());
473             }
474 
475             else {
476                 query.append("ORDER BY ");
477 
478                 query.append("name ASC");
479             }
480 
481             Query q = session.createQuery(query.toString());
482 
483             QueryPos qPos = QueryPos.getInstance(q);
484 
485             if (type != null) {
486                 qPos.add(type);
487             }
488 
489             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, listType);
490 
491             ListType[] array = new ListTypeImpl[3];
492 
493             array[0] = (ListType)objArray[0];
494             array[1] = (ListType)objArray[1];
495             array[2] = (ListType)objArray[2];
496 
497             return array;
498         }
499         catch (Exception e) {
500             throw processException(e);
501         }
502         finally {
503             closeSession(session);
504         }
505     }
506 
507     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
508         throws SystemException {
509         Session session = null;
510 
511         try {
512             session = openSession();
513 
514             dynamicQuery.compile(session);
515 
516             return dynamicQuery.list();
517         }
518         catch (Exception e) {
519             throw processException(e);
520         }
521         finally {
522             closeSession(session);
523         }
524     }
525 
526     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
527         int start, int end) throws SystemException {
528         Session session = null;
529 
530         try {
531             session = openSession();
532 
533             dynamicQuery.setLimit(start, end);
534 
535             dynamicQuery.compile(session);
536 
537             return dynamicQuery.list();
538         }
539         catch (Exception e) {
540             throw processException(e);
541         }
542         finally {
543             closeSession(session);
544         }
545     }
546 
547     public List<ListType> findAll() throws SystemException {
548         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
549     }
550 
551     public List<ListType> findAll(int start, int end) throws SystemException {
552         return findAll(start, end, null);
553     }
554 
555     public List<ListType> findAll(int start, int end, OrderByComparator obc)
556         throws SystemException {
557         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
558         String finderClassName = ListType.class.getName();
559         String finderMethodName = "findAll";
560         String[] finderParams = new String[] {
561                 "java.lang.Integer", "java.lang.Integer",
562                 "com.liferay.portal.kernel.util.OrderByComparator"
563             };
564         Object[] finderArgs = new Object[] {
565                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
566             };
567 
568         Object result = null;
569 
570         if (finderClassNameCacheEnabled) {
571             result = FinderCacheUtil.getResult(finderClassName,
572                     finderMethodName, finderParams, finderArgs, this);
573         }
574 
575         if (result == null) {
576             Session session = null;
577 
578             try {
579                 session = openSession();
580 
581                 StringBuilder query = new StringBuilder();
582 
583                 query.append("FROM com.liferay.portal.model.ListType ");
584 
585                 if (obc != null) {
586                     query.append("ORDER BY ");
587                     query.append(obc.getOrderBy());
588                 }
589 
590                 else {
591                     query.append("ORDER BY ");
592 
593                     query.append("name ASC");
594                 }
595 
596                 Query q = session.createQuery(query.toString());
597 
598                 List<ListType> list = null;
599 
600                 if (obc == null) {
601                     list = (List<ListType>)QueryUtil.list(q, getDialect(),
602                             start, end, false);
603 
604                     Collections.sort(list);
605                 }
606                 else {
607                     list = (List<ListType>)QueryUtil.list(q, getDialect(),
608                             start, end);
609                 }
610 
611                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
612                     finderClassName, finderMethodName, finderParams,
613                     finderArgs, list);
614 
615                 return list;
616             }
617             catch (Exception e) {
618                 throw processException(e);
619             }
620             finally {
621                 closeSession(session);
622             }
623         }
624         else {
625             return (List<ListType>)result;
626         }
627     }
628 
629     public void removeByType(String type) throws SystemException {
630         for (ListType listType : findByType(type)) {
631             remove(listType);
632         }
633     }
634 
635     public void removeAll() throws SystemException {
636         for (ListType listType : findAll()) {
637             remove(listType);
638         }
639     }
640 
641     public int countByType(String type) throws SystemException {
642         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
643         String finderClassName = ListType.class.getName();
644         String finderMethodName = "countByType";
645         String[] finderParams = new String[] { String.class.getName() };
646         Object[] finderArgs = new Object[] { type };
647 
648         Object result = null;
649 
650         if (finderClassNameCacheEnabled) {
651             result = FinderCacheUtil.getResult(finderClassName,
652                     finderMethodName, finderParams, finderArgs, this);
653         }
654 
655         if (result == null) {
656             Session session = null;
657 
658             try {
659                 session = openSession();
660 
661                 StringBuilder query = new StringBuilder();
662 
663                 query.append("SELECT COUNT(*) ");
664                 query.append("FROM com.liferay.portal.model.ListType WHERE ");
665 
666                 if (type == null) {
667                     query.append("type_ IS NULL");
668                 }
669                 else {
670                     query.append("type_ = ?");
671                 }
672 
673                 query.append(" ");
674 
675                 Query q = session.createQuery(query.toString());
676 
677                 QueryPos qPos = QueryPos.getInstance(q);
678 
679                 if (type != null) {
680                     qPos.add(type);
681                 }
682 
683                 Long count = null;
684 
685                 Iterator<Long> itr = q.list().iterator();
686 
687                 if (itr.hasNext()) {
688                     count = itr.next();
689                 }
690 
691                 if (count == null) {
692                     count = new Long(0);
693                 }
694 
695                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
696                     finderClassName, finderMethodName, finderParams,
697                     finderArgs, count);
698 
699                 return count.intValue();
700             }
701             catch (Exception e) {
702                 throw processException(e);
703             }
704             finally {
705                 closeSession(session);
706             }
707         }
708         else {
709             return ((Long)result).intValue();
710         }
711     }
712 
713     public int countAll() throws SystemException {
714         boolean finderClassNameCacheEnabled = ListTypeModelImpl.CACHE_ENABLED;
715         String finderClassName = ListType.class.getName();
716         String finderMethodName = "countAll";
717         String[] finderParams = new String[] {  };
718         Object[] finderArgs = new Object[] {  };
719 
720         Object result = null;
721 
722         if (finderClassNameCacheEnabled) {
723             result = FinderCacheUtil.getResult(finderClassName,
724                     finderMethodName, finderParams, finderArgs, this);
725         }
726 
727         if (result == null) {
728             Session session = null;
729 
730             try {
731                 session = openSession();
732 
733                 Query q = session.createQuery(
734                         "SELECT COUNT(*) FROM com.liferay.portal.model.ListType");
735 
736                 Long count = null;
737 
738                 Iterator<Long> itr = q.list().iterator();
739 
740                 if (itr.hasNext()) {
741                     count = itr.next();
742                 }
743 
744                 if (count == null) {
745                     count = new Long(0);
746                 }
747 
748                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
749                     finderClassName, finderMethodName, finderParams,
750                     finderArgs, count);
751 
752                 return count.intValue();
753             }
754             catch (Exception e) {
755                 throw processException(e);
756             }
757             finally {
758                 closeSession(session);
759             }
760         }
761         else {
762             return ((Long)result).intValue();
763         }
764     }
765 
766     public void afterPropertiesSet() {
767         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
768                     com.liferay.portal.util.PropsUtil.get(
769                         "value.object.listener.com.liferay.portal.model.ListType")));
770 
771         if (listenerClassNames.length > 0) {
772             try {
773                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
774 
775                 for (String listenerClassName : listenerClassNames) {
776                     listenersList.add((ModelListener)Class.forName(
777                             listenerClassName).newInstance());
778                 }
779 
780                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
781             }
782             catch (Exception e) {
783                 _log.error(e);
784             }
785         }
786     }
787 
788     private static Log _log = LogFactoryUtil.getLog(ListTypePersistenceImpl.class);
789 }