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             if (BatchSessionUtil.isEnabled()) {
131                 Object staleObject = session.get(ServiceComponentImpl.class,
132                         serviceComponent.getPrimaryKeyObj());
133 
134                 if (staleObject != null) {
135                     session.evict(staleObject);
136                 }
137             }
138 
139             session.delete(serviceComponent);
140 
141             session.flush();
142 
143             return serviceComponent;
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             closeSession(session);
150 
151             FinderCacheUtil.clearCache(ServiceComponent.class.getName());
152         }
153     }
154 
155     /**
156      * @deprecated Use <code>update(ServiceComponent serviceComponent, boolean merge)</code>.
157      */
158     public ServiceComponent update(ServiceComponent serviceComponent)
159         throws SystemException {
160         if (_log.isWarnEnabled()) {
161             _log.warn(
162                 "Using the deprecated update(ServiceComponent serviceComponent) method. Use update(ServiceComponent serviceComponent, boolean merge) instead.");
163         }
164 
165         return update(serviceComponent, false);
166     }
167 
168     /**
169      * Add, update, or merge, the entity. This method also calls the model
170      * listeners to trigger the proper events associated with adding, deleting,
171      * or updating an entity.
172      *
173      * @param        serviceComponent the entity to add, update, or merge
174      * @param        merge boolean value for whether to merge the entity. The
175      *                default value is false. Setting merge to true is more
176      *                expensive and should only be true when serviceComponent is
177      *                transient. See LEP-5473 for a detailed discussion of this
178      *                method.
179      * @return        true if the portlet can be displayed via Ajax
180      */
181     public ServiceComponent update(ServiceComponent serviceComponent,
182         boolean merge) throws SystemException {
183         boolean isNew = serviceComponent.isNew();
184 
185         if (_listeners.length > 0) {
186             for (ModelListener listener : _listeners) {
187                 if (isNew) {
188                     listener.onBeforeCreate(serviceComponent);
189                 }
190                 else {
191                     listener.onBeforeUpdate(serviceComponent);
192                 }
193             }
194         }
195 
196         serviceComponent = updateImpl(serviceComponent, merge);
197 
198         if (_listeners.length > 0) {
199             for (ModelListener listener : _listeners) {
200                 if (isNew) {
201                     listener.onAfterCreate(serviceComponent);
202                 }
203                 else {
204                     listener.onAfterUpdate(serviceComponent);
205                 }
206             }
207         }
208 
209         return serviceComponent;
210     }
211 
212     public ServiceComponent updateImpl(
213         com.liferay.portal.model.ServiceComponent serviceComponent,
214         boolean merge) throws SystemException {
215         Session session = null;
216 
217         try {
218             session = openSession();
219 
220             BatchSessionUtil.update(session, serviceComponent, merge);
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 = null;
751 
752                 if (obc == null) {
753                     list = (List<ServiceComponent>)QueryUtil.list(q,
754                             getDialect(), start, end, false);
755 
756                     Collections.sort(list);
757                 }
758                 else {
759                     list = (List<ServiceComponent>)QueryUtil.list(q,
760                             getDialect(), start, end);
761                 }
762 
763                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
764                     finderClassName, finderMethodName, finderParams,
765                     finderArgs, list);
766 
767                 return list;
768             }
769             catch (Exception e) {
770                 throw processException(e);
771             }
772             finally {
773                 closeSession(session);
774             }
775         }
776         else {
777             return (List<ServiceComponent>)result;
778         }
779     }
780 
781     public void removeByBuildNamespace(String buildNamespace)
782         throws SystemException {
783         for (ServiceComponent serviceComponent : findByBuildNamespace(
784                 buildNamespace)) {
785             remove(serviceComponent);
786         }
787     }
788 
789     public void removeByBNS_BNU(String buildNamespace, long buildNumber)
790         throws NoSuchServiceComponentException, SystemException {
791         ServiceComponent serviceComponent = findByBNS_BNU(buildNamespace,
792                 buildNumber);
793 
794         remove(serviceComponent);
795     }
796 
797     public void removeAll() throws SystemException {
798         for (ServiceComponent serviceComponent : findAll()) {
799             remove(serviceComponent);
800         }
801     }
802 
803     public int countByBuildNamespace(String buildNamespace)
804         throws SystemException {
805         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
806         String finderClassName = ServiceComponent.class.getName();
807         String finderMethodName = "countByBuildNamespace";
808         String[] finderParams = new String[] { String.class.getName() };
809         Object[] finderArgs = new Object[] { buildNamespace };
810 
811         Object result = null;
812 
813         if (finderClassNameCacheEnabled) {
814             result = FinderCacheUtil.getResult(finderClassName,
815                     finderMethodName, finderParams, finderArgs, this);
816         }
817 
818         if (result == null) {
819             Session session = null;
820 
821             try {
822                 session = openSession();
823 
824                 StringBuilder query = new StringBuilder();
825 
826                 query.append("SELECT COUNT(*) ");
827                 query.append(
828                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
829 
830                 if (buildNamespace == null) {
831                     query.append("buildNamespace IS NULL");
832                 }
833                 else {
834                     query.append("buildNamespace = ?");
835                 }
836 
837                 query.append(" ");
838 
839                 Query q = session.createQuery(query.toString());
840 
841                 QueryPos qPos = QueryPos.getInstance(q);
842 
843                 if (buildNamespace != null) {
844                     qPos.add(buildNamespace);
845                 }
846 
847                 Long count = null;
848 
849                 Iterator<Long> itr = q.list().iterator();
850 
851                 if (itr.hasNext()) {
852                     count = itr.next();
853                 }
854 
855                 if (count == null) {
856                     count = new Long(0);
857                 }
858 
859                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
860                     finderClassName, finderMethodName, finderParams,
861                     finderArgs, count);
862 
863                 return count.intValue();
864             }
865             catch (Exception e) {
866                 throw processException(e);
867             }
868             finally {
869                 closeSession(session);
870             }
871         }
872         else {
873             return ((Long)result).intValue();
874         }
875     }
876 
877     public int countByBNS_BNU(String buildNamespace, long buildNumber)
878         throws SystemException {
879         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
880         String finderClassName = ServiceComponent.class.getName();
881         String finderMethodName = "countByBNS_BNU";
882         String[] finderParams = new String[] {
883                 String.class.getName(), Long.class.getName()
884             };
885         Object[] finderArgs = new Object[] { buildNamespace, new Long(buildNumber) };
886 
887         Object result = null;
888 
889         if (finderClassNameCacheEnabled) {
890             result = FinderCacheUtil.getResult(finderClassName,
891                     finderMethodName, finderParams, finderArgs, this);
892         }
893 
894         if (result == null) {
895             Session session = null;
896 
897             try {
898                 session = openSession();
899 
900                 StringBuilder query = new StringBuilder();
901 
902                 query.append("SELECT COUNT(*) ");
903                 query.append(
904                     "FROM com.liferay.portal.model.ServiceComponent WHERE ");
905 
906                 if (buildNamespace == null) {
907                     query.append("buildNamespace IS NULL");
908                 }
909                 else {
910                     query.append("buildNamespace = ?");
911                 }
912 
913                 query.append(" AND ");
914 
915                 query.append("buildNumber = ?");
916 
917                 query.append(" ");
918 
919                 Query q = session.createQuery(query.toString());
920 
921                 QueryPos qPos = QueryPos.getInstance(q);
922 
923                 if (buildNamespace != null) {
924                     qPos.add(buildNamespace);
925                 }
926 
927                 qPos.add(buildNumber);
928 
929                 Long count = null;
930 
931                 Iterator<Long> itr = q.list().iterator();
932 
933                 if (itr.hasNext()) {
934                     count = itr.next();
935                 }
936 
937                 if (count == null) {
938                     count = new Long(0);
939                 }
940 
941                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
942                     finderClassName, finderMethodName, finderParams,
943                     finderArgs, count);
944 
945                 return count.intValue();
946             }
947             catch (Exception e) {
948                 throw processException(e);
949             }
950             finally {
951                 closeSession(session);
952             }
953         }
954         else {
955             return ((Long)result).intValue();
956         }
957     }
958 
959     public int countAll() throws SystemException {
960         boolean finderClassNameCacheEnabled = ServiceComponentModelImpl.CACHE_ENABLED;
961         String finderClassName = ServiceComponent.class.getName();
962         String finderMethodName = "countAll";
963         String[] finderParams = new String[] {  };
964         Object[] finderArgs = new Object[] {  };
965 
966         Object result = null;
967 
968         if (finderClassNameCacheEnabled) {
969             result = FinderCacheUtil.getResult(finderClassName,
970                     finderMethodName, finderParams, finderArgs, this);
971         }
972 
973         if (result == null) {
974             Session session = null;
975 
976             try {
977                 session = openSession();
978 
979                 Query q = session.createQuery(
980                         "SELECT COUNT(*) FROM com.liferay.portal.model.ServiceComponent");
981 
982                 Long count = null;
983 
984                 Iterator<Long> itr = q.list().iterator();
985 
986                 if (itr.hasNext()) {
987                     count = itr.next();
988                 }
989 
990                 if (count == null) {
991                     count = new Long(0);
992                 }
993 
994                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
995                     finderClassName, finderMethodName, finderParams,
996                     finderArgs, count);
997 
998                 return count.intValue();
999             }
1000            catch (Exception e) {
1001                throw processException(e);
1002            }
1003            finally {
1004                closeSession(session);
1005            }
1006        }
1007        else {
1008            return ((Long)result).intValue();
1009        }
1010    }
1011
1012    public void registerListener(ModelListener listener) {
1013        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1014
1015        listeners.add(listener);
1016
1017        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1018    }
1019
1020    public void unregisterListener(ModelListener listener) {
1021        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1022
1023        listeners.remove(listener);
1024
1025        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1026    }
1027
1028    public void afterPropertiesSet() {
1029        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1030                    com.liferay.portal.util.PropsUtil.get(
1031                        "value.object.listener.com.liferay.portal.model.ServiceComponent")));
1032
1033        if (listenerClassNames.length > 0) {
1034            try {
1035                List<ModelListener> listeners = new ArrayList<ModelListener>();
1036
1037                for (String listenerClassName : listenerClassNames) {
1038                    listeners.add((ModelListener)Class.forName(
1039                            listenerClassName).newInstance());
1040                }
1041
1042                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1043            }
1044            catch (Exception e) {
1045                _log.error(e);
1046            }
1047        }
1048    }
1049
1050    private static Log _log = LogFactory.getLog(ServiceComponentPersistenceImpl.class);
1051    private ModelListener[] _listeners = new ModelListener[0];
1052}