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