001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.exception.NoSuchServiceComponentException;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.SetUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.MVCCModel;
038    import com.liferay.portal.model.ServiceComponent;
039    import com.liferay.portal.model.impl.ServiceComponentImpl;
040    import com.liferay.portal.model.impl.ServiceComponentModelImpl;
041    import com.liferay.portal.service.persistence.ServiceComponentPersistence;
042    
043    import java.io.Serializable;
044    
045    import java.util.Collections;
046    import java.util.HashMap;
047    import java.util.HashSet;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    import java.util.Set;
052    
053    /**
054     * The persistence implementation for the service component service.
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see ServiceComponentPersistence
062     * @see com.liferay.portal.service.persistence.ServiceComponentUtil
063     * @generated
064     */
065    @ProviderType
066    public class ServiceComponentPersistenceImpl extends BasePersistenceImpl<ServiceComponent>
067            implements ServiceComponentPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * Never modify or reference this class directly. Always use {@link ServiceComponentUtil} to access the service component persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
072             */
073            public static final String FINDER_CLASS_NAME_ENTITY = ServiceComponentImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List1";
076            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List2";
078            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
079                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
080                            ServiceComponentImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
081                            "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
083                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
084                            ServiceComponentImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
087                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED, Long.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
089            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_BUILDNAMESPACE =
090                    new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
091                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
092                            ServiceComponentImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
093                            "findByBuildNamespace",
094                            new String[] {
095                                    String.class.getName(),
096                                    
097                            Integer.class.getName(), Integer.class.getName(),
098                                    OrderByComparator.class.getName()
099                            });
100            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BUILDNAMESPACE =
101                    new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
102                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
103                            ServiceComponentImpl.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByBuildNamespace",
105                            new String[] { String.class.getName() },
106                            ServiceComponentModelImpl.BUILDNAMESPACE_COLUMN_BITMASK |
107                            ServiceComponentModelImpl.BUILDNUMBER_COLUMN_BITMASK);
108            public static final FinderPath FINDER_PATH_COUNT_BY_BUILDNAMESPACE = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
109                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED, Long.class,
110                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBuildNamespace",
111                            new String[] { String.class.getName() });
112    
113            /**
114             * Returns all the service components where buildNamespace = &#63;.
115             *
116             * @param buildNamespace the build namespace
117             * @return the matching service components
118             */
119            @Override
120            public List<ServiceComponent> findByBuildNamespace(String buildNamespace) {
121                    return findByBuildNamespace(buildNamespace, QueryUtil.ALL_POS,
122                            QueryUtil.ALL_POS, null);
123            }
124    
125            /**
126             * Returns a range of all the service components where buildNamespace = &#63;.
127             *
128             * <p>
129             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
130             * </p>
131             *
132             * @param buildNamespace the build namespace
133             * @param start the lower bound of the range of service components
134             * @param end the upper bound of the range of service components (not inclusive)
135             * @return the range of matching service components
136             */
137            @Override
138            public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
139                    int start, int end) {
140                    return findByBuildNamespace(buildNamespace, start, end, null);
141            }
142    
143            /**
144             * Returns an ordered range of all the service components where buildNamespace = &#63;.
145             *
146             * <p>
147             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
148             * </p>
149             *
150             * @param buildNamespace the build namespace
151             * @param start the lower bound of the range of service components
152             * @param end the upper bound of the range of service components (not inclusive)
153             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
154             * @return the ordered range of matching service components
155             */
156            @Override
157            public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
158                    int start, int end,
159                    OrderByComparator<ServiceComponent> orderByComparator) {
160                    return findByBuildNamespace(buildNamespace, start, end,
161                            orderByComparator, true);
162            }
163    
164            /**
165             * Returns an ordered range of all the service components where buildNamespace = &#63;.
166             *
167             * <p>
168             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
169             * </p>
170             *
171             * @param buildNamespace the build namespace
172             * @param start the lower bound of the range of service components
173             * @param end the upper bound of the range of service components (not inclusive)
174             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
175             * @param retrieveFromCache whether to retrieve from the finder cache
176             * @return the ordered range of matching service components
177             */
178            @Override
179            public List<ServiceComponent> findByBuildNamespace(String buildNamespace,
180                    int start, int end,
181                    OrderByComparator<ServiceComponent> orderByComparator,
182                    boolean retrieveFromCache) {
183                    boolean pagination = true;
184                    FinderPath finderPath = null;
185                    Object[] finderArgs = null;
186    
187                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
188                                    (orderByComparator == null)) {
189                            pagination = false;
190                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BUILDNAMESPACE;
191                            finderArgs = new Object[] { buildNamespace };
192                    }
193                    else {
194                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_BUILDNAMESPACE;
195                            finderArgs = new Object[] {
196                                            buildNamespace,
197                                            
198                                            start, end, orderByComparator
199                                    };
200                    }
201    
202                    List<ServiceComponent> list = null;
203    
204                    if (retrieveFromCache) {
205                            list = (List<ServiceComponent>)finderCache.getResult(finderPath,
206                                            finderArgs, this);
207    
208                            if ((list != null) && !list.isEmpty()) {
209                                    for (ServiceComponent serviceComponent : list) {
210                                            if (!Validator.equals(buildNamespace,
211                                                                    serviceComponent.getBuildNamespace())) {
212                                                    list = null;
213    
214                                                    break;
215                                            }
216                                    }
217                            }
218                    }
219    
220                    if (list == null) {
221                            StringBundler query = null;
222    
223                            if (orderByComparator != null) {
224                                    query = new StringBundler(3 +
225                                                    (orderByComparator.getOrderByFields().length * 2));
226                            }
227                            else {
228                                    query = new StringBundler(3);
229                            }
230    
231                            query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
232    
233                            boolean bindBuildNamespace = false;
234    
235                            if (buildNamespace == null) {
236                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
237                            }
238                            else if (buildNamespace.equals(StringPool.BLANK)) {
239                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
240                            }
241                            else {
242                                    bindBuildNamespace = true;
243    
244                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
245                            }
246    
247                            if (orderByComparator != null) {
248                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
249                                            orderByComparator);
250                            }
251                            else
252                             if (pagination) {
253                                    query.append(ServiceComponentModelImpl.ORDER_BY_JPQL);
254                            }
255    
256                            String sql = query.toString();
257    
258                            Session session = null;
259    
260                            try {
261                                    session = openSession();
262    
263                                    Query q = session.createQuery(sql);
264    
265                                    QueryPos qPos = QueryPos.getInstance(q);
266    
267                                    if (bindBuildNamespace) {
268                                            qPos.add(buildNamespace);
269                                    }
270    
271                                    if (!pagination) {
272                                            list = (List<ServiceComponent>)QueryUtil.list(q,
273                                                            getDialect(), start, end, false);
274    
275                                            Collections.sort(list);
276    
277                                            list = Collections.unmodifiableList(list);
278                                    }
279                                    else {
280                                            list = (List<ServiceComponent>)QueryUtil.list(q,
281                                                            getDialect(), start, end);
282                                    }
283    
284                                    cacheResult(list);
285    
286                                    finderCache.putResult(finderPath, finderArgs, list);
287                            }
288                            catch (Exception e) {
289                                    finderCache.removeResult(finderPath, finderArgs);
290    
291                                    throw processException(e);
292                            }
293                            finally {
294                                    closeSession(session);
295                            }
296                    }
297    
298                    return list;
299            }
300    
301            /**
302             * Returns the first service component in the ordered set where buildNamespace = &#63;.
303             *
304             * @param buildNamespace the build namespace
305             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
306             * @return the first matching service component
307             * @throws NoSuchServiceComponentException if a matching service component could not be found
308             */
309            @Override
310            public ServiceComponent findByBuildNamespace_First(String buildNamespace,
311                    OrderByComparator<ServiceComponent> orderByComparator)
312                    throws NoSuchServiceComponentException {
313                    ServiceComponent serviceComponent = fetchByBuildNamespace_First(buildNamespace,
314                                    orderByComparator);
315    
316                    if (serviceComponent != null) {
317                            return serviceComponent;
318                    }
319    
320                    StringBundler msg = new StringBundler(4);
321    
322                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
323    
324                    msg.append("buildNamespace=");
325                    msg.append(buildNamespace);
326    
327                    msg.append(StringPool.CLOSE_CURLY_BRACE);
328    
329                    throw new NoSuchServiceComponentException(msg.toString());
330            }
331    
332            /**
333             * Returns the first service component in the ordered set where buildNamespace = &#63;.
334             *
335             * @param buildNamespace the build namespace
336             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
337             * @return the first matching service component, or <code>null</code> if a matching service component could not be found
338             */
339            @Override
340            public ServiceComponent fetchByBuildNamespace_First(String buildNamespace,
341                    OrderByComparator<ServiceComponent> orderByComparator) {
342                    List<ServiceComponent> list = findByBuildNamespace(buildNamespace, 0,
343                                    1, orderByComparator);
344    
345                    if (!list.isEmpty()) {
346                            return list.get(0);
347                    }
348    
349                    return null;
350            }
351    
352            /**
353             * Returns the last service component in the ordered set where buildNamespace = &#63;.
354             *
355             * @param buildNamespace the build namespace
356             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
357             * @return the last matching service component
358             * @throws NoSuchServiceComponentException if a matching service component could not be found
359             */
360            @Override
361            public ServiceComponent findByBuildNamespace_Last(String buildNamespace,
362                    OrderByComparator<ServiceComponent> orderByComparator)
363                    throws NoSuchServiceComponentException {
364                    ServiceComponent serviceComponent = fetchByBuildNamespace_Last(buildNamespace,
365                                    orderByComparator);
366    
367                    if (serviceComponent != null) {
368                            return serviceComponent;
369                    }
370    
371                    StringBundler msg = new StringBundler(4);
372    
373                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
374    
375                    msg.append("buildNamespace=");
376                    msg.append(buildNamespace);
377    
378                    msg.append(StringPool.CLOSE_CURLY_BRACE);
379    
380                    throw new NoSuchServiceComponentException(msg.toString());
381            }
382    
383            /**
384             * Returns the last service component in the ordered set where buildNamespace = &#63;.
385             *
386             * @param buildNamespace the build namespace
387             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
388             * @return the last matching service component, or <code>null</code> if a matching service component could not be found
389             */
390            @Override
391            public ServiceComponent fetchByBuildNamespace_Last(String buildNamespace,
392                    OrderByComparator<ServiceComponent> orderByComparator) {
393                    int count = countByBuildNamespace(buildNamespace);
394    
395                    if (count == 0) {
396                            return null;
397                    }
398    
399                    List<ServiceComponent> list = findByBuildNamespace(buildNamespace,
400                                    count - 1, count, orderByComparator);
401    
402                    if (!list.isEmpty()) {
403                            return list.get(0);
404                    }
405    
406                    return null;
407            }
408    
409            /**
410             * Returns the service components before and after the current service component in the ordered set where buildNamespace = &#63;.
411             *
412             * @param serviceComponentId the primary key of the current service component
413             * @param buildNamespace the build namespace
414             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
415             * @return the previous, current, and next service component
416             * @throws NoSuchServiceComponentException if a service component with the primary key could not be found
417             */
418            @Override
419            public ServiceComponent[] findByBuildNamespace_PrevAndNext(
420                    long serviceComponentId, String buildNamespace,
421                    OrderByComparator<ServiceComponent> orderByComparator)
422                    throws NoSuchServiceComponentException {
423                    ServiceComponent serviceComponent = findByPrimaryKey(serviceComponentId);
424    
425                    Session session = null;
426    
427                    try {
428                            session = openSession();
429    
430                            ServiceComponent[] array = new ServiceComponentImpl[3];
431    
432                            array[0] = getByBuildNamespace_PrevAndNext(session,
433                                            serviceComponent, buildNamespace, orderByComparator, true);
434    
435                            array[1] = serviceComponent;
436    
437                            array[2] = getByBuildNamespace_PrevAndNext(session,
438                                            serviceComponent, buildNamespace, orderByComparator, false);
439    
440                            return array;
441                    }
442                    catch (Exception e) {
443                            throw processException(e);
444                    }
445                    finally {
446                            closeSession(session);
447                    }
448            }
449    
450            protected ServiceComponent getByBuildNamespace_PrevAndNext(
451                    Session session, ServiceComponent serviceComponent,
452                    String buildNamespace,
453                    OrderByComparator<ServiceComponent> orderByComparator, boolean previous) {
454                    StringBundler query = null;
455    
456                    if (orderByComparator != null) {
457                            query = new StringBundler(4 +
458                                            (orderByComparator.getOrderByConditionFields().length * 3) +
459                                            (orderByComparator.getOrderByFields().length * 3));
460                    }
461                    else {
462                            query = new StringBundler(3);
463                    }
464    
465                    query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
466    
467                    boolean bindBuildNamespace = false;
468    
469                    if (buildNamespace == null) {
470                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
471                    }
472                    else if (buildNamespace.equals(StringPool.BLANK)) {
473                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
474                    }
475                    else {
476                            bindBuildNamespace = true;
477    
478                            query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
479                    }
480    
481                    if (orderByComparator != null) {
482                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
483    
484                            if (orderByConditionFields.length > 0) {
485                                    query.append(WHERE_AND);
486                            }
487    
488                            for (int i = 0; i < orderByConditionFields.length; i++) {
489                                    query.append(_ORDER_BY_ENTITY_ALIAS);
490                                    query.append(orderByConditionFields[i]);
491    
492                                    if ((i + 1) < orderByConditionFields.length) {
493                                            if (orderByComparator.isAscending() ^ previous) {
494                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
495                                            }
496                                            else {
497                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
498                                            }
499                                    }
500                                    else {
501                                            if (orderByComparator.isAscending() ^ previous) {
502                                                    query.append(WHERE_GREATER_THAN);
503                                            }
504                                            else {
505                                                    query.append(WHERE_LESSER_THAN);
506                                            }
507                                    }
508                            }
509    
510                            query.append(ORDER_BY_CLAUSE);
511    
512                            String[] orderByFields = orderByComparator.getOrderByFields();
513    
514                            for (int i = 0; i < orderByFields.length; i++) {
515                                    query.append(_ORDER_BY_ENTITY_ALIAS);
516                                    query.append(orderByFields[i]);
517    
518                                    if ((i + 1) < orderByFields.length) {
519                                            if (orderByComparator.isAscending() ^ previous) {
520                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
521                                            }
522                                            else {
523                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
524                                            }
525                                    }
526                                    else {
527                                            if (orderByComparator.isAscending() ^ previous) {
528                                                    query.append(ORDER_BY_ASC);
529                                            }
530                                            else {
531                                                    query.append(ORDER_BY_DESC);
532                                            }
533                                    }
534                            }
535                    }
536                    else {
537                            query.append(ServiceComponentModelImpl.ORDER_BY_JPQL);
538                    }
539    
540                    String sql = query.toString();
541    
542                    Query q = session.createQuery(sql);
543    
544                    q.setFirstResult(0);
545                    q.setMaxResults(2);
546    
547                    QueryPos qPos = QueryPos.getInstance(q);
548    
549                    if (bindBuildNamespace) {
550                            qPos.add(buildNamespace);
551                    }
552    
553                    if (orderByComparator != null) {
554                            Object[] values = orderByComparator.getOrderByConditionValues(serviceComponent);
555    
556                            for (Object value : values) {
557                                    qPos.add(value);
558                            }
559                    }
560    
561                    List<ServiceComponent> list = q.list();
562    
563                    if (list.size() == 2) {
564                            return list.get(1);
565                    }
566                    else {
567                            return null;
568                    }
569            }
570    
571            /**
572             * Removes all the service components where buildNamespace = &#63; from the database.
573             *
574             * @param buildNamespace the build namespace
575             */
576            @Override
577            public void removeByBuildNamespace(String buildNamespace) {
578                    for (ServiceComponent serviceComponent : findByBuildNamespace(
579                                    buildNamespace, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
580                            remove(serviceComponent);
581                    }
582            }
583    
584            /**
585             * Returns the number of service components where buildNamespace = &#63;.
586             *
587             * @param buildNamespace the build namespace
588             * @return the number of matching service components
589             */
590            @Override
591            public int countByBuildNamespace(String buildNamespace) {
592                    FinderPath finderPath = FINDER_PATH_COUNT_BY_BUILDNAMESPACE;
593    
594                    Object[] finderArgs = new Object[] { buildNamespace };
595    
596                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
597    
598                    if (count == null) {
599                            StringBundler query = new StringBundler(2);
600    
601                            query.append(_SQL_COUNT_SERVICECOMPONENT_WHERE);
602    
603                            boolean bindBuildNamespace = false;
604    
605                            if (buildNamespace == null) {
606                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1);
607                            }
608                            else if (buildNamespace.equals(StringPool.BLANK)) {
609                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3);
610                            }
611                            else {
612                                    bindBuildNamespace = true;
613    
614                                    query.append(_FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2);
615                            }
616    
617                            String sql = query.toString();
618    
619                            Session session = null;
620    
621                            try {
622                                    session = openSession();
623    
624                                    Query q = session.createQuery(sql);
625    
626                                    QueryPos qPos = QueryPos.getInstance(q);
627    
628                                    if (bindBuildNamespace) {
629                                            qPos.add(buildNamespace);
630                                    }
631    
632                                    count = (Long)q.uniqueResult();
633    
634                                    finderCache.putResult(finderPath, finderArgs, count);
635                            }
636                            catch (Exception e) {
637                                    finderCache.removeResult(finderPath, finderArgs);
638    
639                                    throw processException(e);
640                            }
641                            finally {
642                                    closeSession(session);
643                            }
644                    }
645    
646                    return count.intValue();
647            }
648    
649            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_1 = "serviceComponent.buildNamespace IS NULL";
650            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_2 = "serviceComponent.buildNamespace = ?";
651            private static final String _FINDER_COLUMN_BUILDNAMESPACE_BUILDNAMESPACE_3 = "(serviceComponent.buildNamespace IS NULL OR serviceComponent.buildNamespace = '')";
652            public static final FinderPath FINDER_PATH_FETCH_BY_BNS_BNU = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
653                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED,
654                            ServiceComponentImpl.class, FINDER_CLASS_NAME_ENTITY,
655                            "fetchByBNS_BNU",
656                            new String[] { String.class.getName(), Long.class.getName() },
657                            ServiceComponentModelImpl.BUILDNAMESPACE_COLUMN_BITMASK |
658                            ServiceComponentModelImpl.BUILDNUMBER_COLUMN_BITMASK);
659            public static final FinderPath FINDER_PATH_COUNT_BY_BNS_BNU = new FinderPath(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
660                            ServiceComponentModelImpl.FINDER_CACHE_ENABLED, Long.class,
661                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBNS_BNU",
662                            new String[] { String.class.getName(), Long.class.getName() });
663    
664            /**
665             * Returns the service component where buildNamespace = &#63; and buildNumber = &#63; or throws a {@link NoSuchServiceComponentException} if it could not be found.
666             *
667             * @param buildNamespace the build namespace
668             * @param buildNumber the build number
669             * @return the matching service component
670             * @throws NoSuchServiceComponentException if a matching service component could not be found
671             */
672            @Override
673            public ServiceComponent findByBNS_BNU(String buildNamespace,
674                    long buildNumber) throws NoSuchServiceComponentException {
675                    ServiceComponent serviceComponent = fetchByBNS_BNU(buildNamespace,
676                                    buildNumber);
677    
678                    if (serviceComponent == null) {
679                            StringBundler msg = new StringBundler(6);
680    
681                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
682    
683                            msg.append("buildNamespace=");
684                            msg.append(buildNamespace);
685    
686                            msg.append(", buildNumber=");
687                            msg.append(buildNumber);
688    
689                            msg.append(StringPool.CLOSE_CURLY_BRACE);
690    
691                            if (_log.isWarnEnabled()) {
692                                    _log.warn(msg.toString());
693                            }
694    
695                            throw new NoSuchServiceComponentException(msg.toString());
696                    }
697    
698                    return serviceComponent;
699            }
700    
701            /**
702             * Returns the service component where buildNamespace = &#63; and buildNumber = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
703             *
704             * @param buildNamespace the build namespace
705             * @param buildNumber the build number
706             * @return the matching service component, or <code>null</code> if a matching service component could not be found
707             */
708            @Override
709            public ServiceComponent fetchByBNS_BNU(String buildNamespace,
710                    long buildNumber) {
711                    return fetchByBNS_BNU(buildNamespace, buildNumber, true);
712            }
713    
714            /**
715             * Returns the service component where buildNamespace = &#63; and buildNumber = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
716             *
717             * @param buildNamespace the build namespace
718             * @param buildNumber the build number
719             * @param retrieveFromCache whether to retrieve from the finder cache
720             * @return the matching service component, or <code>null</code> if a matching service component could not be found
721             */
722            @Override
723            public ServiceComponent fetchByBNS_BNU(String buildNamespace,
724                    long buildNumber, boolean retrieveFromCache) {
725                    Object[] finderArgs = new Object[] { buildNamespace, buildNumber };
726    
727                    Object result = null;
728    
729                    if (retrieveFromCache) {
730                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_BNS_BNU,
731                                            finderArgs, this);
732                    }
733    
734                    if (result instanceof ServiceComponent) {
735                            ServiceComponent serviceComponent = (ServiceComponent)result;
736    
737                            if (!Validator.equals(buildNamespace,
738                                                    serviceComponent.getBuildNamespace()) ||
739                                            (buildNumber != serviceComponent.getBuildNumber())) {
740                                    result = null;
741                            }
742                    }
743    
744                    if (result == null) {
745                            StringBundler query = new StringBundler(4);
746    
747                            query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE);
748    
749                            boolean bindBuildNamespace = false;
750    
751                            if (buildNamespace == null) {
752                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1);
753                            }
754                            else if (buildNamespace.equals(StringPool.BLANK)) {
755                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3);
756                            }
757                            else {
758                                    bindBuildNamespace = true;
759    
760                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2);
761                            }
762    
763                            query.append(_FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2);
764    
765                            String sql = query.toString();
766    
767                            Session session = null;
768    
769                            try {
770                                    session = openSession();
771    
772                                    Query q = session.createQuery(sql);
773    
774                                    QueryPos qPos = QueryPos.getInstance(q);
775    
776                                    if (bindBuildNamespace) {
777                                            qPos.add(buildNamespace);
778                                    }
779    
780                                    qPos.add(buildNumber);
781    
782                                    List<ServiceComponent> list = q.list();
783    
784                                    if (list.isEmpty()) {
785                                            finderCache.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
786                                                    finderArgs, list);
787                                    }
788                                    else {
789                                            ServiceComponent serviceComponent = list.get(0);
790    
791                                            result = serviceComponent;
792    
793                                            cacheResult(serviceComponent);
794    
795                                            if ((serviceComponent.getBuildNamespace() == null) ||
796                                                            !serviceComponent.getBuildNamespace()
797                                                                                                     .equals(buildNamespace) ||
798                                                            (serviceComponent.getBuildNumber() != buildNumber)) {
799                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
800                                                            finderArgs, serviceComponent);
801                                            }
802                                    }
803                            }
804                            catch (Exception e) {
805                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU,
806                                            finderArgs);
807    
808                                    throw processException(e);
809                            }
810                            finally {
811                                    closeSession(session);
812                            }
813                    }
814    
815                    if (result instanceof List<?>) {
816                            return null;
817                    }
818                    else {
819                            return (ServiceComponent)result;
820                    }
821            }
822    
823            /**
824             * Removes the service component where buildNamespace = &#63; and buildNumber = &#63; from the database.
825             *
826             * @param buildNamespace the build namespace
827             * @param buildNumber the build number
828             * @return the service component that was removed
829             */
830            @Override
831            public ServiceComponent removeByBNS_BNU(String buildNamespace,
832                    long buildNumber) throws NoSuchServiceComponentException {
833                    ServiceComponent serviceComponent = findByBNS_BNU(buildNamespace,
834                                    buildNumber);
835    
836                    return remove(serviceComponent);
837            }
838    
839            /**
840             * Returns the number of service components where buildNamespace = &#63; and buildNumber = &#63;.
841             *
842             * @param buildNamespace the build namespace
843             * @param buildNumber the build number
844             * @return the number of matching service components
845             */
846            @Override
847            public int countByBNS_BNU(String buildNamespace, long buildNumber) {
848                    FinderPath finderPath = FINDER_PATH_COUNT_BY_BNS_BNU;
849    
850                    Object[] finderArgs = new Object[] { buildNamespace, buildNumber };
851    
852                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
853    
854                    if (count == null) {
855                            StringBundler query = new StringBundler(3);
856    
857                            query.append(_SQL_COUNT_SERVICECOMPONENT_WHERE);
858    
859                            boolean bindBuildNamespace = false;
860    
861                            if (buildNamespace == null) {
862                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1);
863                            }
864                            else if (buildNamespace.equals(StringPool.BLANK)) {
865                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3);
866                            }
867                            else {
868                                    bindBuildNamespace = true;
869    
870                                    query.append(_FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2);
871                            }
872    
873                            query.append(_FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2);
874    
875                            String sql = query.toString();
876    
877                            Session session = null;
878    
879                            try {
880                                    session = openSession();
881    
882                                    Query q = session.createQuery(sql);
883    
884                                    QueryPos qPos = QueryPos.getInstance(q);
885    
886                                    if (bindBuildNamespace) {
887                                            qPos.add(buildNamespace);
888                                    }
889    
890                                    qPos.add(buildNumber);
891    
892                                    count = (Long)q.uniqueResult();
893    
894                                    finderCache.putResult(finderPath, finderArgs, count);
895                            }
896                            catch (Exception e) {
897                                    finderCache.removeResult(finderPath, finderArgs);
898    
899                                    throw processException(e);
900                            }
901                            finally {
902                                    closeSession(session);
903                            }
904                    }
905    
906                    return count.intValue();
907            }
908    
909            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_1 = "serviceComponent.buildNamespace IS NULL AND ";
910            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_2 = "serviceComponent.buildNamespace = ? AND ";
911            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNAMESPACE_3 = "(serviceComponent.buildNamespace IS NULL OR serviceComponent.buildNamespace = '') AND ";
912            private static final String _FINDER_COLUMN_BNS_BNU_BUILDNUMBER_2 = "serviceComponent.buildNumber = ?";
913    
914            public ServiceComponentPersistenceImpl() {
915                    setModelClass(ServiceComponent.class);
916            }
917    
918            /**
919             * Caches the service component in the entity cache if it is enabled.
920             *
921             * @param serviceComponent the service component
922             */
923            @Override
924            public void cacheResult(ServiceComponent serviceComponent) {
925                    entityCache.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
926                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey(),
927                            serviceComponent);
928    
929                    finderCache.putResult(FINDER_PATH_FETCH_BY_BNS_BNU,
930                            new Object[] {
931                                    serviceComponent.getBuildNamespace(),
932                                    serviceComponent.getBuildNumber()
933                            }, serviceComponent);
934    
935                    serviceComponent.resetOriginalValues();
936            }
937    
938            /**
939             * Caches the service components in the entity cache if it is enabled.
940             *
941             * @param serviceComponents the service components
942             */
943            @Override
944            public void cacheResult(List<ServiceComponent> serviceComponents) {
945                    for (ServiceComponent serviceComponent : serviceComponents) {
946                            if (entityCache.getResult(
947                                                    ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
948                                                    ServiceComponentImpl.class,
949                                                    serviceComponent.getPrimaryKey()) == null) {
950                                    cacheResult(serviceComponent);
951                            }
952                            else {
953                                    serviceComponent.resetOriginalValues();
954                            }
955                    }
956            }
957    
958            /**
959             * Clears the cache for all service components.
960             *
961             * <p>
962             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
963             * </p>
964             */
965            @Override
966            public void clearCache() {
967                    entityCache.clearCache(ServiceComponentImpl.class);
968    
969                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
970                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
971                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
972            }
973    
974            /**
975             * Clears the cache for the service component.
976             *
977             * <p>
978             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
979             * </p>
980             */
981            @Override
982            public void clearCache(ServiceComponent serviceComponent) {
983                    entityCache.removeResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
984                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey());
985    
986                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
987                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
988    
989                    clearUniqueFindersCache((ServiceComponentModelImpl)serviceComponent);
990            }
991    
992            @Override
993            public void clearCache(List<ServiceComponent> serviceComponents) {
994                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
995                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
996    
997                    for (ServiceComponent serviceComponent : serviceComponents) {
998                            entityCache.removeResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
999                                    ServiceComponentImpl.class, serviceComponent.getPrimaryKey());
1000    
1001                            clearUniqueFindersCache((ServiceComponentModelImpl)serviceComponent);
1002                    }
1003            }
1004    
1005            protected void cacheUniqueFindersCache(
1006                    ServiceComponentModelImpl serviceComponentModelImpl, boolean isNew) {
1007                    if (isNew) {
1008                            Object[] args = new Object[] {
1009                                            serviceComponentModelImpl.getBuildNamespace(),
1010                                            serviceComponentModelImpl.getBuildNumber()
1011                                    };
1012    
1013                            finderCache.putResult(FINDER_PATH_COUNT_BY_BNS_BNU, args,
1014                                    Long.valueOf(1));
1015                            finderCache.putResult(FINDER_PATH_FETCH_BY_BNS_BNU, args,
1016                                    serviceComponentModelImpl);
1017                    }
1018                    else {
1019                            if ((serviceComponentModelImpl.getColumnBitmask() &
1020                                            FINDER_PATH_FETCH_BY_BNS_BNU.getColumnBitmask()) != 0) {
1021                                    Object[] args = new Object[] {
1022                                                    serviceComponentModelImpl.getBuildNamespace(),
1023                                                    serviceComponentModelImpl.getBuildNumber()
1024                                            };
1025    
1026                                    finderCache.putResult(FINDER_PATH_COUNT_BY_BNS_BNU, args,
1027                                            Long.valueOf(1));
1028                                    finderCache.putResult(FINDER_PATH_FETCH_BY_BNS_BNU, args,
1029                                            serviceComponentModelImpl);
1030                            }
1031                    }
1032            }
1033    
1034            protected void clearUniqueFindersCache(
1035                    ServiceComponentModelImpl serviceComponentModelImpl) {
1036                    Object[] args = new Object[] {
1037                                    serviceComponentModelImpl.getBuildNamespace(),
1038                                    serviceComponentModelImpl.getBuildNumber()
1039                            };
1040    
1041                    finderCache.removeResult(FINDER_PATH_COUNT_BY_BNS_BNU, args);
1042                    finderCache.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU, args);
1043    
1044                    if ((serviceComponentModelImpl.getColumnBitmask() &
1045                                    FINDER_PATH_FETCH_BY_BNS_BNU.getColumnBitmask()) != 0) {
1046                            args = new Object[] {
1047                                            serviceComponentModelImpl.getOriginalBuildNamespace(),
1048                                            serviceComponentModelImpl.getOriginalBuildNumber()
1049                                    };
1050    
1051                            finderCache.removeResult(FINDER_PATH_COUNT_BY_BNS_BNU, args);
1052                            finderCache.removeResult(FINDER_PATH_FETCH_BY_BNS_BNU, args);
1053                    }
1054            }
1055    
1056            /**
1057             * Creates a new service component with the primary key. Does not add the service component to the database.
1058             *
1059             * @param serviceComponentId the primary key for the new service component
1060             * @return the new service component
1061             */
1062            @Override
1063            public ServiceComponent create(long serviceComponentId) {
1064                    ServiceComponent serviceComponent = new ServiceComponentImpl();
1065    
1066                    serviceComponent.setNew(true);
1067                    serviceComponent.setPrimaryKey(serviceComponentId);
1068    
1069                    return serviceComponent;
1070            }
1071    
1072            /**
1073             * Removes the service component with the primary key from the database. Also notifies the appropriate model listeners.
1074             *
1075             * @param serviceComponentId the primary key of the service component
1076             * @return the service component that was removed
1077             * @throws NoSuchServiceComponentException if a service component with the primary key could not be found
1078             */
1079            @Override
1080            public ServiceComponent remove(long serviceComponentId)
1081                    throws NoSuchServiceComponentException {
1082                    return remove((Serializable)serviceComponentId);
1083            }
1084    
1085            /**
1086             * Removes the service component with the primary key from the database. Also notifies the appropriate model listeners.
1087             *
1088             * @param primaryKey the primary key of the service component
1089             * @return the service component that was removed
1090             * @throws NoSuchServiceComponentException if a service component with the primary key could not be found
1091             */
1092            @Override
1093            public ServiceComponent remove(Serializable primaryKey)
1094                    throws NoSuchServiceComponentException {
1095                    Session session = null;
1096    
1097                    try {
1098                            session = openSession();
1099    
1100                            ServiceComponent serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
1101                                            primaryKey);
1102    
1103                            if (serviceComponent == null) {
1104                                    if (_log.isWarnEnabled()) {
1105                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1106                                    }
1107    
1108                                    throw new NoSuchServiceComponentException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1109                                            primaryKey);
1110                            }
1111    
1112                            return remove(serviceComponent);
1113                    }
1114                    catch (NoSuchServiceComponentException nsee) {
1115                            throw nsee;
1116                    }
1117                    catch (Exception e) {
1118                            throw processException(e);
1119                    }
1120                    finally {
1121                            closeSession(session);
1122                    }
1123            }
1124    
1125            @Override
1126            protected ServiceComponent removeImpl(ServiceComponent serviceComponent) {
1127                    serviceComponent = toUnwrappedModel(serviceComponent);
1128    
1129                    Session session = null;
1130    
1131                    try {
1132                            session = openSession();
1133    
1134                            if (!session.contains(serviceComponent)) {
1135                                    serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
1136                                                    serviceComponent.getPrimaryKeyObj());
1137                            }
1138    
1139                            if (serviceComponent != null) {
1140                                    session.delete(serviceComponent);
1141                            }
1142                    }
1143                    catch (Exception e) {
1144                            throw processException(e);
1145                    }
1146                    finally {
1147                            closeSession(session);
1148                    }
1149    
1150                    if (serviceComponent != null) {
1151                            clearCache(serviceComponent);
1152                    }
1153    
1154                    return serviceComponent;
1155            }
1156    
1157            @Override
1158            public ServiceComponent updateImpl(ServiceComponent serviceComponent) {
1159                    serviceComponent = toUnwrappedModel(serviceComponent);
1160    
1161                    boolean isNew = serviceComponent.isNew();
1162    
1163                    ServiceComponentModelImpl serviceComponentModelImpl = (ServiceComponentModelImpl)serviceComponent;
1164    
1165                    Session session = null;
1166    
1167                    try {
1168                            session = openSession();
1169    
1170                            if (serviceComponent.isNew()) {
1171                                    session.save(serviceComponent);
1172    
1173                                    serviceComponent.setNew(false);
1174                            }
1175                            else {
1176                                    serviceComponent = (ServiceComponent)session.merge(serviceComponent);
1177                            }
1178                    }
1179                    catch (Exception e) {
1180                            throw processException(e);
1181                    }
1182                    finally {
1183                            closeSession(session);
1184                    }
1185    
1186                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1187    
1188                    if (isNew || !ServiceComponentModelImpl.COLUMN_BITMASK_ENABLED) {
1189                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1190                    }
1191    
1192                    else {
1193                            if ((serviceComponentModelImpl.getColumnBitmask() &
1194                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BUILDNAMESPACE.getColumnBitmask()) != 0) {
1195                                    Object[] args = new Object[] {
1196                                                    serviceComponentModelImpl.getOriginalBuildNamespace()
1197                                            };
1198    
1199                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_BUILDNAMESPACE,
1200                                            args);
1201                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BUILDNAMESPACE,
1202                                            args);
1203    
1204                                    args = new Object[] {
1205                                                    serviceComponentModelImpl.getBuildNamespace()
1206                                            };
1207    
1208                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_BUILDNAMESPACE,
1209                                            args);
1210                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BUILDNAMESPACE,
1211                                            args);
1212                            }
1213                    }
1214    
1215                    entityCache.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1216                            ServiceComponentImpl.class, serviceComponent.getPrimaryKey(),
1217                            serviceComponent, false);
1218    
1219                    clearUniqueFindersCache(serviceComponentModelImpl);
1220                    cacheUniqueFindersCache(serviceComponentModelImpl, isNew);
1221    
1222                    serviceComponent.resetOriginalValues();
1223    
1224                    return serviceComponent;
1225            }
1226    
1227            protected ServiceComponent toUnwrappedModel(
1228                    ServiceComponent serviceComponent) {
1229                    if (serviceComponent instanceof ServiceComponentImpl) {
1230                            return serviceComponent;
1231                    }
1232    
1233                    ServiceComponentImpl serviceComponentImpl = new ServiceComponentImpl();
1234    
1235                    serviceComponentImpl.setNew(serviceComponent.isNew());
1236                    serviceComponentImpl.setPrimaryKey(serviceComponent.getPrimaryKey());
1237    
1238                    serviceComponentImpl.setMvccVersion(serviceComponent.getMvccVersion());
1239                    serviceComponentImpl.setServiceComponentId(serviceComponent.getServiceComponentId());
1240                    serviceComponentImpl.setBuildNamespace(serviceComponent.getBuildNamespace());
1241                    serviceComponentImpl.setBuildNumber(serviceComponent.getBuildNumber());
1242                    serviceComponentImpl.setBuildDate(serviceComponent.getBuildDate());
1243                    serviceComponentImpl.setData(serviceComponent.getData());
1244    
1245                    return serviceComponentImpl;
1246            }
1247    
1248            /**
1249             * Returns the service component with the primary key or throws a {@link com.liferay.portal.exception.NoSuchModelException} if it could not be found.
1250             *
1251             * @param primaryKey the primary key of the service component
1252             * @return the service component
1253             * @throws NoSuchServiceComponentException if a service component with the primary key could not be found
1254             */
1255            @Override
1256            public ServiceComponent findByPrimaryKey(Serializable primaryKey)
1257                    throws NoSuchServiceComponentException {
1258                    ServiceComponent serviceComponent = fetchByPrimaryKey(primaryKey);
1259    
1260                    if (serviceComponent == null) {
1261                            if (_log.isWarnEnabled()) {
1262                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1263                            }
1264    
1265                            throw new NoSuchServiceComponentException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1266                                    primaryKey);
1267                    }
1268    
1269                    return serviceComponent;
1270            }
1271    
1272            /**
1273             * Returns the service component with the primary key or throws a {@link NoSuchServiceComponentException} if it could not be found.
1274             *
1275             * @param serviceComponentId the primary key of the service component
1276             * @return the service component
1277             * @throws NoSuchServiceComponentException if a service component with the primary key could not be found
1278             */
1279            @Override
1280            public ServiceComponent findByPrimaryKey(long serviceComponentId)
1281                    throws NoSuchServiceComponentException {
1282                    return findByPrimaryKey((Serializable)serviceComponentId);
1283            }
1284    
1285            /**
1286             * Returns the service component with the primary key or returns <code>null</code> if it could not be found.
1287             *
1288             * @param primaryKey the primary key of the service component
1289             * @return the service component, or <code>null</code> if a service component with the primary key could not be found
1290             */
1291            @Override
1292            public ServiceComponent fetchByPrimaryKey(Serializable primaryKey) {
1293                    ServiceComponent serviceComponent = (ServiceComponent)entityCache.getResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1294                                    ServiceComponentImpl.class, primaryKey);
1295    
1296                    if (serviceComponent == _nullServiceComponent) {
1297                            return null;
1298                    }
1299    
1300                    if (serviceComponent == null) {
1301                            Session session = null;
1302    
1303                            try {
1304                                    session = openSession();
1305    
1306                                    serviceComponent = (ServiceComponent)session.get(ServiceComponentImpl.class,
1307                                                    primaryKey);
1308    
1309                                    if (serviceComponent != null) {
1310                                            cacheResult(serviceComponent);
1311                                    }
1312                                    else {
1313                                            entityCache.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1314                                                    ServiceComponentImpl.class, primaryKey,
1315                                                    _nullServiceComponent);
1316                                    }
1317                            }
1318                            catch (Exception e) {
1319                                    entityCache.removeResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1320                                            ServiceComponentImpl.class, primaryKey);
1321    
1322                                    throw processException(e);
1323                            }
1324                            finally {
1325                                    closeSession(session);
1326                            }
1327                    }
1328    
1329                    return serviceComponent;
1330            }
1331    
1332            /**
1333             * Returns the service component with the primary key or returns <code>null</code> if it could not be found.
1334             *
1335             * @param serviceComponentId the primary key of the service component
1336             * @return the service component, or <code>null</code> if a service component with the primary key could not be found
1337             */
1338            @Override
1339            public ServiceComponent fetchByPrimaryKey(long serviceComponentId) {
1340                    return fetchByPrimaryKey((Serializable)serviceComponentId);
1341            }
1342    
1343            @Override
1344            public Map<Serializable, ServiceComponent> fetchByPrimaryKeys(
1345                    Set<Serializable> primaryKeys) {
1346                    if (primaryKeys.isEmpty()) {
1347                            return Collections.emptyMap();
1348                    }
1349    
1350                    Map<Serializable, ServiceComponent> map = new HashMap<Serializable, ServiceComponent>();
1351    
1352                    if (primaryKeys.size() == 1) {
1353                            Iterator<Serializable> iterator = primaryKeys.iterator();
1354    
1355                            Serializable primaryKey = iterator.next();
1356    
1357                            ServiceComponent serviceComponent = fetchByPrimaryKey(primaryKey);
1358    
1359                            if (serviceComponent != null) {
1360                                    map.put(primaryKey, serviceComponent);
1361                            }
1362    
1363                            return map;
1364                    }
1365    
1366                    Set<Serializable> uncachedPrimaryKeys = null;
1367    
1368                    for (Serializable primaryKey : primaryKeys) {
1369                            ServiceComponent serviceComponent = (ServiceComponent)entityCache.getResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1370                                            ServiceComponentImpl.class, primaryKey);
1371    
1372                            if (serviceComponent == null) {
1373                                    if (uncachedPrimaryKeys == null) {
1374                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1375                                    }
1376    
1377                                    uncachedPrimaryKeys.add(primaryKey);
1378                            }
1379                            else {
1380                                    map.put(primaryKey, serviceComponent);
1381                            }
1382                    }
1383    
1384                    if (uncachedPrimaryKeys == null) {
1385                            return map;
1386                    }
1387    
1388                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1389                                    1);
1390    
1391                    query.append(_SQL_SELECT_SERVICECOMPONENT_WHERE_PKS_IN);
1392    
1393                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1394                            query.append(String.valueOf(primaryKey));
1395    
1396                            query.append(StringPool.COMMA);
1397                    }
1398    
1399                    query.setIndex(query.index() - 1);
1400    
1401                    query.append(StringPool.CLOSE_PARENTHESIS);
1402    
1403                    String sql = query.toString();
1404    
1405                    Session session = null;
1406    
1407                    try {
1408                            session = openSession();
1409    
1410                            Query q = session.createQuery(sql);
1411    
1412                            for (ServiceComponent serviceComponent : (List<ServiceComponent>)q.list()) {
1413                                    map.put(serviceComponent.getPrimaryKeyObj(), serviceComponent);
1414    
1415                                    cacheResult(serviceComponent);
1416    
1417                                    uncachedPrimaryKeys.remove(serviceComponent.getPrimaryKeyObj());
1418                            }
1419    
1420                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1421                                    entityCache.putResult(ServiceComponentModelImpl.ENTITY_CACHE_ENABLED,
1422                                            ServiceComponentImpl.class, primaryKey,
1423                                            _nullServiceComponent);
1424                            }
1425                    }
1426                    catch (Exception e) {
1427                            throw processException(e);
1428                    }
1429                    finally {
1430                            closeSession(session);
1431                    }
1432    
1433                    return map;
1434            }
1435    
1436            /**
1437             * Returns all the service components.
1438             *
1439             * @return the service components
1440             */
1441            @Override
1442            public List<ServiceComponent> findAll() {
1443                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1444            }
1445    
1446            /**
1447             * Returns a range of all the service components.
1448             *
1449             * <p>
1450             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1451             * </p>
1452             *
1453             * @param start the lower bound of the range of service components
1454             * @param end the upper bound of the range of service components (not inclusive)
1455             * @return the range of service components
1456             */
1457            @Override
1458            public List<ServiceComponent> findAll(int start, int end) {
1459                    return findAll(start, end, null);
1460            }
1461    
1462            /**
1463             * Returns an ordered range of all the service components.
1464             *
1465             * <p>
1466             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1467             * </p>
1468             *
1469             * @param start the lower bound of the range of service components
1470             * @param end the upper bound of the range of service components (not inclusive)
1471             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1472             * @return the ordered range of service components
1473             */
1474            @Override
1475            public List<ServiceComponent> findAll(int start, int end,
1476                    OrderByComparator<ServiceComponent> orderByComparator) {
1477                    return findAll(start, end, orderByComparator, true);
1478            }
1479    
1480            /**
1481             * Returns an ordered range of all the service components.
1482             *
1483             * <p>
1484             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ServiceComponentModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1485             * </p>
1486             *
1487             * @param start the lower bound of the range of service components
1488             * @param end the upper bound of the range of service components (not inclusive)
1489             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1490             * @param retrieveFromCache whether to retrieve from the finder cache
1491             * @return the ordered range of service components
1492             */
1493            @Override
1494            public List<ServiceComponent> findAll(int start, int end,
1495                    OrderByComparator<ServiceComponent> orderByComparator,
1496                    boolean retrieveFromCache) {
1497                    boolean pagination = true;
1498                    FinderPath finderPath = null;
1499                    Object[] finderArgs = null;
1500    
1501                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1502                                    (orderByComparator == null)) {
1503                            pagination = false;
1504                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1505                            finderArgs = FINDER_ARGS_EMPTY;
1506                    }
1507                    else {
1508                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1509                            finderArgs = new Object[] { start, end, orderByComparator };
1510                    }
1511    
1512                    List<ServiceComponent> list = null;
1513    
1514                    if (retrieveFromCache) {
1515                            list = (List<ServiceComponent>)finderCache.getResult(finderPath,
1516                                            finderArgs, this);
1517                    }
1518    
1519                    if (list == null) {
1520                            StringBundler query = null;
1521                            String sql = null;
1522    
1523                            if (orderByComparator != null) {
1524                                    query = new StringBundler(2 +
1525                                                    (orderByComparator.getOrderByFields().length * 2));
1526    
1527                                    query.append(_SQL_SELECT_SERVICECOMPONENT);
1528    
1529                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1530                                            orderByComparator);
1531    
1532                                    sql = query.toString();
1533                            }
1534                            else {
1535                                    sql = _SQL_SELECT_SERVICECOMPONENT;
1536    
1537                                    if (pagination) {
1538                                            sql = sql.concat(ServiceComponentModelImpl.ORDER_BY_JPQL);
1539                                    }
1540                            }
1541    
1542                            Session session = null;
1543    
1544                            try {
1545                                    session = openSession();
1546    
1547                                    Query q = session.createQuery(sql);
1548    
1549                                    if (!pagination) {
1550                                            list = (List<ServiceComponent>)QueryUtil.list(q,
1551                                                            getDialect(), start, end, false);
1552    
1553                                            Collections.sort(list);
1554    
1555                                            list = Collections.unmodifiableList(list);
1556                                    }
1557                                    else {
1558                                            list = (List<ServiceComponent>)QueryUtil.list(q,
1559                                                            getDialect(), start, end);
1560                                    }
1561    
1562                                    cacheResult(list);
1563    
1564                                    finderCache.putResult(finderPath, finderArgs, list);
1565                            }
1566                            catch (Exception e) {
1567                                    finderCache.removeResult(finderPath, finderArgs);
1568    
1569                                    throw processException(e);
1570                            }
1571                            finally {
1572                                    closeSession(session);
1573                            }
1574                    }
1575    
1576                    return list;
1577            }
1578    
1579            /**
1580             * Removes all the service components from the database.
1581             *
1582             */
1583            @Override
1584            public void removeAll() {
1585                    for (ServiceComponent serviceComponent : findAll()) {
1586                            remove(serviceComponent);
1587                    }
1588            }
1589    
1590            /**
1591             * Returns the number of service components.
1592             *
1593             * @return the number of service components
1594             */
1595            @Override
1596            public int countAll() {
1597                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
1598                                    FINDER_ARGS_EMPTY, this);
1599    
1600                    if (count == null) {
1601                            Session session = null;
1602    
1603                            try {
1604                                    session = openSession();
1605    
1606                                    Query q = session.createQuery(_SQL_COUNT_SERVICECOMPONENT);
1607    
1608                                    count = (Long)q.uniqueResult();
1609    
1610                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
1611                                            count);
1612                            }
1613                            catch (Exception e) {
1614                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
1615                                            FINDER_ARGS_EMPTY);
1616    
1617                                    throw processException(e);
1618                            }
1619                            finally {
1620                                    closeSession(session);
1621                            }
1622                    }
1623    
1624                    return count.intValue();
1625            }
1626    
1627            @Override
1628            public Set<String> getBadColumnNames() {
1629                    return _badColumnNames;
1630            }
1631    
1632            @Override
1633            protected Map<String, Integer> getTableColumnsMap() {
1634                    return ServiceComponentModelImpl.TABLE_COLUMNS_MAP;
1635            }
1636    
1637            /**
1638             * Initializes the service component persistence.
1639             */
1640            public void afterPropertiesSet() {
1641            }
1642    
1643            public void destroy() {
1644                    entityCache.removeCache(ServiceComponentImpl.class.getName());
1645                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1646                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1647                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1648            }
1649    
1650            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1651            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1652            private static final String _SQL_SELECT_SERVICECOMPONENT = "SELECT serviceComponent FROM ServiceComponent serviceComponent";
1653            private static final String _SQL_SELECT_SERVICECOMPONENT_WHERE_PKS_IN = "SELECT serviceComponent FROM ServiceComponent serviceComponent WHERE serviceComponentId IN (";
1654            private static final String _SQL_SELECT_SERVICECOMPONENT_WHERE = "SELECT serviceComponent FROM ServiceComponent serviceComponent WHERE ";
1655            private static final String _SQL_COUNT_SERVICECOMPONENT = "SELECT COUNT(serviceComponent) FROM ServiceComponent serviceComponent";
1656            private static final String _SQL_COUNT_SERVICECOMPONENT_WHERE = "SELECT COUNT(serviceComponent) FROM ServiceComponent serviceComponent WHERE ";
1657            private static final String _ORDER_BY_ENTITY_ALIAS = "serviceComponent.";
1658            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ServiceComponent exists with the primary key ";
1659            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ServiceComponent exists with the key {";
1660            private static final Log _log = LogFactoryUtil.getLog(ServiceComponentPersistenceImpl.class);
1661            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1662                                    "data"
1663                            });
1664            private static final ServiceComponent _nullServiceComponent = new ServiceComponentImpl() {
1665                            @Override
1666                            public Object clone() {
1667                                    return this;
1668                            }
1669    
1670                            @Override
1671                            public CacheModel<ServiceComponent> toCacheModel() {
1672                                    return _nullServiceComponentCacheModel;
1673                            }
1674                    };
1675    
1676            private static final CacheModel<ServiceComponent> _nullServiceComponentCacheModel =
1677                    new NullCacheModel();
1678    
1679            private static class NullCacheModel implements CacheModel<ServiceComponent>,
1680                    MVCCModel {
1681                    @Override
1682                    public long getMvccVersion() {
1683                            return -1;
1684                    }
1685    
1686                    @Override
1687                    public void setMvccVersion(long mvccVersion) {
1688                    }
1689    
1690                    @Override
1691                    public ServiceComponent toEntityModel() {
1692                            return _nullServiceComponent;
1693                    }
1694            }
1695    }