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