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