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.NoSuchServiceComponentException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.model.ModelListener;
35  import com.liferay.portal.model.ServiceComponent;
36  import com.liferay.portal.model.impl.ServiceComponentImpl;
37  import com.liferay.portal.model.impl.ServiceComponentModelImpl;
38  import com.liferay.portal.spring.hibernate.FinderCache;
39  import com.liferay.portal.spring.hibernate.HibernateUtil;
40  import com.liferay.portal.util.PropsUtil;
41  
42  import com.liferay.util.dao.hibernate.QueryUtil;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.hibernate.Query;
48  import org.hibernate.Session;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  /**
56   * <a href="ServiceComponentPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class ServiceComponentPersistenceImpl extends BasePersistence
62      implements ServiceComponentPersistence {
63      public ServiceComponent create(long serviceComponentId) {
64          ServiceComponent serviceComponent = new ServiceComponentImpl();
65  
66          serviceComponent.setNew(true);
67          serviceComponent.setPrimaryKey(serviceComponentId);
68  
69          return serviceComponent;
70      }
71  
72      public ServiceComponent remove(long serviceComponentId)
73          throws NoSuchServiceComponentException, SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              ServiceComponent serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
80                      new Long(serviceComponentId));
81  
82              if (serviceComponent == null) {
83                  if (_log.isWarnEnabled()) {
84                      _log.warn(
85                          "No ServiceComponent exists with the primary key " +
86                          serviceComponentId);
87                  }
88  
89                  throw new NoSuchServiceComponentException(
90                      "No ServiceComponent exists with the primary key " +
91                      serviceComponentId);
92              }
93  
94              return remove(serviceComponent);
95          }
96          catch (NoSuchServiceComponentException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw HibernateUtil.processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public ServiceComponent remove(ServiceComponent serviceComponent)
108         throws SystemException {
109         if (_listeners != null) {
110             for (ModelListener listener : _listeners) {
111                 listener.onBeforeRemove(serviceComponent);
112             }
113         }
114 
115         serviceComponent = removeImpl(serviceComponent);
116 
117         if (_listeners != null) {
118             for (ModelListener listener : _listeners) {
119                 listener.onAfterRemove(serviceComponent);
120             }
121         }
122 
123         return serviceComponent;
124     }
125 
126     protected ServiceComponent removeImpl(ServiceComponent serviceComponent)
127         throws SystemException {
128         Session session = null;
129 
130         try {
131             session = openSession();
132 
133             session.delete(serviceComponent);
134 
135             session.flush();
136 
137             return serviceComponent;
138         }
139         catch (Exception e) {
140             throw HibernateUtil.processException(e);
141         }
142         finally {
143             closeSession(session);
144 
145             FinderCache.clearCache(ServiceComponent.class.getName());
146         }
147     }
148 
149     /**
150      * @deprecated Use <code>update(ServiceComponent serviceComponent, boolean merge)</code>.
151      */
152     public ServiceComponent update(ServiceComponent serviceComponent)
153         throws SystemException {
154         if (_log.isWarnEnabled()) {
155             _log.warn(
156                 "Using the deprecated update(ServiceComponent serviceComponent) method. Use update(ServiceComponent serviceComponent, boolean merge) instead.");
157         }
158 
159         return update(serviceComponent, false);
160     }
161 
162     /**
163      * Add, update, or merge, the entity. This method also calls the model
164      * listeners to trigger the proper events associated with adding, deleting,
165      * or updating an entity.
166      *
167      * @param        serviceComponent the entity to add, update, or merge
168      * @param        merge boolean value for whether to merge the entity. The
169      *                default value is false. Setting merge to true is more
170      *                expensive and should only be true when serviceComponent is
171      *                transient. See LEP-5473 for a detailed discussion of this
172      *                method.
173      * @return        true if the portlet can be displayed via Ajax
174      */
175     public ServiceComponent update(ServiceComponent serviceComponent,
176         boolean merge) throws SystemException {
177         boolean isNew = serviceComponent.isNew();
178 
179         if (_listeners != null) {
180             for (ModelListener listener : _listeners) {
181                 if (isNew) {
182                     listener.onBeforeCreate(serviceComponent);
183                 }
184                 else {
185                     listener.onBeforeUpdate(serviceComponent);
186                 }
187             }
188         }
189 
190         serviceComponent = updateImpl(serviceComponent, merge);
191 
192         if (_listeners != null) {
193             for (ModelListener listener : _listeners) {
194                 if (isNew) {
195                     listener.onAfterCreate(serviceComponent);
196                 }
197                 else {
198                     listener.onAfterUpdate(serviceComponent);
199                 }
200             }
201         }
202 
203         return serviceComponent;
204     }
205 
206     public ServiceComponent updateImpl(
207         com.liferay.portal.model.ServiceComponent serviceComponent,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             if (merge) {
215                 session.merge(serviceComponent);
216             }
217             else {
218                 if (serviceComponent.isNew()) {
219                     session.save(serviceComponent);
220                 }
221             }
222 
223             session.flush();
224 
225             serviceComponent.setNew(false);
226 
227             return serviceComponent;
228         }
229         catch (Exception e) {
230             throw HibernateUtil.processException(e);
231         }
232         finally {
233             closeSession(session);
234 
235             FinderCache.clearCache(ServiceComponent.class.getName());
236         }
237     }
238 
239     public ServiceComponent findByPrimaryKey(long serviceComponentId)
240         throws NoSuchServiceComponentException, SystemException {
241         ServiceComponent serviceComponent = fetchByPrimaryKey(serviceComponentId);
242 
243         if (serviceComponent == null) {
244             if (_log.isWarnEnabled()) {
245                 _log.warn("No ServiceComponent exists with the primary key " +
246                     serviceComponentId);
247             }
248 
249             throw new NoSuchServiceComponentException(
250                 "No ServiceComponent exists with the primary key " +
251                 serviceComponentId);
252         }
253 
254         return serviceComponent;
255     }
256 
257     public ServiceComponent fetchByPrimaryKey(long serviceComponentId)
258         throws SystemException {
259         Session session = null;
260 
261         try {
262             session = openSession();
263 
264             return (ServiceComponent)session.get(ServiceComponentImpl.class,
265                 new Long(serviceComponentId));
266         }
267         catch (Exception e) {
268             throw HibernateUtil.processException(e);
269         }
270         finally {
271             closeSession(session);
272         }
273     }
274 
275     public List<ServiceComponent> findByBuildNamespace(String buildNamespace)
276         throws SystemException {
277         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
278         String finderClassName = ServiceComponent.class.getName();
279         String finderMethodName = "findByBuildNamespace";
280         String[] finderParams = new String[] { String.class.getName() };
281         Object[] finderArgs = new Object[] { buildNamespace };
282 
283         Object result = null;
284 
285         if (finderClassNameCacheEnabled) {
286             result = FinderCache.getResult(finderClassName, finderMethodName,
287                     finderParams, finderArgs, getSessionFactory());
288         }
289 
290         if (result == null) {
291             Session session = null;
292 
293             try {
294                 session = openSession();
295 
296                 StringMaker query = new StringMaker();
297 
298                 query.append(
299                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
300 
301                 if (buildNamespace == null) {
302                     query.append("buildNamespace IS NULL");
303                 }
304                 else {
305                     query.append("buildNamespace = ?");
306                 }
307 
308                 query.append(" ");
309 
310                 query.append("ORDER BY ");
311 
312                 query.append("buildNamespace DESC, ");
313                 query.append("buildNumber DESC");
314 
315                 Query q = session.createQuery(query.toString());
316 
317                 int queryPos = 0;
318 
319                 if (buildNamespace != null) {
320                     q.setString(queryPos++, buildNamespace);
321                 }
322 
323                 List<ServiceComponent> list = q.list();
324 
325                 FinderCache.putResult(finderClassNameCacheEnabled,
326                     finderClassName, finderMethodName, finderParams,
327                     finderArgs, list);
328 
329                 return list;
330             }
331             catch (Exception e) {
332                 throw HibernateUtil.processException(e);
333             }
334             finally {
335                 closeSession(session);
336             }
337         }
338         else {
339             return (List<ServiceComponent>)result;
340         }
341     }
342 
343     public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
344         int begin, int end) throws SystemException {
345         return findByBuildNamespace(buildNamespace, begin, end, null);
346     }
347 
348     public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
349         int begin, int end, OrderByComparator obc) throws SystemException {
350         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
351         String finderClassName = ServiceComponent.class.getName();
352         String finderMethodName = "findByBuildNamespace";
353         String[] finderParams = new String[] {
354                 String.class.getName(),
355                 
356                 "java.lang.Integer", "java.lang.Integer",
357                 "com.liferay.portal.kernel.util.OrderByComparator"
358             };
359         Object[] finderArgs = new Object[] {
360                 buildNamespace,
361                 
362                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
363             };
364 
365         Object result = null;
366 
367         if (finderClassNameCacheEnabled) {
368             result = FinderCache.getResult(finderClassName, finderMethodName,
369                     finderParams, finderArgs, getSessionFactory());
370         }
371 
372         if (result == null) {
373             Session session = null;
374 
375             try {
376                 session = openSession();
377 
378                 StringMaker query = new StringMaker();
379 
380                 query.append(
381                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
382 
383                 if (buildNamespace == null) {
384                     query.append("buildNamespace IS NULL");
385                 }
386                 else {
387                     query.append("buildNamespace = ?");
388                 }
389 
390                 query.append(" ");
391 
392                 if (obc != null) {
393                     query.append("ORDER BY ");
394                     query.append(obc.getOrderBy());
395                 }
396 
397                 else {
398                     query.append("ORDER BY ");
399 
400                     query.append("buildNamespace DESC, ");
401                     query.append("buildNumber DESC");
402                 }
403 
404                 Query q = session.createQuery(query.toString());
405 
406                 int queryPos = 0;
407 
408                 if (buildNamespace != null) {
409                     q.setString(queryPos++, buildNamespace);
410                 }
411 
412                 List<ServiceComponent> list = (List<ServiceComponent>)QueryUtil.list(q,
413                         getDialect(), begin, end);
414 
415                 FinderCache.putResult(finderClassNameCacheEnabled,
416                     finderClassName, finderMethodName, finderParams,
417                     finderArgs, list);
418 
419                 return list;
420             }
421             catch (Exception e) {
422                 throw HibernateUtil.processException(e);
423             }
424             finally {
425                 closeSession(session);
426             }
427         }
428         else {
429             return (List<ServiceComponent>)result;
430         }
431     }
432 
433     public ServiceComponent findByBuildNamespace_First(String buildNamespace,
434         OrderByComparator obc)
435         throws NoSuchServiceComponentException, SystemException {
436         List<ServiceComponent> list = findByBuildNamespace(buildNamespace, 0,
437                 1, obc);
438 
439         if (list.size() == 0) {
440             StringMaker msg = new StringMaker();
441 
442             msg.append("No ServiceComponent exists with the key {");
443 
444             msg.append("buildNamespace=" + buildNamespace);
445 
446             msg.append(StringPool.CLOSE_CURLY_BRACE);
447 
448             throw new NoSuchServiceComponentException(msg.toString());
449         }
450         else {
451             return list.get(0);
452         }
453     }
454 
455     public ServiceComponent findByBuildNamespace_Last(String buildNamespace,
456         OrderByComparator obc)
457         throws NoSuchServiceComponentException, SystemException {
458         int count = countByBuildNamespace(buildNamespace);
459 
460         List<ServiceComponent> list = findByBuildNamespace(buildNamespace,
461                 count - 1, count, obc);
462 
463         if (list.size() == 0) {
464             StringMaker msg = new StringMaker();
465 
466             msg.append("No ServiceComponent exists with the key {");
467 
468             msg.append("buildNamespace=" + buildNamespace);
469 
470             msg.append(StringPool.CLOSE_CURLY_BRACE);
471 
472             throw new NoSuchServiceComponentException(msg.toString());
473         }
474         else {
475             return list.get(0);
476         }
477     }
478 
479     public ServiceComponent[] findByBuildNamespace_PrevAndNext(
480         long serviceComponentId, String buildNamespace, OrderByComparator obc)
481         throws NoSuchServiceComponentException, SystemException {
482         ServiceComponent serviceComponent = findByPrimaryKey(serviceComponentId);
483 
484         int count = countByBuildNamespace(buildNamespace);
485 
486         Session session = null;
487 
488         try {
489             session = openSession();
490 
491             StringMaker query = new StringMaker();
492 
493             query.append(
494                 "FROM com.liferay.portal.model.ServiceComponent WHERE ");
495 
496             if (buildNamespace == null) {
497                 query.append("buildNamespace IS NULL");
498             }
499             else {
500                 query.append("buildNamespace = ?");
501             }
502 
503             query.append(" ");
504 
505             if (obc != null) {
506                 query.append("ORDER BY ");
507                 query.append(obc.getOrderBy());
508             }
509 
510             else {
511                 query.append("ORDER BY ");
512 
513                 query.append("buildNamespace DESC, ");
514                 query.append("buildNumber DESC");
515             }
516 
517             Query q = session.createQuery(query.toString());
518 
519             int queryPos = 0;
520 
521             if (buildNamespace != null) {
522                 q.setString(queryPos++, buildNamespace);
523             }
524 
525             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
526                     serviceComponent);
527 
528             ServiceComponent[] array = new ServiceComponentImpl[3];
529 
530             array[0] = (ServiceComponent)objArray[0];
531             array[1] = (ServiceComponent)objArray[1];
532             array[2] = (ServiceComponent)objArray[2];
533 
534             return array;
535         }
536         catch (Exception e) {
537             throw HibernateUtil.processException(e);
538         }
539         finally {
540             closeSession(session);
541         }
542     }
543 
544     public ServiceComponent findByBNS_BNU(String buildNamespace,
545         long buildNumber)
546         throws NoSuchServiceComponentException, SystemException {
547         ServiceComponent serviceComponent = fetchByBNS_BNU(buildNamespace,
548                 buildNumber);
549 
550         if (serviceComponent == null) {
551             StringMaker msg = new StringMaker();
552 
553             msg.append("No ServiceComponent exists with the key {");
554 
555             msg.append("buildNamespace=" + buildNamespace);
556 
557             msg.append(", ");
558             msg.append("buildNumber=" + buildNumber);
559 
560             msg.append(StringPool.CLOSE_CURLY_BRACE);
561 
562             if (_log.isWarnEnabled()) {
563                 _log.warn(msg.toString());
564             }
565 
566             throw new NoSuchServiceComponentException(msg.toString());
567         }
568 
569         return serviceComponent;
570     }
571 
572     public ServiceComponent fetchByBNS_BNU(String buildNamespace,
573         long buildNumber) throws SystemException {
574         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
575         String finderClassName = ServiceComponent.class.getName();
576         String finderMethodName = "fetchByBNS_BNU";
577         String[] finderParams = new String[] {
578                 String.class.getName(), Long.class.getName()
579             };
580         Object[] finderArgs = new Object[] { buildNamespace, new Long(buildNumber) };
581 
582         Object result = null;
583 
584         if (finderClassNameCacheEnabled) {
585             result = FinderCache.getResult(finderClassName, finderMethodName,
586                     finderParams, finderArgs, getSessionFactory());
587         }
588 
589         if (result == null) {
590             Session session = null;
591 
592             try {
593                 session = openSession();
594 
595                 StringMaker query = new StringMaker();
596 
597                 query.append(
598                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
599 
600                 if (buildNamespace == null) {
601                     query.append("buildNamespace IS NULL");
602                 }
603                 else {
604                     query.append("buildNamespace = ?");
605                 }
606 
607                 query.append(" AND ");
608 
609                 query.append("buildNumber = ?");
610 
611                 query.append(" ");
612 
613                 query.append("ORDER BY ");
614 
615                 query.append("buildNamespace DESC, ");
616                 query.append("buildNumber DESC");
617 
618                 Query q = session.createQuery(query.toString());
619 
620                 int queryPos = 0;
621 
622                 if (buildNamespace != null) {
623                     q.setString(queryPos++, buildNamespace);
624                 }
625 
626                 q.setLong(queryPos++, buildNumber);
627 
628                 List<ServiceComponent> list = q.list();
629 
630                 FinderCache.putResult(finderClassNameCacheEnabled,
631                     finderClassName, finderMethodName, finderParams,
632                     finderArgs, list);
633 
634                 if (list.size() == 0) {
635                     return null;
636                 }
637                 else {
638                     return list.get(0);
639                 }
640             }
641             catch (Exception e) {
642                 throw HibernateUtil.processException(e);
643             }
644             finally {
645                 closeSession(session);
646             }
647         }
648         else {
649             List<ServiceComponent> list = (List<ServiceComponent>)result;
650 
651             if (list.size() == 0) {
652                 return null;
653             }
654             else {
655                 return list.get(0);
656             }
657         }
658     }
659 
660     public List<ServiceComponent> findWithDynamicQuery(
661         DynamicQueryInitializer queryInitializer) throws SystemException {
662         Session session = null;
663 
664         try {
665             session = openSession();
666 
667             DynamicQuery query = queryInitializer.initialize(session);
668 
669             return query.list();
670         }
671         catch (Exception e) {
672             throw HibernateUtil.processException(e);
673         }
674         finally {
675             closeSession(session);
676         }
677     }
678 
679     public List<ServiceComponent> findWithDynamicQuery(
680         DynamicQueryInitializer queryInitializer, int begin, int end)
681         throws SystemException {
682         Session session = null;
683 
684         try {
685             session = openSession();
686 
687             DynamicQuery query = queryInitializer.initialize(session);
688 
689             query.setLimit(begin, end);
690 
691             return query.list();
692         }
693         catch (Exception e) {
694             throw HibernateUtil.processException(e);
695         }
696         finally {
697             closeSession(session);
698         }
699     }
700 
701     public List<ServiceComponent> findAll() throws SystemException {
702         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
703     }
704 
705     public List<ServiceComponent> findAll(int begin, int end)
706         throws SystemException {
707         return findAll(begin, end, null);
708     }
709 
710     public List<ServiceComponent> findAll(int begin, int end,
711         OrderByComparator obc) throws SystemException {
712         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
713         String finderClassName = ServiceComponent.class.getName();
714         String finderMethodName = "findAll";
715         String[] finderParams = new String[] {
716                 "java.lang.Integer", "java.lang.Integer",
717                 "com.liferay.portal.kernel.util.OrderByComparator"
718             };
719         Object[] finderArgs = new Object[] {
720                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
721             };
722 
723         Object result = null;
724 
725         if (finderClassNameCacheEnabled) {
726             result = FinderCache.getResult(finderClassName, finderMethodName,
727                     finderParams, finderArgs, getSessionFactory());
728         }
729 
730         if (result == null) {
731             Session session = null;
732 
733             try {
734                 session = openSession();
735 
736                 StringMaker query = new StringMaker();
737 
738                 query.append("FROM com.liferay.portal.model.ServiceComponent ");
739 
740                 if (obc != null) {
741                     query.append("ORDER BY ");
742                     query.append(obc.getOrderBy());
743                 }
744 
745                 else {
746                     query.append("ORDER BY ");
747 
748                     query.append("buildNamespace DESC, ");
749                     query.append("buildNumber DESC");
750                 }
751 
752                 Query q = session.createQuery(query.toString());
753 
754                 List<ServiceComponent> list = (List<ServiceComponent>)QueryUtil.list(q,
755                         getDialect(), begin, end);
756 
757                 if (obc == null) {
758                     Collections.sort(list);
759                 }
760 
761                 FinderCache.putResult(finderClassNameCacheEnabled,
762                     finderClassName, finderMethodName, finderParams,
763                     finderArgs, list);
764 
765                 return list;
766             }
767             catch (Exception e) {
768                 throw HibernateUtil.processException(e);
769             }
770             finally {
771                 closeSession(session);
772             }
773         }
774         else {
775             return (List<ServiceComponent>)result;
776         }
777     }
778 
779     public void removeByBuildNamespace(String buildNamespace)
780         throws SystemException {
781         for (ServiceComponent serviceComponent : findByBuildNamespace(
782                 buildNamespace)) {
783             remove(serviceComponent);
784         }
785     }
786 
787     public void removeByBNS_BNU(String buildNamespace, long buildNumber)
788         throws NoSuchServiceComponentException, SystemException {
789         ServiceComponent serviceComponent = findByBNS_BNU(buildNamespace,
790                 buildNumber);
791 
792         remove(serviceComponent);
793     }
794 
795     public void removeAll() throws SystemException {
796         for (ServiceComponent serviceComponent : findAll()) {
797             remove(serviceComponent);
798         }
799     }
800 
801     public int countByBuildNamespace(String buildNamespace)
802         throws SystemException {
803         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
804         String finderClassName = ServiceComponent.class.getName();
805         String finderMethodName = "countByBuildNamespace";
806         String[] finderParams = new String[] { String.class.getName() };
807         Object[] finderArgs = new Object[] { buildNamespace };
808 
809         Object result = null;
810 
811         if (finderClassNameCacheEnabled) {
812             result = FinderCache.getResult(finderClassName, finderMethodName,
813                     finderParams, finderArgs, getSessionFactory());
814         }
815 
816         if (result == null) {
817             Session session = null;
818 
819             try {
820                 session = openSession();
821 
822                 StringMaker query = new StringMaker();
823 
824                 query.append("SELECT COUNT(*) ");
825                 query.append(
826                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
827 
828                 if (buildNamespace == null) {
829                     query.append("buildNamespace IS NULL");
830                 }
831                 else {
832                     query.append("buildNamespace = ?");
833                 }
834 
835                 query.append(" ");
836 
837                 Query q = session.createQuery(query.toString());
838 
839                 int queryPos = 0;
840 
841                 if (buildNamespace != null) {
842                     q.setString(queryPos++, buildNamespace);
843                 }
844 
845                 Long count = null;
846 
847                 Iterator<Long> itr = q.list().iterator();
848 
849                 if (itr.hasNext()) {
850                     count = itr.next();
851                 }
852 
853                 if (count == null) {
854                     count = new Long(0);
855                 }
856 
857                 FinderCache.putResult(finderClassNameCacheEnabled,
858                     finderClassName, finderMethodName, finderParams,
859                     finderArgs, count);
860 
861                 return count.intValue();
862             }
863             catch (Exception e) {
864                 throw HibernateUtil.processException(e);
865             }
866             finally {
867                 closeSession(session);
868             }
869         }
870         else {
871             return ((Long)result).intValue();
872         }
873     }
874 
875     public int countByBNS_BNU(String buildNamespace, long buildNumber)
876         throws SystemException {
877         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
878         String finderClassName = ServiceComponent.class.getName();
879         String finderMethodName = "countByBNS_BNU";
880         String[] finderParams = new String[] {
881                 String.class.getName(), Long.class.getName()
882             };
883         Object[] finderArgs = new Object[] { buildNamespace, new Long(buildNumber) };
884 
885         Object result = null;
886 
887         if (finderClassNameCacheEnabled) {
888             result = FinderCache.getResult(finderClassName, finderMethodName,
889                     finderParams, finderArgs, getSessionFactory());
890         }
891 
892         if (result == null) {
893             Session session = null;
894 
895             try {
896                 session = openSession();
897 
898                 StringMaker query = new StringMaker();
899 
900                 query.append("SELECT COUNT(*) ");
901                 query.append(
902                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
903 
904                 if (buildNamespace == null) {
905                     query.append("buildNamespace IS NULL");
906                 }
907                 else {
908                     query.append("buildNamespace = ?");
909                 }
910 
911                 query.append(" AND ");
912 
913                 query.append("buildNumber = ?");
914 
915                 query.append(" ");
916 
917                 Query q = session.createQuery(query.toString());
918 
919                 int queryPos = 0;
920 
921                 if (buildNamespace != null) {
922                     q.setString(queryPos++, buildNamespace);
923                 }
924 
925                 q.setLong(queryPos++, buildNumber);
926 
927                 Long count = null;
928 
929                 Iterator<Long> itr = q.list().iterator();
930 
931                 if (itr.hasNext()) {
932                     count = itr.next();
933                 }
934 
935                 if (count == null) {
936                     count = new Long(0);
937                 }
938 
939                 FinderCache.putResult(finderClassNameCacheEnabled,
940                     finderClassName, finderMethodName, finderParams,
941                     finderArgs, count);
942 
943                 return count.intValue();
944             }
945             catch (Exception e) {
946                 throw HibernateUtil.processException(e);
947             }
948             finally {
949                 closeSession(session);
950             }
951         }
952         else {
953             return ((Long)result).intValue();
954         }
955     }
956 
957     public int countAll() throws SystemException {
958         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
959         String finderClassName = ServiceComponent.class.getName();
960         String finderMethodName = "countAll";
961         String[] finderParams = new String[] {  };
962         Object[] finderArgs = new Object[] {  };
963 
964         Object result = null;
965 
966         if (finderClassNameCacheEnabled) {
967             result = FinderCache.getResult(finderClassName, finderMethodName,
968                     finderParams, finderArgs, getSessionFactory());
969         }
970 
971         if (result == null) {
972             Session session = null;
973 
974             try {
975                 session = openSession();
976 
977                 Query q = session.createQuery(
978                         "SELECT COUNT(*) FROM com.liferay.portal.model.ServiceComponent");
979 
980                 Long count = null;
981 
982                 Iterator<Long> itr = q.list().iterator();
983 
984                 if (itr.hasNext()) {
985                     count = itr.next();
986                 }
987 
988                 if (count == null) {
989                     count = new Long(0);
990                 }
991 
992                 FinderCache.putResult(finderClassNameCacheEnabled,
993                     finderClassName, finderMethodName, finderParams,
994                     finderArgs, count);
995 
996                 return count.intValue();
997             }
998             catch (Exception e) {
999                 throw HibernateUtil.processException(e);
1000            }
1001            finally {
1002                closeSession(session);
1003            }
1004        }
1005        else {
1006            return ((Long)result).intValue();
1007        }
1008    }
1009
1010    protected void initDao() {
1011        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1012                    PropsUtil.get(
1013                        "value.object.listener.com.liferay.portal.model.ServiceComponent")));
1014
1015        if (listenerClassNames.length > 0) {
1016            try {
1017                List<ModelListener> listeners = new ArrayList<ModelListener>();
1018
1019                for (String listenerClassName : listenerClassNames) {
1020                    listeners.add((ModelListener)Class.forName(
1021                            listenerClassName).newInstance());
1022                }
1023
1024                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1025            }
1026            catch (Exception e) {
1027                _log.error(e);
1028            }
1029        }
1030    }
1031
1032    private static Log _log = LogFactory.getLog(ServiceComponentPersistenceImpl.class);
1033    private ModelListener[] _listeners;
1034}