001    /**
002     * Copyright (c) 2000-2013 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.NoSuchResourceBlockException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.InstanceFactory;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UnmodifiableList;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.CacheModel;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.ResourceBlock;
040    import com.liferay.portal.model.impl.ResourceBlockImpl;
041    import com.liferay.portal.model.impl.ResourceBlockModelImpl;
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     * The persistence implementation for the resource block service.
052     *
053     * <p>
054     * Caching information and settings can be found in <code>portal.properties</code>
055     * </p>
056     *
057     * @author Brian Wing Shun Chan
058     * @see ResourceBlockPersistence
059     * @see ResourceBlockUtil
060     * @generated
061     */
062    public class ResourceBlockPersistenceImpl extends BasePersistenceImpl<ResourceBlock>
063            implements ResourceBlockPersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * Never modify or reference this class directly. Always use {@link ResourceBlockUtil} to access the resource block persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = ResourceBlockImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
075                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
076                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
077                            "findAll", new String[0]);
078            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
079                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
080                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
081                            "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
083                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED, Long.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
085            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
086                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
087                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
088                            "findByC_N",
089                            new String[] {
090                                    Long.class.getName(), String.class.getName(),
091                                    
092                            Integer.class.getName(), Integer.class.getName(),
093                                    OrderByComparator.class.getName()
094                            });
095            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
096                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
097                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
098                            "findByC_N",
099                            new String[] { Long.class.getName(), String.class.getName() },
100                            ResourceBlockModelImpl.COMPANYID_COLUMN_BITMASK |
101                            ResourceBlockModelImpl.NAME_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
103                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N",
105                            new String[] { Long.class.getName(), String.class.getName() });
106    
107            /**
108             * Returns all the resource blocks where companyId = &#63; and name = &#63;.
109             *
110             * @param companyId the company ID
111             * @param name the name
112             * @return the matching resource blocks
113             * @throws SystemException if a system exception occurred
114             */
115            @Override
116            public List<ResourceBlock> findByC_N(long companyId, String name)
117                    throws SystemException {
118                    return findByC_N(companyId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
119                            null);
120            }
121    
122            /**
123             * Returns a range of all the resource blocks where companyId = &#63; and name = &#63;.
124             *
125             * <p>
126             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
127             * </p>
128             *
129             * @param companyId the company ID
130             * @param name the name
131             * @param start the lower bound of the range of resource blocks
132             * @param end the upper bound of the range of resource blocks (not inclusive)
133             * @return the range of matching resource blocks
134             * @throws SystemException if a system exception occurred
135             */
136            @Override
137            public List<ResourceBlock> findByC_N(long companyId, String name,
138                    int start, int end) throws SystemException {
139                    return findByC_N(companyId, name, start, end, null);
140            }
141    
142            /**
143             * Returns an ordered range of all the resource blocks where companyId = &#63; and name = &#63;.
144             *
145             * <p>
146             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
147             * </p>
148             *
149             * @param companyId the company ID
150             * @param name the name
151             * @param start the lower bound of the range of resource blocks
152             * @param end the upper bound of the range of resource blocks (not inclusive)
153             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
154             * @return the ordered range of matching resource blocks
155             * @throws SystemException if a system exception occurred
156             */
157            @Override
158            public List<ResourceBlock> findByC_N(long companyId, String name,
159                    int start, int end, OrderByComparator orderByComparator)
160                    throws SystemException {
161                    boolean pagination = true;
162                    FinderPath finderPath = null;
163                    Object[] finderArgs = null;
164    
165                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
166                                    (orderByComparator == null)) {
167                            pagination = false;
168                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N;
169                            finderArgs = new Object[] { companyId, name };
170                    }
171                    else {
172                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N;
173                            finderArgs = new Object[] {
174                                            companyId, name,
175                                            
176                                            start, end, orderByComparator
177                                    };
178                    }
179    
180                    List<ResourceBlock> list = (List<ResourceBlock>)FinderCacheUtil.getResult(finderPath,
181                                    finderArgs, this);
182    
183                    if ((list != null) && !list.isEmpty()) {
184                            for (ResourceBlock resourceBlock : list) {
185                                    if ((companyId != resourceBlock.getCompanyId()) ||
186                                                    !Validator.equals(name, resourceBlock.getName())) {
187                                            list = null;
188    
189                                            break;
190                                    }
191                            }
192                    }
193    
194                    if (list == null) {
195                            StringBundler query = null;
196    
197                            if (orderByComparator != null) {
198                                    query = new StringBundler(4 +
199                                                    (orderByComparator.getOrderByFields().length * 3));
200                            }
201                            else {
202                                    query = new StringBundler(4);
203                            }
204    
205                            query.append(_SQL_SELECT_RESOURCEBLOCK_WHERE);
206    
207                            query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
208    
209                            boolean bindName = false;
210    
211                            if (name == null) {
212                                    query.append(_FINDER_COLUMN_C_N_NAME_1);
213                            }
214                            else if (name.equals(StringPool.BLANK)) {
215                                    query.append(_FINDER_COLUMN_C_N_NAME_3);
216                            }
217                            else {
218                                    bindName = true;
219    
220                                    query.append(_FINDER_COLUMN_C_N_NAME_2);
221                            }
222    
223                            if (orderByComparator != null) {
224                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
225                                            orderByComparator);
226                            }
227                            else
228                             if (pagination) {
229                                    query.append(ResourceBlockModelImpl.ORDER_BY_JPQL);
230                            }
231    
232                            String sql = query.toString();
233    
234                            Session session = null;
235    
236                            try {
237                                    session = openSession();
238    
239                                    Query q = session.createQuery(sql);
240    
241                                    QueryPos qPos = QueryPos.getInstance(q);
242    
243                                    qPos.add(companyId);
244    
245                                    if (bindName) {
246                                            qPos.add(name);
247                                    }
248    
249                                    if (!pagination) {
250                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
251                                                            start, end, false);
252    
253                                            Collections.sort(list);
254    
255                                            list = new UnmodifiableList<ResourceBlock>(list);
256                                    }
257                                    else {
258                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
259                                                            start, end);
260                                    }
261    
262                                    cacheResult(list);
263    
264                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
265                            }
266                            catch (Exception e) {
267                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
268    
269                                    throw processException(e);
270                            }
271                            finally {
272                                    closeSession(session);
273                            }
274                    }
275    
276                    return list;
277            }
278    
279            /**
280             * Returns the first resource block in the ordered set where companyId = &#63; and name = &#63;.
281             *
282             * @param companyId the company ID
283             * @param name the name
284             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
285             * @return the first matching resource block
286             * @throws com.liferay.portal.NoSuchResourceBlockException if a matching resource block could not be found
287             * @throws SystemException if a system exception occurred
288             */
289            @Override
290            public ResourceBlock findByC_N_First(long companyId, String name,
291                    OrderByComparator orderByComparator)
292                    throws NoSuchResourceBlockException, SystemException {
293                    ResourceBlock resourceBlock = fetchByC_N_First(companyId, name,
294                                    orderByComparator);
295    
296                    if (resourceBlock != null) {
297                            return resourceBlock;
298                    }
299    
300                    StringBundler msg = new StringBundler(6);
301    
302                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
303    
304                    msg.append("companyId=");
305                    msg.append(companyId);
306    
307                    msg.append(", name=");
308                    msg.append(name);
309    
310                    msg.append(StringPool.CLOSE_CURLY_BRACE);
311    
312                    throw new NoSuchResourceBlockException(msg.toString());
313            }
314    
315            /**
316             * Returns the first resource block in the ordered set where companyId = &#63; and name = &#63;.
317             *
318             * @param companyId the company ID
319             * @param name the name
320             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
321             * @return the first matching resource block, or <code>null</code> if a matching resource block could not be found
322             * @throws SystemException if a system exception occurred
323             */
324            @Override
325            public ResourceBlock fetchByC_N_First(long companyId, String name,
326                    OrderByComparator orderByComparator) throws SystemException {
327                    List<ResourceBlock> list = findByC_N(companyId, name, 0, 1,
328                                    orderByComparator);
329    
330                    if (!list.isEmpty()) {
331                            return list.get(0);
332                    }
333    
334                    return null;
335            }
336    
337            /**
338             * Returns the last resource block in the ordered set where companyId = &#63; and name = &#63;.
339             *
340             * @param companyId the company ID
341             * @param name the name
342             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
343             * @return the last matching resource block
344             * @throws com.liferay.portal.NoSuchResourceBlockException if a matching resource block could not be found
345             * @throws SystemException if a system exception occurred
346             */
347            @Override
348            public ResourceBlock findByC_N_Last(long companyId, String name,
349                    OrderByComparator orderByComparator)
350                    throws NoSuchResourceBlockException, SystemException {
351                    ResourceBlock resourceBlock = fetchByC_N_Last(companyId, name,
352                                    orderByComparator);
353    
354                    if (resourceBlock != null) {
355                            return resourceBlock;
356                    }
357    
358                    StringBundler msg = new StringBundler(6);
359    
360                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
361    
362                    msg.append("companyId=");
363                    msg.append(companyId);
364    
365                    msg.append(", name=");
366                    msg.append(name);
367    
368                    msg.append(StringPool.CLOSE_CURLY_BRACE);
369    
370                    throw new NoSuchResourceBlockException(msg.toString());
371            }
372    
373            /**
374             * Returns the last resource block in the ordered set where companyId = &#63; and name = &#63;.
375             *
376             * @param companyId the company ID
377             * @param name the name
378             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
379             * @return the last matching resource block, or <code>null</code> if a matching resource block could not be found
380             * @throws SystemException if a system exception occurred
381             */
382            @Override
383            public ResourceBlock fetchByC_N_Last(long companyId, String name,
384                    OrderByComparator orderByComparator) throws SystemException {
385                    int count = countByC_N(companyId, name);
386    
387                    if (count == 0) {
388                            return null;
389                    }
390    
391                    List<ResourceBlock> list = findByC_N(companyId, name, count - 1, count,
392                                    orderByComparator);
393    
394                    if (!list.isEmpty()) {
395                            return list.get(0);
396                    }
397    
398                    return null;
399            }
400    
401            /**
402             * Returns the resource blocks before and after the current resource block in the ordered set where companyId = &#63; and name = &#63;.
403             *
404             * @param resourceBlockId the primary key of the current resource block
405             * @param companyId the company ID
406             * @param name the name
407             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
408             * @return the previous, current, and next resource block
409             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
410             * @throws SystemException if a system exception occurred
411             */
412            @Override
413            public ResourceBlock[] findByC_N_PrevAndNext(long resourceBlockId,
414                    long companyId, String name, OrderByComparator orderByComparator)
415                    throws NoSuchResourceBlockException, SystemException {
416                    ResourceBlock resourceBlock = findByPrimaryKey(resourceBlockId);
417    
418                    Session session = null;
419    
420                    try {
421                            session = openSession();
422    
423                            ResourceBlock[] array = new ResourceBlockImpl[3];
424    
425                            array[0] = getByC_N_PrevAndNext(session, resourceBlock, companyId,
426                                            name, orderByComparator, true);
427    
428                            array[1] = resourceBlock;
429    
430                            array[2] = getByC_N_PrevAndNext(session, resourceBlock, companyId,
431                                            name, orderByComparator, false);
432    
433                            return array;
434                    }
435                    catch (Exception e) {
436                            throw processException(e);
437                    }
438                    finally {
439                            closeSession(session);
440                    }
441            }
442    
443            protected ResourceBlock getByC_N_PrevAndNext(Session session,
444                    ResourceBlock resourceBlock, long companyId, String name,
445                    OrderByComparator orderByComparator, boolean previous) {
446                    StringBundler query = null;
447    
448                    if (orderByComparator != null) {
449                            query = new StringBundler(6 +
450                                            (orderByComparator.getOrderByFields().length * 6));
451                    }
452                    else {
453                            query = new StringBundler(3);
454                    }
455    
456                    query.append(_SQL_SELECT_RESOURCEBLOCK_WHERE);
457    
458                    query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
459    
460                    boolean bindName = false;
461    
462                    if (name == null) {
463                            query.append(_FINDER_COLUMN_C_N_NAME_1);
464                    }
465                    else if (name.equals(StringPool.BLANK)) {
466                            query.append(_FINDER_COLUMN_C_N_NAME_3);
467                    }
468                    else {
469                            bindName = true;
470    
471                            query.append(_FINDER_COLUMN_C_N_NAME_2);
472                    }
473    
474                    if (orderByComparator != null) {
475                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
476    
477                            if (orderByConditionFields.length > 0) {
478                                    query.append(WHERE_AND);
479                            }
480    
481                            for (int i = 0; i < orderByConditionFields.length; i++) {
482                                    query.append(_ORDER_BY_ENTITY_ALIAS);
483                                    query.append(orderByConditionFields[i]);
484    
485                                    if ((i + 1) < orderByConditionFields.length) {
486                                            if (orderByComparator.isAscending() ^ previous) {
487                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
488                                            }
489                                            else {
490                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
491                                            }
492                                    }
493                                    else {
494                                            if (orderByComparator.isAscending() ^ previous) {
495                                                    query.append(WHERE_GREATER_THAN);
496                                            }
497                                            else {
498                                                    query.append(WHERE_LESSER_THAN);
499                                            }
500                                    }
501                            }
502    
503                            query.append(ORDER_BY_CLAUSE);
504    
505                            String[] orderByFields = orderByComparator.getOrderByFields();
506    
507                            for (int i = 0; i < orderByFields.length; i++) {
508                                    query.append(_ORDER_BY_ENTITY_ALIAS);
509                                    query.append(orderByFields[i]);
510    
511                                    if ((i + 1) < orderByFields.length) {
512                                            if (orderByComparator.isAscending() ^ previous) {
513                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
514                                            }
515                                            else {
516                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
517                                            }
518                                    }
519                                    else {
520                                            if (orderByComparator.isAscending() ^ previous) {
521                                                    query.append(ORDER_BY_ASC);
522                                            }
523                                            else {
524                                                    query.append(ORDER_BY_DESC);
525                                            }
526                                    }
527                            }
528                    }
529                    else {
530                            query.append(ResourceBlockModelImpl.ORDER_BY_JPQL);
531                    }
532    
533                    String sql = query.toString();
534    
535                    Query q = session.createQuery(sql);
536    
537                    q.setFirstResult(0);
538                    q.setMaxResults(2);
539    
540                    QueryPos qPos = QueryPos.getInstance(q);
541    
542                    qPos.add(companyId);
543    
544                    if (bindName) {
545                            qPos.add(name);
546                    }
547    
548                    if (orderByComparator != null) {
549                            Object[] values = orderByComparator.getOrderByConditionValues(resourceBlock);
550    
551                            for (Object value : values) {
552                                    qPos.add(value);
553                            }
554                    }
555    
556                    List<ResourceBlock> list = q.list();
557    
558                    if (list.size() == 2) {
559                            return list.get(1);
560                    }
561                    else {
562                            return null;
563                    }
564            }
565    
566            /**
567             * Removes all the resource blocks where companyId = &#63; and name = &#63; from the database.
568             *
569             * @param companyId the company ID
570             * @param name the name
571             * @throws SystemException if a system exception occurred
572             */
573            @Override
574            public void removeByC_N(long companyId, String name)
575                    throws SystemException {
576                    for (ResourceBlock resourceBlock : findByC_N(companyId, name,
577                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
578                            remove(resourceBlock);
579                    }
580            }
581    
582            /**
583             * Returns the number of resource blocks where companyId = &#63; and name = &#63;.
584             *
585             * @param companyId the company ID
586             * @param name the name
587             * @return the number of matching resource blocks
588             * @throws SystemException if a system exception occurred
589             */
590            @Override
591            public int countByC_N(long companyId, String name)
592                    throws SystemException {
593                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N;
594    
595                    Object[] finderArgs = new Object[] { companyId, name };
596    
597                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
598                                    this);
599    
600                    if (count == null) {
601                            StringBundler query = new StringBundler(3);
602    
603                            query.append(_SQL_COUNT_RESOURCEBLOCK_WHERE);
604    
605                            query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
606    
607                            boolean bindName = false;
608    
609                            if (name == null) {
610                                    query.append(_FINDER_COLUMN_C_N_NAME_1);
611                            }
612                            else if (name.equals(StringPool.BLANK)) {
613                                    query.append(_FINDER_COLUMN_C_N_NAME_3);
614                            }
615                            else {
616                                    bindName = true;
617    
618                                    query.append(_FINDER_COLUMN_C_N_NAME_2);
619                            }
620    
621                            String sql = query.toString();
622    
623                            Session session = null;
624    
625                            try {
626                                    session = openSession();
627    
628                                    Query q = session.createQuery(sql);
629    
630                                    QueryPos qPos = QueryPos.getInstance(q);
631    
632                                    qPos.add(companyId);
633    
634                                    if (bindName) {
635                                            qPos.add(name);
636                                    }
637    
638                                    count = (Long)q.uniqueResult();
639    
640                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
641                            }
642                            catch (Exception e) {
643                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
644    
645                                    throw processException(e);
646                            }
647                            finally {
648                                    closeSession(session);
649                            }
650                    }
651    
652                    return count.intValue();
653            }
654    
655            private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "resourceBlock.companyId = ? AND ";
656            private static final String _FINDER_COLUMN_C_N_NAME_1 = "resourceBlock.name IS NULL";
657            private static final String _FINDER_COLUMN_C_N_NAME_2 = "resourceBlock.name = ?";
658            private static final String _FINDER_COLUMN_C_N_NAME_3 = "(resourceBlock.name IS NULL OR resourceBlock.name = '')";
659            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_G_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
660                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
661                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
662                            "findByC_G_N",
663                            new String[] {
664                                    Long.class.getName(), Long.class.getName(),
665                                    String.class.getName(),
666                                    
667                            Integer.class.getName(), Integer.class.getName(),
668                                    OrderByComparator.class.getName()
669                            });
670            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
671                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
672                            ResourceBlockImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
673                            "findByC_G_N",
674                            new String[] {
675                                    Long.class.getName(), Long.class.getName(),
676                                    String.class.getName()
677                            },
678                            ResourceBlockModelImpl.COMPANYID_COLUMN_BITMASK |
679                            ResourceBlockModelImpl.GROUPID_COLUMN_BITMASK |
680                            ResourceBlockModelImpl.NAME_COLUMN_BITMASK);
681            public static final FinderPath FINDER_PATH_COUNT_BY_C_G_N = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
682                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED, Long.class,
683                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_G_N",
684                            new String[] {
685                                    Long.class.getName(), Long.class.getName(),
686                                    String.class.getName()
687                            });
688    
689            /**
690             * Returns all the resource blocks where companyId = &#63; and groupId = &#63; and name = &#63;.
691             *
692             * @param companyId the company ID
693             * @param groupId the group ID
694             * @param name the name
695             * @return the matching resource blocks
696             * @throws SystemException if a system exception occurred
697             */
698            @Override
699            public List<ResourceBlock> findByC_G_N(long companyId, long groupId,
700                    String name) throws SystemException {
701                    return findByC_G_N(companyId, groupId, name, QueryUtil.ALL_POS,
702                            QueryUtil.ALL_POS, null);
703            }
704    
705            /**
706             * Returns a range of all the resource blocks where companyId = &#63; and groupId = &#63; and name = &#63;.
707             *
708             * <p>
709             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
710             * </p>
711             *
712             * @param companyId the company ID
713             * @param groupId the group ID
714             * @param name the name
715             * @param start the lower bound of the range of resource blocks
716             * @param end the upper bound of the range of resource blocks (not inclusive)
717             * @return the range of matching resource blocks
718             * @throws SystemException if a system exception occurred
719             */
720            @Override
721            public List<ResourceBlock> findByC_G_N(long companyId, long groupId,
722                    String name, int start, int end) throws SystemException {
723                    return findByC_G_N(companyId, groupId, name, start, end, null);
724            }
725    
726            /**
727             * Returns an ordered range of all the resource blocks where companyId = &#63; and groupId = &#63; and name = &#63;.
728             *
729             * <p>
730             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
731             * </p>
732             *
733             * @param companyId the company ID
734             * @param groupId the group ID
735             * @param name the name
736             * @param start the lower bound of the range of resource blocks
737             * @param end the upper bound of the range of resource blocks (not inclusive)
738             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
739             * @return the ordered range of matching resource blocks
740             * @throws SystemException if a system exception occurred
741             */
742            @Override
743            public List<ResourceBlock> findByC_G_N(long companyId, long groupId,
744                    String name, int start, int end, OrderByComparator orderByComparator)
745                    throws SystemException {
746                    boolean pagination = true;
747                    FinderPath finderPath = null;
748                    Object[] finderArgs = null;
749    
750                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
751                                    (orderByComparator == null)) {
752                            pagination = false;
753                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G_N;
754                            finderArgs = new Object[] { companyId, groupId, name };
755                    }
756                    else {
757                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_G_N;
758                            finderArgs = new Object[] {
759                                            companyId, groupId, name,
760                                            
761                                            start, end, orderByComparator
762                                    };
763                    }
764    
765                    List<ResourceBlock> list = (List<ResourceBlock>)FinderCacheUtil.getResult(finderPath,
766                                    finderArgs, this);
767    
768                    if ((list != null) && !list.isEmpty()) {
769                            for (ResourceBlock resourceBlock : list) {
770                                    if ((companyId != resourceBlock.getCompanyId()) ||
771                                                    (groupId != resourceBlock.getGroupId()) ||
772                                                    !Validator.equals(name, resourceBlock.getName())) {
773                                            list = null;
774    
775                                            break;
776                                    }
777                            }
778                    }
779    
780                    if (list == null) {
781                            StringBundler query = null;
782    
783                            if (orderByComparator != null) {
784                                    query = new StringBundler(5 +
785                                                    (orderByComparator.getOrderByFields().length * 3));
786                            }
787                            else {
788                                    query = new StringBundler(5);
789                            }
790    
791                            query.append(_SQL_SELECT_RESOURCEBLOCK_WHERE);
792    
793                            query.append(_FINDER_COLUMN_C_G_N_COMPANYID_2);
794    
795                            query.append(_FINDER_COLUMN_C_G_N_GROUPID_2);
796    
797                            boolean bindName = false;
798    
799                            if (name == null) {
800                                    query.append(_FINDER_COLUMN_C_G_N_NAME_1);
801                            }
802                            else if (name.equals(StringPool.BLANK)) {
803                                    query.append(_FINDER_COLUMN_C_G_N_NAME_3);
804                            }
805                            else {
806                                    bindName = true;
807    
808                                    query.append(_FINDER_COLUMN_C_G_N_NAME_2);
809                            }
810    
811                            if (orderByComparator != null) {
812                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
813                                            orderByComparator);
814                            }
815                            else
816                             if (pagination) {
817                                    query.append(ResourceBlockModelImpl.ORDER_BY_JPQL);
818                            }
819    
820                            String sql = query.toString();
821    
822                            Session session = null;
823    
824                            try {
825                                    session = openSession();
826    
827                                    Query q = session.createQuery(sql);
828    
829                                    QueryPos qPos = QueryPos.getInstance(q);
830    
831                                    qPos.add(companyId);
832    
833                                    qPos.add(groupId);
834    
835                                    if (bindName) {
836                                            qPos.add(name);
837                                    }
838    
839                                    if (!pagination) {
840                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
841                                                            start, end, false);
842    
843                                            Collections.sort(list);
844    
845                                            list = new UnmodifiableList<ResourceBlock>(list);
846                                    }
847                                    else {
848                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
849                                                            start, end);
850                                    }
851    
852                                    cacheResult(list);
853    
854                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
855                            }
856                            catch (Exception e) {
857                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
858    
859                                    throw processException(e);
860                            }
861                            finally {
862                                    closeSession(session);
863                            }
864                    }
865    
866                    return list;
867            }
868    
869            /**
870             * Returns the first resource block in the ordered set where companyId = &#63; and groupId = &#63; and name = &#63;.
871             *
872             * @param companyId the company ID
873             * @param groupId the group ID
874             * @param name the name
875             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
876             * @return the first matching resource block
877             * @throws com.liferay.portal.NoSuchResourceBlockException if a matching resource block could not be found
878             * @throws SystemException if a system exception occurred
879             */
880            @Override
881            public ResourceBlock findByC_G_N_First(long companyId, long groupId,
882                    String name, OrderByComparator orderByComparator)
883                    throws NoSuchResourceBlockException, SystemException {
884                    ResourceBlock resourceBlock = fetchByC_G_N_First(companyId, groupId,
885                                    name, orderByComparator);
886    
887                    if (resourceBlock != null) {
888                            return resourceBlock;
889                    }
890    
891                    StringBundler msg = new StringBundler(8);
892    
893                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
894    
895                    msg.append("companyId=");
896                    msg.append(companyId);
897    
898                    msg.append(", groupId=");
899                    msg.append(groupId);
900    
901                    msg.append(", name=");
902                    msg.append(name);
903    
904                    msg.append(StringPool.CLOSE_CURLY_BRACE);
905    
906                    throw new NoSuchResourceBlockException(msg.toString());
907            }
908    
909            /**
910             * Returns the first resource block in the ordered set where companyId = &#63; and groupId = &#63; and name = &#63;.
911             *
912             * @param companyId the company ID
913             * @param groupId the group ID
914             * @param name the name
915             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
916             * @return the first matching resource block, or <code>null</code> if a matching resource block could not be found
917             * @throws SystemException if a system exception occurred
918             */
919            @Override
920            public ResourceBlock fetchByC_G_N_First(long companyId, long groupId,
921                    String name, OrderByComparator orderByComparator)
922                    throws SystemException {
923                    List<ResourceBlock> list = findByC_G_N(companyId, groupId, name, 0, 1,
924                                    orderByComparator);
925    
926                    if (!list.isEmpty()) {
927                            return list.get(0);
928                    }
929    
930                    return null;
931            }
932    
933            /**
934             * Returns the last resource block in the ordered set where companyId = &#63; and groupId = &#63; and name = &#63;.
935             *
936             * @param companyId the company ID
937             * @param groupId the group ID
938             * @param name the name
939             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
940             * @return the last matching resource block
941             * @throws com.liferay.portal.NoSuchResourceBlockException if a matching resource block could not be found
942             * @throws SystemException if a system exception occurred
943             */
944            @Override
945            public ResourceBlock findByC_G_N_Last(long companyId, long groupId,
946                    String name, OrderByComparator orderByComparator)
947                    throws NoSuchResourceBlockException, SystemException {
948                    ResourceBlock resourceBlock = fetchByC_G_N_Last(companyId, groupId,
949                                    name, orderByComparator);
950    
951                    if (resourceBlock != null) {
952                            return resourceBlock;
953                    }
954    
955                    StringBundler msg = new StringBundler(8);
956    
957                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
958    
959                    msg.append("companyId=");
960                    msg.append(companyId);
961    
962                    msg.append(", groupId=");
963                    msg.append(groupId);
964    
965                    msg.append(", name=");
966                    msg.append(name);
967    
968                    msg.append(StringPool.CLOSE_CURLY_BRACE);
969    
970                    throw new NoSuchResourceBlockException(msg.toString());
971            }
972    
973            /**
974             * Returns the last resource block in the ordered set where companyId = &#63; and groupId = &#63; and name = &#63;.
975             *
976             * @param companyId the company ID
977             * @param groupId the group ID
978             * @param name the name
979             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
980             * @return the last matching resource block, or <code>null</code> if a matching resource block could not be found
981             * @throws SystemException if a system exception occurred
982             */
983            @Override
984            public ResourceBlock fetchByC_G_N_Last(long companyId, long groupId,
985                    String name, OrderByComparator orderByComparator)
986                    throws SystemException {
987                    int count = countByC_G_N(companyId, groupId, name);
988    
989                    if (count == 0) {
990                            return null;
991                    }
992    
993                    List<ResourceBlock> list = findByC_G_N(companyId, groupId, name,
994                                    count - 1, count, orderByComparator);
995    
996                    if (!list.isEmpty()) {
997                            return list.get(0);
998                    }
999    
1000                    return null;
1001            }
1002    
1003            /**
1004             * Returns the resource blocks before and after the current resource block in the ordered set where companyId = &#63; and groupId = &#63; and name = &#63;.
1005             *
1006             * @param resourceBlockId the primary key of the current resource block
1007             * @param companyId the company ID
1008             * @param groupId the group ID
1009             * @param name the name
1010             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1011             * @return the previous, current, and next resource block
1012             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
1013             * @throws SystemException if a system exception occurred
1014             */
1015            @Override
1016            public ResourceBlock[] findByC_G_N_PrevAndNext(long resourceBlockId,
1017                    long companyId, long groupId, String name,
1018                    OrderByComparator orderByComparator)
1019                    throws NoSuchResourceBlockException, SystemException {
1020                    ResourceBlock resourceBlock = findByPrimaryKey(resourceBlockId);
1021    
1022                    Session session = null;
1023    
1024                    try {
1025                            session = openSession();
1026    
1027                            ResourceBlock[] array = new ResourceBlockImpl[3];
1028    
1029                            array[0] = getByC_G_N_PrevAndNext(session, resourceBlock,
1030                                            companyId, groupId, name, orderByComparator, true);
1031    
1032                            array[1] = resourceBlock;
1033    
1034                            array[2] = getByC_G_N_PrevAndNext(session, resourceBlock,
1035                                            companyId, groupId, name, orderByComparator, false);
1036    
1037                            return array;
1038                    }
1039                    catch (Exception e) {
1040                            throw processException(e);
1041                    }
1042                    finally {
1043                            closeSession(session);
1044                    }
1045            }
1046    
1047            protected ResourceBlock getByC_G_N_PrevAndNext(Session session,
1048                    ResourceBlock resourceBlock, long companyId, long groupId, String name,
1049                    OrderByComparator orderByComparator, boolean previous) {
1050                    StringBundler query = null;
1051    
1052                    if (orderByComparator != null) {
1053                            query = new StringBundler(6 +
1054                                            (orderByComparator.getOrderByFields().length * 6));
1055                    }
1056                    else {
1057                            query = new StringBundler(3);
1058                    }
1059    
1060                    query.append(_SQL_SELECT_RESOURCEBLOCK_WHERE);
1061    
1062                    query.append(_FINDER_COLUMN_C_G_N_COMPANYID_2);
1063    
1064                    query.append(_FINDER_COLUMN_C_G_N_GROUPID_2);
1065    
1066                    boolean bindName = false;
1067    
1068                    if (name == null) {
1069                            query.append(_FINDER_COLUMN_C_G_N_NAME_1);
1070                    }
1071                    else if (name.equals(StringPool.BLANK)) {
1072                            query.append(_FINDER_COLUMN_C_G_N_NAME_3);
1073                    }
1074                    else {
1075                            bindName = true;
1076    
1077                            query.append(_FINDER_COLUMN_C_G_N_NAME_2);
1078                    }
1079    
1080                    if (orderByComparator != null) {
1081                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1082    
1083                            if (orderByConditionFields.length > 0) {
1084                                    query.append(WHERE_AND);
1085                            }
1086    
1087                            for (int i = 0; i < orderByConditionFields.length; i++) {
1088                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1089                                    query.append(orderByConditionFields[i]);
1090    
1091                                    if ((i + 1) < orderByConditionFields.length) {
1092                                            if (orderByComparator.isAscending() ^ previous) {
1093                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1094                                            }
1095                                            else {
1096                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1097                                            }
1098                                    }
1099                                    else {
1100                                            if (orderByComparator.isAscending() ^ previous) {
1101                                                    query.append(WHERE_GREATER_THAN);
1102                                            }
1103                                            else {
1104                                                    query.append(WHERE_LESSER_THAN);
1105                                            }
1106                                    }
1107                            }
1108    
1109                            query.append(ORDER_BY_CLAUSE);
1110    
1111                            String[] orderByFields = orderByComparator.getOrderByFields();
1112    
1113                            for (int i = 0; i < orderByFields.length; i++) {
1114                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1115                                    query.append(orderByFields[i]);
1116    
1117                                    if ((i + 1) < orderByFields.length) {
1118                                            if (orderByComparator.isAscending() ^ previous) {
1119                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1120                                            }
1121                                            else {
1122                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1123                                            }
1124                                    }
1125                                    else {
1126                                            if (orderByComparator.isAscending() ^ previous) {
1127                                                    query.append(ORDER_BY_ASC);
1128                                            }
1129                                            else {
1130                                                    query.append(ORDER_BY_DESC);
1131                                            }
1132                                    }
1133                            }
1134                    }
1135                    else {
1136                            query.append(ResourceBlockModelImpl.ORDER_BY_JPQL);
1137                    }
1138    
1139                    String sql = query.toString();
1140    
1141                    Query q = session.createQuery(sql);
1142    
1143                    q.setFirstResult(0);
1144                    q.setMaxResults(2);
1145    
1146                    QueryPos qPos = QueryPos.getInstance(q);
1147    
1148                    qPos.add(companyId);
1149    
1150                    qPos.add(groupId);
1151    
1152                    if (bindName) {
1153                            qPos.add(name);
1154                    }
1155    
1156                    if (orderByComparator != null) {
1157                            Object[] values = orderByComparator.getOrderByConditionValues(resourceBlock);
1158    
1159                            for (Object value : values) {
1160                                    qPos.add(value);
1161                            }
1162                    }
1163    
1164                    List<ResourceBlock> list = q.list();
1165    
1166                    if (list.size() == 2) {
1167                            return list.get(1);
1168                    }
1169                    else {
1170                            return null;
1171                    }
1172            }
1173    
1174            /**
1175             * Removes all the resource blocks where companyId = &#63; and groupId = &#63; and name = &#63; from the database.
1176             *
1177             * @param companyId the company ID
1178             * @param groupId the group ID
1179             * @param name the name
1180             * @throws SystemException if a system exception occurred
1181             */
1182            @Override
1183            public void removeByC_G_N(long companyId, long groupId, String name)
1184                    throws SystemException {
1185                    for (ResourceBlock resourceBlock : findByC_G_N(companyId, groupId,
1186                                    name, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1187                            remove(resourceBlock);
1188                    }
1189            }
1190    
1191            /**
1192             * Returns the number of resource blocks where companyId = &#63; and groupId = &#63; and name = &#63;.
1193             *
1194             * @param companyId the company ID
1195             * @param groupId the group ID
1196             * @param name the name
1197             * @return the number of matching resource blocks
1198             * @throws SystemException if a system exception occurred
1199             */
1200            @Override
1201            public int countByC_G_N(long companyId, long groupId, String name)
1202                    throws SystemException {
1203                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_G_N;
1204    
1205                    Object[] finderArgs = new Object[] { companyId, groupId, name };
1206    
1207                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1208                                    this);
1209    
1210                    if (count == null) {
1211                            StringBundler query = new StringBundler(4);
1212    
1213                            query.append(_SQL_COUNT_RESOURCEBLOCK_WHERE);
1214    
1215                            query.append(_FINDER_COLUMN_C_G_N_COMPANYID_2);
1216    
1217                            query.append(_FINDER_COLUMN_C_G_N_GROUPID_2);
1218    
1219                            boolean bindName = false;
1220    
1221                            if (name == null) {
1222                                    query.append(_FINDER_COLUMN_C_G_N_NAME_1);
1223                            }
1224                            else if (name.equals(StringPool.BLANK)) {
1225                                    query.append(_FINDER_COLUMN_C_G_N_NAME_3);
1226                            }
1227                            else {
1228                                    bindName = true;
1229    
1230                                    query.append(_FINDER_COLUMN_C_G_N_NAME_2);
1231                            }
1232    
1233                            String sql = query.toString();
1234    
1235                            Session session = null;
1236    
1237                            try {
1238                                    session = openSession();
1239    
1240                                    Query q = session.createQuery(sql);
1241    
1242                                    QueryPos qPos = QueryPos.getInstance(q);
1243    
1244                                    qPos.add(companyId);
1245    
1246                                    qPos.add(groupId);
1247    
1248                                    if (bindName) {
1249                                            qPos.add(name);
1250                                    }
1251    
1252                                    count = (Long)q.uniqueResult();
1253    
1254                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1255                            }
1256                            catch (Exception e) {
1257                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1258    
1259                                    throw processException(e);
1260                            }
1261                            finally {
1262                                    closeSession(session);
1263                            }
1264                    }
1265    
1266                    return count.intValue();
1267            }
1268    
1269            private static final String _FINDER_COLUMN_C_G_N_COMPANYID_2 = "resourceBlock.companyId = ? AND ";
1270            private static final String _FINDER_COLUMN_C_G_N_GROUPID_2 = "resourceBlock.groupId = ? AND ";
1271            private static final String _FINDER_COLUMN_C_G_N_NAME_1 = "resourceBlock.name IS NULL";
1272            private static final String _FINDER_COLUMN_C_G_N_NAME_2 = "resourceBlock.name = ?";
1273            private static final String _FINDER_COLUMN_C_G_N_NAME_3 = "(resourceBlock.name IS NULL OR resourceBlock.name = '')";
1274            public static final FinderPath FINDER_PATH_FETCH_BY_C_G_N_P = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1275                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED,
1276                            ResourceBlockImpl.class, FINDER_CLASS_NAME_ENTITY,
1277                            "fetchByC_G_N_P",
1278                            new String[] {
1279                                    Long.class.getName(), Long.class.getName(),
1280                                    String.class.getName(), String.class.getName()
1281                            },
1282                            ResourceBlockModelImpl.COMPANYID_COLUMN_BITMASK |
1283                            ResourceBlockModelImpl.GROUPID_COLUMN_BITMASK |
1284                            ResourceBlockModelImpl.NAME_COLUMN_BITMASK |
1285                            ResourceBlockModelImpl.PERMISSIONSHASH_COLUMN_BITMASK);
1286            public static final FinderPath FINDER_PATH_COUNT_BY_C_G_N_P = new FinderPath(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1287                            ResourceBlockModelImpl.FINDER_CACHE_ENABLED, Long.class,
1288                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_G_N_P",
1289                            new String[] {
1290                                    Long.class.getName(), Long.class.getName(),
1291                                    String.class.getName(), String.class.getName()
1292                            });
1293    
1294            /**
1295             * Returns the resource block where companyId = &#63; and groupId = &#63; and name = &#63; and permissionsHash = &#63; or throws a {@link com.liferay.portal.NoSuchResourceBlockException} if it could not be found.
1296             *
1297             * @param companyId the company ID
1298             * @param groupId the group ID
1299             * @param name the name
1300             * @param permissionsHash the permissions hash
1301             * @return the matching resource block
1302             * @throws com.liferay.portal.NoSuchResourceBlockException if a matching resource block could not be found
1303             * @throws SystemException if a system exception occurred
1304             */
1305            @Override
1306            public ResourceBlock findByC_G_N_P(long companyId, long groupId,
1307                    String name, String permissionsHash)
1308                    throws NoSuchResourceBlockException, SystemException {
1309                    ResourceBlock resourceBlock = fetchByC_G_N_P(companyId, groupId, name,
1310                                    permissionsHash);
1311    
1312                    if (resourceBlock == null) {
1313                            StringBundler msg = new StringBundler(10);
1314    
1315                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1316    
1317                            msg.append("companyId=");
1318                            msg.append(companyId);
1319    
1320                            msg.append(", groupId=");
1321                            msg.append(groupId);
1322    
1323                            msg.append(", name=");
1324                            msg.append(name);
1325    
1326                            msg.append(", permissionsHash=");
1327                            msg.append(permissionsHash);
1328    
1329                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1330    
1331                            if (_log.isWarnEnabled()) {
1332                                    _log.warn(msg.toString());
1333                            }
1334    
1335                            throw new NoSuchResourceBlockException(msg.toString());
1336                    }
1337    
1338                    return resourceBlock;
1339            }
1340    
1341            /**
1342             * Returns the resource block where companyId = &#63; and groupId = &#63; and name = &#63; and permissionsHash = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1343             *
1344             * @param companyId the company ID
1345             * @param groupId the group ID
1346             * @param name the name
1347             * @param permissionsHash the permissions hash
1348             * @return the matching resource block, or <code>null</code> if a matching resource block could not be found
1349             * @throws SystemException if a system exception occurred
1350             */
1351            @Override
1352            public ResourceBlock fetchByC_G_N_P(long companyId, long groupId,
1353                    String name, String permissionsHash) throws SystemException {
1354                    return fetchByC_G_N_P(companyId, groupId, name, permissionsHash, true);
1355            }
1356    
1357            /**
1358             * Returns the resource block where companyId = &#63; and groupId = &#63; and name = &#63; and permissionsHash = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1359             *
1360             * @param companyId the company ID
1361             * @param groupId the group ID
1362             * @param name the name
1363             * @param permissionsHash the permissions hash
1364             * @param retrieveFromCache whether to use the finder cache
1365             * @return the matching resource block, or <code>null</code> if a matching resource block could not be found
1366             * @throws SystemException if a system exception occurred
1367             */
1368            @Override
1369            public ResourceBlock fetchByC_G_N_P(long companyId, long groupId,
1370                    String name, String permissionsHash, boolean retrieveFromCache)
1371                    throws SystemException {
1372                    Object[] finderArgs = new Object[] {
1373                                    companyId, groupId, name, permissionsHash
1374                            };
1375    
1376                    Object result = null;
1377    
1378                    if (retrieveFromCache) {
1379                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_G_N_P,
1380                                            finderArgs, this);
1381                    }
1382    
1383                    if (result instanceof ResourceBlock) {
1384                            ResourceBlock resourceBlock = (ResourceBlock)result;
1385    
1386                            if ((companyId != resourceBlock.getCompanyId()) ||
1387                                            (groupId != resourceBlock.getGroupId()) ||
1388                                            !Validator.equals(name, resourceBlock.getName()) ||
1389                                            !Validator.equals(permissionsHash,
1390                                                    resourceBlock.getPermissionsHash())) {
1391                                    result = null;
1392                            }
1393                    }
1394    
1395                    if (result == null) {
1396                            StringBundler query = new StringBundler(6);
1397    
1398                            query.append(_SQL_SELECT_RESOURCEBLOCK_WHERE);
1399    
1400                            query.append(_FINDER_COLUMN_C_G_N_P_COMPANYID_2);
1401    
1402                            query.append(_FINDER_COLUMN_C_G_N_P_GROUPID_2);
1403    
1404                            boolean bindName = false;
1405    
1406                            if (name == null) {
1407                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_1);
1408                            }
1409                            else if (name.equals(StringPool.BLANK)) {
1410                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_3);
1411                            }
1412                            else {
1413                                    bindName = true;
1414    
1415                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_2);
1416                            }
1417    
1418                            boolean bindPermissionsHash = false;
1419    
1420                            if (permissionsHash == null) {
1421                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_1);
1422                            }
1423                            else if (permissionsHash.equals(StringPool.BLANK)) {
1424                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_3);
1425                            }
1426                            else {
1427                                    bindPermissionsHash = true;
1428    
1429                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_2);
1430                            }
1431    
1432                            String sql = query.toString();
1433    
1434                            Session session = null;
1435    
1436                            try {
1437                                    session = openSession();
1438    
1439                                    Query q = session.createQuery(sql);
1440    
1441                                    QueryPos qPos = QueryPos.getInstance(q);
1442    
1443                                    qPos.add(companyId);
1444    
1445                                    qPos.add(groupId);
1446    
1447                                    if (bindName) {
1448                                            qPos.add(name);
1449                                    }
1450    
1451                                    if (bindPermissionsHash) {
1452                                            qPos.add(permissionsHash);
1453                                    }
1454    
1455                                    List<ResourceBlock> list = q.list();
1456    
1457                                    if (list.isEmpty()) {
1458                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_G_N_P,
1459                                                    finderArgs, list);
1460                                    }
1461                                    else {
1462                                            ResourceBlock resourceBlock = list.get(0);
1463    
1464                                            result = resourceBlock;
1465    
1466                                            cacheResult(resourceBlock);
1467    
1468                                            if ((resourceBlock.getCompanyId() != companyId) ||
1469                                                            (resourceBlock.getGroupId() != groupId) ||
1470                                                            (resourceBlock.getName() == null) ||
1471                                                            !resourceBlock.getName().equals(name) ||
1472                                                            (resourceBlock.getPermissionsHash() == null) ||
1473                                                            !resourceBlock.getPermissionsHash()
1474                                                                                              .equals(permissionsHash)) {
1475                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_G_N_P,
1476                                                            finderArgs, resourceBlock);
1477                                            }
1478                                    }
1479                            }
1480                            catch (Exception e) {
1481                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_G_N_P,
1482                                            finderArgs);
1483    
1484                                    throw processException(e);
1485                            }
1486                            finally {
1487                                    closeSession(session);
1488                            }
1489                    }
1490    
1491                    if (result instanceof List<?>) {
1492                            return null;
1493                    }
1494                    else {
1495                            return (ResourceBlock)result;
1496                    }
1497            }
1498    
1499            /**
1500             * Removes the resource block where companyId = &#63; and groupId = &#63; and name = &#63; and permissionsHash = &#63; from the database.
1501             *
1502             * @param companyId the company ID
1503             * @param groupId the group ID
1504             * @param name the name
1505             * @param permissionsHash the permissions hash
1506             * @return the resource block that was removed
1507             * @throws SystemException if a system exception occurred
1508             */
1509            @Override
1510            public ResourceBlock removeByC_G_N_P(long companyId, long groupId,
1511                    String name, String permissionsHash)
1512                    throws NoSuchResourceBlockException, SystemException {
1513                    ResourceBlock resourceBlock = findByC_G_N_P(companyId, groupId, name,
1514                                    permissionsHash);
1515    
1516                    return remove(resourceBlock);
1517            }
1518    
1519            /**
1520             * Returns the number of resource blocks where companyId = &#63; and groupId = &#63; and name = &#63; and permissionsHash = &#63;.
1521             *
1522             * @param companyId the company ID
1523             * @param groupId the group ID
1524             * @param name the name
1525             * @param permissionsHash the permissions hash
1526             * @return the number of matching resource blocks
1527             * @throws SystemException if a system exception occurred
1528             */
1529            @Override
1530            public int countByC_G_N_P(long companyId, long groupId, String name,
1531                    String permissionsHash) throws SystemException {
1532                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_G_N_P;
1533    
1534                    Object[] finderArgs = new Object[] {
1535                                    companyId, groupId, name, permissionsHash
1536                            };
1537    
1538                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1539                                    this);
1540    
1541                    if (count == null) {
1542                            StringBundler query = new StringBundler(5);
1543    
1544                            query.append(_SQL_COUNT_RESOURCEBLOCK_WHERE);
1545    
1546                            query.append(_FINDER_COLUMN_C_G_N_P_COMPANYID_2);
1547    
1548                            query.append(_FINDER_COLUMN_C_G_N_P_GROUPID_2);
1549    
1550                            boolean bindName = false;
1551    
1552                            if (name == null) {
1553                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_1);
1554                            }
1555                            else if (name.equals(StringPool.BLANK)) {
1556                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_3);
1557                            }
1558                            else {
1559                                    bindName = true;
1560    
1561                                    query.append(_FINDER_COLUMN_C_G_N_P_NAME_2);
1562                            }
1563    
1564                            boolean bindPermissionsHash = false;
1565    
1566                            if (permissionsHash == null) {
1567                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_1);
1568                            }
1569                            else if (permissionsHash.equals(StringPool.BLANK)) {
1570                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_3);
1571                            }
1572                            else {
1573                                    bindPermissionsHash = true;
1574    
1575                                    query.append(_FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_2);
1576                            }
1577    
1578                            String sql = query.toString();
1579    
1580                            Session session = null;
1581    
1582                            try {
1583                                    session = openSession();
1584    
1585                                    Query q = session.createQuery(sql);
1586    
1587                                    QueryPos qPos = QueryPos.getInstance(q);
1588    
1589                                    qPos.add(companyId);
1590    
1591                                    qPos.add(groupId);
1592    
1593                                    if (bindName) {
1594                                            qPos.add(name);
1595                                    }
1596    
1597                                    if (bindPermissionsHash) {
1598                                            qPos.add(permissionsHash);
1599                                    }
1600    
1601                                    count = (Long)q.uniqueResult();
1602    
1603                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1604                            }
1605                            catch (Exception e) {
1606                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1607    
1608                                    throw processException(e);
1609                            }
1610                            finally {
1611                                    closeSession(session);
1612                            }
1613                    }
1614    
1615                    return count.intValue();
1616            }
1617    
1618            private static final String _FINDER_COLUMN_C_G_N_P_COMPANYID_2 = "resourceBlock.companyId = ? AND ";
1619            private static final String _FINDER_COLUMN_C_G_N_P_GROUPID_2 = "resourceBlock.groupId = ? AND ";
1620            private static final String _FINDER_COLUMN_C_G_N_P_NAME_1 = "resourceBlock.name IS NULL AND ";
1621            private static final String _FINDER_COLUMN_C_G_N_P_NAME_2 = "resourceBlock.name = ? AND ";
1622            private static final String _FINDER_COLUMN_C_G_N_P_NAME_3 = "(resourceBlock.name IS NULL OR resourceBlock.name = '') AND ";
1623            private static final String _FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_1 = "resourceBlock.permissionsHash IS NULL";
1624            private static final String _FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_2 = "resourceBlock.permissionsHash = ?";
1625            private static final String _FINDER_COLUMN_C_G_N_P_PERMISSIONSHASH_3 = "(resourceBlock.permissionsHash IS NULL OR resourceBlock.permissionsHash = '')";
1626    
1627            public ResourceBlockPersistenceImpl() {
1628                    setModelClass(ResourceBlock.class);
1629            }
1630    
1631            /**
1632             * Caches the resource block in the entity cache if it is enabled.
1633             *
1634             * @param resourceBlock the resource block
1635             */
1636            @Override
1637            public void cacheResult(ResourceBlock resourceBlock) {
1638                    EntityCacheUtil.putResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1639                            ResourceBlockImpl.class, resourceBlock.getPrimaryKey(),
1640                            resourceBlock);
1641    
1642                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_G_N_P,
1643                            new Object[] {
1644                                    resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
1645                                    resourceBlock.getName(), resourceBlock.getPermissionsHash()
1646                            }, resourceBlock);
1647    
1648                    resourceBlock.resetOriginalValues();
1649            }
1650    
1651            /**
1652             * Caches the resource blocks in the entity cache if it is enabled.
1653             *
1654             * @param resourceBlocks the resource blocks
1655             */
1656            @Override
1657            public void cacheResult(List<ResourceBlock> resourceBlocks) {
1658                    for (ResourceBlock resourceBlock : resourceBlocks) {
1659                            if (EntityCacheUtil.getResult(
1660                                                    ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1661                                                    ResourceBlockImpl.class, resourceBlock.getPrimaryKey()) == null) {
1662                                    cacheResult(resourceBlock);
1663                            }
1664                            else {
1665                                    resourceBlock.resetOriginalValues();
1666                            }
1667                    }
1668            }
1669    
1670            /**
1671             * Clears the cache for all resource blocks.
1672             *
1673             * <p>
1674             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1675             * </p>
1676             */
1677            @Override
1678            public void clearCache() {
1679                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1680                            CacheRegistryUtil.clear(ResourceBlockImpl.class.getName());
1681                    }
1682    
1683                    EntityCacheUtil.clearCache(ResourceBlockImpl.class.getName());
1684    
1685                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1686                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1687                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1688            }
1689    
1690            /**
1691             * Clears the cache for the resource block.
1692             *
1693             * <p>
1694             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1695             * </p>
1696             */
1697            @Override
1698            public void clearCache(ResourceBlock resourceBlock) {
1699                    EntityCacheUtil.removeResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1700                            ResourceBlockImpl.class, resourceBlock.getPrimaryKey());
1701    
1702                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1703                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1704    
1705                    clearUniqueFindersCache(resourceBlock);
1706            }
1707    
1708            @Override
1709            public void clearCache(List<ResourceBlock> resourceBlocks) {
1710                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1711                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1712    
1713                    for (ResourceBlock resourceBlock : resourceBlocks) {
1714                            EntityCacheUtil.removeResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1715                                    ResourceBlockImpl.class, resourceBlock.getPrimaryKey());
1716    
1717                            clearUniqueFindersCache(resourceBlock);
1718                    }
1719            }
1720    
1721            protected void cacheUniqueFindersCache(ResourceBlock resourceBlock) {
1722                    if (resourceBlock.isNew()) {
1723                            Object[] args = new Object[] {
1724                                            resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
1725                                            resourceBlock.getName(), resourceBlock.getPermissionsHash()
1726                                    };
1727    
1728                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_G_N_P, args,
1729                                    Long.valueOf(1));
1730                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_G_N_P, args,
1731                                    resourceBlock);
1732                    }
1733                    else {
1734                            ResourceBlockModelImpl resourceBlockModelImpl = (ResourceBlockModelImpl)resourceBlock;
1735    
1736                            if ((resourceBlockModelImpl.getColumnBitmask() &
1737                                            FINDER_PATH_FETCH_BY_C_G_N_P.getColumnBitmask()) != 0) {
1738                                    Object[] args = new Object[] {
1739                                                    resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
1740                                                    resourceBlock.getName(),
1741                                                    resourceBlock.getPermissionsHash()
1742                                            };
1743    
1744                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_G_N_P, args,
1745                                            Long.valueOf(1));
1746                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_G_N_P, args,
1747                                            resourceBlock);
1748                            }
1749                    }
1750            }
1751    
1752            protected void clearUniqueFindersCache(ResourceBlock resourceBlock) {
1753                    ResourceBlockModelImpl resourceBlockModelImpl = (ResourceBlockModelImpl)resourceBlock;
1754    
1755                    Object[] args = new Object[] {
1756                                    resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
1757                                    resourceBlock.getName(), resourceBlock.getPermissionsHash()
1758                            };
1759    
1760                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_G_N_P, args);
1761                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_G_N_P, args);
1762    
1763                    if ((resourceBlockModelImpl.getColumnBitmask() &
1764                                    FINDER_PATH_FETCH_BY_C_G_N_P.getColumnBitmask()) != 0) {
1765                            args = new Object[] {
1766                                            resourceBlockModelImpl.getOriginalCompanyId(),
1767                                            resourceBlockModelImpl.getOriginalGroupId(),
1768                                            resourceBlockModelImpl.getOriginalName(),
1769                                            resourceBlockModelImpl.getOriginalPermissionsHash()
1770                                    };
1771    
1772                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_G_N_P, args);
1773                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_G_N_P, args);
1774                    }
1775            }
1776    
1777            /**
1778             * Creates a new resource block with the primary key. Does not add the resource block to the database.
1779             *
1780             * @param resourceBlockId the primary key for the new resource block
1781             * @return the new resource block
1782             */
1783            @Override
1784            public ResourceBlock create(long resourceBlockId) {
1785                    ResourceBlock resourceBlock = new ResourceBlockImpl();
1786    
1787                    resourceBlock.setNew(true);
1788                    resourceBlock.setPrimaryKey(resourceBlockId);
1789    
1790                    return resourceBlock;
1791            }
1792    
1793            /**
1794             * Removes the resource block with the primary key from the database. Also notifies the appropriate model listeners.
1795             *
1796             * @param resourceBlockId the primary key of the resource block
1797             * @return the resource block that was removed
1798             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
1799             * @throws SystemException if a system exception occurred
1800             */
1801            @Override
1802            public ResourceBlock remove(long resourceBlockId)
1803                    throws NoSuchResourceBlockException, SystemException {
1804                    return remove((Serializable)resourceBlockId);
1805            }
1806    
1807            /**
1808             * Removes the resource block with the primary key from the database. Also notifies the appropriate model listeners.
1809             *
1810             * @param primaryKey the primary key of the resource block
1811             * @return the resource block that was removed
1812             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
1813             * @throws SystemException if a system exception occurred
1814             */
1815            @Override
1816            public ResourceBlock remove(Serializable primaryKey)
1817                    throws NoSuchResourceBlockException, SystemException {
1818                    Session session = null;
1819    
1820                    try {
1821                            session = openSession();
1822    
1823                            ResourceBlock resourceBlock = (ResourceBlock)session.get(ResourceBlockImpl.class,
1824                                            primaryKey);
1825    
1826                            if (resourceBlock == null) {
1827                                    if (_log.isWarnEnabled()) {
1828                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1829                                    }
1830    
1831                                    throw new NoSuchResourceBlockException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1832                                            primaryKey);
1833                            }
1834    
1835                            return remove(resourceBlock);
1836                    }
1837                    catch (NoSuchResourceBlockException nsee) {
1838                            throw nsee;
1839                    }
1840                    catch (Exception e) {
1841                            throw processException(e);
1842                    }
1843                    finally {
1844                            closeSession(session);
1845                    }
1846            }
1847    
1848            @Override
1849            protected ResourceBlock removeImpl(ResourceBlock resourceBlock)
1850                    throws SystemException {
1851                    resourceBlock = toUnwrappedModel(resourceBlock);
1852    
1853                    Session session = null;
1854    
1855                    try {
1856                            session = openSession();
1857    
1858                            if (!session.contains(resourceBlock)) {
1859                                    resourceBlock = (ResourceBlock)session.get(ResourceBlockImpl.class,
1860                                                    resourceBlock.getPrimaryKeyObj());
1861                            }
1862    
1863                            if (resourceBlock != null) {
1864                                    session.delete(resourceBlock);
1865                            }
1866                    }
1867                    catch (Exception e) {
1868                            throw processException(e);
1869                    }
1870                    finally {
1871                            closeSession(session);
1872                    }
1873    
1874                    if (resourceBlock != null) {
1875                            clearCache(resourceBlock);
1876                    }
1877    
1878                    return resourceBlock;
1879            }
1880    
1881            @Override
1882            public ResourceBlock updateImpl(
1883                    com.liferay.portal.model.ResourceBlock resourceBlock)
1884                    throws SystemException {
1885                    resourceBlock = toUnwrappedModel(resourceBlock);
1886    
1887                    boolean isNew = resourceBlock.isNew();
1888    
1889                    ResourceBlockModelImpl resourceBlockModelImpl = (ResourceBlockModelImpl)resourceBlock;
1890    
1891                    Session session = null;
1892    
1893                    try {
1894                            session = openSession();
1895    
1896                            if (resourceBlock.isNew()) {
1897                                    session.save(resourceBlock);
1898    
1899                                    resourceBlock.setNew(false);
1900                            }
1901                            else {
1902                                    session.merge(resourceBlock);
1903                            }
1904                    }
1905                    catch (Exception e) {
1906                            throw processException(e);
1907                    }
1908                    finally {
1909                            closeSession(session);
1910                    }
1911    
1912                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1913    
1914                    if (isNew || !ResourceBlockModelImpl.COLUMN_BITMASK_ENABLED) {
1915                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1916                    }
1917    
1918                    else {
1919                            if ((resourceBlockModelImpl.getColumnBitmask() &
1920                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N.getColumnBitmask()) != 0) {
1921                                    Object[] args = new Object[] {
1922                                                    resourceBlockModelImpl.getOriginalCompanyId(),
1923                                                    resourceBlockModelImpl.getOriginalName()
1924                                            };
1925    
1926                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N, args);
1927                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N,
1928                                            args);
1929    
1930                                    args = new Object[] {
1931                                                    resourceBlockModelImpl.getCompanyId(),
1932                                                    resourceBlockModelImpl.getName()
1933                                            };
1934    
1935                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N, args);
1936                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N,
1937                                            args);
1938                            }
1939    
1940                            if ((resourceBlockModelImpl.getColumnBitmask() &
1941                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G_N.getColumnBitmask()) != 0) {
1942                                    Object[] args = new Object[] {
1943                                                    resourceBlockModelImpl.getOriginalCompanyId(),
1944                                                    resourceBlockModelImpl.getOriginalGroupId(),
1945                                                    resourceBlockModelImpl.getOriginalName()
1946                                            };
1947    
1948                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_G_N, args);
1949                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G_N,
1950                                            args);
1951    
1952                                    args = new Object[] {
1953                                                    resourceBlockModelImpl.getCompanyId(),
1954                                                    resourceBlockModelImpl.getGroupId(),
1955                                                    resourceBlockModelImpl.getName()
1956                                            };
1957    
1958                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_G_N, args);
1959                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_G_N,
1960                                            args);
1961                            }
1962                    }
1963    
1964                    EntityCacheUtil.putResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
1965                            ResourceBlockImpl.class, resourceBlock.getPrimaryKey(),
1966                            resourceBlock);
1967    
1968                    clearUniqueFindersCache(resourceBlock);
1969                    cacheUniqueFindersCache(resourceBlock);
1970    
1971                    return resourceBlock;
1972            }
1973    
1974            protected ResourceBlock toUnwrappedModel(ResourceBlock resourceBlock) {
1975                    if (resourceBlock instanceof ResourceBlockImpl) {
1976                            return resourceBlock;
1977                    }
1978    
1979                    ResourceBlockImpl resourceBlockImpl = new ResourceBlockImpl();
1980    
1981                    resourceBlockImpl.setNew(resourceBlock.isNew());
1982                    resourceBlockImpl.setPrimaryKey(resourceBlock.getPrimaryKey());
1983    
1984                    resourceBlockImpl.setResourceBlockId(resourceBlock.getResourceBlockId());
1985                    resourceBlockImpl.setCompanyId(resourceBlock.getCompanyId());
1986                    resourceBlockImpl.setGroupId(resourceBlock.getGroupId());
1987                    resourceBlockImpl.setName(resourceBlock.getName());
1988                    resourceBlockImpl.setPermissionsHash(resourceBlock.getPermissionsHash());
1989                    resourceBlockImpl.setReferenceCount(resourceBlock.getReferenceCount());
1990    
1991                    return resourceBlockImpl;
1992            }
1993    
1994            /**
1995             * Returns the resource block with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1996             *
1997             * @param primaryKey the primary key of the resource block
1998             * @return the resource block
1999             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
2000             * @throws SystemException if a system exception occurred
2001             */
2002            @Override
2003            public ResourceBlock findByPrimaryKey(Serializable primaryKey)
2004                    throws NoSuchResourceBlockException, SystemException {
2005                    ResourceBlock resourceBlock = fetchByPrimaryKey(primaryKey);
2006    
2007                    if (resourceBlock == null) {
2008                            if (_log.isWarnEnabled()) {
2009                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2010                            }
2011    
2012                            throw new NoSuchResourceBlockException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2013                                    primaryKey);
2014                    }
2015    
2016                    return resourceBlock;
2017            }
2018    
2019            /**
2020             * Returns the resource block with the primary key or throws a {@link com.liferay.portal.NoSuchResourceBlockException} if it could not be found.
2021             *
2022             * @param resourceBlockId the primary key of the resource block
2023             * @return the resource block
2024             * @throws com.liferay.portal.NoSuchResourceBlockException if a resource block with the primary key could not be found
2025             * @throws SystemException if a system exception occurred
2026             */
2027            @Override
2028            public ResourceBlock findByPrimaryKey(long resourceBlockId)
2029                    throws NoSuchResourceBlockException, SystemException {
2030                    return findByPrimaryKey((Serializable)resourceBlockId);
2031            }
2032    
2033            /**
2034             * Returns the resource block with the primary key or returns <code>null</code> if it could not be found.
2035             *
2036             * @param primaryKey the primary key of the resource block
2037             * @return the resource block, or <code>null</code> if a resource block with the primary key could not be found
2038             * @throws SystemException if a system exception occurred
2039             */
2040            @Override
2041            public ResourceBlock fetchByPrimaryKey(Serializable primaryKey)
2042                    throws SystemException {
2043                    ResourceBlock resourceBlock = (ResourceBlock)EntityCacheUtil.getResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
2044                                    ResourceBlockImpl.class, primaryKey);
2045    
2046                    if (resourceBlock == _nullResourceBlock) {
2047                            return null;
2048                    }
2049    
2050                    if (resourceBlock == null) {
2051                            Session session = null;
2052    
2053                            try {
2054                                    session = openSession();
2055    
2056                                    resourceBlock = (ResourceBlock)session.get(ResourceBlockImpl.class,
2057                                                    primaryKey);
2058    
2059                                    if (resourceBlock != null) {
2060                                            cacheResult(resourceBlock);
2061                                    }
2062                                    else {
2063                                            EntityCacheUtil.putResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
2064                                                    ResourceBlockImpl.class, primaryKey, _nullResourceBlock);
2065                                    }
2066                            }
2067                            catch (Exception e) {
2068                                    EntityCacheUtil.removeResult(ResourceBlockModelImpl.ENTITY_CACHE_ENABLED,
2069                                            ResourceBlockImpl.class, primaryKey);
2070    
2071                                    throw processException(e);
2072                            }
2073                            finally {
2074                                    closeSession(session);
2075                            }
2076                    }
2077    
2078                    return resourceBlock;
2079            }
2080    
2081            /**
2082             * Returns the resource block with the primary key or returns <code>null</code> if it could not be found.
2083             *
2084             * @param resourceBlockId the primary key of the resource block
2085             * @return the resource block, or <code>null</code> if a resource block with the primary key could not be found
2086             * @throws SystemException if a system exception occurred
2087             */
2088            @Override
2089            public ResourceBlock fetchByPrimaryKey(long resourceBlockId)
2090                    throws SystemException {
2091                    return fetchByPrimaryKey((Serializable)resourceBlockId);
2092            }
2093    
2094            /**
2095             * Returns all the resource blocks.
2096             *
2097             * @return the resource blocks
2098             * @throws SystemException if a system exception occurred
2099             */
2100            @Override
2101            public List<ResourceBlock> findAll() throws SystemException {
2102                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2103            }
2104    
2105            /**
2106             * Returns a range of all the resource blocks.
2107             *
2108             * <p>
2109             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
2110             * </p>
2111             *
2112             * @param start the lower bound of the range of resource blocks
2113             * @param end the upper bound of the range of resource blocks (not inclusive)
2114             * @return the range of resource blocks
2115             * @throws SystemException if a system exception occurred
2116             */
2117            @Override
2118            public List<ResourceBlock> findAll(int start, int end)
2119                    throws SystemException {
2120                    return findAll(start, end, null);
2121            }
2122    
2123            /**
2124             * Returns an ordered range of all the resource blocks.
2125             *
2126             * <p>
2127             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourceBlockModelImpl}. 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.
2128             * </p>
2129             *
2130             * @param start the lower bound of the range of resource blocks
2131             * @param end the upper bound of the range of resource blocks (not inclusive)
2132             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2133             * @return the ordered range of resource blocks
2134             * @throws SystemException if a system exception occurred
2135             */
2136            @Override
2137            public List<ResourceBlock> findAll(int start, int end,
2138                    OrderByComparator orderByComparator) throws SystemException {
2139                    boolean pagination = true;
2140                    FinderPath finderPath = null;
2141                    Object[] finderArgs = null;
2142    
2143                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2144                                    (orderByComparator == null)) {
2145                            pagination = false;
2146                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
2147                            finderArgs = FINDER_ARGS_EMPTY;
2148                    }
2149                    else {
2150                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
2151                            finderArgs = new Object[] { start, end, orderByComparator };
2152                    }
2153    
2154                    List<ResourceBlock> list = (List<ResourceBlock>)FinderCacheUtil.getResult(finderPath,
2155                                    finderArgs, this);
2156    
2157                    if (list == null) {
2158                            StringBundler query = null;
2159                            String sql = null;
2160    
2161                            if (orderByComparator != null) {
2162                                    query = new StringBundler(2 +
2163                                                    (orderByComparator.getOrderByFields().length * 3));
2164    
2165                                    query.append(_SQL_SELECT_RESOURCEBLOCK);
2166    
2167                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2168                                            orderByComparator);
2169    
2170                                    sql = query.toString();
2171                            }
2172                            else {
2173                                    sql = _SQL_SELECT_RESOURCEBLOCK;
2174    
2175                                    if (pagination) {
2176                                            sql = sql.concat(ResourceBlockModelImpl.ORDER_BY_JPQL);
2177                                    }
2178                            }
2179    
2180                            Session session = null;
2181    
2182                            try {
2183                                    session = openSession();
2184    
2185                                    Query q = session.createQuery(sql);
2186    
2187                                    if (!pagination) {
2188                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
2189                                                            start, end, false);
2190    
2191                                            Collections.sort(list);
2192    
2193                                            list = new UnmodifiableList<ResourceBlock>(list);
2194                                    }
2195                                    else {
2196                                            list = (List<ResourceBlock>)QueryUtil.list(q, getDialect(),
2197                                                            start, end);
2198                                    }
2199    
2200                                    cacheResult(list);
2201    
2202                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2203                            }
2204                            catch (Exception e) {
2205                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2206    
2207                                    throw processException(e);
2208                            }
2209                            finally {
2210                                    closeSession(session);
2211                            }
2212                    }
2213    
2214                    return list;
2215            }
2216    
2217            /**
2218             * Removes all the resource blocks from the database.
2219             *
2220             * @throws SystemException if a system exception occurred
2221             */
2222            @Override
2223            public void removeAll() throws SystemException {
2224                    for (ResourceBlock resourceBlock : findAll()) {
2225                            remove(resourceBlock);
2226                    }
2227            }
2228    
2229            /**
2230             * Returns the number of resource blocks.
2231             *
2232             * @return the number of resource blocks
2233             * @throws SystemException if a system exception occurred
2234             */
2235            @Override
2236            public int countAll() throws SystemException {
2237                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2238                                    FINDER_ARGS_EMPTY, this);
2239    
2240                    if (count == null) {
2241                            Session session = null;
2242    
2243                            try {
2244                                    session = openSession();
2245    
2246                                    Query q = session.createQuery(_SQL_COUNT_RESOURCEBLOCK);
2247    
2248                                    count = (Long)q.uniqueResult();
2249    
2250                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
2251                                            FINDER_ARGS_EMPTY, count);
2252                            }
2253                            catch (Exception e) {
2254                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
2255                                            FINDER_ARGS_EMPTY);
2256    
2257                                    throw processException(e);
2258                            }
2259                            finally {
2260                                    closeSession(session);
2261                            }
2262                    }
2263    
2264                    return count.intValue();
2265            }
2266    
2267            /**
2268             * Initializes the resource block persistence.
2269             */
2270            public void afterPropertiesSet() {
2271                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2272                                            com.liferay.portal.util.PropsUtil.get(
2273                                                    "value.object.listener.com.liferay.portal.model.ResourceBlock")));
2274    
2275                    if (listenerClassNames.length > 0) {
2276                            try {
2277                                    List<ModelListener<ResourceBlock>> listenersList = new ArrayList<ModelListener<ResourceBlock>>();
2278    
2279                                    for (String listenerClassName : listenerClassNames) {
2280                                            listenersList.add((ModelListener<ResourceBlock>)InstanceFactory.newInstance(
2281                                                            getClassLoader(), listenerClassName));
2282                                    }
2283    
2284                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2285                            }
2286                            catch (Exception e) {
2287                                    _log.error(e);
2288                            }
2289                    }
2290            }
2291    
2292            public void destroy() {
2293                    EntityCacheUtil.removeCache(ResourceBlockImpl.class.getName());
2294                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2295                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2296                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2297            }
2298    
2299            private static final String _SQL_SELECT_RESOURCEBLOCK = "SELECT resourceBlock FROM ResourceBlock resourceBlock";
2300            private static final String _SQL_SELECT_RESOURCEBLOCK_WHERE = "SELECT resourceBlock FROM ResourceBlock resourceBlock WHERE ";
2301            private static final String _SQL_COUNT_RESOURCEBLOCK = "SELECT COUNT(resourceBlock) FROM ResourceBlock resourceBlock";
2302            private static final String _SQL_COUNT_RESOURCEBLOCK_WHERE = "SELECT COUNT(resourceBlock) FROM ResourceBlock resourceBlock WHERE ";
2303            private static final String _ORDER_BY_ENTITY_ALIAS = "resourceBlock.";
2304            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ResourceBlock exists with the primary key ";
2305            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ResourceBlock exists with the key {";
2306            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
2307            private static Log _log = LogFactoryUtil.getLog(ResourceBlockPersistenceImpl.class);
2308            private static ResourceBlock _nullResourceBlock = new ResourceBlockImpl() {
2309                            @Override
2310                            public Object clone() {
2311                                    return this;
2312                            }
2313    
2314                            @Override
2315                            public CacheModel<ResourceBlock> toCacheModel() {
2316                                    return _nullResourceBlockCacheModel;
2317                            }
2318                    };
2319    
2320            private static CacheModel<ResourceBlock> _nullResourceBlockCacheModel = new CacheModel<ResourceBlock>() {
2321                            @Override
2322                            public ResourceBlock toEntityModel() {
2323                                    return _nullResourceBlock;
2324                            }
2325                    };
2326    }