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