001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.NoSuchServiceComponentException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.ServiceComponent;
040    import com.liferay.portal.model.impl.ServiceComponentImpl;
041    import com.liferay.portal.model.impl.ServiceComponentModelImpl;
042    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * @author    Brian Wing Shun Chan
052     * @see       ServiceComponentPersistence
053     * @see       ServiceComponentUtil
054     * @generated
055     */
056    public class ServiceComponentPersistenceImpl extends BasePersistenceImpl<ServiceComponent>
057            implements ServiceComponentPersistence {
058            public static final String FINDER_CLASS_NAME_ENTITY = ServiceComponentImpl.class.getName();
059            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
060                    ".List";
061            public static final FinderPath FINDER_PATH_FIND_BY_BUILDNAMESPACE = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
062                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
063                            FINDER_CLASS_NAME_LIST, "findByBuildNamespace",
064                            new String[] {
065                                    String.class.getName(),
066                                    
067                            "java.lang.Integer", "java.lang.Integer",
068                                    "com.liferay.portal.kernel.util.OrderByComparator"
069                            });
070            public static final FinderPath FINDER_PATH_COUNT_BY_BUILDNAMESPACE = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
071                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
072                            FINDER_CLASS_NAME_LIST, "countByBuildNamespace",
073                            new String[] { String.class.getName() });
074            public static final FinderPath FINDER_PATH_FETCH_BY_BNS_BNU = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
075                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
076                            FINDER_CLASS_NAME_ENTITY, "fetchByBNS_BNU",
077                            new String[] { String.class.getName(), Long.class.getName() });
078            public static final FinderPath FINDER_PATH_COUNT_BY_BNS_BNU = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
079                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
080                            FINDER_CLASS_NAME_LIST, "countByBNS_BNU",
081                            new String[] { String.class.getName(), Long.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
083                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
084                            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
086                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
087                            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
088    
089            public void cacheResult(ServiceComponent serviceComponent) {
090                    EntityCacheUtil.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
091                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey(),
092                            serviceComponent);
093    
094                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
095                            new Object[] {
096                                    serviceComponent.getBuildNamespace(),
097                                    new Long(serviceComponent.getBuildNumber())
098                            }, serviceComponent);
099            }
100    
101            public void cacheResult(List<ServiceComponent> serviceComponents) {
102                    for (ServiceComponent serviceComponent : serviceComponents) {
103                            if (EntityCacheUtil.getResult(
104                                                    ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
105                                                    ServiceComponentImpl.class,
106                                                    serviceComponent.getPrimaryKey(), this) == null) {
107                                    cacheResult(serviceComponent);
108                            }
109                    }
110            }
111    
112            public void clearCache() {
113                    CacheRegistryUtil.clear(ServiceComponentImpl.class.getName());
114                    EntityCacheUtil.clearCache(ServiceComponentImpl.class.getName());
115                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
116                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
117            }
118    
119            public void clearCache(ServiceComponent serviceComponent) {
120                    EntityCacheUtil.removeResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
121                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey());
122    
123                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU,
124                            new Object[] {
125                                    serviceComponent.getBuildNamespace(),
126                                    new Long(serviceComponent.getBuildNumber())
127                            });
128            }
129    
130            public ServiceComponent create(long serviceComponentId) {
131                    ServiceComponent serviceComponent = new ServiceComponentImpl();
132    
133                    serviceComponent.setNew(true);
134                    serviceComponent.setPrimaryKey(serviceComponentId);
135    
136                    return serviceComponent;
137            }
138    
139            public ServiceComponent remove(Serializable primaryKey)
140                    throws NoSuchModelException, SystemException {
141                    return remove(((Long)primaryKey).longValue());
142            }
143    
144            public ServiceComponent remove(long serviceComponentId)
145                    throws NoSuchServiceComponentException, SystemException {
146                    Session session = null;
147    
148                    try {
149                            session = openSession();
150    
151                            ServiceComponent serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
152                                            new Long(serviceComponentId));
153    
154                            if (serviceComponent == null) {
155                                    if (_log.isWarnEnabled()) {
156                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
157                                                    serviceComponentId);
158                                    }
159    
160                                    throw new NoSuchServiceComponentException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
161                                            serviceComponentId);
162                            }
163    
164                            return remove(serviceComponent);
165                    }
166                    catch (NoSuchServiceComponentException nsee) {
167                            throw nsee;
168                    }
169                    catch (Exception e) {
170                            throw processException(e);
171                    }
172                    finally {
173                            closeSession(session);
174                    }
175            }
176    
177            protected ServiceComponent removeImpl(ServiceComponent serviceComponent)
178                    throws SystemException {
179                    serviceComponent = toUnwrappedModel(serviceComponent);
180    
181                    Session session = null;
182    
183                    try {
184                            session = openSession();
185    
186                            if (serviceComponent.isCachedModel() ||
187                                            BatchSessionUtil.isEnabled()) {
188                                    Object staleObject = session.get(ServiceComponentImpl.class,
189                                                    serviceComponent.getPrimaryKeyObj());
190    
191                                    if (staleObject != null) {
192                                            session.evict(staleObject);
193                                    }
194                            }
195    
196                            session.delete(serviceComponent);
197    
198                            session.flush();
199                    }
200                    catch (Exception e) {
201                            throw processException(e);
202                    }
203                    finally {
204                            closeSession(session);
205                    }
206    
207                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
208    
209                    ServiceComponentModelImpl serviceComponentModelImpl = (ServiceComponentModelImpl)serviceComponent;
210    
211                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU,
212                            new Object[] {
213                                    serviceComponentModelImpl.getOriginalBuildNamespace(),
214                                    new Long(serviceComponentModelImpl.getOriginalBuildNumber())
215                            });
216    
217                    EntityCacheUtil.removeResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
218                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey());
219    
220                    return serviceComponent;
221            }
222    
223            public ServiceComponent updateImpl(
224                    com.liferay.portal.model.ServiceComponent serviceComponent,
225                    boolean merge) throws SystemException {
226                    serviceComponent = toUnwrappedModel(serviceComponent);
227    
228                    boolean isNew = serviceComponent.isNew();
229    
230                    ServiceComponentModelImpl serviceComponentModelImpl = (ServiceComponentModelImpl)serviceComponent;
231    
232                    Session session = null;
233    
234                    try {
235                            session = openSession();
236    
237                            BatchSessionUtil.update(session, serviceComponent, merge);
238    
239                            serviceComponent.setNew(false);
240                    }
241                    catch (Exception e) {
242                            throw processException(e);
243                    }
244                    finally {
245                            closeSession(session);
246                    }
247    
248                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
249    
250                    EntityCacheUtil.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
251                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey(),
252                            serviceComponent);
253    
254                    if (!isNew &&
255                                    (!Validator.equals(serviceComponent.getBuildNamespace(),
256                                            serviceComponentModelImpl.getOriginalBuildNamespace()) ||
257                                    (serviceComponent.getBuildNumber() != serviceComponentModelImpl.getOriginalBuildNumber()))) {
258                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU,
259                                    new Object[] {
260                                            serviceComponentModelImpl.getOriginalBuildNamespace(),
261                                            new Long(serviceComponentModelImpl.getOriginalBuildNumber())
262                                    });
263                    }
264    
265                    if (isNew ||
266                                    (!Validator.equals(serviceComponent.getBuildNamespace(),
267                                            serviceComponentModelImpl.getOriginalBuildNamespace()) ||
268                                    (serviceComponent.getBuildNumber() != serviceComponentModelImpl.getOriginalBuildNumber()))) {
269                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
270                                    new Object[] {
271                                            serviceComponent.getBuildNamespace(),
272                                            new Long(serviceComponent.getBuildNumber())
273                                    }, serviceComponent);
274                    }
275    
276                    return serviceComponent;
277            }
278    
279            protected ServiceComponent toUnwrappedModel(
280                    ServiceComponent serviceComponent) {
281                    if (serviceComponent instanceof ServiceComponentImpl) {
282                            return serviceComponent;
283                    }
284    
285                    ServiceComponentImpl serviceComponentImpl = new ServiceComponentImpl();
286    
287                    serviceComponentImpl.setNew(serviceComponent.isNew());
288                    serviceComponentImpl.setPrimaryKey(serviceComponent.getPrimaryKey());
289    
290                    serviceComponentImpl.setServiceComponentId(serviceComponent.getServiceComponentId());
291                    serviceComponentImpl.setBuildNamespace(serviceComponent.getBuildNamespace());
292                    serviceComponentImpl.setBuildNumber(serviceComponent.getBuildNumber());
293                    serviceComponentImpl.setBuildDate(serviceComponent.getBuildDate());
294                    serviceComponentImpl.setData(serviceComponent.getData());
295    
296                    return serviceComponentImpl;
297            }
298    
299            public ServiceComponent findByPrimaryKey(Serializable primaryKey)
300                    throws NoSuchModelException, SystemException {
301                    return findByPrimaryKey(((Long)primaryKey).longValue());
302            }
303    
304            public ServiceComponent findByPrimaryKey(long serviceComponentId)
305                    throws NoSuchServiceComponentException, SystemException {
306                    ServiceComponent serviceComponent = fetchByPrimaryKey(serviceComponentId);
307    
308                    if (serviceComponent == null) {
309                            if (_log.isWarnEnabled()) {
310                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
311                                            serviceComponentId);
312                            }
313    
314                            throw new NoSuchServiceComponentException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
315                                    serviceComponentId);
316                    }
317    
318                    return serviceComponent;
319            }
320    
321            public ServiceComponent fetchByPrimaryKey(Serializable primaryKey)
322                    throws SystemException {
323                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
324            }
325    
326            public ServiceComponent fetchByPrimaryKey(long serviceComponentId)
327                    throws SystemException {
328                    ServiceComponent serviceComponent = (ServiceComponent)EntityCacheUtil.getResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
329                                    ServiceComponentImpl.class, serviceComponentId, this);
330    
331                    if (serviceComponent == null) {
332                            Session session = null;
333    
334                            try {
335                                    session = openSession();
336    
337                                    serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
338                                                    new Long(serviceComponentId));
339                            }
340                            catch (Exception e) {
341                                    throw processException(e);
342                            }
343                            finally {
344                                    if (serviceComponent != null) {
345                                            cacheResult(serviceComponent);
346                                    }
347    
348                                    closeSession(session);
349                            }
350                    }
351    
352                    return serviceComponent;
353            }
354    
355            public List<ServiceComponent> findByBuildNamespace(String buildNamespace)
356                    throws SystemException {
357                    return findByBuildNamespace(buildNamespace, QueryUtil.ALL_POS,
358                            QueryUtil.ALL_POS, null);
359            }
360    
361            public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
362                    int start, int end) throws SystemException {
363                    return findByBuildNamespace(buildNamespace, start, end, null);
364            }
365    
366            public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
367                    int start, int end, OrderByComparator orderByComparator)
368                    throws SystemException {
369                    Object[] finderArgs = new Object[] {
370                                    buildNamespace,
371                                    
372                                    String.valueOf(start), String.valueOf(end),
373                                    String.valueOf(orderByComparator)
374                            };
375    
376                    List<ServiceComponent> list = (List<ServiceComponent>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_BUILDNAMESPACE,
377                                    finderArgs, this);
378    
379                    if (list == null) {
380                            Session session = null;
381    
382                            try {
383                                    session = openSession();
384    
385                                    StringBundler query = null;
386    
387                                    if (orderByComparator != null) {
388                                            query = new StringBundler(3 +
389                                                            (orderByComparator.getOrderByFields().length * 3));
390                                    }
391                                    else {
392                                            query = new StringBundler(3);
393                                    }
394    
395                                    query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
396    
397                                    if (buildNamespace == null) {
398                                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
399                                    }
400                                    else {
401                                            if (buildNamespace.equals(StringPool.BLANK)) {
402                                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
403                                            }
404                                            else {
405                                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
406                                            }
407                                    }
408    
409                                    if (orderByComparator != null) {
410                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
411                                                    orderByComparator);
412                                    }
413    
414                                    else {
415                                            query.append(ServiceComponentModelImpl.ORDER_BY_JPQL);
416                                    }
417    
418                                    String sql = query.toString();
419    
420                                    Query q = session.createQuery(sql);
421    
422                                    QueryPos qPos = QueryPos.getInstance(q);
423    
424                                    if (buildNamespace != null) {
425                                            qPos.add(buildNamespace);
426                                    }
427    
428                                    list = (List<ServiceComponent>)QueryUtil.list(q, getDialect(),
429                                                    start, end);
430                            }
431                            catch (Exception e) {
432                                    throw processException(e);
433                            }
434                            finally {
435                                    if (list == null) {
436                                            list = new ArrayList<ServiceComponent>();
437                                    }
438    
439                                    cacheResult(list);
440    
441                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_BUILDNAMESPACE,
442                                            finderArgs, list);
443    
444                                    closeSession(session);
445                            }
446                    }
447    
448                    return list;
449            }
450    
451            public ServiceComponent findByBuildNamespace_First(String buildNamespace,
452                    OrderByComparator orderByComparator)
453                    throws NoSuchServiceComponentException, SystemException {
454                    List<ServiceComponent> list = findByBuildNamespace(buildNamespace, 0,
455                                    1, orderByComparator);
456    
457                    if (list.isEmpty()) {
458                            StringBundler msg = new StringBundler(4);
459    
460                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
461    
462                            msg.append("buildNamespace=");
463                            msg.append(buildNamespace);
464    
465                            msg.append(StringPool.CLOSE_CURLY_BRACE);
466    
467                            throw new NoSuchServiceComponentException(msg.toString());
468                    }
469                    else {
470                            return list.get(0);
471                    }
472            }
473    
474            public ServiceComponent findByBuildNamespace_Last(String buildNamespace,
475                    OrderByComparator orderByComparator)
476                    throws NoSuchServiceComponentException, SystemException {
477                    int count = countByBuildNamespace(buildNamespace);
478    
479                    List<ServiceComponent> list = findByBuildNamespace(buildNamespace,
480                                    count - 1, count, orderByComparator);
481    
482                    if (list.isEmpty()) {
483                            StringBundler msg = new StringBundler(4);
484    
485                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
486    
487                            msg.append("buildNamespace=");
488                            msg.append(buildNamespace);
489    
490                            msg.append(StringPool.CLOSE_CURLY_BRACE);
491    
492                            throw new NoSuchServiceComponentException(msg.toString());
493                    }
494                    else {
495                            return list.get(0);
496                    }
497            }
498    
499            public ServiceComponent[] findByBuildNamespace_PrevAndNext(
500                    long serviceComponentId, String buildNamespace,
501                    OrderByComparator orderByComparator)
502                    throws NoSuchServiceComponentException, SystemException {
503                    ServiceComponent serviceComponent = findByPrimaryKey(serviceComponentId);
504    
505                    Session session = null;
506    
507                    try {
508                            session = openSession();
509    
510                            ServiceComponent[] array = new ServiceComponentImpl[3];
511    
512                            array[0] = getByBuildNamespace_PrevAndNext(session,
513                                            serviceComponent, buildNamespace, orderByComparator, true);
514    
515                            array[1] = serviceComponent;
516    
517                            array[2] = getByBuildNamespace_PrevAndNext(session,
518                                            serviceComponent, buildNamespace, orderByComparator, false);
519    
520                            return array;
521                    }
522                    catch (Exception e) {
523                            throw processException(e);
524                    }
525                    finally {
526                            closeSession(session);
527                    }
528            }
529    
530            protected ServiceComponent getByBuildNamespace_PrevAndNext(
531                    Session session, ServiceComponent serviceComponent,
532                    String buildNamespace, OrderByComparator orderByComparator,
533                    boolean previous) {
534                    StringBundler query = null;
535    
536                    if (orderByComparator != null) {
537                            query = new StringBundler(6 +
538                                            (orderByComparator.getOrderByFields().length * 6));
539                    }
540                    else {
541                            query = new StringBundler(3);
542                    }
543    
544                    query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
545    
546                    if (buildNamespace == null) {
547                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
548                    }
549                    else {
550                            if (buildNamespace.equals(StringPool.BLANK)) {
551                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
552                            }
553                            else {
554                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
555                            }
556                    }
557    
558                    if (orderByComparator != null) {
559                            String[] orderByFields = orderByComparator.getOrderByFields();
560    
561                            if (orderByFields.length > 0) {
562                                    query.append(WHERE_AND);
563                            }
564    
565                            for (int i = 0; i < orderByFields.length; i++) {
566                                    query.append(_ORDER_BY_ENTITY_ALIAS);
567                                    query.append(orderByFields[i]);
568    
569                                    if ((i + 1) < orderByFields.length) {
570                                            if (orderByComparator.isAscending() ^ previous) {
571                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
572                                            }
573                                            else {
574                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
575                                            }
576                                    }
577                                    else {
578                                            if (orderByComparator.isAscending() ^ previous) {
579                                                    query.append(WHERE_GREATER_THAN);
580                                            }
581                                            else {
582                                                    query.append(WHERE_LESSER_THAN);
583                                            }
584                                    }
585                            }
586    
587                            query.append(ORDER_BY_CLAUSE);
588    
589                            for (int i = 0; i < orderByFields.length; i++) {
590                                    query.append(_ORDER_BY_ENTITY_ALIAS);
591                                    query.append(orderByFields[i]);
592    
593                                    if ((i + 1) < orderByFields.length) {
594                                            if (orderByComparator.isAscending() ^ previous) {
595                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
596                                            }
597                                            else {
598                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
599                                            }
600                                    }
601                                    else {
602                                            if (orderByComparator.isAscending() ^ previous) {
603                                                    query.append(ORDER_BY_ASC);
604                                            }
605                                            else {
606                                                    query.append(ORDER_BY_DESC);
607                                            }
608                                    }
609                            }
610                    }
611    
612                    else {
613                            query.append(ServiceComponentModelImpl.ORDER_BY_JPQL);
614                    }
615    
616                    String sql = query.toString();
617    
618                    Query q = session.createQuery(sql);
619    
620                    q.setFirstResult(0);
621                    q.setMaxResults(2);
622    
623                    QueryPos qPos = QueryPos.getInstance(q);
624    
625                    if (buildNamespace != null) {
626                            qPos.add(buildNamespace);
627                    }
628    
629                    if (orderByComparator != null) {
630                            Object[] values = orderByComparator.getOrderByValues(serviceComponent);
631    
632                            for (Object value : values) {
633                                    qPos.add(value);
634                            }
635                    }
636    
637                    List<ServiceComponent> list = q.list();
638    
639                    if (list.size() == 2) {
640                            return list.get(1);
641                    }
642                    else {
643                            return null;
644                    }
645            }
646    
647            public ServiceComponent findByBNS_BNU(String buildNamespace,
648                    long buildNumber)
649                    throws NoSuchServiceComponentException, SystemException {
650                    ServiceComponent serviceComponent = fetchByBNS_BNU(buildNamespace,
651                                    buildNumber);
652    
653                    if (serviceComponent == null) {
654                            StringBundler msg = new StringBundler(6);
655    
656                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
657    
658                            msg.append("buildNamespace=");
659                            msg.append(buildNamespace);
660    
661                            msg.append(", buildNumber=");
662                            msg.append(buildNumber);
663    
664                            msg.append(StringPool.CLOSE_CURLY_BRACE);
665    
666                            if (_log.isWarnEnabled()) {
667                                    _log.warn(msg.toString());
668                            }
669    
670                            throw new NoSuchServiceComponentException(msg.toString());
671                    }
672    
673                    return serviceComponent;
674            }
675    
676            public ServiceComponent fetchByBNS_BNU(String buildNamespace,
677                    long buildNumber) throws SystemException {
678                    return fetchByBNS_BNU(buildNamespace, buildNumber, true);
679            }
680    
681            public ServiceComponent fetchByBNS_BNU(String buildNamespace,
682                    long buildNumber, boolean retrieveFromCache) throws SystemException {
683                    Object[] finderArgs = new Object[] { buildNamespace, buildNumber };
684    
685                    Object result = null;
686    
687                    if (retrieveFromCache) {
688                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_BNS_BNU,
689                                            finderArgs, this);
690                    }
691    
692                    if (result == null) {
693                            Session session = null;
694    
695                            try {
696                                    session = openSession();
697    
698                                    StringBundler query = new StringBundler(4);
699    
700                                    query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
701    
702                                    if (buildNamespace == null) {
703                                            query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1);
704                                    }
705                                    else {
706                                            if (buildNamespace.equals(StringPool.BLANK)) {
707                                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3);
708                                            }
709                                            else {
710                                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2);
711                                            }
712                                    }
713    
714                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2);
715    
716                                    query.append(ServiceComponentModelImpl.ORDER_BY_JPQL);
717    
718                                    String sql = query.toString();
719    
720                                    Query q = session.createQuery(sql);
721    
722                                    QueryPos qPos = QueryPos.getInstance(q);
723    
724                                    if (buildNamespace != null) {
725                                            qPos.add(buildNamespace);
726                                    }
727    
728                                    qPos.add(buildNumber);
729    
730                                    List<ServiceComponent> list = q.list();
731    
732                                    result = list;
733    
734                                    ServiceComponent serviceComponent = null;
735    
736                                    if (list.isEmpty()) {
737                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
738                                                    finderArgs, list);
739                                    }
740                                    else {
741                                            serviceComponent = list.get(0);
742    
743                                            cacheResult(serviceComponent);
744    
745                                            if ((serviceComponent.getBuildNamespace() == null) ||
746                                                            !serviceComponent.getBuildNamespace()
747                                                                                                     .equals(buildNamespace) ||
748                                                            (serviceComponent.getBuildNumber() != buildNumber)) {
749                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
750                                                            finderArgs, serviceComponent);
751                                            }
752                                    }
753    
754                                    return serviceComponent;
755                            }
756                            catch (Exception e) {
757                                    throw processException(e);
758                            }
759                            finally {
760                                    if (result == null) {
761                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
762                                                    finderArgs, new ArrayList<ServiceComponent>());
763                                    }
764    
765                                    closeSession(session);
766                            }
767                    }
768                    else {
769                            if (result instanceof List<?>) {
770                                    return null;
771                            }
772                            else {
773                                    return (ServiceComponent)result;
774                            }
775                    }
776            }
777    
778            public List<ServiceComponent> findAll() throws SystemException {
779                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
780            }
781    
782            public List<ServiceComponent> findAll(int start, int end)
783                    throws SystemException {
784                    return findAll(start, end, null);
785            }
786    
787            public List<ServiceComponent> findAll(int start, int end,
788                    OrderByComparator orderByComparator) throws SystemException {
789                    Object[] finderArgs = new Object[] {
790                                    String.valueOf(start), String.valueOf(end),
791                                    String.valueOf(orderByComparator)
792                            };
793    
794                    List<ServiceComponent> list = (List<ServiceComponent>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
795                                    finderArgs, this);
796    
797                    if (list == null) {
798                            Session session = null;
799    
800                            try {
801                                    session = openSession();
802    
803                                    StringBundler query = null;
804                                    String sql = null;
805    
806                                    if (orderByComparator != null) {
807                                            query = new StringBundler(2 +
808                                                            (orderByComparator.getOrderByFields().length * 3));
809    
810                                            query.append(_SQL_SELECT_SERVICECOMPONENT);
811    
812                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
813                                                    orderByComparator);
814    
815                                            sql = query.toString();
816                                    }
817                                    else {
818                                            sql = _SQL_SELECT_SERVICECOMPONENT.concat(ServiceComponentModelImpl.ORDER_BY_JPQL);
819                                    }
820    
821                                    Query q = session.createQuery(sql);
822    
823                                    if (orderByComparator == null) {
824                                            list = (List<ServiceComponent>)QueryUtil.list(q,
825                                                            getDialect(), start, end, false);
826    
827                                            Collections.sort(list);
828                                    }
829                                    else {
830                                            list = (List<ServiceComponent>)QueryUtil.list(q,
831                                                            getDialect(), start, end);
832                                    }
833                            }
834                            catch (Exception e) {
835                                    throw processException(e);
836                            }
837                            finally {
838                                    if (list == null) {
839                                            list = new ArrayList<ServiceComponent>();
840                                    }
841    
842                                    cacheResult(list);
843    
844                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
845    
846                                    closeSession(session);
847                            }
848                    }
849    
850                    return list;
851            }
852    
853            public void removeByBuildNamespace(String buildNamespace)
854                    throws SystemException {
855                    for (ServiceComponent serviceComponent : findByBuildNamespace(
856                                    buildNamespace)) {
857                            remove(serviceComponent);
858                    }
859            }
860    
861            public void removeByBNS_BNU(String buildNamespace, long buildNumber)
862                    throws NoSuchServiceComponentException, SystemException {
863                    ServiceComponent serviceComponent = findByBNS_BNU(buildNamespace,
864                                    buildNumber);
865    
866                    remove(serviceComponent);
867            }
868    
869            public void removeAll() throws SystemException {
870                    for (ServiceComponent serviceComponent : findAll()) {
871                            remove(serviceComponent);
872                    }
873            }
874    
875            public int countByBuildNamespace(String buildNamespace)
876                    throws SystemException {
877                    Object[] finderArgs = new Object[] { buildNamespace };
878    
879                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_BUILDNAMESPACE,
880                                    finderArgs, this);
881    
882                    if (count == null) {
883                            Session session = null;
884    
885                            try {
886                                    session = openSession();
887    
888                                    StringBundler query = new StringBundler(2);
889    
890                                    query.append(_SQL_COUNT_SERVICECOMPONENT_WHERE);
891    
892                                    if (buildNamespace == null) {
893                                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
894                                    }
895                                    else {
896                                            if (buildNamespace.equals(StringPool.BLANK)) {
897                                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
898                                            }
899                                            else {
900                                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
901                                            }
902                                    }
903    
904                                    String sql = query.toString();
905    
906                                    Query q = session.createQuery(sql);
907    
908                                    QueryPos qPos = QueryPos.getInstance(q);
909    
910                                    if (buildNamespace != null) {
911                                            qPos.add(buildNamespace);
912                                    }
913    
914                                    count = (Long)q.uniqueResult();
915                            }
916                            catch (Exception e) {
917                                    throw processException(e);
918                            }
919                            finally {
920                                    if (count == null) {
921                                            count = Long.valueOf(0);
922                                    }
923    
924                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_BUILDNAMESPACE,
925                                            finderArgs, count);
926    
927                                    closeSession(session);
928                            }
929                    }
930    
931                    return count.intValue();
932            }
933    
934            public int countByBNS_BNU(String buildNamespace, long buildNumber)
935                    throws SystemException {
936                    Object[] finderArgs = new Object[] { buildNamespace, buildNumber };
937    
938                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_BNS_BNU,
939                                    finderArgs, this);
940    
941                    if (count == null) {
942                            Session session = null;
943    
944                            try {
945                                    session = openSession();
946    
947                                    StringBundler query = new StringBundler(3);
948    
949                                    query.append(_SQL_COUNT_SERVICECOMPONENT_WHERE);
950    
951                                    if (buildNamespace == null) {
952                                            query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1);
953                                    }
954                                    else {
955                                            if (buildNamespace.equals(StringPool.BLANK)) {
956                                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3);
957                                            }
958                                            else {
959                                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2);
960                                            }
961                                    }
962    
963                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2);
964    
965                                    String sql = query.toString();
966    
967                                    Query q = session.createQuery(sql);
968    
969                                    QueryPos qPos = QueryPos.getInstance(q);
970    
971                                    if (buildNamespace != null) {
972                                            qPos.add(buildNamespace);
973                                    }
974    
975                                    qPos.add(buildNumber);
976    
977                                    count = (Long)q.uniqueResult();
978                            }
979                            catch (Exception e) {
980                                    throw processException(e);
981                            }
982                            finally {
983                                    if (count == null) {
984                                            count = Long.valueOf(0);
985                                    }
986    
987                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_BNS_BNU,
988                                            finderArgs, count);
989    
990                                    closeSession(session);
991                            }
992                    }
993    
994                    return count.intValue();
995            }
996    
997            public int countAll() throws SystemException {
998                    Object[] finderArgs = new Object[0];
999    
1000                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1001                                    finderArgs, this);
1002    
1003                    if (count == null) {
1004                            Session session = null;
1005    
1006                            try {
1007                                    session = openSession();
1008    
1009                                    Query q = session.createQuery(_SQL_COUNT_SERVICECOMPONENT);
1010    
1011                                    count = (Long)q.uniqueResult();
1012                            }
1013                            catch (Exception e) {
1014                                    throw processException(e);
1015                            }
1016                            finally {
1017                                    if (count == null) {
1018                                            count = Long.valueOf(0);
1019                                    }
1020    
1021                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1022                                            count);
1023    
1024                                    closeSession(session);
1025                            }
1026                    }
1027    
1028                    return count.intValue();
1029            }
1030    
1031            public void afterPropertiesSet() {
1032                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1033                                            com.liferay.portal.util.PropsUtil.get(
1034                                                    "value.object.listener.com.liferay.portal.model.ServiceComponent")));
1035    
1036                    if (listenerClassNames.length > 0) {
1037                            try {
1038                                    List<ModelListener<ServiceComponent>> listenersList = new ArrayList<ModelListener<ServiceComponent>>();
1039    
1040                                    for (String listenerClassName : listenerClassNames) {
1041                                            listenersList.add((ModelListener<ServiceComponent>)InstanceFactory.newInstance(
1042                                                            listenerClassName));
1043                                    }
1044    
1045                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1046                            }
1047                            catch (Exception e) {
1048                                    _log.error(e);
1049                            }
1050                    }
1051            }
1052    
1053            @BeanReference(type = AccountPersistence.class)
1054            protected AccountPersistence accountPersistence;
1055            @BeanReference(type = AddressPersistence.class)
1056            protected AddressPersistence addressPersistence;
1057            @BeanReference(type = BrowserTrackerPersistence.class)
1058            protected BrowserTrackerPersistence browserTrackerPersistence;
1059            @BeanReference(type = ClassNamePersistence.class)
1060            protected ClassNamePersistence classNamePersistence;
1061            @BeanReference(type = CompanyPersistence.class)
1062            protected CompanyPersistence companyPersistence;
1063            @BeanReference(type = ContactPersistence.class)
1064            protected ContactPersistence contactPersistence;
1065            @BeanReference(type = CountryPersistence.class)
1066            protected CountryPersistence countryPersistence;
1067            @BeanReference(type = EmailAddressPersistence.class)
1068            protected EmailAddressPersistence emailAddressPersistence;
1069            @BeanReference(type = GroupPersistence.class)
1070            protected GroupPersistence groupPersistence;
1071            @BeanReference(type = ImagePersistence.class)
1072            protected ImagePersistence imagePersistence;
1073            @BeanReference(type = LayoutPersistence.class)
1074            protected LayoutPersistence layoutPersistence;
1075            @BeanReference(type = LayoutPrototypePersistence.class)
1076            protected LayoutPrototypePersistence layoutPrototypePersistence;
1077            @BeanReference(type = LayoutSetPersistence.class)
1078            protected LayoutSetPersistence layoutSetPersistence;
1079            @BeanReference(type = LayoutSetPrototypePersistence.class)
1080            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1081            @BeanReference(type = ListTypePersistence.class)
1082            protected ListTypePersistence listTypePersistence;
1083            @BeanReference(type = LockPersistence.class)
1084            protected LockPersistence lockPersistence;
1085            @BeanReference(type = MembershipRequestPersistence.class)
1086            protected MembershipRequestPersistence membershipRequestPersistence;
1087            @BeanReference(type = OrganizationPersistence.class)
1088            protected OrganizationPersistence organizationPersistence;
1089            @BeanReference(type = OrgGroupPermissionPersistence.class)
1090            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1091            @BeanReference(type = OrgGroupRolePersistence.class)
1092            protected OrgGroupRolePersistence orgGroupRolePersistence;
1093            @BeanReference(type = OrgLaborPersistence.class)
1094            protected OrgLaborPersistence orgLaborPersistence;
1095            @BeanReference(type = PasswordPolicyPersistence.class)
1096            protected PasswordPolicyPersistence passwordPolicyPersistence;
1097            @BeanReference(type = PasswordPolicyRelPersistence.class)
1098            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1099            @BeanReference(type = PasswordTrackerPersistence.class)
1100            protected PasswordTrackerPersistence passwordTrackerPersistence;
1101            @BeanReference(type = PermissionPersistence.class)
1102            protected PermissionPersistence permissionPersistence;
1103            @BeanReference(type = PhonePersistence.class)
1104            protected PhonePersistence phonePersistence;
1105            @BeanReference(type = PluginSettingPersistence.class)
1106            protected PluginSettingPersistence pluginSettingPersistence;
1107            @BeanReference(type = PortletPersistence.class)
1108            protected PortletPersistence portletPersistence;
1109            @BeanReference(type = PortletItemPersistence.class)
1110            protected PortletItemPersistence portletItemPersistence;
1111            @BeanReference(type = PortletPreferencesPersistence.class)
1112            protected PortletPreferencesPersistence portletPreferencesPersistence;
1113            @BeanReference(type = RegionPersistence.class)
1114            protected RegionPersistence regionPersistence;
1115            @BeanReference(type = ReleasePersistence.class)
1116            protected ReleasePersistence releasePersistence;
1117            @BeanReference(type = ResourcePersistence.class)
1118            protected ResourcePersistence resourcePersistence;
1119            @BeanReference(type = ResourceActionPersistence.class)
1120            protected ResourceActionPersistence resourceActionPersistence;
1121            @BeanReference(type = ResourceCodePersistence.class)
1122            protected ResourceCodePersistence resourceCodePersistence;
1123            @BeanReference(type = ResourcePermissionPersistence.class)
1124            protected ResourcePermissionPersistence resourcePermissionPersistence;
1125            @BeanReference(type = RolePersistence.class)
1126            protected RolePersistence rolePersistence;
1127            @BeanReference(type = ServiceComponentPersistence.class)
1128            protected ServiceComponentPersistence serviceComponentPersistence;
1129            @BeanReference(type = ShardPersistence.class)
1130            protected ShardPersistence shardPersistence;
1131            @BeanReference(type = SubscriptionPersistence.class)
1132            protected SubscriptionPersistence subscriptionPersistence;
1133            @BeanReference(type = TicketPersistence.class)
1134            protected TicketPersistence ticketPersistence;
1135            @BeanReference(type = TeamPersistence.class)
1136            protected TeamPersistence teamPersistence;
1137            @BeanReference(type = UserPersistence.class)
1138            protected UserPersistence userPersistence;
1139            @BeanReference(type = UserGroupPersistence.class)
1140            protected UserGroupPersistence userGroupPersistence;
1141            @BeanReference(type = UserGroupGroupRolePersistence.class)
1142            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1143            @BeanReference(type = UserGroupRolePersistence.class)
1144            protected UserGroupRolePersistence userGroupRolePersistence;
1145            @BeanReference(type = UserIdMapperPersistence.class)
1146            protected UserIdMapperPersistence userIdMapperPersistence;
1147            @BeanReference(type = UserTrackerPersistence.class)
1148            protected UserTrackerPersistence userTrackerPersistence;
1149            @BeanReference(type = UserTrackerPathPersistence.class)
1150            protected UserTrackerPathPersistence userTrackerPathPersistence;
1151            @BeanReference(type = WebDAVPropsPersistence.class)
1152            protected WebDAVPropsPersistence webDAVPropsPersistence;
1153            @BeanReference(type = WebsitePersistence.class)
1154            protected WebsitePersistence websitePersistence;
1155            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1156            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1157            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1158            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1159            private static final String _SQL_SELECT_SERVICECOMPONENT = "SELECT serviceComponent FROM ServiceComponent serviceComponent";
1160            private static final String _SQL_SELECT_SERVICECOMPONENT_WHERE = "SELECT serviceComponent FROM ServiceComponent serviceComponent WHERE ";
1161            private static final String _SQL_COUNT_SERVICECOMPONENT = "SELECT COUNT(serviceComponent) FROM ServiceComponent serviceComponent";
1162            private static final String _SQL_COUNT_SERVICECOMPONENT_WHERE = "SELECT COUNT(serviceComponent) FROM ServiceComponent serviceComponent WHERE ";
1163            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1 = "serviceComponent.buildNamespace IS NULL";
1164            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2 = "serviceComponent.buildNamespace = ?";
1165            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3 = "(serviceComponent.buildNamespace IS NULL OR serviceComponent.buildNamespace = ?)";
1166            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1 = "serviceComponent.buildNamespace IS NULL AND ";
1167            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2 = "serviceComponent.buildNamespace = ? AND ";
1168            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3 = "(serviceComponent.buildNamespace IS NULL OR serviceComponent.buildNamespace = ?) AND ";
1169            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2 = "serviceComponent.buildNumber = ?";
1170            private static final String _ORDER_BY_ENTITY_ALIAS = "serviceComponent.";
1171            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ServiceComponent exists with the primary key ";
1172            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ServiceComponent exists with the key {";
1173            private static Log _log = LogFactoryUtil.getLog(ServiceComponentPersistenceImpl.class);
1174    }