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