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