001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatamapping.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.SQLQuery;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.UnmodifiableList;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
039    import com.liferay.portal.model.CacheModel;
040    import com.liferay.portal.model.ModelListener;
041    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
042    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043    
044    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
045    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
046    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMTemplateImpl;
047    import com.liferay.portlet.dynamicdatamapping.model.impl.DDMTemplateModelImpl;
048    
049    import java.io.Serializable;
050    
051    import java.util.ArrayList;
052    import java.util.Collections;
053    import java.util.List;
054    
055    /**
056     * The persistence implementation for the d d m template service.
057     *
058     * <p>
059     * Caching information and settings can be found in <code>portal.properties</code>
060     * </p>
061     *
062     * @author Brian Wing Shun Chan
063     * @see DDMTemplatePersistence
064     * @see DDMTemplateUtil
065     * @generated
066     */
067    public class DDMTemplatePersistenceImpl extends BasePersistenceImpl<DDMTemplate>
068            implements DDMTemplatePersistence {
069            /*
070             * NOTE FOR DEVELOPERS:
071             *
072             * Never modify or reference this class directly. Always use {@link DDMTemplateUtil} to access the d d m template persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
073             */
074            public static final String FINDER_CLASS_NAME_ENTITY = DDMTemplateImpl.class.getName();
075            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076                    ".List1";
077            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
078                    ".List2";
079            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
080                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
083                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
086                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
087                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
088            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
089                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
090                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
091                            new String[] {
092                                    String.class.getName(),
093                                    
094                            Integer.class.getName(), Integer.class.getName(),
095                                    OrderByComparator.class.getName()
096                            });
097            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
098                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
100                            new String[] { String.class.getName() },
101                            DDMTemplateModelImpl.UUID_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
103                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
105                            new String[] { String.class.getName() });
106    
107            /**
108             * Returns all the d d m templates where uuid = &#63;.
109             *
110             * @param uuid the uuid
111             * @return the matching d d m templates
112             * @throws SystemException if a system exception occurred
113             */
114            public List<DDMTemplate> findByUuid(String uuid) throws SystemException {
115                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
116            }
117    
118            /**
119             * Returns a range of all the d d m templates where uuid = &#63;.
120             *
121             * <p>
122             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
123             * </p>
124             *
125             * @param uuid the uuid
126             * @param start the lower bound of the range of d d m templates
127             * @param end the upper bound of the range of d d m templates (not inclusive)
128             * @return the range of matching d d m templates
129             * @throws SystemException if a system exception occurred
130             */
131            public List<DDMTemplate> findByUuid(String uuid, int start, int end)
132                    throws SystemException {
133                    return findByUuid(uuid, start, end, null);
134            }
135    
136            /**
137             * Returns an ordered range of all the d d m templates where uuid = &#63;.
138             *
139             * <p>
140             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
141             * </p>
142             *
143             * @param uuid the uuid
144             * @param start the lower bound of the range of d d m templates
145             * @param end the upper bound of the range of d d m templates (not inclusive)
146             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
147             * @return the ordered range of matching d d m templates
148             * @throws SystemException if a system exception occurred
149             */
150            public List<DDMTemplate> findByUuid(String uuid, int start, int end,
151                    OrderByComparator orderByComparator) throws SystemException {
152                    boolean pagination = true;
153                    FinderPath finderPath = null;
154                    Object[] finderArgs = null;
155    
156                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
157                                    (orderByComparator == null)) {
158                            pagination = false;
159                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
160                            finderArgs = new Object[] { uuid };
161                    }
162                    else {
163                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
164                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
165                    }
166    
167                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
168                                    finderArgs, this);
169    
170                    if ((list != null) && !list.isEmpty()) {
171                            for (DDMTemplate ddmTemplate : list) {
172                                    if (!Validator.equals(uuid, ddmTemplate.getUuid())) {
173                                            list = null;
174    
175                                            break;
176                                    }
177                            }
178                    }
179    
180                    if (list == null) {
181                            StringBundler query = null;
182    
183                            if (orderByComparator != null) {
184                                    query = new StringBundler(3 +
185                                                    (orderByComparator.getOrderByFields().length * 3));
186                            }
187                            else {
188                                    query = new StringBundler(3);
189                            }
190    
191                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
192    
193                            if (uuid == null) {
194                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
195                            }
196                            else {
197                                    if (uuid.equals(StringPool.BLANK)) {
198                                            query.append(_FINDER_COLUMN_UUID_UUID_3);
199                                    }
200                                    else {
201                                            query.append(_FINDER_COLUMN_UUID_UUID_2);
202                                    }
203                            }
204    
205                            if (orderByComparator != null) {
206                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
207                                            orderByComparator);
208                            }
209                            else
210                             if (pagination) {
211                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
212                            }
213    
214                            String sql = query.toString();
215    
216                            Session session = null;
217    
218                            try {
219                                    session = openSession();
220    
221                                    Query q = session.createQuery(sql);
222    
223                                    QueryPos qPos = QueryPos.getInstance(q);
224    
225                                    if (uuid != null) {
226                                            qPos.add(uuid);
227                                    }
228    
229                                    if (!pagination) {
230                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
231                                                            start, end, false);
232    
233                                            Collections.sort(list);
234    
235                                            list = new UnmodifiableList<DDMTemplate>(list);
236                                    }
237                                    else {
238                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
239                                                            start, end);
240                                    }
241    
242                                    cacheResult(list);
243    
244                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
245                            }
246                            catch (Exception e) {
247                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
248    
249                                    throw processException(e);
250                            }
251                            finally {
252                                    closeSession(session);
253                            }
254                    }
255    
256                    return list;
257            }
258    
259            /**
260             * Returns the first d d m template in the ordered set where uuid = &#63;.
261             *
262             * @param uuid the uuid
263             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
264             * @return the first matching d d m template
265             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
266             * @throws SystemException if a system exception occurred
267             */
268            public DDMTemplate findByUuid_First(String uuid,
269                    OrderByComparator orderByComparator)
270                    throws NoSuchTemplateException, SystemException {
271                    DDMTemplate ddmTemplate = fetchByUuid_First(uuid, orderByComparator);
272    
273                    if (ddmTemplate != null) {
274                            return ddmTemplate;
275                    }
276    
277                    StringBundler msg = new StringBundler(4);
278    
279                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
280    
281                    msg.append("uuid=");
282                    msg.append(uuid);
283    
284                    msg.append(StringPool.CLOSE_CURLY_BRACE);
285    
286                    throw new NoSuchTemplateException(msg.toString());
287            }
288    
289            /**
290             * Returns the first d d m template in the ordered set where uuid = &#63;.
291             *
292             * @param uuid the uuid
293             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
294             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
295             * @throws SystemException if a system exception occurred
296             */
297            public DDMTemplate fetchByUuid_First(String uuid,
298                    OrderByComparator orderByComparator) throws SystemException {
299                    List<DDMTemplate> list = findByUuid(uuid, 0, 1, orderByComparator);
300    
301                    if (!list.isEmpty()) {
302                            return list.get(0);
303                    }
304    
305                    return null;
306            }
307    
308            /**
309             * Returns the last d d m template in the ordered set where uuid = &#63;.
310             *
311             * @param uuid the uuid
312             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
313             * @return the last matching d d m template
314             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
315             * @throws SystemException if a system exception occurred
316             */
317            public DDMTemplate findByUuid_Last(String uuid,
318                    OrderByComparator orderByComparator)
319                    throws NoSuchTemplateException, SystemException {
320                    DDMTemplate ddmTemplate = fetchByUuid_Last(uuid, orderByComparator);
321    
322                    if (ddmTemplate != null) {
323                            return ddmTemplate;
324                    }
325    
326                    StringBundler msg = new StringBundler(4);
327    
328                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
329    
330                    msg.append("uuid=");
331                    msg.append(uuid);
332    
333                    msg.append(StringPool.CLOSE_CURLY_BRACE);
334    
335                    throw new NoSuchTemplateException(msg.toString());
336            }
337    
338            /**
339             * Returns the last d d m template in the ordered set where uuid = &#63;.
340             *
341             * @param uuid the uuid
342             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
343             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
344             * @throws SystemException if a system exception occurred
345             */
346            public DDMTemplate fetchByUuid_Last(String uuid,
347                    OrderByComparator orderByComparator) throws SystemException {
348                    int count = countByUuid(uuid);
349    
350                    List<DDMTemplate> list = findByUuid(uuid, count - 1, count,
351                                    orderByComparator);
352    
353                    if (!list.isEmpty()) {
354                            return list.get(0);
355                    }
356    
357                    return null;
358            }
359    
360            /**
361             * Returns the d d m templates before and after the current d d m template in the ordered set where uuid = &#63;.
362             *
363             * @param templateId the primary key of the current d d m template
364             * @param uuid the uuid
365             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
366             * @return the previous, current, and next d d m template
367             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
368             * @throws SystemException if a system exception occurred
369             */
370            public DDMTemplate[] findByUuid_PrevAndNext(long templateId, String uuid,
371                    OrderByComparator orderByComparator)
372                    throws NoSuchTemplateException, SystemException {
373                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
374    
375                    Session session = null;
376    
377                    try {
378                            session = openSession();
379    
380                            DDMTemplate[] array = new DDMTemplateImpl[3];
381    
382                            array[0] = getByUuid_PrevAndNext(session, ddmTemplate, uuid,
383                                            orderByComparator, true);
384    
385                            array[1] = ddmTemplate;
386    
387                            array[2] = getByUuid_PrevAndNext(session, ddmTemplate, uuid,
388                                            orderByComparator, false);
389    
390                            return array;
391                    }
392                    catch (Exception e) {
393                            throw processException(e);
394                    }
395                    finally {
396                            closeSession(session);
397                    }
398            }
399    
400            protected DDMTemplate getByUuid_PrevAndNext(Session session,
401                    DDMTemplate ddmTemplate, String uuid,
402                    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_DDMTEMPLATE_WHERE);
414    
415                    if (uuid == null) {
416                            query.append(_FINDER_COLUMN_UUID_UUID_1);
417                    }
418                    else {
419                            if (uuid.equals(StringPool.BLANK)) {
420                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
421                            }
422                            else {
423                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
424                            }
425                    }
426    
427                    if (orderByComparator != null) {
428                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
429    
430                            if (orderByConditionFields.length > 0) {
431                                    query.append(WHERE_AND);
432                            }
433    
434                            for (int i = 0; i < orderByConditionFields.length; i++) {
435                                    query.append(_ORDER_BY_ENTITY_ALIAS);
436                                    query.append(orderByConditionFields[i]);
437    
438                                    if ((i + 1) < orderByConditionFields.length) {
439                                            if (orderByComparator.isAscending() ^ previous) {
440                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
441                                            }
442                                            else {
443                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
444                                            }
445                                    }
446                                    else {
447                                            if (orderByComparator.isAscending() ^ previous) {
448                                                    query.append(WHERE_GREATER_THAN);
449                                            }
450                                            else {
451                                                    query.append(WHERE_LESSER_THAN);
452                                            }
453                                    }
454                            }
455    
456                            query.append(ORDER_BY_CLAUSE);
457    
458                            String[] orderByFields = orderByComparator.getOrderByFields();
459    
460                            for (int i = 0; i < orderByFields.length; i++) {
461                                    query.append(_ORDER_BY_ENTITY_ALIAS);
462                                    query.append(orderByFields[i]);
463    
464                                    if ((i + 1) < orderByFields.length) {
465                                            if (orderByComparator.isAscending() ^ previous) {
466                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
467                                            }
468                                            else {
469                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
470                                            }
471                                    }
472                                    else {
473                                            if (orderByComparator.isAscending() ^ previous) {
474                                                    query.append(ORDER_BY_ASC);
475                                            }
476                                            else {
477                                                    query.append(ORDER_BY_DESC);
478                                            }
479                                    }
480                            }
481                    }
482                    else {
483                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
484                    }
485    
486                    String sql = query.toString();
487    
488                    Query q = session.createQuery(sql);
489    
490                    q.setFirstResult(0);
491                    q.setMaxResults(2);
492    
493                    QueryPos qPos = QueryPos.getInstance(q);
494    
495                    if (uuid != null) {
496                            qPos.add(uuid);
497                    }
498    
499                    if (orderByComparator != null) {
500                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
501    
502                            for (Object value : values) {
503                                    qPos.add(value);
504                            }
505                    }
506    
507                    List<DDMTemplate> list = q.list();
508    
509                    if (list.size() == 2) {
510                            return list.get(1);
511                    }
512                    else {
513                            return null;
514                    }
515            }
516    
517            /**
518             * Removes all the d d m templates where uuid = &#63; from the database.
519             *
520             * @param uuid the uuid
521             * @throws SystemException if a system exception occurred
522             */
523            public void removeByUuid(String uuid) throws SystemException {
524                    for (DDMTemplate ddmTemplate : findByUuid(uuid, QueryUtil.ALL_POS,
525                                    QueryUtil.ALL_POS, null)) {
526                            remove(ddmTemplate);
527                    }
528            }
529    
530            /**
531             * Returns the number of d d m templates where uuid = &#63;.
532             *
533             * @param uuid the uuid
534             * @return the number of matching d d m templates
535             * @throws SystemException if a system exception occurred
536             */
537            public int countByUuid(String uuid) throws SystemException {
538                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
539    
540                    Object[] finderArgs = new Object[] { uuid };
541    
542                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
543                                    this);
544    
545                    if (count == null) {
546                            StringBundler query = new StringBundler(2);
547    
548                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
549    
550                            if (uuid == null) {
551                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
552                            }
553                            else {
554                                    if (uuid.equals(StringPool.BLANK)) {
555                                            query.append(_FINDER_COLUMN_UUID_UUID_3);
556                                    }
557                                    else {
558                                            query.append(_FINDER_COLUMN_UUID_UUID_2);
559                                    }
560                            }
561    
562                            String sql = query.toString();
563    
564                            Session session = null;
565    
566                            try {
567                                    session = openSession();
568    
569                                    Query q = session.createQuery(sql);
570    
571                                    QueryPos qPos = QueryPos.getInstance(q);
572    
573                                    if (uuid != null) {
574                                            qPos.add(uuid);
575                                    }
576    
577                                    count = (Long)q.uniqueResult();
578    
579                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
580                            }
581                            catch (Exception e) {
582                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
583    
584                                    throw processException(e);
585                            }
586                            finally {
587                                    closeSession(session);
588                            }
589                    }
590    
591                    return count.intValue();
592            }
593    
594            private static final String _FINDER_COLUMN_UUID_UUID_1 = "ddmTemplate.uuid IS NULL";
595            private static final String _FINDER_COLUMN_UUID_UUID_2 = "ddmTemplate.uuid = ?";
596            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(ddmTemplate.uuid IS NULL OR ddmTemplate.uuid = ?)";
597            public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
598                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
599                            FINDER_CLASS_NAME_ENTITY, "fetchByUUID_G",
600                            new String[] { String.class.getName(), Long.class.getName() },
601                            DDMTemplateModelImpl.UUID_COLUMN_BITMASK |
602                            DDMTemplateModelImpl.GROUPID_COLUMN_BITMASK);
603            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
604                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
605                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUUID_G",
606                            new String[] { String.class.getName(), Long.class.getName() });
607    
608            /**
609             * Returns the d d m template where uuid = &#63; and groupId = &#63; or throws a {@link com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException} if it could not be found.
610             *
611             * @param uuid the uuid
612             * @param groupId the group ID
613             * @return the matching d d m template
614             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
615             * @throws SystemException if a system exception occurred
616             */
617            public DDMTemplate findByUUID_G(String uuid, long groupId)
618                    throws NoSuchTemplateException, SystemException {
619                    DDMTemplate ddmTemplate = fetchByUUID_G(uuid, groupId);
620    
621                    if (ddmTemplate == null) {
622                            StringBundler msg = new StringBundler(6);
623    
624                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
625    
626                            msg.append("uuid=");
627                            msg.append(uuid);
628    
629                            msg.append(", groupId=");
630                            msg.append(groupId);
631    
632                            msg.append(StringPool.CLOSE_CURLY_BRACE);
633    
634                            if (_log.isWarnEnabled()) {
635                                    _log.warn(msg.toString());
636                            }
637    
638                            throw new NoSuchTemplateException(msg.toString());
639                    }
640    
641                    return ddmTemplate;
642            }
643    
644            /**
645             * Returns the d d m template where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
646             *
647             * @param uuid the uuid
648             * @param groupId the group ID
649             * @return the matching d d m template, or <code>null</code> if a matching d d m template could not be found
650             * @throws SystemException if a system exception occurred
651             */
652            public DDMTemplate fetchByUUID_G(String uuid, long groupId)
653                    throws SystemException {
654                    return fetchByUUID_G(uuid, groupId, true);
655            }
656    
657            /**
658             * Returns the d d m template where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
659             *
660             * @param uuid the uuid
661             * @param groupId the group ID
662             * @param retrieveFromCache whether to use the finder cache
663             * @return the matching d d m template, or <code>null</code> if a matching d d m template could not be found
664             * @throws SystemException if a system exception occurred
665             */
666            public DDMTemplate fetchByUUID_G(String uuid, long groupId,
667                    boolean retrieveFromCache) throws SystemException {
668                    Object[] finderArgs = new Object[] { uuid, groupId };
669    
670                    Object result = null;
671    
672                    if (retrieveFromCache) {
673                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G,
674                                            finderArgs, this);
675                    }
676    
677                    if (result instanceof DDMTemplate) {
678                            DDMTemplate ddmTemplate = (DDMTemplate)result;
679    
680                            if (!Validator.equals(uuid, ddmTemplate.getUuid()) ||
681                                            (groupId != ddmTemplate.getGroupId())) {
682                                    result = null;
683                            }
684                    }
685    
686                    if (result == null) {
687                            StringBundler query = new StringBundler(4);
688    
689                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
690    
691                            if (uuid == null) {
692                                    query.append(_FINDER_COLUMN_UUID_G_UUID_1);
693                            }
694                            else {
695                                    if (uuid.equals(StringPool.BLANK)) {
696                                            query.append(_FINDER_COLUMN_UUID_G_UUID_3);
697                                    }
698                                    else {
699                                            query.append(_FINDER_COLUMN_UUID_G_UUID_2);
700                                    }
701                            }
702    
703                            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
704    
705                            String sql = query.toString();
706    
707                            Session session = null;
708    
709                            try {
710                                    session = openSession();
711    
712                                    Query q = session.createQuery(sql);
713    
714                                    QueryPos qPos = QueryPos.getInstance(q);
715    
716                                    if (uuid != null) {
717                                            qPos.add(uuid);
718                                    }
719    
720                                    qPos.add(groupId);
721    
722                                    List<DDMTemplate> list = q.list();
723    
724                                    if (list.isEmpty()) {
725                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
726                                                    finderArgs, list);
727                                    }
728                                    else {
729                                            DDMTemplate ddmTemplate = list.get(0);
730    
731                                            result = ddmTemplate;
732    
733                                            cacheResult(ddmTemplate);
734    
735                                            if ((ddmTemplate.getUuid() == null) ||
736                                                            !ddmTemplate.getUuid().equals(uuid) ||
737                                                            (ddmTemplate.getGroupId() != groupId)) {
738                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
739                                                            finderArgs, ddmTemplate);
740                                            }
741                                    }
742                            }
743                            catch (Exception e) {
744                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
745                                            finderArgs);
746    
747                                    throw processException(e);
748                            }
749                            finally {
750                                    closeSession(session);
751                            }
752                    }
753    
754                    if (result instanceof List<?>) {
755                            return null;
756                    }
757                    else {
758                            return (DDMTemplate)result;
759                    }
760            }
761    
762            /**
763             * Removes the d d m template where uuid = &#63; and groupId = &#63; from the database.
764             *
765             * @param uuid the uuid
766             * @param groupId the group ID
767             * @return the d d m template that was removed
768             * @throws SystemException if a system exception occurred
769             */
770            public DDMTemplate removeByUUID_G(String uuid, long groupId)
771                    throws NoSuchTemplateException, SystemException {
772                    DDMTemplate ddmTemplate = findByUUID_G(uuid, groupId);
773    
774                    return remove(ddmTemplate);
775            }
776    
777            /**
778             * Returns the number of d d m templates where uuid = &#63; and groupId = &#63;.
779             *
780             * @param uuid the uuid
781             * @param groupId the group ID
782             * @return the number of matching d d m templates
783             * @throws SystemException if a system exception occurred
784             */
785            public int countByUUID_G(String uuid, long groupId)
786                    throws SystemException {
787                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_G;
788    
789                    Object[] finderArgs = new Object[] { uuid, groupId };
790    
791                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
792                                    this);
793    
794                    if (count == null) {
795                            StringBundler query = new StringBundler(3);
796    
797                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
798    
799                            if (uuid == null) {
800                                    query.append(_FINDER_COLUMN_UUID_G_UUID_1);
801                            }
802                            else {
803                                    if (uuid.equals(StringPool.BLANK)) {
804                                            query.append(_FINDER_COLUMN_UUID_G_UUID_3);
805                                    }
806                                    else {
807                                            query.append(_FINDER_COLUMN_UUID_G_UUID_2);
808                                    }
809                            }
810    
811                            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
812    
813                            String sql = query.toString();
814    
815                            Session session = null;
816    
817                            try {
818                                    session = openSession();
819    
820                                    Query q = session.createQuery(sql);
821    
822                                    QueryPos qPos = QueryPos.getInstance(q);
823    
824                                    if (uuid != null) {
825                                            qPos.add(uuid);
826                                    }
827    
828                                    qPos.add(groupId);
829    
830                                    count = (Long)q.uniqueResult();
831    
832                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
833                            }
834                            catch (Exception e) {
835                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
836    
837                                    throw processException(e);
838                            }
839                            finally {
840                                    closeSession(session);
841                            }
842                    }
843    
844                    return count.intValue();
845            }
846    
847            private static final String _FINDER_COLUMN_UUID_G_UUID_1 = "ddmTemplate.uuid IS NULL AND ";
848            private static final String _FINDER_COLUMN_UUID_G_UUID_2 = "ddmTemplate.uuid = ? AND ";
849            private static final String _FINDER_COLUMN_UUID_G_UUID_3 = "(ddmTemplate.uuid IS NULL OR ddmTemplate.uuid = ?) AND ";
850            private static final String _FINDER_COLUMN_UUID_G_GROUPID_2 = "ddmTemplate.groupId = ?";
851            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
852                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
853                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
854                            new String[] {
855                                    String.class.getName(), Long.class.getName(),
856                                    
857                            Integer.class.getName(), Integer.class.getName(),
858                                    OrderByComparator.class.getName()
859                            });
860            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
861                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
862                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
863                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
864                            new String[] { String.class.getName(), Long.class.getName() },
865                            DDMTemplateModelImpl.UUID_COLUMN_BITMASK |
866                            DDMTemplateModelImpl.COMPANYID_COLUMN_BITMASK);
867            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
868                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
869                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
870                            new String[] { String.class.getName(), Long.class.getName() });
871    
872            /**
873             * Returns all the d d m templates where uuid = &#63; and companyId = &#63;.
874             *
875             * @param uuid the uuid
876             * @param companyId the company ID
877             * @return the matching d d m templates
878             * @throws SystemException if a system exception occurred
879             */
880            public List<DDMTemplate> findByUuid_C(String uuid, long companyId)
881                    throws SystemException {
882                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
883                            QueryUtil.ALL_POS, null);
884            }
885    
886            /**
887             * Returns a range of all the d d m templates where uuid = &#63; and companyId = &#63;.
888             *
889             * <p>
890             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
891             * </p>
892             *
893             * @param uuid the uuid
894             * @param companyId the company ID
895             * @param start the lower bound of the range of d d m templates
896             * @param end the upper bound of the range of d d m templates (not inclusive)
897             * @return the range of matching d d m templates
898             * @throws SystemException if a system exception occurred
899             */
900            public List<DDMTemplate> findByUuid_C(String uuid, long companyId,
901                    int start, int end) throws SystemException {
902                    return findByUuid_C(uuid, companyId, start, end, null);
903            }
904    
905            /**
906             * Returns an ordered range of all the d d m templates where uuid = &#63; and companyId = &#63;.
907             *
908             * <p>
909             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
910             * </p>
911             *
912             * @param uuid the uuid
913             * @param companyId the company ID
914             * @param start the lower bound of the range of d d m templates
915             * @param end the upper bound of the range of d d m templates (not inclusive)
916             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
917             * @return the ordered range of matching d d m templates
918             * @throws SystemException if a system exception occurred
919             */
920            public List<DDMTemplate> findByUuid_C(String uuid, long companyId,
921                    int start, int end, OrderByComparator orderByComparator)
922                    throws SystemException {
923                    boolean pagination = true;
924                    FinderPath finderPath = null;
925                    Object[] finderArgs = null;
926    
927                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
928                                    (orderByComparator == null)) {
929                            pagination = false;
930                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
931                            finderArgs = new Object[] { uuid, companyId };
932                    }
933                    else {
934                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
935                            finderArgs = new Object[] {
936                                            uuid, companyId,
937                                            
938                                            start, end, orderByComparator
939                                    };
940                    }
941    
942                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
943                                    finderArgs, this);
944    
945                    if ((list != null) && !list.isEmpty()) {
946                            for (DDMTemplate ddmTemplate : list) {
947                                    if (!Validator.equals(uuid, ddmTemplate.getUuid()) ||
948                                                    (companyId != ddmTemplate.getCompanyId())) {
949                                            list = null;
950    
951                                            break;
952                                    }
953                            }
954                    }
955    
956                    if (list == null) {
957                            StringBundler query = null;
958    
959                            if (orderByComparator != null) {
960                                    query = new StringBundler(4 +
961                                                    (orderByComparator.getOrderByFields().length * 3));
962                            }
963                            else {
964                                    query = new StringBundler(4);
965                            }
966    
967                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
968    
969                            if (uuid == null) {
970                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
971                            }
972                            else {
973                                    if (uuid.equals(StringPool.BLANK)) {
974                                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
975                                    }
976                                    else {
977                                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
978                                    }
979                            }
980    
981                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
982    
983                            if (orderByComparator != null) {
984                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
985                                            orderByComparator);
986                            }
987                            else
988                             if (pagination) {
989                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
990                            }
991    
992                            String sql = query.toString();
993    
994                            Session session = null;
995    
996                            try {
997                                    session = openSession();
998    
999                                    Query q = session.createQuery(sql);
1000    
1001                                    QueryPos qPos = QueryPos.getInstance(q);
1002    
1003                                    if (uuid != null) {
1004                                            qPos.add(uuid);
1005                                    }
1006    
1007                                    qPos.add(companyId);
1008    
1009                                    if (!pagination) {
1010                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
1011                                                            start, end, false);
1012    
1013                                            Collections.sort(list);
1014    
1015                                            list = new UnmodifiableList<DDMTemplate>(list);
1016                                    }
1017                                    else {
1018                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
1019                                                            start, end);
1020                                    }
1021    
1022                                    cacheResult(list);
1023    
1024                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1025                            }
1026                            catch (Exception e) {
1027                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1028    
1029                                    throw processException(e);
1030                            }
1031                            finally {
1032                                    closeSession(session);
1033                            }
1034                    }
1035    
1036                    return list;
1037            }
1038    
1039            /**
1040             * Returns the first d d m template in the ordered set where uuid = &#63; and companyId = &#63;.
1041             *
1042             * @param uuid the uuid
1043             * @param companyId the company ID
1044             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1045             * @return the first matching d d m template
1046             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
1047             * @throws SystemException if a system exception occurred
1048             */
1049            public DDMTemplate findByUuid_C_First(String uuid, long companyId,
1050                    OrderByComparator orderByComparator)
1051                    throws NoSuchTemplateException, SystemException {
1052                    DDMTemplate ddmTemplate = fetchByUuid_C_First(uuid, companyId,
1053                                    orderByComparator);
1054    
1055                    if (ddmTemplate != null) {
1056                            return ddmTemplate;
1057                    }
1058    
1059                    StringBundler msg = new StringBundler(6);
1060    
1061                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1062    
1063                    msg.append("uuid=");
1064                    msg.append(uuid);
1065    
1066                    msg.append(", companyId=");
1067                    msg.append(companyId);
1068    
1069                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1070    
1071                    throw new NoSuchTemplateException(msg.toString());
1072            }
1073    
1074            /**
1075             * Returns the first d d m template in the ordered set where uuid = &#63; and companyId = &#63;.
1076             *
1077             * @param uuid the uuid
1078             * @param companyId the company ID
1079             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1080             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
1081             * @throws SystemException if a system exception occurred
1082             */
1083            public DDMTemplate fetchByUuid_C_First(String uuid, long companyId,
1084                    OrderByComparator orderByComparator) throws SystemException {
1085                    List<DDMTemplate> list = findByUuid_C(uuid, companyId, 0, 1,
1086                                    orderByComparator);
1087    
1088                    if (!list.isEmpty()) {
1089                            return list.get(0);
1090                    }
1091    
1092                    return null;
1093            }
1094    
1095            /**
1096             * Returns the last d d m template in the ordered set where uuid = &#63; and companyId = &#63;.
1097             *
1098             * @param uuid the uuid
1099             * @param companyId the company ID
1100             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1101             * @return the last matching d d m template
1102             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
1103             * @throws SystemException if a system exception occurred
1104             */
1105            public DDMTemplate findByUuid_C_Last(String uuid, long companyId,
1106                    OrderByComparator orderByComparator)
1107                    throws NoSuchTemplateException, SystemException {
1108                    DDMTemplate ddmTemplate = fetchByUuid_C_Last(uuid, companyId,
1109                                    orderByComparator);
1110    
1111                    if (ddmTemplate != null) {
1112                            return ddmTemplate;
1113                    }
1114    
1115                    StringBundler msg = new StringBundler(6);
1116    
1117                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1118    
1119                    msg.append("uuid=");
1120                    msg.append(uuid);
1121    
1122                    msg.append(", companyId=");
1123                    msg.append(companyId);
1124    
1125                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1126    
1127                    throw new NoSuchTemplateException(msg.toString());
1128            }
1129    
1130            /**
1131             * Returns the last d d m template in the ordered set where uuid = &#63; and companyId = &#63;.
1132             *
1133             * @param uuid the uuid
1134             * @param companyId the company ID
1135             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1136             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
1137             * @throws SystemException if a system exception occurred
1138             */
1139            public DDMTemplate fetchByUuid_C_Last(String uuid, long companyId,
1140                    OrderByComparator orderByComparator) throws SystemException {
1141                    int count = countByUuid_C(uuid, companyId);
1142    
1143                    List<DDMTemplate> list = findByUuid_C(uuid, companyId, count - 1,
1144                                    count, orderByComparator);
1145    
1146                    if (!list.isEmpty()) {
1147                            return list.get(0);
1148                    }
1149    
1150                    return null;
1151            }
1152    
1153            /**
1154             * Returns the d d m templates before and after the current d d m template in the ordered set where uuid = &#63; and companyId = &#63;.
1155             *
1156             * @param templateId the primary key of the current d d m template
1157             * @param uuid the uuid
1158             * @param companyId the company ID
1159             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1160             * @return the previous, current, and next d d m template
1161             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
1162             * @throws SystemException if a system exception occurred
1163             */
1164            public DDMTemplate[] findByUuid_C_PrevAndNext(long templateId, String uuid,
1165                    long companyId, OrderByComparator orderByComparator)
1166                    throws NoSuchTemplateException, SystemException {
1167                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
1168    
1169                    Session session = null;
1170    
1171                    try {
1172                            session = openSession();
1173    
1174                            DDMTemplate[] array = new DDMTemplateImpl[3];
1175    
1176                            array[0] = getByUuid_C_PrevAndNext(session, ddmTemplate, uuid,
1177                                            companyId, orderByComparator, true);
1178    
1179                            array[1] = ddmTemplate;
1180    
1181                            array[2] = getByUuid_C_PrevAndNext(session, ddmTemplate, uuid,
1182                                            companyId, orderByComparator, false);
1183    
1184                            return array;
1185                    }
1186                    catch (Exception e) {
1187                            throw processException(e);
1188                    }
1189                    finally {
1190                            closeSession(session);
1191                    }
1192            }
1193    
1194            protected DDMTemplate getByUuid_C_PrevAndNext(Session session,
1195                    DDMTemplate ddmTemplate, String uuid, long companyId,
1196                    OrderByComparator orderByComparator, boolean previous) {
1197                    StringBundler query = null;
1198    
1199                    if (orderByComparator != null) {
1200                            query = new StringBundler(6 +
1201                                            (orderByComparator.getOrderByFields().length * 6));
1202                    }
1203                    else {
1204                            query = new StringBundler(3);
1205                    }
1206    
1207                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
1208    
1209                    if (uuid == null) {
1210                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1211                    }
1212                    else {
1213                            if (uuid.equals(StringPool.BLANK)) {
1214                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1215                            }
1216                            else {
1217                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1218                            }
1219                    }
1220    
1221                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1222    
1223                    if (orderByComparator != null) {
1224                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1225    
1226                            if (orderByConditionFields.length > 0) {
1227                                    query.append(WHERE_AND);
1228                            }
1229    
1230                            for (int i = 0; i < orderByConditionFields.length; i++) {
1231                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1232                                    query.append(orderByConditionFields[i]);
1233    
1234                                    if ((i + 1) < orderByConditionFields.length) {
1235                                            if (orderByComparator.isAscending() ^ previous) {
1236                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1237                                            }
1238                                            else {
1239                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1240                                            }
1241                                    }
1242                                    else {
1243                                            if (orderByComparator.isAscending() ^ previous) {
1244                                                    query.append(WHERE_GREATER_THAN);
1245                                            }
1246                                            else {
1247                                                    query.append(WHERE_LESSER_THAN);
1248                                            }
1249                                    }
1250                            }
1251    
1252                            query.append(ORDER_BY_CLAUSE);
1253    
1254                            String[] orderByFields = orderByComparator.getOrderByFields();
1255    
1256                            for (int i = 0; i < orderByFields.length; i++) {
1257                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1258                                    query.append(orderByFields[i]);
1259    
1260                                    if ((i + 1) < orderByFields.length) {
1261                                            if (orderByComparator.isAscending() ^ previous) {
1262                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1263                                            }
1264                                            else {
1265                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1266                                            }
1267                                    }
1268                                    else {
1269                                            if (orderByComparator.isAscending() ^ previous) {
1270                                                    query.append(ORDER_BY_ASC);
1271                                            }
1272                                            else {
1273                                                    query.append(ORDER_BY_DESC);
1274                                            }
1275                                    }
1276                            }
1277                    }
1278                    else {
1279                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
1280                    }
1281    
1282                    String sql = query.toString();
1283    
1284                    Query q = session.createQuery(sql);
1285    
1286                    q.setFirstResult(0);
1287                    q.setMaxResults(2);
1288    
1289                    QueryPos qPos = QueryPos.getInstance(q);
1290    
1291                    if (uuid != null) {
1292                            qPos.add(uuid);
1293                    }
1294    
1295                    qPos.add(companyId);
1296    
1297                    if (orderByComparator != null) {
1298                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
1299    
1300                            for (Object value : values) {
1301                                    qPos.add(value);
1302                            }
1303                    }
1304    
1305                    List<DDMTemplate> list = q.list();
1306    
1307                    if (list.size() == 2) {
1308                            return list.get(1);
1309                    }
1310                    else {
1311                            return null;
1312                    }
1313            }
1314    
1315            /**
1316             * Removes all the d d m templates where uuid = &#63; and companyId = &#63; from the database.
1317             *
1318             * @param uuid the uuid
1319             * @param companyId the company ID
1320             * @throws SystemException if a system exception occurred
1321             */
1322            public void removeByUuid_C(String uuid, long companyId)
1323                    throws SystemException {
1324                    for (DDMTemplate ddmTemplate : findByUuid_C(uuid, companyId,
1325                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1326                            remove(ddmTemplate);
1327                    }
1328            }
1329    
1330            /**
1331             * Returns the number of d d m templates where uuid = &#63; and companyId = &#63;.
1332             *
1333             * @param uuid the uuid
1334             * @param companyId the company ID
1335             * @return the number of matching d d m templates
1336             * @throws SystemException if a system exception occurred
1337             */
1338            public int countByUuid_C(String uuid, long companyId)
1339                    throws SystemException {
1340                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1341    
1342                    Object[] finderArgs = new Object[] { uuid, companyId };
1343    
1344                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1345                                    this);
1346    
1347                    if (count == null) {
1348                            StringBundler query = new StringBundler(3);
1349    
1350                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
1351    
1352                            if (uuid == null) {
1353                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1354                            }
1355                            else {
1356                                    if (uuid.equals(StringPool.BLANK)) {
1357                                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1358                                    }
1359                                    else {
1360                                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1361                                    }
1362                            }
1363    
1364                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1365    
1366                            String sql = query.toString();
1367    
1368                            Session session = null;
1369    
1370                            try {
1371                                    session = openSession();
1372    
1373                                    Query q = session.createQuery(sql);
1374    
1375                                    QueryPos qPos = QueryPos.getInstance(q);
1376    
1377                                    if (uuid != null) {
1378                                            qPos.add(uuid);
1379                                    }
1380    
1381                                    qPos.add(companyId);
1382    
1383                                    count = (Long)q.uniqueResult();
1384    
1385                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1386                            }
1387                            catch (Exception e) {
1388                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1389    
1390                                    throw processException(e);
1391                            }
1392                            finally {
1393                                    closeSession(session);
1394                            }
1395                    }
1396    
1397                    return count.intValue();
1398            }
1399    
1400            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "ddmTemplate.uuid IS NULL AND ";
1401            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "ddmTemplate.uuid = ? AND ";
1402            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(ddmTemplate.uuid IS NULL OR ddmTemplate.uuid = ?) AND ";
1403            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "ddmTemplate.companyId = ?";
1404            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
1405                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
1406                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByGroupId",
1407                            new String[] {
1408                                    Long.class.getName(),
1409                                    
1410                            Integer.class.getName(), Integer.class.getName(),
1411                                    OrderByComparator.class.getName()
1412                            });
1413            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
1414                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
1415                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
1416                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
1417                            new String[] { Long.class.getName() },
1418                            DDMTemplateModelImpl.GROUPID_COLUMN_BITMASK);
1419            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
1420                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
1421                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
1422                            new String[] { Long.class.getName() });
1423    
1424            /**
1425             * Returns all the d d m templates where groupId = &#63;.
1426             *
1427             * @param groupId the group ID
1428             * @return the matching d d m templates
1429             * @throws SystemException if a system exception occurred
1430             */
1431            public List<DDMTemplate> findByGroupId(long groupId)
1432                    throws SystemException {
1433                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1434            }
1435    
1436            /**
1437             * Returns a range of all the d d m templates where groupId = &#63;.
1438             *
1439             * <p>
1440             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
1441             * </p>
1442             *
1443             * @param groupId the group ID
1444             * @param start the lower bound of the range of d d m templates
1445             * @param end the upper bound of the range of d d m templates (not inclusive)
1446             * @return the range of matching d d m templates
1447             * @throws SystemException if a system exception occurred
1448             */
1449            public List<DDMTemplate> findByGroupId(long groupId, int start, int end)
1450                    throws SystemException {
1451                    return findByGroupId(groupId, start, end, null);
1452            }
1453    
1454            /**
1455             * Returns an ordered range of all the d d m templates where groupId = &#63;.
1456             *
1457             * <p>
1458             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
1459             * </p>
1460             *
1461             * @param groupId the group ID
1462             * @param start the lower bound of the range of d d m templates
1463             * @param end the upper bound of the range of d d m templates (not inclusive)
1464             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1465             * @return the ordered range of matching d d m templates
1466             * @throws SystemException if a system exception occurred
1467             */
1468            public List<DDMTemplate> findByGroupId(long groupId, int start, int end,
1469                    OrderByComparator orderByComparator) throws SystemException {
1470                    boolean pagination = true;
1471                    FinderPath finderPath = null;
1472                    Object[] finderArgs = null;
1473    
1474                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1475                                    (orderByComparator == null)) {
1476                            pagination = false;
1477                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
1478                            finderArgs = new Object[] { groupId };
1479                    }
1480                    else {
1481                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
1482                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
1483                    }
1484    
1485                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
1486                                    finderArgs, this);
1487    
1488                    if ((list != null) && !list.isEmpty()) {
1489                            for (DDMTemplate ddmTemplate : list) {
1490                                    if ((groupId != ddmTemplate.getGroupId())) {
1491                                            list = null;
1492    
1493                                            break;
1494                                    }
1495                            }
1496                    }
1497    
1498                    if (list == null) {
1499                            StringBundler query = null;
1500    
1501                            if (orderByComparator != null) {
1502                                    query = new StringBundler(3 +
1503                                                    (orderByComparator.getOrderByFields().length * 3));
1504                            }
1505                            else {
1506                                    query = new StringBundler(3);
1507                            }
1508    
1509                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
1510    
1511                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1512    
1513                            if (orderByComparator != null) {
1514                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1515                                            orderByComparator);
1516                            }
1517                            else
1518                             if (pagination) {
1519                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
1520                            }
1521    
1522                            String sql = query.toString();
1523    
1524                            Session session = null;
1525    
1526                            try {
1527                                    session = openSession();
1528    
1529                                    Query q = session.createQuery(sql);
1530    
1531                                    QueryPos qPos = QueryPos.getInstance(q);
1532    
1533                                    qPos.add(groupId);
1534    
1535                                    if (!pagination) {
1536                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
1537                                                            start, end, false);
1538    
1539                                            Collections.sort(list);
1540    
1541                                            list = new UnmodifiableList<DDMTemplate>(list);
1542                                    }
1543                                    else {
1544                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
1545                                                            start, end);
1546                                    }
1547    
1548                                    cacheResult(list);
1549    
1550                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1551                            }
1552                            catch (Exception e) {
1553                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1554    
1555                                    throw processException(e);
1556                            }
1557                            finally {
1558                                    closeSession(session);
1559                            }
1560                    }
1561    
1562                    return list;
1563            }
1564    
1565            /**
1566             * Returns the first d d m template in the ordered set where groupId = &#63;.
1567             *
1568             * @param groupId the group ID
1569             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1570             * @return the first matching d d m template
1571             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
1572             * @throws SystemException if a system exception occurred
1573             */
1574            public DDMTemplate findByGroupId_First(long groupId,
1575                    OrderByComparator orderByComparator)
1576                    throws NoSuchTemplateException, SystemException {
1577                    DDMTemplate ddmTemplate = fetchByGroupId_First(groupId,
1578                                    orderByComparator);
1579    
1580                    if (ddmTemplate != null) {
1581                            return ddmTemplate;
1582                    }
1583    
1584                    StringBundler msg = new StringBundler(4);
1585    
1586                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1587    
1588                    msg.append("groupId=");
1589                    msg.append(groupId);
1590    
1591                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1592    
1593                    throw new NoSuchTemplateException(msg.toString());
1594            }
1595    
1596            /**
1597             * Returns the first d d m template in the ordered set where groupId = &#63;.
1598             *
1599             * @param groupId the group ID
1600             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1601             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
1602             * @throws SystemException if a system exception occurred
1603             */
1604            public DDMTemplate fetchByGroupId_First(long groupId,
1605                    OrderByComparator orderByComparator) throws SystemException {
1606                    List<DDMTemplate> list = findByGroupId(groupId, 0, 1, orderByComparator);
1607    
1608                    if (!list.isEmpty()) {
1609                            return list.get(0);
1610                    }
1611    
1612                    return null;
1613            }
1614    
1615            /**
1616             * Returns the last d d m template in the ordered set where groupId = &#63;.
1617             *
1618             * @param groupId the group ID
1619             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1620             * @return the last matching d d m template
1621             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
1622             * @throws SystemException if a system exception occurred
1623             */
1624            public DDMTemplate findByGroupId_Last(long groupId,
1625                    OrderByComparator orderByComparator)
1626                    throws NoSuchTemplateException, SystemException {
1627                    DDMTemplate ddmTemplate = fetchByGroupId_Last(groupId, orderByComparator);
1628    
1629                    if (ddmTemplate != null) {
1630                            return ddmTemplate;
1631                    }
1632    
1633                    StringBundler msg = new StringBundler(4);
1634    
1635                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1636    
1637                    msg.append("groupId=");
1638                    msg.append(groupId);
1639    
1640                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1641    
1642                    throw new NoSuchTemplateException(msg.toString());
1643            }
1644    
1645            /**
1646             * Returns the last d d m template in the ordered set where groupId = &#63;.
1647             *
1648             * @param groupId the group ID
1649             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1650             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
1651             * @throws SystemException if a system exception occurred
1652             */
1653            public DDMTemplate fetchByGroupId_Last(long groupId,
1654                    OrderByComparator orderByComparator) throws SystemException {
1655                    int count = countByGroupId(groupId);
1656    
1657                    List<DDMTemplate> list = findByGroupId(groupId, count - 1, count,
1658                                    orderByComparator);
1659    
1660                    if (!list.isEmpty()) {
1661                            return list.get(0);
1662                    }
1663    
1664                    return null;
1665            }
1666    
1667            /**
1668             * Returns the d d m templates before and after the current d d m template in the ordered set where groupId = &#63;.
1669             *
1670             * @param templateId the primary key of the current d d m template
1671             * @param groupId the group ID
1672             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1673             * @return the previous, current, and next d d m template
1674             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
1675             * @throws SystemException if a system exception occurred
1676             */
1677            public DDMTemplate[] findByGroupId_PrevAndNext(long templateId,
1678                    long groupId, OrderByComparator orderByComparator)
1679                    throws NoSuchTemplateException, SystemException {
1680                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
1681    
1682                    Session session = null;
1683    
1684                    try {
1685                            session = openSession();
1686    
1687                            DDMTemplate[] array = new DDMTemplateImpl[3];
1688    
1689                            array[0] = getByGroupId_PrevAndNext(session, ddmTemplate, groupId,
1690                                            orderByComparator, true);
1691    
1692                            array[1] = ddmTemplate;
1693    
1694                            array[2] = getByGroupId_PrevAndNext(session, ddmTemplate, groupId,
1695                                            orderByComparator, false);
1696    
1697                            return array;
1698                    }
1699                    catch (Exception e) {
1700                            throw processException(e);
1701                    }
1702                    finally {
1703                            closeSession(session);
1704                    }
1705            }
1706    
1707            protected DDMTemplate getByGroupId_PrevAndNext(Session session,
1708                    DDMTemplate ddmTemplate, long groupId,
1709                    OrderByComparator orderByComparator, boolean previous) {
1710                    StringBundler query = null;
1711    
1712                    if (orderByComparator != null) {
1713                            query = new StringBundler(6 +
1714                                            (orderByComparator.getOrderByFields().length * 6));
1715                    }
1716                    else {
1717                            query = new StringBundler(3);
1718                    }
1719    
1720                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
1721    
1722                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1723    
1724                    if (orderByComparator != null) {
1725                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1726    
1727                            if (orderByConditionFields.length > 0) {
1728                                    query.append(WHERE_AND);
1729                            }
1730    
1731                            for (int i = 0; i < orderByConditionFields.length; i++) {
1732                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1733                                    query.append(orderByConditionFields[i]);
1734    
1735                                    if ((i + 1) < orderByConditionFields.length) {
1736                                            if (orderByComparator.isAscending() ^ previous) {
1737                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1738                                            }
1739                                            else {
1740                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1741                                            }
1742                                    }
1743                                    else {
1744                                            if (orderByComparator.isAscending() ^ previous) {
1745                                                    query.append(WHERE_GREATER_THAN);
1746                                            }
1747                                            else {
1748                                                    query.append(WHERE_LESSER_THAN);
1749                                            }
1750                                    }
1751                            }
1752    
1753                            query.append(ORDER_BY_CLAUSE);
1754    
1755                            String[] orderByFields = orderByComparator.getOrderByFields();
1756    
1757                            for (int i = 0; i < orderByFields.length; i++) {
1758                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1759                                    query.append(orderByFields[i]);
1760    
1761                                    if ((i + 1) < orderByFields.length) {
1762                                            if (orderByComparator.isAscending() ^ previous) {
1763                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1764                                            }
1765                                            else {
1766                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1767                                            }
1768                                    }
1769                                    else {
1770                                            if (orderByComparator.isAscending() ^ previous) {
1771                                                    query.append(ORDER_BY_ASC);
1772                                            }
1773                                            else {
1774                                                    query.append(ORDER_BY_DESC);
1775                                            }
1776                                    }
1777                            }
1778                    }
1779                    else {
1780                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
1781                    }
1782    
1783                    String sql = query.toString();
1784    
1785                    Query q = session.createQuery(sql);
1786    
1787                    q.setFirstResult(0);
1788                    q.setMaxResults(2);
1789    
1790                    QueryPos qPos = QueryPos.getInstance(q);
1791    
1792                    qPos.add(groupId);
1793    
1794                    if (orderByComparator != null) {
1795                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
1796    
1797                            for (Object value : values) {
1798                                    qPos.add(value);
1799                            }
1800                    }
1801    
1802                    List<DDMTemplate> list = q.list();
1803    
1804                    if (list.size() == 2) {
1805                            return list.get(1);
1806                    }
1807                    else {
1808                            return null;
1809                    }
1810            }
1811    
1812            /**
1813             * Returns all the d d m templates that the user has permission to view where groupId = &#63;.
1814             *
1815             * @param groupId the group ID
1816             * @return the matching d d m templates that the user has permission to view
1817             * @throws SystemException if a system exception occurred
1818             */
1819            public List<DDMTemplate> filterFindByGroupId(long groupId)
1820                    throws SystemException {
1821                    return filterFindByGroupId(groupId, QueryUtil.ALL_POS,
1822                            QueryUtil.ALL_POS, null);
1823            }
1824    
1825            /**
1826             * Returns a range of all the d d m templates that the user has permission to view where groupId = &#63;.
1827             *
1828             * <p>
1829             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
1830             * </p>
1831             *
1832             * @param groupId the group ID
1833             * @param start the lower bound of the range of d d m templates
1834             * @param end the upper bound of the range of d d m templates (not inclusive)
1835             * @return the range of matching d d m templates that the user has permission to view
1836             * @throws SystemException if a system exception occurred
1837             */
1838            public List<DDMTemplate> filterFindByGroupId(long groupId, int start,
1839                    int end) throws SystemException {
1840                    return filterFindByGroupId(groupId, start, end, null);
1841            }
1842    
1843            /**
1844             * Returns an ordered range of all the d d m templates that the user has permissions to view where groupId = &#63;.
1845             *
1846             * <p>
1847             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
1848             * </p>
1849             *
1850             * @param groupId the group ID
1851             * @param start the lower bound of the range of d d m templates
1852             * @param end the upper bound of the range of d d m templates (not inclusive)
1853             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1854             * @return the ordered range of matching d d m templates that the user has permission to view
1855             * @throws SystemException if a system exception occurred
1856             */
1857            public List<DDMTemplate> filterFindByGroupId(long groupId, int start,
1858                    int end, OrderByComparator orderByComparator) throws SystemException {
1859                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
1860                            return findByGroupId(groupId, start, end, orderByComparator);
1861                    }
1862    
1863                    StringBundler query = null;
1864    
1865                    if (orderByComparator != null) {
1866                            query = new StringBundler(3 +
1867                                            (orderByComparator.getOrderByFields().length * 3));
1868                    }
1869                    else {
1870                            query = new StringBundler(3);
1871                    }
1872    
1873                    if (getDB().isSupportsInlineDistinct()) {
1874                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
1875                    }
1876                    else {
1877                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
1878                    }
1879    
1880                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1881    
1882                    if (!getDB().isSupportsInlineDistinct()) {
1883                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
1884                    }
1885    
1886                    if (orderByComparator != null) {
1887                            if (getDB().isSupportsInlineDistinct()) {
1888                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1889                                            orderByComparator);
1890                            }
1891                            else {
1892                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
1893                                            orderByComparator);
1894                            }
1895                    }
1896                    else {
1897                            if (getDB().isSupportsInlineDistinct()) {
1898                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
1899                            }
1900                            else {
1901                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
1902                            }
1903                    }
1904    
1905                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
1906                                    DDMTemplate.class.getName(),
1907                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
1908    
1909                    Session session = null;
1910    
1911                    try {
1912                            session = openSession();
1913    
1914                            SQLQuery q = session.createSQLQuery(sql);
1915    
1916                            if (getDB().isSupportsInlineDistinct()) {
1917                                    q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
1918                            }
1919                            else {
1920                                    q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
1921                            }
1922    
1923                            QueryPos qPos = QueryPos.getInstance(q);
1924    
1925                            qPos.add(groupId);
1926    
1927                            return (List<DDMTemplate>)QueryUtil.list(q, getDialect(), start, end);
1928                    }
1929                    catch (Exception e) {
1930                            throw processException(e);
1931                    }
1932                    finally {
1933                            closeSession(session);
1934                    }
1935            }
1936    
1937            /**
1938             * Returns the d d m templates before and after the current d d m template in the ordered set of d d m templates that the user has permission to view where groupId = &#63;.
1939             *
1940             * @param templateId the primary key of the current d d m template
1941             * @param groupId the group ID
1942             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1943             * @return the previous, current, and next d d m template
1944             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
1945             * @throws SystemException if a system exception occurred
1946             */
1947            public DDMTemplate[] filterFindByGroupId_PrevAndNext(long templateId,
1948                    long groupId, OrderByComparator orderByComparator)
1949                    throws NoSuchTemplateException, SystemException {
1950                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
1951                            return findByGroupId_PrevAndNext(templateId, groupId,
1952                                    orderByComparator);
1953                    }
1954    
1955                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
1956    
1957                    Session session = null;
1958    
1959                    try {
1960                            session = openSession();
1961    
1962                            DDMTemplate[] array = new DDMTemplateImpl[3];
1963    
1964                            array[0] = filterGetByGroupId_PrevAndNext(session, ddmTemplate,
1965                                            groupId, orderByComparator, true);
1966    
1967                            array[1] = ddmTemplate;
1968    
1969                            array[2] = filterGetByGroupId_PrevAndNext(session, ddmTemplate,
1970                                            groupId, orderByComparator, false);
1971    
1972                            return array;
1973                    }
1974                    catch (Exception e) {
1975                            throw processException(e);
1976                    }
1977                    finally {
1978                            closeSession(session);
1979                    }
1980            }
1981    
1982            protected DDMTemplate filterGetByGroupId_PrevAndNext(Session session,
1983                    DDMTemplate ddmTemplate, long groupId,
1984                    OrderByComparator orderByComparator, boolean previous) {
1985                    StringBundler query = null;
1986    
1987                    if (orderByComparator != null) {
1988                            query = new StringBundler(6 +
1989                                            (orderByComparator.getOrderByFields().length * 6));
1990                    }
1991                    else {
1992                            query = new StringBundler(3);
1993                    }
1994    
1995                    if (getDB().isSupportsInlineDistinct()) {
1996                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
1997                    }
1998                    else {
1999                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
2000                    }
2001    
2002                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
2003    
2004                    if (!getDB().isSupportsInlineDistinct()) {
2005                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
2006                    }
2007    
2008                    if (orderByComparator != null) {
2009                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2010    
2011                            if (orderByConditionFields.length > 0) {
2012                                    query.append(WHERE_AND);
2013                            }
2014    
2015                            for (int i = 0; i < orderByConditionFields.length; i++) {
2016                                    if (getDB().isSupportsInlineDistinct()) {
2017                                            query.append(_ORDER_BY_ENTITY_ALIAS);
2018                                    }
2019                                    else {
2020                                            query.append(_ORDER_BY_ENTITY_TABLE);
2021                                    }
2022    
2023                                    query.append(orderByConditionFields[i]);
2024    
2025                                    if ((i + 1) < orderByConditionFields.length) {
2026                                            if (orderByComparator.isAscending() ^ previous) {
2027                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2028                                            }
2029                                            else {
2030                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2031                                            }
2032                                    }
2033                                    else {
2034                                            if (orderByComparator.isAscending() ^ previous) {
2035                                                    query.append(WHERE_GREATER_THAN);
2036                                            }
2037                                            else {
2038                                                    query.append(WHERE_LESSER_THAN);
2039                                            }
2040                                    }
2041                            }
2042    
2043                            query.append(ORDER_BY_CLAUSE);
2044    
2045                            String[] orderByFields = orderByComparator.getOrderByFields();
2046    
2047                            for (int i = 0; i < orderByFields.length; i++) {
2048                                    if (getDB().isSupportsInlineDistinct()) {
2049                                            query.append(_ORDER_BY_ENTITY_ALIAS);
2050                                    }
2051                                    else {
2052                                            query.append(_ORDER_BY_ENTITY_TABLE);
2053                                    }
2054    
2055                                    query.append(orderByFields[i]);
2056    
2057                                    if ((i + 1) < orderByFields.length) {
2058                                            if (orderByComparator.isAscending() ^ previous) {
2059                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2060                                            }
2061                                            else {
2062                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2063                                            }
2064                                    }
2065                                    else {
2066                                            if (orderByComparator.isAscending() ^ previous) {
2067                                                    query.append(ORDER_BY_ASC);
2068                                            }
2069                                            else {
2070                                                    query.append(ORDER_BY_DESC);
2071                                            }
2072                                    }
2073                            }
2074                    }
2075                    else {
2076                            if (getDB().isSupportsInlineDistinct()) {
2077                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
2078                            }
2079                            else {
2080                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
2081                            }
2082                    }
2083    
2084                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
2085                                    DDMTemplate.class.getName(),
2086                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
2087    
2088                    SQLQuery q = session.createSQLQuery(sql);
2089    
2090                    q.setFirstResult(0);
2091                    q.setMaxResults(2);
2092    
2093                    if (getDB().isSupportsInlineDistinct()) {
2094                            q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
2095                    }
2096                    else {
2097                            q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
2098                    }
2099    
2100                    QueryPos qPos = QueryPos.getInstance(q);
2101    
2102                    qPos.add(groupId);
2103    
2104                    if (orderByComparator != null) {
2105                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
2106    
2107                            for (Object value : values) {
2108                                    qPos.add(value);
2109                            }
2110                    }
2111    
2112                    List<DDMTemplate> list = q.list();
2113    
2114                    if (list.size() == 2) {
2115                            return list.get(1);
2116                    }
2117                    else {
2118                            return null;
2119                    }
2120            }
2121    
2122            /**
2123             * Removes all the d d m templates where groupId = &#63; from the database.
2124             *
2125             * @param groupId the group ID
2126             * @throws SystemException if a system exception occurred
2127             */
2128            public void removeByGroupId(long groupId) throws SystemException {
2129                    for (DDMTemplate ddmTemplate : findByGroupId(groupId,
2130                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2131                            remove(ddmTemplate);
2132                    }
2133            }
2134    
2135            /**
2136             * Returns the number of d d m templates where groupId = &#63;.
2137             *
2138             * @param groupId the group ID
2139             * @return the number of matching d d m templates
2140             * @throws SystemException if a system exception occurred
2141             */
2142            public int countByGroupId(long groupId) throws SystemException {
2143                    FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUPID;
2144    
2145                    Object[] finderArgs = new Object[] { groupId };
2146    
2147                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2148                                    this);
2149    
2150                    if (count == null) {
2151                            StringBundler query = new StringBundler(2);
2152    
2153                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
2154    
2155                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
2156    
2157                            String sql = query.toString();
2158    
2159                            Session session = null;
2160    
2161                            try {
2162                                    session = openSession();
2163    
2164                                    Query q = session.createQuery(sql);
2165    
2166                                    QueryPos qPos = QueryPos.getInstance(q);
2167    
2168                                    qPos.add(groupId);
2169    
2170                                    count = (Long)q.uniqueResult();
2171    
2172                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2173                            }
2174                            catch (Exception e) {
2175                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2176    
2177                                    throw processException(e);
2178                            }
2179                            finally {
2180                                    closeSession(session);
2181                            }
2182                    }
2183    
2184                    return count.intValue();
2185            }
2186    
2187            /**
2188             * Returns the number of d d m templates that the user has permission to view where groupId = &#63;.
2189             *
2190             * @param groupId the group ID
2191             * @return the number of matching d d m templates that the user has permission to view
2192             * @throws SystemException if a system exception occurred
2193             */
2194            public int filterCountByGroupId(long groupId) throws SystemException {
2195                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
2196                            return countByGroupId(groupId);
2197                    }
2198    
2199                    StringBundler query = new StringBundler(2);
2200    
2201                    query.append(_FILTER_SQL_COUNT_DDMTEMPLATE_WHERE);
2202    
2203                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
2204    
2205                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
2206                                    DDMTemplate.class.getName(),
2207                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
2208    
2209                    Session session = null;
2210    
2211                    try {
2212                            session = openSession();
2213    
2214                            SQLQuery q = session.createSQLQuery(sql);
2215    
2216                            q.addScalar(COUNT_COLUMN_NAME,
2217                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
2218    
2219                            QueryPos qPos = QueryPos.getInstance(q);
2220    
2221                            qPos.add(groupId);
2222    
2223                            Long count = (Long)q.uniqueResult();
2224    
2225                            return count.intValue();
2226                    }
2227                    catch (Exception e) {
2228                            throw processException(e);
2229                    }
2230                    finally {
2231                            closeSession(session);
2232                    }
2233            }
2234    
2235            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "ddmTemplate.groupId = ?";
2236            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSPK = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2237                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
2238                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByClassPK",
2239                            new String[] {
2240                                    Long.class.getName(),
2241                                    
2242                            Integer.class.getName(), Integer.class.getName(),
2243                                    OrderByComparator.class.getName()
2244                            });
2245            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK =
2246                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2247                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
2248                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByClassPK",
2249                            new String[] { Long.class.getName() },
2250                            DDMTemplateModelImpl.CLASSPK_COLUMN_BITMASK);
2251            public static final FinderPath FINDER_PATH_COUNT_BY_CLASSPK = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2252                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
2253                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByClassPK",
2254                            new String[] { Long.class.getName() });
2255    
2256            /**
2257             * Returns all the d d m templates where classPK = &#63;.
2258             *
2259             * @param classPK the class p k
2260             * @return the matching d d m templates
2261             * @throws SystemException if a system exception occurred
2262             */
2263            public List<DDMTemplate> findByClassPK(long classPK)
2264                    throws SystemException {
2265                    return findByClassPK(classPK, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2266            }
2267    
2268            /**
2269             * Returns a range of all the d d m templates where classPK = &#63;.
2270             *
2271             * <p>
2272             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
2273             * </p>
2274             *
2275             * @param classPK the class p k
2276             * @param start the lower bound of the range of d d m templates
2277             * @param end the upper bound of the range of d d m templates (not inclusive)
2278             * @return the range of matching d d m templates
2279             * @throws SystemException if a system exception occurred
2280             */
2281            public List<DDMTemplate> findByClassPK(long classPK, int start, int end)
2282                    throws SystemException {
2283                    return findByClassPK(classPK, start, end, null);
2284            }
2285    
2286            /**
2287             * Returns an ordered range of all the d d m templates where classPK = &#63;.
2288             *
2289             * <p>
2290             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
2291             * </p>
2292             *
2293             * @param classPK the class p k
2294             * @param start the lower bound of the range of d d m templates
2295             * @param end the upper bound of the range of d d m templates (not inclusive)
2296             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2297             * @return the ordered range of matching d d m templates
2298             * @throws SystemException if a system exception occurred
2299             */
2300            public List<DDMTemplate> findByClassPK(long classPK, int start, int end,
2301                    OrderByComparator orderByComparator) throws SystemException {
2302                    boolean pagination = true;
2303                    FinderPath finderPath = null;
2304                    Object[] finderArgs = null;
2305    
2306                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2307                                    (orderByComparator == null)) {
2308                            pagination = false;
2309                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK;
2310                            finderArgs = new Object[] { classPK };
2311                    }
2312                    else {
2313                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSPK;
2314                            finderArgs = new Object[] { classPK, start, end, orderByComparator };
2315                    }
2316    
2317                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
2318                                    finderArgs, this);
2319    
2320                    if ((list != null) && !list.isEmpty()) {
2321                            for (DDMTemplate ddmTemplate : list) {
2322                                    if ((classPK != ddmTemplate.getClassPK())) {
2323                                            list = null;
2324    
2325                                            break;
2326                                    }
2327                            }
2328                    }
2329    
2330                    if (list == null) {
2331                            StringBundler query = null;
2332    
2333                            if (orderByComparator != null) {
2334                                    query = new StringBundler(3 +
2335                                                    (orderByComparator.getOrderByFields().length * 3));
2336                            }
2337                            else {
2338                                    query = new StringBundler(3);
2339                            }
2340    
2341                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
2342    
2343                            query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
2344    
2345                            if (orderByComparator != null) {
2346                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2347                                            orderByComparator);
2348                            }
2349                            else
2350                             if (pagination) {
2351                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
2352                            }
2353    
2354                            String sql = query.toString();
2355    
2356                            Session session = null;
2357    
2358                            try {
2359                                    session = openSession();
2360    
2361                                    Query q = session.createQuery(sql);
2362    
2363                                    QueryPos qPos = QueryPos.getInstance(q);
2364    
2365                                    qPos.add(classPK);
2366    
2367                                    if (!pagination) {
2368                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
2369                                                            start, end, false);
2370    
2371                                            Collections.sort(list);
2372    
2373                                            list = new UnmodifiableList<DDMTemplate>(list);
2374                                    }
2375                                    else {
2376                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
2377                                                            start, end);
2378                                    }
2379    
2380                                    cacheResult(list);
2381    
2382                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2383                            }
2384                            catch (Exception e) {
2385                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2386    
2387                                    throw processException(e);
2388                            }
2389                            finally {
2390                                    closeSession(session);
2391                            }
2392                    }
2393    
2394                    return list;
2395            }
2396    
2397            /**
2398             * Returns the first d d m template in the ordered set where classPK = &#63;.
2399             *
2400             * @param classPK the class p k
2401             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2402             * @return the first matching d d m template
2403             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
2404             * @throws SystemException if a system exception occurred
2405             */
2406            public DDMTemplate findByClassPK_First(long classPK,
2407                    OrderByComparator orderByComparator)
2408                    throws NoSuchTemplateException, SystemException {
2409                    DDMTemplate ddmTemplate = fetchByClassPK_First(classPK,
2410                                    orderByComparator);
2411    
2412                    if (ddmTemplate != null) {
2413                            return ddmTemplate;
2414                    }
2415    
2416                    StringBundler msg = new StringBundler(4);
2417    
2418                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2419    
2420                    msg.append("classPK=");
2421                    msg.append(classPK);
2422    
2423                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2424    
2425                    throw new NoSuchTemplateException(msg.toString());
2426            }
2427    
2428            /**
2429             * Returns the first d d m template in the ordered set where classPK = &#63;.
2430             *
2431             * @param classPK the class p k
2432             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2433             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
2434             * @throws SystemException if a system exception occurred
2435             */
2436            public DDMTemplate fetchByClassPK_First(long classPK,
2437                    OrderByComparator orderByComparator) throws SystemException {
2438                    List<DDMTemplate> list = findByClassPK(classPK, 0, 1, orderByComparator);
2439    
2440                    if (!list.isEmpty()) {
2441                            return list.get(0);
2442                    }
2443    
2444                    return null;
2445            }
2446    
2447            /**
2448             * Returns the last d d m template in the ordered set where classPK = &#63;.
2449             *
2450             * @param classPK the class p k
2451             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2452             * @return the last matching d d m template
2453             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
2454             * @throws SystemException if a system exception occurred
2455             */
2456            public DDMTemplate findByClassPK_Last(long classPK,
2457                    OrderByComparator orderByComparator)
2458                    throws NoSuchTemplateException, SystemException {
2459                    DDMTemplate ddmTemplate = fetchByClassPK_Last(classPK, orderByComparator);
2460    
2461                    if (ddmTemplate != null) {
2462                            return ddmTemplate;
2463                    }
2464    
2465                    StringBundler msg = new StringBundler(4);
2466    
2467                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2468    
2469                    msg.append("classPK=");
2470                    msg.append(classPK);
2471    
2472                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2473    
2474                    throw new NoSuchTemplateException(msg.toString());
2475            }
2476    
2477            /**
2478             * Returns the last d d m template in the ordered set where classPK = &#63;.
2479             *
2480             * @param classPK the class p k
2481             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2482             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
2483             * @throws SystemException if a system exception occurred
2484             */
2485            public DDMTemplate fetchByClassPK_Last(long classPK,
2486                    OrderByComparator orderByComparator) throws SystemException {
2487                    int count = countByClassPK(classPK);
2488    
2489                    List<DDMTemplate> list = findByClassPK(classPK, count - 1, count,
2490                                    orderByComparator);
2491    
2492                    if (!list.isEmpty()) {
2493                            return list.get(0);
2494                    }
2495    
2496                    return null;
2497            }
2498    
2499            /**
2500             * Returns the d d m templates before and after the current d d m template in the ordered set where classPK = &#63;.
2501             *
2502             * @param templateId the primary key of the current d d m template
2503             * @param classPK the class p k
2504             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2505             * @return the previous, current, and next d d m template
2506             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
2507             * @throws SystemException if a system exception occurred
2508             */
2509            public DDMTemplate[] findByClassPK_PrevAndNext(long templateId,
2510                    long classPK, OrderByComparator orderByComparator)
2511                    throws NoSuchTemplateException, SystemException {
2512                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
2513    
2514                    Session session = null;
2515    
2516                    try {
2517                            session = openSession();
2518    
2519                            DDMTemplate[] array = new DDMTemplateImpl[3];
2520    
2521                            array[0] = getByClassPK_PrevAndNext(session, ddmTemplate, classPK,
2522                                            orderByComparator, true);
2523    
2524                            array[1] = ddmTemplate;
2525    
2526                            array[2] = getByClassPK_PrevAndNext(session, ddmTemplate, classPK,
2527                                            orderByComparator, false);
2528    
2529                            return array;
2530                    }
2531                    catch (Exception e) {
2532                            throw processException(e);
2533                    }
2534                    finally {
2535                            closeSession(session);
2536                    }
2537            }
2538    
2539            protected DDMTemplate getByClassPK_PrevAndNext(Session session,
2540                    DDMTemplate ddmTemplate, long classPK,
2541                    OrderByComparator orderByComparator, boolean previous) {
2542                    StringBundler query = null;
2543    
2544                    if (orderByComparator != null) {
2545                            query = new StringBundler(6 +
2546                                            (orderByComparator.getOrderByFields().length * 6));
2547                    }
2548                    else {
2549                            query = new StringBundler(3);
2550                    }
2551    
2552                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
2553    
2554                    query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
2555    
2556                    if (orderByComparator != null) {
2557                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2558    
2559                            if (orderByConditionFields.length > 0) {
2560                                    query.append(WHERE_AND);
2561                            }
2562    
2563                            for (int i = 0; i < orderByConditionFields.length; i++) {
2564                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2565                                    query.append(orderByConditionFields[i]);
2566    
2567                                    if ((i + 1) < orderByConditionFields.length) {
2568                                            if (orderByComparator.isAscending() ^ previous) {
2569                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2570                                            }
2571                                            else {
2572                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2573                                            }
2574                                    }
2575                                    else {
2576                                            if (orderByComparator.isAscending() ^ previous) {
2577                                                    query.append(WHERE_GREATER_THAN);
2578                                            }
2579                                            else {
2580                                                    query.append(WHERE_LESSER_THAN);
2581                                            }
2582                                    }
2583                            }
2584    
2585                            query.append(ORDER_BY_CLAUSE);
2586    
2587                            String[] orderByFields = orderByComparator.getOrderByFields();
2588    
2589                            for (int i = 0; i < orderByFields.length; i++) {
2590                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2591                                    query.append(orderByFields[i]);
2592    
2593                                    if ((i + 1) < orderByFields.length) {
2594                                            if (orderByComparator.isAscending() ^ previous) {
2595                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2596                                            }
2597                                            else {
2598                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2599                                            }
2600                                    }
2601                                    else {
2602                                            if (orderByComparator.isAscending() ^ previous) {
2603                                                    query.append(ORDER_BY_ASC);
2604                                            }
2605                                            else {
2606                                                    query.append(ORDER_BY_DESC);
2607                                            }
2608                                    }
2609                            }
2610                    }
2611                    else {
2612                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
2613                    }
2614    
2615                    String sql = query.toString();
2616    
2617                    Query q = session.createQuery(sql);
2618    
2619                    q.setFirstResult(0);
2620                    q.setMaxResults(2);
2621    
2622                    QueryPos qPos = QueryPos.getInstance(q);
2623    
2624                    qPos.add(classPK);
2625    
2626                    if (orderByComparator != null) {
2627                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
2628    
2629                            for (Object value : values) {
2630                                    qPos.add(value);
2631                            }
2632                    }
2633    
2634                    List<DDMTemplate> list = q.list();
2635    
2636                    if (list.size() == 2) {
2637                            return list.get(1);
2638                    }
2639                    else {
2640                            return null;
2641                    }
2642            }
2643    
2644            /**
2645             * Removes all the d d m templates where classPK = &#63; from the database.
2646             *
2647             * @param classPK the class p k
2648             * @throws SystemException if a system exception occurred
2649             */
2650            public void removeByClassPK(long classPK) throws SystemException {
2651                    for (DDMTemplate ddmTemplate : findByClassPK(classPK,
2652                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2653                            remove(ddmTemplate);
2654                    }
2655            }
2656    
2657            /**
2658             * Returns the number of d d m templates where classPK = &#63;.
2659             *
2660             * @param classPK the class p k
2661             * @return the number of matching d d m templates
2662             * @throws SystemException if a system exception occurred
2663             */
2664            public int countByClassPK(long classPK) throws SystemException {
2665                    FinderPath finderPath = FINDER_PATH_COUNT_BY_CLASSPK;
2666    
2667                    Object[] finderArgs = new Object[] { classPK };
2668    
2669                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2670                                    this);
2671    
2672                    if (count == null) {
2673                            StringBundler query = new StringBundler(2);
2674    
2675                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
2676    
2677                            query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
2678    
2679                            String sql = query.toString();
2680    
2681                            Session session = null;
2682    
2683                            try {
2684                                    session = openSession();
2685    
2686                                    Query q = session.createQuery(sql);
2687    
2688                                    QueryPos qPos = QueryPos.getInstance(q);
2689    
2690                                    qPos.add(classPK);
2691    
2692                                    count = (Long)q.uniqueResult();
2693    
2694                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2695                            }
2696                            catch (Exception e) {
2697                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2698    
2699                                    throw processException(e);
2700                            }
2701                            finally {
2702                                    closeSession(session);
2703                            }
2704                    }
2705    
2706                    return count.intValue();
2707            }
2708    
2709            private static final String _FINDER_COLUMN_CLASSPK_CLASSPK_2 = "ddmTemplate.classPK = ?";
2710            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TEMPLATEKEY =
2711                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2712                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
2713                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTemplateKey",
2714                            new String[] {
2715                                    String.class.getName(),
2716                                    
2717                            Integer.class.getName(), Integer.class.getName(),
2718                                    OrderByComparator.class.getName()
2719                            });
2720            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TEMPLATEKEY =
2721                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2722                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
2723                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByTemplateKey",
2724                            new String[] { String.class.getName() },
2725                            DDMTemplateModelImpl.TEMPLATEKEY_COLUMN_BITMASK);
2726            public static final FinderPath FINDER_PATH_COUNT_BY_TEMPLATEKEY = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
2727                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
2728                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByTemplateKey",
2729                            new String[] { String.class.getName() });
2730    
2731            /**
2732             * Returns all the d d m templates where templateKey = &#63;.
2733             *
2734             * @param templateKey the template key
2735             * @return the matching d d m templates
2736             * @throws SystemException if a system exception occurred
2737             */
2738            public List<DDMTemplate> findByTemplateKey(String templateKey)
2739                    throws SystemException {
2740                    return findByTemplateKey(templateKey, QueryUtil.ALL_POS,
2741                            QueryUtil.ALL_POS, null);
2742            }
2743    
2744            /**
2745             * Returns a range of all the d d m templates where templateKey = &#63;.
2746             *
2747             * <p>
2748             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
2749             * </p>
2750             *
2751             * @param templateKey the template key
2752             * @param start the lower bound of the range of d d m templates
2753             * @param end the upper bound of the range of d d m templates (not inclusive)
2754             * @return the range of matching d d m templates
2755             * @throws SystemException if a system exception occurred
2756             */
2757            public List<DDMTemplate> findByTemplateKey(String templateKey, int start,
2758                    int end) throws SystemException {
2759                    return findByTemplateKey(templateKey, start, end, null);
2760            }
2761    
2762            /**
2763             * Returns an ordered range of all the d d m templates where templateKey = &#63;.
2764             *
2765             * <p>
2766             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
2767             * </p>
2768             *
2769             * @param templateKey the template key
2770             * @param start the lower bound of the range of d d m templates
2771             * @param end the upper bound of the range of d d m templates (not inclusive)
2772             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2773             * @return the ordered range of matching d d m templates
2774             * @throws SystemException if a system exception occurred
2775             */
2776            public List<DDMTemplate> findByTemplateKey(String templateKey, int start,
2777                    int end, OrderByComparator orderByComparator) throws SystemException {
2778                    boolean pagination = true;
2779                    FinderPath finderPath = null;
2780                    Object[] finderArgs = null;
2781    
2782                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2783                                    (orderByComparator == null)) {
2784                            pagination = false;
2785                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TEMPLATEKEY;
2786                            finderArgs = new Object[] { templateKey };
2787                    }
2788                    else {
2789                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TEMPLATEKEY;
2790                            finderArgs = new Object[] { templateKey, start, end, orderByComparator };
2791                    }
2792    
2793                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
2794                                    finderArgs, this);
2795    
2796                    if ((list != null) && !list.isEmpty()) {
2797                            for (DDMTemplate ddmTemplate : list) {
2798                                    if (!Validator.equals(templateKey, ddmTemplate.getTemplateKey())) {
2799                                            list = null;
2800    
2801                                            break;
2802                                    }
2803                            }
2804                    }
2805    
2806                    if (list == null) {
2807                            StringBundler query = null;
2808    
2809                            if (orderByComparator != null) {
2810                                    query = new StringBundler(3 +
2811                                                    (orderByComparator.getOrderByFields().length * 3));
2812                            }
2813                            else {
2814                                    query = new StringBundler(3);
2815                            }
2816    
2817                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
2818    
2819                            if (templateKey == null) {
2820                                    query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_1);
2821                            }
2822                            else {
2823                                    if (templateKey.equals(StringPool.BLANK)) {
2824                                            query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_3);
2825                                    }
2826                                    else {
2827                                            query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_2);
2828                                    }
2829                            }
2830    
2831                            if (orderByComparator != null) {
2832                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2833                                            orderByComparator);
2834                            }
2835                            else
2836                             if (pagination) {
2837                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
2838                            }
2839    
2840                            String sql = query.toString();
2841    
2842                            Session session = null;
2843    
2844                            try {
2845                                    session = openSession();
2846    
2847                                    Query q = session.createQuery(sql);
2848    
2849                                    QueryPos qPos = QueryPos.getInstance(q);
2850    
2851                                    if (templateKey != null) {
2852                                            qPos.add(templateKey);
2853                                    }
2854    
2855                                    if (!pagination) {
2856                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
2857                                                            start, end, false);
2858    
2859                                            Collections.sort(list);
2860    
2861                                            list = new UnmodifiableList<DDMTemplate>(list);
2862                                    }
2863                                    else {
2864                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
2865                                                            start, end);
2866                                    }
2867    
2868                                    cacheResult(list);
2869    
2870                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2871                            }
2872                            catch (Exception e) {
2873                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2874    
2875                                    throw processException(e);
2876                            }
2877                            finally {
2878                                    closeSession(session);
2879                            }
2880                    }
2881    
2882                    return list;
2883            }
2884    
2885            /**
2886             * Returns the first d d m template in the ordered set where templateKey = &#63;.
2887             *
2888             * @param templateKey the template key
2889             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2890             * @return the first matching d d m template
2891             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
2892             * @throws SystemException if a system exception occurred
2893             */
2894            public DDMTemplate findByTemplateKey_First(String templateKey,
2895                    OrderByComparator orderByComparator)
2896                    throws NoSuchTemplateException, SystemException {
2897                    DDMTemplate ddmTemplate = fetchByTemplateKey_First(templateKey,
2898                                    orderByComparator);
2899    
2900                    if (ddmTemplate != null) {
2901                            return ddmTemplate;
2902                    }
2903    
2904                    StringBundler msg = new StringBundler(4);
2905    
2906                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2907    
2908                    msg.append("templateKey=");
2909                    msg.append(templateKey);
2910    
2911                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2912    
2913                    throw new NoSuchTemplateException(msg.toString());
2914            }
2915    
2916            /**
2917             * Returns the first d d m template in the ordered set where templateKey = &#63;.
2918             *
2919             * @param templateKey the template key
2920             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2921             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
2922             * @throws SystemException if a system exception occurred
2923             */
2924            public DDMTemplate fetchByTemplateKey_First(String templateKey,
2925                    OrderByComparator orderByComparator) throws SystemException {
2926                    List<DDMTemplate> list = findByTemplateKey(templateKey, 0, 1,
2927                                    orderByComparator);
2928    
2929                    if (!list.isEmpty()) {
2930                            return list.get(0);
2931                    }
2932    
2933                    return null;
2934            }
2935    
2936            /**
2937             * Returns the last d d m template in the ordered set where templateKey = &#63;.
2938             *
2939             * @param templateKey the template key
2940             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2941             * @return the last matching d d m template
2942             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
2943             * @throws SystemException if a system exception occurred
2944             */
2945            public DDMTemplate findByTemplateKey_Last(String templateKey,
2946                    OrderByComparator orderByComparator)
2947                    throws NoSuchTemplateException, SystemException {
2948                    DDMTemplate ddmTemplate = fetchByTemplateKey_Last(templateKey,
2949                                    orderByComparator);
2950    
2951                    if (ddmTemplate != null) {
2952                            return ddmTemplate;
2953                    }
2954    
2955                    StringBundler msg = new StringBundler(4);
2956    
2957                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2958    
2959                    msg.append("templateKey=");
2960                    msg.append(templateKey);
2961    
2962                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2963    
2964                    throw new NoSuchTemplateException(msg.toString());
2965            }
2966    
2967            /**
2968             * Returns the last d d m template in the ordered set where templateKey = &#63;.
2969             *
2970             * @param templateKey the template key
2971             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2972             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
2973             * @throws SystemException if a system exception occurred
2974             */
2975            public DDMTemplate fetchByTemplateKey_Last(String templateKey,
2976                    OrderByComparator orderByComparator) throws SystemException {
2977                    int count = countByTemplateKey(templateKey);
2978    
2979                    List<DDMTemplate> list = findByTemplateKey(templateKey, count - 1,
2980                                    count, orderByComparator);
2981    
2982                    if (!list.isEmpty()) {
2983                            return list.get(0);
2984                    }
2985    
2986                    return null;
2987            }
2988    
2989            /**
2990             * Returns the d d m templates before and after the current d d m template in the ordered set where templateKey = &#63;.
2991             *
2992             * @param templateId the primary key of the current d d m template
2993             * @param templateKey the template key
2994             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2995             * @return the previous, current, and next d d m template
2996             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
2997             * @throws SystemException if a system exception occurred
2998             */
2999            public DDMTemplate[] findByTemplateKey_PrevAndNext(long templateId,
3000                    String templateKey, OrderByComparator orderByComparator)
3001                    throws NoSuchTemplateException, SystemException {
3002                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
3003    
3004                    Session session = null;
3005    
3006                    try {
3007                            session = openSession();
3008    
3009                            DDMTemplate[] array = new DDMTemplateImpl[3];
3010    
3011                            array[0] = getByTemplateKey_PrevAndNext(session, ddmTemplate,
3012                                            templateKey, orderByComparator, true);
3013    
3014                            array[1] = ddmTemplate;
3015    
3016                            array[2] = getByTemplateKey_PrevAndNext(session, ddmTemplate,
3017                                            templateKey, orderByComparator, false);
3018    
3019                            return array;
3020                    }
3021                    catch (Exception e) {
3022                            throw processException(e);
3023                    }
3024                    finally {
3025                            closeSession(session);
3026                    }
3027            }
3028    
3029            protected DDMTemplate getByTemplateKey_PrevAndNext(Session session,
3030                    DDMTemplate ddmTemplate, String templateKey,
3031                    OrderByComparator orderByComparator, boolean previous) {
3032                    StringBundler query = null;
3033    
3034                    if (orderByComparator != null) {
3035                            query = new StringBundler(6 +
3036                                            (orderByComparator.getOrderByFields().length * 6));
3037                    }
3038                    else {
3039                            query = new StringBundler(3);
3040                    }
3041    
3042                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
3043    
3044                    if (templateKey == null) {
3045                            query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_1);
3046                    }
3047                    else {
3048                            if (templateKey.equals(StringPool.BLANK)) {
3049                                    query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_3);
3050                            }
3051                            else {
3052                                    query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_2);
3053                            }
3054                    }
3055    
3056                    if (orderByComparator != null) {
3057                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3058    
3059                            if (orderByConditionFields.length > 0) {
3060                                    query.append(WHERE_AND);
3061                            }
3062    
3063                            for (int i = 0; i < orderByConditionFields.length; i++) {
3064                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3065                                    query.append(orderByConditionFields[i]);
3066    
3067                                    if ((i + 1) < orderByConditionFields.length) {
3068                                            if (orderByComparator.isAscending() ^ previous) {
3069                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3070                                            }
3071                                            else {
3072                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3073                                            }
3074                                    }
3075                                    else {
3076                                            if (orderByComparator.isAscending() ^ previous) {
3077                                                    query.append(WHERE_GREATER_THAN);
3078                                            }
3079                                            else {
3080                                                    query.append(WHERE_LESSER_THAN);
3081                                            }
3082                                    }
3083                            }
3084    
3085                            query.append(ORDER_BY_CLAUSE);
3086    
3087                            String[] orderByFields = orderByComparator.getOrderByFields();
3088    
3089                            for (int i = 0; i < orderByFields.length; i++) {
3090                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3091                                    query.append(orderByFields[i]);
3092    
3093                                    if ((i + 1) < orderByFields.length) {
3094                                            if (orderByComparator.isAscending() ^ previous) {
3095                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3096                                            }
3097                                            else {
3098                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3099                                            }
3100                                    }
3101                                    else {
3102                                            if (orderByComparator.isAscending() ^ previous) {
3103                                                    query.append(ORDER_BY_ASC);
3104                                            }
3105                                            else {
3106                                                    query.append(ORDER_BY_DESC);
3107                                            }
3108                                    }
3109                            }
3110                    }
3111                    else {
3112                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
3113                    }
3114    
3115                    String sql = query.toString();
3116    
3117                    Query q = session.createQuery(sql);
3118    
3119                    q.setFirstResult(0);
3120                    q.setMaxResults(2);
3121    
3122                    QueryPos qPos = QueryPos.getInstance(q);
3123    
3124                    if (templateKey != null) {
3125                            qPos.add(templateKey);
3126                    }
3127    
3128                    if (orderByComparator != null) {
3129                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
3130    
3131                            for (Object value : values) {
3132                                    qPos.add(value);
3133                            }
3134                    }
3135    
3136                    List<DDMTemplate> list = q.list();
3137    
3138                    if (list.size() == 2) {
3139                            return list.get(1);
3140                    }
3141                    else {
3142                            return null;
3143                    }
3144            }
3145    
3146            /**
3147             * Removes all the d d m templates where templateKey = &#63; from the database.
3148             *
3149             * @param templateKey the template key
3150             * @throws SystemException if a system exception occurred
3151             */
3152            public void removeByTemplateKey(String templateKey)
3153                    throws SystemException {
3154                    for (DDMTemplate ddmTemplate : findByTemplateKey(templateKey,
3155                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3156                            remove(ddmTemplate);
3157                    }
3158            }
3159    
3160            /**
3161             * Returns the number of d d m templates where templateKey = &#63;.
3162             *
3163             * @param templateKey the template key
3164             * @return the number of matching d d m templates
3165             * @throws SystemException if a system exception occurred
3166             */
3167            public int countByTemplateKey(String templateKey) throws SystemException {
3168                    FinderPath finderPath = FINDER_PATH_COUNT_BY_TEMPLATEKEY;
3169    
3170                    Object[] finderArgs = new Object[] { templateKey };
3171    
3172                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3173                                    this);
3174    
3175                    if (count == null) {
3176                            StringBundler query = new StringBundler(2);
3177    
3178                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
3179    
3180                            if (templateKey == null) {
3181                                    query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_1);
3182                            }
3183                            else {
3184                                    if (templateKey.equals(StringPool.BLANK)) {
3185                                            query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_3);
3186                                    }
3187                                    else {
3188                                            query.append(_FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_2);
3189                                    }
3190                            }
3191    
3192                            String sql = query.toString();
3193    
3194                            Session session = null;
3195    
3196                            try {
3197                                    session = openSession();
3198    
3199                                    Query q = session.createQuery(sql);
3200    
3201                                    QueryPos qPos = QueryPos.getInstance(q);
3202    
3203                                    if (templateKey != null) {
3204                                            qPos.add(templateKey);
3205                                    }
3206    
3207                                    count = (Long)q.uniqueResult();
3208    
3209                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3210                            }
3211                            catch (Exception e) {
3212                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3213    
3214                                    throw processException(e);
3215                            }
3216                            finally {
3217                                    closeSession(session);
3218                            }
3219                    }
3220    
3221                    return count.intValue();
3222            }
3223    
3224            private static final String _FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_1 = "ddmTemplate.templateKey IS NULL";
3225            private static final String _FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_2 = "ddmTemplate.templateKey = ?";
3226            private static final String _FINDER_COLUMN_TEMPLATEKEY_TEMPLATEKEY_3 = "(ddmTemplate.templateKey IS NULL OR ddmTemplate.templateKey = ?)";
3227            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TYPE = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3228                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
3229                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByType",
3230                            new String[] {
3231                                    String.class.getName(),
3232                                    
3233                            Integer.class.getName(), Integer.class.getName(),
3234                                    OrderByComparator.class.getName()
3235                            });
3236            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3237                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
3238                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByType",
3239                            new String[] { String.class.getName() },
3240                            DDMTemplateModelImpl.TYPE_COLUMN_BITMASK);
3241            public static final FinderPath FINDER_PATH_COUNT_BY_TYPE = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3242                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
3243                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByType",
3244                            new String[] { String.class.getName() });
3245    
3246            /**
3247             * Returns all the d d m templates where type = &#63;.
3248             *
3249             * @param type the type
3250             * @return the matching d d m templates
3251             * @throws SystemException if a system exception occurred
3252             */
3253            public List<DDMTemplate> findByType(String type) throws SystemException {
3254                    return findByType(type, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3255            }
3256    
3257            /**
3258             * Returns a range of all the d d m templates where type = &#63;.
3259             *
3260             * <p>
3261             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
3262             * </p>
3263             *
3264             * @param type the type
3265             * @param start the lower bound of the range of d d m templates
3266             * @param end the upper bound of the range of d d m templates (not inclusive)
3267             * @return the range of matching d d m templates
3268             * @throws SystemException if a system exception occurred
3269             */
3270            public List<DDMTemplate> findByType(String type, int start, int end)
3271                    throws SystemException {
3272                    return findByType(type, start, end, null);
3273            }
3274    
3275            /**
3276             * Returns an ordered range of all the d d m templates where type = &#63;.
3277             *
3278             * <p>
3279             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
3280             * </p>
3281             *
3282             * @param type the type
3283             * @param start the lower bound of the range of d d m templates
3284             * @param end the upper bound of the range of d d m templates (not inclusive)
3285             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3286             * @return the ordered range of matching d d m templates
3287             * @throws SystemException if a system exception occurred
3288             */
3289            public List<DDMTemplate> findByType(String type, int start, int end,
3290                    OrderByComparator orderByComparator) throws SystemException {
3291                    boolean pagination = true;
3292                    FinderPath finderPath = null;
3293                    Object[] finderArgs = null;
3294    
3295                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3296                                    (orderByComparator == null)) {
3297                            pagination = false;
3298                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE;
3299                            finderArgs = new Object[] { type };
3300                    }
3301                    else {
3302                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TYPE;
3303                            finderArgs = new Object[] { type, start, end, orderByComparator };
3304                    }
3305    
3306                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
3307                                    finderArgs, this);
3308    
3309                    if ((list != null) && !list.isEmpty()) {
3310                            for (DDMTemplate ddmTemplate : list) {
3311                                    if (!Validator.equals(type, ddmTemplate.getType())) {
3312                                            list = null;
3313    
3314                                            break;
3315                                    }
3316                            }
3317                    }
3318    
3319                    if (list == null) {
3320                            StringBundler query = null;
3321    
3322                            if (orderByComparator != null) {
3323                                    query = new StringBundler(3 +
3324                                                    (orderByComparator.getOrderByFields().length * 3));
3325                            }
3326                            else {
3327                                    query = new StringBundler(3);
3328                            }
3329    
3330                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
3331    
3332                            if (type == null) {
3333                                    query.append(_FINDER_COLUMN_TYPE_TYPE_1);
3334                            }
3335                            else {
3336                                    if (type.equals(StringPool.BLANK)) {
3337                                            query.append(_FINDER_COLUMN_TYPE_TYPE_3);
3338                                    }
3339                                    else {
3340                                            query.append(_FINDER_COLUMN_TYPE_TYPE_2);
3341                                    }
3342                            }
3343    
3344                            if (orderByComparator != null) {
3345                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3346                                            orderByComparator);
3347                            }
3348                            else
3349                             if (pagination) {
3350                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
3351                            }
3352    
3353                            String sql = query.toString();
3354    
3355                            Session session = null;
3356    
3357                            try {
3358                                    session = openSession();
3359    
3360                                    Query q = session.createQuery(sql);
3361    
3362                                    QueryPos qPos = QueryPos.getInstance(q);
3363    
3364                                    if (type != null) {
3365                                            qPos.add(type);
3366                                    }
3367    
3368                                    if (!pagination) {
3369                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
3370                                                            start, end, false);
3371    
3372                                            Collections.sort(list);
3373    
3374                                            list = new UnmodifiableList<DDMTemplate>(list);
3375                                    }
3376                                    else {
3377                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
3378                                                            start, end);
3379                                    }
3380    
3381                                    cacheResult(list);
3382    
3383                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3384                            }
3385                            catch (Exception e) {
3386                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3387    
3388                                    throw processException(e);
3389                            }
3390                            finally {
3391                                    closeSession(session);
3392                            }
3393                    }
3394    
3395                    return list;
3396            }
3397    
3398            /**
3399             * Returns the first d d m template in the ordered set where type = &#63;.
3400             *
3401             * @param type the type
3402             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3403             * @return the first matching d d m template
3404             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
3405             * @throws SystemException if a system exception occurred
3406             */
3407            public DDMTemplate findByType_First(String type,
3408                    OrderByComparator orderByComparator)
3409                    throws NoSuchTemplateException, SystemException {
3410                    DDMTemplate ddmTemplate = fetchByType_First(type, orderByComparator);
3411    
3412                    if (ddmTemplate != null) {
3413                            return ddmTemplate;
3414                    }
3415    
3416                    StringBundler msg = new StringBundler(4);
3417    
3418                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3419    
3420                    msg.append("type=");
3421                    msg.append(type);
3422    
3423                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3424    
3425                    throw new NoSuchTemplateException(msg.toString());
3426            }
3427    
3428            /**
3429             * Returns the first d d m template in the ordered set where type = &#63;.
3430             *
3431             * @param type the type
3432             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3433             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
3434             * @throws SystemException if a system exception occurred
3435             */
3436            public DDMTemplate fetchByType_First(String type,
3437                    OrderByComparator orderByComparator) throws SystemException {
3438                    List<DDMTemplate> list = findByType(type, 0, 1, orderByComparator);
3439    
3440                    if (!list.isEmpty()) {
3441                            return list.get(0);
3442                    }
3443    
3444                    return null;
3445            }
3446    
3447            /**
3448             * Returns the last d d m template in the ordered set where type = &#63;.
3449             *
3450             * @param type the type
3451             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3452             * @return the last matching d d m template
3453             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
3454             * @throws SystemException if a system exception occurred
3455             */
3456            public DDMTemplate findByType_Last(String type,
3457                    OrderByComparator orderByComparator)
3458                    throws NoSuchTemplateException, SystemException {
3459                    DDMTemplate ddmTemplate = fetchByType_Last(type, orderByComparator);
3460    
3461                    if (ddmTemplate != null) {
3462                            return ddmTemplate;
3463                    }
3464    
3465                    StringBundler msg = new StringBundler(4);
3466    
3467                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3468    
3469                    msg.append("type=");
3470                    msg.append(type);
3471    
3472                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3473    
3474                    throw new NoSuchTemplateException(msg.toString());
3475            }
3476    
3477            /**
3478             * Returns the last d d m template in the ordered set where type = &#63;.
3479             *
3480             * @param type the type
3481             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3482             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
3483             * @throws SystemException if a system exception occurred
3484             */
3485            public DDMTemplate fetchByType_Last(String type,
3486                    OrderByComparator orderByComparator) throws SystemException {
3487                    int count = countByType(type);
3488    
3489                    List<DDMTemplate> list = findByType(type, count - 1, count,
3490                                    orderByComparator);
3491    
3492                    if (!list.isEmpty()) {
3493                            return list.get(0);
3494                    }
3495    
3496                    return null;
3497            }
3498    
3499            /**
3500             * Returns the d d m templates before and after the current d d m template in the ordered set where type = &#63;.
3501             *
3502             * @param templateId the primary key of the current d d m template
3503             * @param type the type
3504             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3505             * @return the previous, current, and next d d m template
3506             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
3507             * @throws SystemException if a system exception occurred
3508             */
3509            public DDMTemplate[] findByType_PrevAndNext(long templateId, String type,
3510                    OrderByComparator orderByComparator)
3511                    throws NoSuchTemplateException, SystemException {
3512                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
3513    
3514                    Session session = null;
3515    
3516                    try {
3517                            session = openSession();
3518    
3519                            DDMTemplate[] array = new DDMTemplateImpl[3];
3520    
3521                            array[0] = getByType_PrevAndNext(session, ddmTemplate, type,
3522                                            orderByComparator, true);
3523    
3524                            array[1] = ddmTemplate;
3525    
3526                            array[2] = getByType_PrevAndNext(session, ddmTemplate, type,
3527                                            orderByComparator, false);
3528    
3529                            return array;
3530                    }
3531                    catch (Exception e) {
3532                            throw processException(e);
3533                    }
3534                    finally {
3535                            closeSession(session);
3536                    }
3537            }
3538    
3539            protected DDMTemplate getByType_PrevAndNext(Session session,
3540                    DDMTemplate ddmTemplate, String type,
3541                    OrderByComparator orderByComparator, boolean previous) {
3542                    StringBundler query = null;
3543    
3544                    if (orderByComparator != null) {
3545                            query = new StringBundler(6 +
3546                                            (orderByComparator.getOrderByFields().length * 6));
3547                    }
3548                    else {
3549                            query = new StringBundler(3);
3550                    }
3551    
3552                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
3553    
3554                    if (type == null) {
3555                            query.append(_FINDER_COLUMN_TYPE_TYPE_1);
3556                    }
3557                    else {
3558                            if (type.equals(StringPool.BLANK)) {
3559                                    query.append(_FINDER_COLUMN_TYPE_TYPE_3);
3560                            }
3561                            else {
3562                                    query.append(_FINDER_COLUMN_TYPE_TYPE_2);
3563                            }
3564                    }
3565    
3566                    if (orderByComparator != null) {
3567                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3568    
3569                            if (orderByConditionFields.length > 0) {
3570                                    query.append(WHERE_AND);
3571                            }
3572    
3573                            for (int i = 0; i < orderByConditionFields.length; i++) {
3574                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3575                                    query.append(orderByConditionFields[i]);
3576    
3577                                    if ((i + 1) < orderByConditionFields.length) {
3578                                            if (orderByComparator.isAscending() ^ previous) {
3579                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3580                                            }
3581                                            else {
3582                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3583                                            }
3584                                    }
3585                                    else {
3586                                            if (orderByComparator.isAscending() ^ previous) {
3587                                                    query.append(WHERE_GREATER_THAN);
3588                                            }
3589                                            else {
3590                                                    query.append(WHERE_LESSER_THAN);
3591                                            }
3592                                    }
3593                            }
3594    
3595                            query.append(ORDER_BY_CLAUSE);
3596    
3597                            String[] orderByFields = orderByComparator.getOrderByFields();
3598    
3599                            for (int i = 0; i < orderByFields.length; i++) {
3600                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3601                                    query.append(orderByFields[i]);
3602    
3603                                    if ((i + 1) < orderByFields.length) {
3604                                            if (orderByComparator.isAscending() ^ previous) {
3605                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3606                                            }
3607                                            else {
3608                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3609                                            }
3610                                    }
3611                                    else {
3612                                            if (orderByComparator.isAscending() ^ previous) {
3613                                                    query.append(ORDER_BY_ASC);
3614                                            }
3615                                            else {
3616                                                    query.append(ORDER_BY_DESC);
3617                                            }
3618                                    }
3619                            }
3620                    }
3621                    else {
3622                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
3623                    }
3624    
3625                    String sql = query.toString();
3626    
3627                    Query q = session.createQuery(sql);
3628    
3629                    q.setFirstResult(0);
3630                    q.setMaxResults(2);
3631    
3632                    QueryPos qPos = QueryPos.getInstance(q);
3633    
3634                    if (type != null) {
3635                            qPos.add(type);
3636                    }
3637    
3638                    if (orderByComparator != null) {
3639                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
3640    
3641                            for (Object value : values) {
3642                                    qPos.add(value);
3643                            }
3644                    }
3645    
3646                    List<DDMTemplate> list = q.list();
3647    
3648                    if (list.size() == 2) {
3649                            return list.get(1);
3650                    }
3651                    else {
3652                            return null;
3653                    }
3654            }
3655    
3656            /**
3657             * Removes all the d d m templates where type = &#63; from the database.
3658             *
3659             * @param type the type
3660             * @throws SystemException if a system exception occurred
3661             */
3662            public void removeByType(String type) throws SystemException {
3663                    for (DDMTemplate ddmTemplate : findByType(type, QueryUtil.ALL_POS,
3664                                    QueryUtil.ALL_POS, null)) {
3665                            remove(ddmTemplate);
3666                    }
3667            }
3668    
3669            /**
3670             * Returns the number of d d m templates where type = &#63;.
3671             *
3672             * @param type the type
3673             * @return the number of matching d d m templates
3674             * @throws SystemException if a system exception occurred
3675             */
3676            public int countByType(String type) throws SystemException {
3677                    FinderPath finderPath = FINDER_PATH_COUNT_BY_TYPE;
3678    
3679                    Object[] finderArgs = new Object[] { type };
3680    
3681                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3682                                    this);
3683    
3684                    if (count == null) {
3685                            StringBundler query = new StringBundler(2);
3686    
3687                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
3688    
3689                            if (type == null) {
3690                                    query.append(_FINDER_COLUMN_TYPE_TYPE_1);
3691                            }
3692                            else {
3693                                    if (type.equals(StringPool.BLANK)) {
3694                                            query.append(_FINDER_COLUMN_TYPE_TYPE_3);
3695                                    }
3696                                    else {
3697                                            query.append(_FINDER_COLUMN_TYPE_TYPE_2);
3698                                    }
3699                            }
3700    
3701                            String sql = query.toString();
3702    
3703                            Session session = null;
3704    
3705                            try {
3706                                    session = openSession();
3707    
3708                                    Query q = session.createQuery(sql);
3709    
3710                                    QueryPos qPos = QueryPos.getInstance(q);
3711    
3712                                    if (type != null) {
3713                                            qPos.add(type);
3714                                    }
3715    
3716                                    count = (Long)q.uniqueResult();
3717    
3718                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3719                            }
3720                            catch (Exception e) {
3721                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3722    
3723                                    throw processException(e);
3724                            }
3725                            finally {
3726                                    closeSession(session);
3727                            }
3728                    }
3729    
3730                    return count.intValue();
3731            }
3732    
3733            private static final String _FINDER_COLUMN_TYPE_TYPE_1 = "ddmTemplate.type IS NULL";
3734            private static final String _FINDER_COLUMN_TYPE_TYPE_2 = "ddmTemplate.type = ?";
3735            private static final String _FINDER_COLUMN_TYPE_TYPE_3 = "(ddmTemplate.type IS NULL OR ddmTemplate.type = ?)";
3736            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_LANGUAGE = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3737                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
3738                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByLanguage",
3739                            new String[] {
3740                                    String.class.getName(),
3741                                    
3742                            Integer.class.getName(), Integer.class.getName(),
3743                                    OrderByComparator.class.getName()
3744                            });
3745            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LANGUAGE =
3746                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3747                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
3748                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByLanguage",
3749                            new String[] { String.class.getName() },
3750                            DDMTemplateModelImpl.LANGUAGE_COLUMN_BITMASK);
3751            public static final FinderPath FINDER_PATH_COUNT_BY_LANGUAGE = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
3752                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
3753                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByLanguage",
3754                            new String[] { String.class.getName() });
3755    
3756            /**
3757             * Returns all the d d m templates where language = &#63;.
3758             *
3759             * @param language the language
3760             * @return the matching d d m templates
3761             * @throws SystemException if a system exception occurred
3762             */
3763            public List<DDMTemplate> findByLanguage(String language)
3764                    throws SystemException {
3765                    return findByLanguage(language, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
3766                            null);
3767            }
3768    
3769            /**
3770             * Returns a range of all the d d m templates where language = &#63;.
3771             *
3772             * <p>
3773             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
3774             * </p>
3775             *
3776             * @param language the language
3777             * @param start the lower bound of the range of d d m templates
3778             * @param end the upper bound of the range of d d m templates (not inclusive)
3779             * @return the range of matching d d m templates
3780             * @throws SystemException if a system exception occurred
3781             */
3782            public List<DDMTemplate> findByLanguage(String language, int start, int end)
3783                    throws SystemException {
3784                    return findByLanguage(language, start, end, null);
3785            }
3786    
3787            /**
3788             * Returns an ordered range of all the d d m templates where language = &#63;.
3789             *
3790             * <p>
3791             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
3792             * </p>
3793             *
3794             * @param language the language
3795             * @param start the lower bound of the range of d d m templates
3796             * @param end the upper bound of the range of d d m templates (not inclusive)
3797             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3798             * @return the ordered range of matching d d m templates
3799             * @throws SystemException if a system exception occurred
3800             */
3801            public List<DDMTemplate> findByLanguage(String language, int start,
3802                    int end, OrderByComparator orderByComparator) throws SystemException {
3803                    boolean pagination = true;
3804                    FinderPath finderPath = null;
3805                    Object[] finderArgs = null;
3806    
3807                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3808                                    (orderByComparator == null)) {
3809                            pagination = false;
3810                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LANGUAGE;
3811                            finderArgs = new Object[] { language };
3812                    }
3813                    else {
3814                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_LANGUAGE;
3815                            finderArgs = new Object[] { language, start, end, orderByComparator };
3816                    }
3817    
3818                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
3819                                    finderArgs, this);
3820    
3821                    if ((list != null) && !list.isEmpty()) {
3822                            for (DDMTemplate ddmTemplate : list) {
3823                                    if (!Validator.equals(language, ddmTemplate.getLanguage())) {
3824                                            list = null;
3825    
3826                                            break;
3827                                    }
3828                            }
3829                    }
3830    
3831                    if (list == null) {
3832                            StringBundler query = null;
3833    
3834                            if (orderByComparator != null) {
3835                                    query = new StringBundler(3 +
3836                                                    (orderByComparator.getOrderByFields().length * 3));
3837                            }
3838                            else {
3839                                    query = new StringBundler(3);
3840                            }
3841    
3842                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
3843    
3844                            if (language == null) {
3845                                    query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_1);
3846                            }
3847                            else {
3848                                    if (language.equals(StringPool.BLANK)) {
3849                                            query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_3);
3850                                    }
3851                                    else {
3852                                            query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_2);
3853                                    }
3854                            }
3855    
3856                            if (orderByComparator != null) {
3857                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3858                                            orderByComparator);
3859                            }
3860                            else
3861                             if (pagination) {
3862                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
3863                            }
3864    
3865                            String sql = query.toString();
3866    
3867                            Session session = null;
3868    
3869                            try {
3870                                    session = openSession();
3871    
3872                                    Query q = session.createQuery(sql);
3873    
3874                                    QueryPos qPos = QueryPos.getInstance(q);
3875    
3876                                    if (language != null) {
3877                                            qPos.add(language);
3878                                    }
3879    
3880                                    if (!pagination) {
3881                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
3882                                                            start, end, false);
3883    
3884                                            Collections.sort(list);
3885    
3886                                            list = new UnmodifiableList<DDMTemplate>(list);
3887                                    }
3888                                    else {
3889                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
3890                                                            start, end);
3891                                    }
3892    
3893                                    cacheResult(list);
3894    
3895                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3896                            }
3897                            catch (Exception e) {
3898                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3899    
3900                                    throw processException(e);
3901                            }
3902                            finally {
3903                                    closeSession(session);
3904                            }
3905                    }
3906    
3907                    return list;
3908            }
3909    
3910            /**
3911             * Returns the first d d m template in the ordered set where language = &#63;.
3912             *
3913             * @param language the language
3914             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3915             * @return the first matching d d m template
3916             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
3917             * @throws SystemException if a system exception occurred
3918             */
3919            public DDMTemplate findByLanguage_First(String language,
3920                    OrderByComparator orderByComparator)
3921                    throws NoSuchTemplateException, SystemException {
3922                    DDMTemplate ddmTemplate = fetchByLanguage_First(language,
3923                                    orderByComparator);
3924    
3925                    if (ddmTemplate != null) {
3926                            return ddmTemplate;
3927                    }
3928    
3929                    StringBundler msg = new StringBundler(4);
3930    
3931                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3932    
3933                    msg.append("language=");
3934                    msg.append(language);
3935    
3936                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3937    
3938                    throw new NoSuchTemplateException(msg.toString());
3939            }
3940    
3941            /**
3942             * Returns the first d d m template in the ordered set where language = &#63;.
3943             *
3944             * @param language the language
3945             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3946             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
3947             * @throws SystemException if a system exception occurred
3948             */
3949            public DDMTemplate fetchByLanguage_First(String language,
3950                    OrderByComparator orderByComparator) throws SystemException {
3951                    List<DDMTemplate> list = findByLanguage(language, 0, 1,
3952                                    orderByComparator);
3953    
3954                    if (!list.isEmpty()) {
3955                            return list.get(0);
3956                    }
3957    
3958                    return null;
3959            }
3960    
3961            /**
3962             * Returns the last d d m template in the ordered set where language = &#63;.
3963             *
3964             * @param language the language
3965             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3966             * @return the last matching d d m template
3967             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
3968             * @throws SystemException if a system exception occurred
3969             */
3970            public DDMTemplate findByLanguage_Last(String language,
3971                    OrderByComparator orderByComparator)
3972                    throws NoSuchTemplateException, SystemException {
3973                    DDMTemplate ddmTemplate = fetchByLanguage_Last(language,
3974                                    orderByComparator);
3975    
3976                    if (ddmTemplate != null) {
3977                            return ddmTemplate;
3978                    }
3979    
3980                    StringBundler msg = new StringBundler(4);
3981    
3982                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3983    
3984                    msg.append("language=");
3985                    msg.append(language);
3986    
3987                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3988    
3989                    throw new NoSuchTemplateException(msg.toString());
3990            }
3991    
3992            /**
3993             * Returns the last d d m template in the ordered set where language = &#63;.
3994             *
3995             * @param language the language
3996             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3997             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
3998             * @throws SystemException if a system exception occurred
3999             */
4000            public DDMTemplate fetchByLanguage_Last(String language,
4001                    OrderByComparator orderByComparator) throws SystemException {
4002                    int count = countByLanguage(language);
4003    
4004                    List<DDMTemplate> list = findByLanguage(language, count - 1, count,
4005                                    orderByComparator);
4006    
4007                    if (!list.isEmpty()) {
4008                            return list.get(0);
4009                    }
4010    
4011                    return null;
4012            }
4013    
4014            /**
4015             * Returns the d d m templates before and after the current d d m template in the ordered set where language = &#63;.
4016             *
4017             * @param templateId the primary key of the current d d m template
4018             * @param language the language
4019             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4020             * @return the previous, current, and next d d m template
4021             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
4022             * @throws SystemException if a system exception occurred
4023             */
4024            public DDMTemplate[] findByLanguage_PrevAndNext(long templateId,
4025                    String language, OrderByComparator orderByComparator)
4026                    throws NoSuchTemplateException, SystemException {
4027                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
4028    
4029                    Session session = null;
4030    
4031                    try {
4032                            session = openSession();
4033    
4034                            DDMTemplate[] array = new DDMTemplateImpl[3];
4035    
4036                            array[0] = getByLanguage_PrevAndNext(session, ddmTemplate,
4037                                            language, orderByComparator, true);
4038    
4039                            array[1] = ddmTemplate;
4040    
4041                            array[2] = getByLanguage_PrevAndNext(session, ddmTemplate,
4042                                            language, orderByComparator, false);
4043    
4044                            return array;
4045                    }
4046                    catch (Exception e) {
4047                            throw processException(e);
4048                    }
4049                    finally {
4050                            closeSession(session);
4051                    }
4052            }
4053    
4054            protected DDMTemplate getByLanguage_PrevAndNext(Session session,
4055                    DDMTemplate ddmTemplate, String language,
4056                    OrderByComparator orderByComparator, boolean previous) {
4057                    StringBundler query = null;
4058    
4059                    if (orderByComparator != null) {
4060                            query = new StringBundler(6 +
4061                                            (orderByComparator.getOrderByFields().length * 6));
4062                    }
4063                    else {
4064                            query = new StringBundler(3);
4065                    }
4066    
4067                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
4068    
4069                    if (language == null) {
4070                            query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_1);
4071                    }
4072                    else {
4073                            if (language.equals(StringPool.BLANK)) {
4074                                    query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_3);
4075                            }
4076                            else {
4077                                    query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_2);
4078                            }
4079                    }
4080    
4081                    if (orderByComparator != null) {
4082                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
4083    
4084                            if (orderByConditionFields.length > 0) {
4085                                    query.append(WHERE_AND);
4086                            }
4087    
4088                            for (int i = 0; i < orderByConditionFields.length; i++) {
4089                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4090                                    query.append(orderByConditionFields[i]);
4091    
4092                                    if ((i + 1) < orderByConditionFields.length) {
4093                                            if (orderByComparator.isAscending() ^ previous) {
4094                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
4095                                            }
4096                                            else {
4097                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
4098                                            }
4099                                    }
4100                                    else {
4101                                            if (orderByComparator.isAscending() ^ previous) {
4102                                                    query.append(WHERE_GREATER_THAN);
4103                                            }
4104                                            else {
4105                                                    query.append(WHERE_LESSER_THAN);
4106                                            }
4107                                    }
4108                            }
4109    
4110                            query.append(ORDER_BY_CLAUSE);
4111    
4112                            String[] orderByFields = orderByComparator.getOrderByFields();
4113    
4114                            for (int i = 0; i < orderByFields.length; i++) {
4115                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4116                                    query.append(orderByFields[i]);
4117    
4118                                    if ((i + 1) < orderByFields.length) {
4119                                            if (orderByComparator.isAscending() ^ previous) {
4120                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
4121                                            }
4122                                            else {
4123                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
4124                                            }
4125                                    }
4126                                    else {
4127                                            if (orderByComparator.isAscending() ^ previous) {
4128                                                    query.append(ORDER_BY_ASC);
4129                                            }
4130                                            else {
4131                                                    query.append(ORDER_BY_DESC);
4132                                            }
4133                                    }
4134                            }
4135                    }
4136                    else {
4137                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
4138                    }
4139    
4140                    String sql = query.toString();
4141    
4142                    Query q = session.createQuery(sql);
4143    
4144                    q.setFirstResult(0);
4145                    q.setMaxResults(2);
4146    
4147                    QueryPos qPos = QueryPos.getInstance(q);
4148    
4149                    if (language != null) {
4150                            qPos.add(language);
4151                    }
4152    
4153                    if (orderByComparator != null) {
4154                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
4155    
4156                            for (Object value : values) {
4157                                    qPos.add(value);
4158                            }
4159                    }
4160    
4161                    List<DDMTemplate> list = q.list();
4162    
4163                    if (list.size() == 2) {
4164                            return list.get(1);
4165                    }
4166                    else {
4167                            return null;
4168                    }
4169            }
4170    
4171            /**
4172             * Removes all the d d m templates where language = &#63; from the database.
4173             *
4174             * @param language the language
4175             * @throws SystemException if a system exception occurred
4176             */
4177            public void removeByLanguage(String language) throws SystemException {
4178                    for (DDMTemplate ddmTemplate : findByLanguage(language,
4179                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
4180                            remove(ddmTemplate);
4181                    }
4182            }
4183    
4184            /**
4185             * Returns the number of d d m templates where language = &#63;.
4186             *
4187             * @param language the language
4188             * @return the number of matching d d m templates
4189             * @throws SystemException if a system exception occurred
4190             */
4191            public int countByLanguage(String language) throws SystemException {
4192                    FinderPath finderPath = FINDER_PATH_COUNT_BY_LANGUAGE;
4193    
4194                    Object[] finderArgs = new Object[] { language };
4195    
4196                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
4197                                    this);
4198    
4199                    if (count == null) {
4200                            StringBundler query = new StringBundler(2);
4201    
4202                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
4203    
4204                            if (language == null) {
4205                                    query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_1);
4206                            }
4207                            else {
4208                                    if (language.equals(StringPool.BLANK)) {
4209                                            query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_3);
4210                                    }
4211                                    else {
4212                                            query.append(_FINDER_COLUMN_LANGUAGE_LANGUAGE_2);
4213                                    }
4214                            }
4215    
4216                            String sql = query.toString();
4217    
4218                            Session session = null;
4219    
4220                            try {
4221                                    session = openSession();
4222    
4223                                    Query q = session.createQuery(sql);
4224    
4225                                    QueryPos qPos = QueryPos.getInstance(q);
4226    
4227                                    if (language != null) {
4228                                            qPos.add(language);
4229                                    }
4230    
4231                                    count = (Long)q.uniqueResult();
4232    
4233                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
4234                            }
4235                            catch (Exception e) {
4236                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4237    
4238                                    throw processException(e);
4239                            }
4240                            finally {
4241                                    closeSession(session);
4242                            }
4243                    }
4244    
4245                    return count.intValue();
4246            }
4247    
4248            private static final String _FINDER_COLUMN_LANGUAGE_LANGUAGE_1 = "ddmTemplate.language IS NULL";
4249            private static final String _FINDER_COLUMN_LANGUAGE_LANGUAGE_2 = "ddmTemplate.language = ?";
4250            private static final String _FINDER_COLUMN_LANGUAGE_LANGUAGE_3 = "(ddmTemplate.language IS NULL OR ddmTemplate.language = ?)";
4251            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
4252                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
4253                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByG_C",
4254                            new String[] {
4255                                    Long.class.getName(), Long.class.getName(),
4256                                    
4257                            Integer.class.getName(), Integer.class.getName(),
4258                                    OrderByComparator.class.getName()
4259                            });
4260            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
4261                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
4262                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByG_C",
4263                            new String[] { Long.class.getName(), Long.class.getName() },
4264                            DDMTemplateModelImpl.GROUPID_COLUMN_BITMASK |
4265                            DDMTemplateModelImpl.CLASSNAMEID_COLUMN_BITMASK);
4266            public static final FinderPath FINDER_PATH_COUNT_BY_G_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
4267                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
4268                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_C",
4269                            new String[] { Long.class.getName(), Long.class.getName() });
4270    
4271            /**
4272             * Returns all the d d m templates where groupId = &#63; and classNameId = &#63;.
4273             *
4274             * @param groupId the group ID
4275             * @param classNameId the class name ID
4276             * @return the matching d d m templates
4277             * @throws SystemException if a system exception occurred
4278             */
4279            public List<DDMTemplate> findByG_C(long groupId, long classNameId)
4280                    throws SystemException {
4281                    return findByG_C(groupId, classNameId, QueryUtil.ALL_POS,
4282                            QueryUtil.ALL_POS, null);
4283            }
4284    
4285            /**
4286             * Returns a range of all the d d m templates where groupId = &#63; and classNameId = &#63;.
4287             *
4288             * <p>
4289             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
4290             * </p>
4291             *
4292             * @param groupId the group ID
4293             * @param classNameId the class name ID
4294             * @param start the lower bound of the range of d d m templates
4295             * @param end the upper bound of the range of d d m templates (not inclusive)
4296             * @return the range of matching d d m templates
4297             * @throws SystemException if a system exception occurred
4298             */
4299            public List<DDMTemplate> findByG_C(long groupId, long classNameId,
4300                    int start, int end) throws SystemException {
4301                    return findByG_C(groupId, classNameId, start, end, null);
4302            }
4303    
4304            /**
4305             * Returns an ordered range of all the d d m templates where groupId = &#63; and classNameId = &#63;.
4306             *
4307             * <p>
4308             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
4309             * </p>
4310             *
4311             * @param groupId the group ID
4312             * @param classNameId the class name ID
4313             * @param start the lower bound of the range of d d m templates
4314             * @param end the upper bound of the range of d d m templates (not inclusive)
4315             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4316             * @return the ordered range of matching d d m templates
4317             * @throws SystemException if a system exception occurred
4318             */
4319            public List<DDMTemplate> findByG_C(long groupId, long classNameId,
4320                    int start, int end, OrderByComparator orderByComparator)
4321                    throws SystemException {
4322                    boolean pagination = true;
4323                    FinderPath finderPath = null;
4324                    Object[] finderArgs = null;
4325    
4326                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4327                                    (orderByComparator == null)) {
4328                            pagination = false;
4329                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C;
4330                            finderArgs = new Object[] { groupId, classNameId };
4331                    }
4332                    else {
4333                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C;
4334                            finderArgs = new Object[] {
4335                                            groupId, classNameId,
4336                                            
4337                                            start, end, orderByComparator
4338                                    };
4339                    }
4340    
4341                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
4342                                    finderArgs, this);
4343    
4344                    if ((list != null) && !list.isEmpty()) {
4345                            for (DDMTemplate ddmTemplate : list) {
4346                                    if ((groupId != ddmTemplate.getGroupId()) ||
4347                                                    (classNameId != ddmTemplate.getClassNameId())) {
4348                                            list = null;
4349    
4350                                            break;
4351                                    }
4352                            }
4353                    }
4354    
4355                    if (list == null) {
4356                            StringBundler query = null;
4357    
4358                            if (orderByComparator != null) {
4359                                    query = new StringBundler(4 +
4360                                                    (orderByComparator.getOrderByFields().length * 3));
4361                            }
4362                            else {
4363                                    query = new StringBundler(4);
4364                            }
4365    
4366                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
4367    
4368                            query.append(_FINDER_COLUMN_G_C_GROUPID_2);
4369    
4370                            query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
4371    
4372                            if (orderByComparator != null) {
4373                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4374                                            orderByComparator);
4375                            }
4376                            else
4377                             if (pagination) {
4378                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
4379                            }
4380    
4381                            String sql = query.toString();
4382    
4383                            Session session = null;
4384    
4385                            try {
4386                                    session = openSession();
4387    
4388                                    Query q = session.createQuery(sql);
4389    
4390                                    QueryPos qPos = QueryPos.getInstance(q);
4391    
4392                                    qPos.add(groupId);
4393    
4394                                    qPos.add(classNameId);
4395    
4396                                    if (!pagination) {
4397                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
4398                                                            start, end, false);
4399    
4400                                            Collections.sort(list);
4401    
4402                                            list = new UnmodifiableList<DDMTemplate>(list);
4403                                    }
4404                                    else {
4405                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
4406                                                            start, end);
4407                                    }
4408    
4409                                    cacheResult(list);
4410    
4411                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
4412                            }
4413                            catch (Exception e) {
4414                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4415    
4416                                    throw processException(e);
4417                            }
4418                            finally {
4419                                    closeSession(session);
4420                            }
4421                    }
4422    
4423                    return list;
4424            }
4425    
4426            /**
4427             * Returns the first d d m template in the ordered set where groupId = &#63; and classNameId = &#63;.
4428             *
4429             * @param groupId the group ID
4430             * @param classNameId the class name ID
4431             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4432             * @return the first matching d d m template
4433             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
4434             * @throws SystemException if a system exception occurred
4435             */
4436            public DDMTemplate findByG_C_First(long groupId, long classNameId,
4437                    OrderByComparator orderByComparator)
4438                    throws NoSuchTemplateException, SystemException {
4439                    DDMTemplate ddmTemplate = fetchByG_C_First(groupId, classNameId,
4440                                    orderByComparator);
4441    
4442                    if (ddmTemplate != null) {
4443                            return ddmTemplate;
4444                    }
4445    
4446                    StringBundler msg = new StringBundler(6);
4447    
4448                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4449    
4450                    msg.append("groupId=");
4451                    msg.append(groupId);
4452    
4453                    msg.append(", classNameId=");
4454                    msg.append(classNameId);
4455    
4456                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4457    
4458                    throw new NoSuchTemplateException(msg.toString());
4459            }
4460    
4461            /**
4462             * Returns the first d d m template in the ordered set where groupId = &#63; and classNameId = &#63;.
4463             *
4464             * @param groupId the group ID
4465             * @param classNameId the class name ID
4466             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4467             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
4468             * @throws SystemException if a system exception occurred
4469             */
4470            public DDMTemplate fetchByG_C_First(long groupId, long classNameId,
4471                    OrderByComparator orderByComparator) throws SystemException {
4472                    List<DDMTemplate> list = findByG_C(groupId, classNameId, 0, 1,
4473                                    orderByComparator);
4474    
4475                    if (!list.isEmpty()) {
4476                            return list.get(0);
4477                    }
4478    
4479                    return null;
4480            }
4481    
4482            /**
4483             * Returns the last d d m template in the ordered set where groupId = &#63; and classNameId = &#63;.
4484             *
4485             * @param groupId the group ID
4486             * @param classNameId the class name ID
4487             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4488             * @return the last matching d d m template
4489             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
4490             * @throws SystemException if a system exception occurred
4491             */
4492            public DDMTemplate findByG_C_Last(long groupId, long classNameId,
4493                    OrderByComparator orderByComparator)
4494                    throws NoSuchTemplateException, SystemException {
4495                    DDMTemplate ddmTemplate = fetchByG_C_Last(groupId, classNameId,
4496                                    orderByComparator);
4497    
4498                    if (ddmTemplate != null) {
4499                            return ddmTemplate;
4500                    }
4501    
4502                    StringBundler msg = new StringBundler(6);
4503    
4504                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4505    
4506                    msg.append("groupId=");
4507                    msg.append(groupId);
4508    
4509                    msg.append(", classNameId=");
4510                    msg.append(classNameId);
4511    
4512                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4513    
4514                    throw new NoSuchTemplateException(msg.toString());
4515            }
4516    
4517            /**
4518             * Returns the last d d m template in the ordered set where groupId = &#63; and classNameId = &#63;.
4519             *
4520             * @param groupId the group ID
4521             * @param classNameId the class name ID
4522             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4523             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
4524             * @throws SystemException if a system exception occurred
4525             */
4526            public DDMTemplate fetchByG_C_Last(long groupId, long classNameId,
4527                    OrderByComparator orderByComparator) throws SystemException {
4528                    int count = countByG_C(groupId, classNameId);
4529    
4530                    List<DDMTemplate> list = findByG_C(groupId, classNameId, count - 1,
4531                                    count, orderByComparator);
4532    
4533                    if (!list.isEmpty()) {
4534                            return list.get(0);
4535                    }
4536    
4537                    return null;
4538            }
4539    
4540            /**
4541             * Returns the d d m templates before and after the current d d m template in the ordered set where groupId = &#63; and classNameId = &#63;.
4542             *
4543             * @param templateId the primary key of the current d d m template
4544             * @param groupId the group ID
4545             * @param classNameId the class name ID
4546             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4547             * @return the previous, current, and next d d m template
4548             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
4549             * @throws SystemException if a system exception occurred
4550             */
4551            public DDMTemplate[] findByG_C_PrevAndNext(long templateId, long groupId,
4552                    long classNameId, OrderByComparator orderByComparator)
4553                    throws NoSuchTemplateException, SystemException {
4554                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
4555    
4556                    Session session = null;
4557    
4558                    try {
4559                            session = openSession();
4560    
4561                            DDMTemplate[] array = new DDMTemplateImpl[3];
4562    
4563                            array[0] = getByG_C_PrevAndNext(session, ddmTemplate, groupId,
4564                                            classNameId, orderByComparator, true);
4565    
4566                            array[1] = ddmTemplate;
4567    
4568                            array[2] = getByG_C_PrevAndNext(session, ddmTemplate, groupId,
4569                                            classNameId, orderByComparator, false);
4570    
4571                            return array;
4572                    }
4573                    catch (Exception e) {
4574                            throw processException(e);
4575                    }
4576                    finally {
4577                            closeSession(session);
4578                    }
4579            }
4580    
4581            protected DDMTemplate getByG_C_PrevAndNext(Session session,
4582                    DDMTemplate ddmTemplate, long groupId, long classNameId,
4583                    OrderByComparator orderByComparator, boolean previous) {
4584                    StringBundler query = null;
4585    
4586                    if (orderByComparator != null) {
4587                            query = new StringBundler(6 +
4588                                            (orderByComparator.getOrderByFields().length * 6));
4589                    }
4590                    else {
4591                            query = new StringBundler(3);
4592                    }
4593    
4594                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
4595    
4596                    query.append(_FINDER_COLUMN_G_C_GROUPID_2);
4597    
4598                    query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
4599    
4600                    if (orderByComparator != null) {
4601                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
4602    
4603                            if (orderByConditionFields.length > 0) {
4604                                    query.append(WHERE_AND);
4605                            }
4606    
4607                            for (int i = 0; i < orderByConditionFields.length; i++) {
4608                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4609                                    query.append(orderByConditionFields[i]);
4610    
4611                                    if ((i + 1) < orderByConditionFields.length) {
4612                                            if (orderByComparator.isAscending() ^ previous) {
4613                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
4614                                            }
4615                                            else {
4616                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
4617                                            }
4618                                    }
4619                                    else {
4620                                            if (orderByComparator.isAscending() ^ previous) {
4621                                                    query.append(WHERE_GREATER_THAN);
4622                                            }
4623                                            else {
4624                                                    query.append(WHERE_LESSER_THAN);
4625                                            }
4626                                    }
4627                            }
4628    
4629                            query.append(ORDER_BY_CLAUSE);
4630    
4631                            String[] orderByFields = orderByComparator.getOrderByFields();
4632    
4633                            for (int i = 0; i < orderByFields.length; i++) {
4634                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4635                                    query.append(orderByFields[i]);
4636    
4637                                    if ((i + 1) < orderByFields.length) {
4638                                            if (orderByComparator.isAscending() ^ previous) {
4639                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
4640                                            }
4641                                            else {
4642                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
4643                                            }
4644                                    }
4645                                    else {
4646                                            if (orderByComparator.isAscending() ^ previous) {
4647                                                    query.append(ORDER_BY_ASC);
4648                                            }
4649                                            else {
4650                                                    query.append(ORDER_BY_DESC);
4651                                            }
4652                                    }
4653                            }
4654                    }
4655                    else {
4656                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
4657                    }
4658    
4659                    String sql = query.toString();
4660    
4661                    Query q = session.createQuery(sql);
4662    
4663                    q.setFirstResult(0);
4664                    q.setMaxResults(2);
4665    
4666                    QueryPos qPos = QueryPos.getInstance(q);
4667    
4668                    qPos.add(groupId);
4669    
4670                    qPos.add(classNameId);
4671    
4672                    if (orderByComparator != null) {
4673                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
4674    
4675                            for (Object value : values) {
4676                                    qPos.add(value);
4677                            }
4678                    }
4679    
4680                    List<DDMTemplate> list = q.list();
4681    
4682                    if (list.size() == 2) {
4683                            return list.get(1);
4684                    }
4685                    else {
4686                            return null;
4687                    }
4688            }
4689    
4690            /**
4691             * Returns all the d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63;.
4692             *
4693             * @param groupId the group ID
4694             * @param classNameId the class name ID
4695             * @return the matching d d m templates that the user has permission to view
4696             * @throws SystemException if a system exception occurred
4697             */
4698            public List<DDMTemplate> filterFindByG_C(long groupId, long classNameId)
4699                    throws SystemException {
4700                    return filterFindByG_C(groupId, classNameId, QueryUtil.ALL_POS,
4701                            QueryUtil.ALL_POS, null);
4702            }
4703    
4704            /**
4705             * Returns a range of all the d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63;.
4706             *
4707             * <p>
4708             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
4709             * </p>
4710             *
4711             * @param groupId the group ID
4712             * @param classNameId the class name ID
4713             * @param start the lower bound of the range of d d m templates
4714             * @param end the upper bound of the range of d d m templates (not inclusive)
4715             * @return the range of matching d d m templates that the user has permission to view
4716             * @throws SystemException if a system exception occurred
4717             */
4718            public List<DDMTemplate> filterFindByG_C(long groupId, long classNameId,
4719                    int start, int end) throws SystemException {
4720                    return filterFindByG_C(groupId, classNameId, start, end, null);
4721            }
4722    
4723            /**
4724             * Returns an ordered range of all the d d m templates that the user has permissions to view where groupId = &#63; and classNameId = &#63;.
4725             *
4726             * <p>
4727             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
4728             * </p>
4729             *
4730             * @param groupId the group ID
4731             * @param classNameId the class name ID
4732             * @param start the lower bound of the range of d d m templates
4733             * @param end the upper bound of the range of d d m templates (not inclusive)
4734             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4735             * @return the ordered range of matching d d m templates that the user has permission to view
4736             * @throws SystemException if a system exception occurred
4737             */
4738            public List<DDMTemplate> filterFindByG_C(long groupId, long classNameId,
4739                    int start, int end, OrderByComparator orderByComparator)
4740                    throws SystemException {
4741                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
4742                            return findByG_C(groupId, classNameId, start, end, orderByComparator);
4743                    }
4744    
4745                    StringBundler query = null;
4746    
4747                    if (orderByComparator != null) {
4748                            query = new StringBundler(4 +
4749                                            (orderByComparator.getOrderByFields().length * 3));
4750                    }
4751                    else {
4752                            query = new StringBundler(4);
4753                    }
4754    
4755                    if (getDB().isSupportsInlineDistinct()) {
4756                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
4757                    }
4758                    else {
4759                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
4760                    }
4761    
4762                    query.append(_FINDER_COLUMN_G_C_GROUPID_2);
4763    
4764                    query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
4765    
4766                    if (!getDB().isSupportsInlineDistinct()) {
4767                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
4768                    }
4769    
4770                    if (orderByComparator != null) {
4771                            if (getDB().isSupportsInlineDistinct()) {
4772                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4773                                            orderByComparator);
4774                            }
4775                            else {
4776                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
4777                                            orderByComparator);
4778                            }
4779                    }
4780                    else {
4781                            if (getDB().isSupportsInlineDistinct()) {
4782                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
4783                            }
4784                            else {
4785                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
4786                            }
4787                    }
4788    
4789                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
4790                                    DDMTemplate.class.getName(),
4791                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
4792    
4793                    Session session = null;
4794    
4795                    try {
4796                            session = openSession();
4797    
4798                            SQLQuery q = session.createSQLQuery(sql);
4799    
4800                            if (getDB().isSupportsInlineDistinct()) {
4801                                    q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
4802                            }
4803                            else {
4804                                    q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
4805                            }
4806    
4807                            QueryPos qPos = QueryPos.getInstance(q);
4808    
4809                            qPos.add(groupId);
4810    
4811                            qPos.add(classNameId);
4812    
4813                            return (List<DDMTemplate>)QueryUtil.list(q, getDialect(), start, end);
4814                    }
4815                    catch (Exception e) {
4816                            throw processException(e);
4817                    }
4818                    finally {
4819                            closeSession(session);
4820                    }
4821            }
4822    
4823            /**
4824             * Returns the d d m templates before and after the current d d m template in the ordered set of d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63;.
4825             *
4826             * @param templateId the primary key of the current d d m template
4827             * @param groupId the group ID
4828             * @param classNameId the class name ID
4829             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4830             * @return the previous, current, and next d d m template
4831             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
4832             * @throws SystemException if a system exception occurred
4833             */
4834            public DDMTemplate[] filterFindByG_C_PrevAndNext(long templateId,
4835                    long groupId, long classNameId, OrderByComparator orderByComparator)
4836                    throws NoSuchTemplateException, SystemException {
4837                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
4838                            return findByG_C_PrevAndNext(templateId, groupId, classNameId,
4839                                    orderByComparator);
4840                    }
4841    
4842                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
4843    
4844                    Session session = null;
4845    
4846                    try {
4847                            session = openSession();
4848    
4849                            DDMTemplate[] array = new DDMTemplateImpl[3];
4850    
4851                            array[0] = filterGetByG_C_PrevAndNext(session, ddmTemplate,
4852                                            groupId, classNameId, orderByComparator, true);
4853    
4854                            array[1] = ddmTemplate;
4855    
4856                            array[2] = filterGetByG_C_PrevAndNext(session, ddmTemplate,
4857                                            groupId, classNameId, orderByComparator, false);
4858    
4859                            return array;
4860                    }
4861                    catch (Exception e) {
4862                            throw processException(e);
4863                    }
4864                    finally {
4865                            closeSession(session);
4866                    }
4867            }
4868    
4869            protected DDMTemplate filterGetByG_C_PrevAndNext(Session session,
4870                    DDMTemplate ddmTemplate, long groupId, long classNameId,
4871                    OrderByComparator orderByComparator, boolean previous) {
4872                    StringBundler query = null;
4873    
4874                    if (orderByComparator != null) {
4875                            query = new StringBundler(6 +
4876                                            (orderByComparator.getOrderByFields().length * 6));
4877                    }
4878                    else {
4879                            query = new StringBundler(3);
4880                    }
4881    
4882                    if (getDB().isSupportsInlineDistinct()) {
4883                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
4884                    }
4885                    else {
4886                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
4887                    }
4888    
4889                    query.append(_FINDER_COLUMN_G_C_GROUPID_2);
4890    
4891                    query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
4892    
4893                    if (!getDB().isSupportsInlineDistinct()) {
4894                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
4895                    }
4896    
4897                    if (orderByComparator != null) {
4898                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
4899    
4900                            if (orderByConditionFields.length > 0) {
4901                                    query.append(WHERE_AND);
4902                            }
4903    
4904                            for (int i = 0; i < orderByConditionFields.length; i++) {
4905                                    if (getDB().isSupportsInlineDistinct()) {
4906                                            query.append(_ORDER_BY_ENTITY_ALIAS);
4907                                    }
4908                                    else {
4909                                            query.append(_ORDER_BY_ENTITY_TABLE);
4910                                    }
4911    
4912                                    query.append(orderByConditionFields[i]);
4913    
4914                                    if ((i + 1) < orderByConditionFields.length) {
4915                                            if (orderByComparator.isAscending() ^ previous) {
4916                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
4917                                            }
4918                                            else {
4919                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
4920                                            }
4921                                    }
4922                                    else {
4923                                            if (orderByComparator.isAscending() ^ previous) {
4924                                                    query.append(WHERE_GREATER_THAN);
4925                                            }
4926                                            else {
4927                                                    query.append(WHERE_LESSER_THAN);
4928                                            }
4929                                    }
4930                            }
4931    
4932                            query.append(ORDER_BY_CLAUSE);
4933    
4934                            String[] orderByFields = orderByComparator.getOrderByFields();
4935    
4936                            for (int i = 0; i < orderByFields.length; i++) {
4937                                    if (getDB().isSupportsInlineDistinct()) {
4938                                            query.append(_ORDER_BY_ENTITY_ALIAS);
4939                                    }
4940                                    else {
4941                                            query.append(_ORDER_BY_ENTITY_TABLE);
4942                                    }
4943    
4944                                    query.append(orderByFields[i]);
4945    
4946                                    if ((i + 1) < orderByFields.length) {
4947                                            if (orderByComparator.isAscending() ^ previous) {
4948                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
4949                                            }
4950                                            else {
4951                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
4952                                            }
4953                                    }
4954                                    else {
4955                                            if (orderByComparator.isAscending() ^ previous) {
4956                                                    query.append(ORDER_BY_ASC);
4957                                            }
4958                                            else {
4959                                                    query.append(ORDER_BY_DESC);
4960                                            }
4961                                    }
4962                            }
4963                    }
4964                    else {
4965                            if (getDB().isSupportsInlineDistinct()) {
4966                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
4967                            }
4968                            else {
4969                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
4970                            }
4971                    }
4972    
4973                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
4974                                    DDMTemplate.class.getName(),
4975                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
4976    
4977                    SQLQuery q = session.createSQLQuery(sql);
4978    
4979                    q.setFirstResult(0);
4980                    q.setMaxResults(2);
4981    
4982                    if (getDB().isSupportsInlineDistinct()) {
4983                            q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
4984                    }
4985                    else {
4986                            q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
4987                    }
4988    
4989                    QueryPos qPos = QueryPos.getInstance(q);
4990    
4991                    qPos.add(groupId);
4992    
4993                    qPos.add(classNameId);
4994    
4995                    if (orderByComparator != null) {
4996                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
4997    
4998                            for (Object value : values) {
4999                                    qPos.add(value);
5000                            }
5001                    }
5002    
5003                    List<DDMTemplate> list = q.list();
5004    
5005                    if (list.size() == 2) {
5006                            return list.get(1);
5007                    }
5008                    else {
5009                            return null;
5010                    }
5011            }
5012    
5013            /**
5014             * Removes all the d d m templates where groupId = &#63; and classNameId = &#63; from the database.
5015             *
5016             * @param groupId the group ID
5017             * @param classNameId the class name ID
5018             * @throws SystemException if a system exception occurred
5019             */
5020            public void removeByG_C(long groupId, long classNameId)
5021                    throws SystemException {
5022                    for (DDMTemplate ddmTemplate : findByG_C(groupId, classNameId,
5023                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
5024                            remove(ddmTemplate);
5025                    }
5026            }
5027    
5028            /**
5029             * Returns the number of d d m templates where groupId = &#63; and classNameId = &#63;.
5030             *
5031             * @param groupId the group ID
5032             * @param classNameId the class name ID
5033             * @return the number of matching d d m templates
5034             * @throws SystemException if a system exception occurred
5035             */
5036            public int countByG_C(long groupId, long classNameId)
5037                    throws SystemException {
5038                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_C;
5039    
5040                    Object[] finderArgs = new Object[] { groupId, classNameId };
5041    
5042                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
5043                                    this);
5044    
5045                    if (count == null) {
5046                            StringBundler query = new StringBundler(3);
5047    
5048                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
5049    
5050                            query.append(_FINDER_COLUMN_G_C_GROUPID_2);
5051    
5052                            query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
5053    
5054                            String sql = query.toString();
5055    
5056                            Session session = null;
5057    
5058                            try {
5059                                    session = openSession();
5060    
5061                                    Query q = session.createQuery(sql);
5062    
5063                                    QueryPos qPos = QueryPos.getInstance(q);
5064    
5065                                    qPos.add(groupId);
5066    
5067                                    qPos.add(classNameId);
5068    
5069                                    count = (Long)q.uniqueResult();
5070    
5071                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
5072                            }
5073                            catch (Exception e) {
5074                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
5075    
5076                                    throw processException(e);
5077                            }
5078                            finally {
5079                                    closeSession(session);
5080                            }
5081                    }
5082    
5083                    return count.intValue();
5084            }
5085    
5086            /**
5087             * Returns the number of d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63;.
5088             *
5089             * @param groupId the group ID
5090             * @param classNameId the class name ID
5091             * @return the number of matching d d m templates that the user has permission to view
5092             * @throws SystemException if a system exception occurred
5093             */
5094            public int filterCountByG_C(long groupId, long classNameId)
5095                    throws SystemException {
5096                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
5097                            return countByG_C(groupId, classNameId);
5098                    }
5099    
5100                    StringBundler query = new StringBundler(3);
5101    
5102                    query.append(_FILTER_SQL_COUNT_DDMTEMPLATE_WHERE);
5103    
5104                    query.append(_FINDER_COLUMN_G_C_GROUPID_2);
5105    
5106                    query.append(_FINDER_COLUMN_G_C_CLASSNAMEID_2);
5107    
5108                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
5109                                    DDMTemplate.class.getName(),
5110                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
5111    
5112                    Session session = null;
5113    
5114                    try {
5115                            session = openSession();
5116    
5117                            SQLQuery q = session.createSQLQuery(sql);
5118    
5119                            q.addScalar(COUNT_COLUMN_NAME,
5120                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
5121    
5122                            QueryPos qPos = QueryPos.getInstance(q);
5123    
5124                            qPos.add(groupId);
5125    
5126                            qPos.add(classNameId);
5127    
5128                            Long count = (Long)q.uniqueResult();
5129    
5130                            return count.intValue();
5131                    }
5132                    catch (Exception e) {
5133                            throw processException(e);
5134                    }
5135                    finally {
5136                            closeSession(session);
5137                    }
5138            }
5139    
5140            private static final String _FINDER_COLUMN_G_C_GROUPID_2 = "ddmTemplate.groupId = ? AND ";
5141            private static final String _FINDER_COLUMN_G_C_CLASSNAMEID_2 = "ddmTemplate.classNameId = ?";
5142            public static final FinderPath FINDER_PATH_FETCH_BY_G_T = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
5143                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
5144                            FINDER_CLASS_NAME_ENTITY, "fetchByG_T",
5145                            new String[] { Long.class.getName(), String.class.getName() },
5146                            DDMTemplateModelImpl.GROUPID_COLUMN_BITMASK |
5147                            DDMTemplateModelImpl.TEMPLATEKEY_COLUMN_BITMASK);
5148            public static final FinderPath FINDER_PATH_COUNT_BY_G_T = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
5149                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
5150                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_T",
5151                            new String[] { Long.class.getName(), String.class.getName() });
5152    
5153            /**
5154             * Returns the d d m template where groupId = &#63; and templateKey = &#63; or throws a {@link com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException} if it could not be found.
5155             *
5156             * @param groupId the group ID
5157             * @param templateKey the template key
5158             * @return the matching d d m template
5159             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
5160             * @throws SystemException if a system exception occurred
5161             */
5162            public DDMTemplate findByG_T(long groupId, String templateKey)
5163                    throws NoSuchTemplateException, SystemException {
5164                    DDMTemplate ddmTemplate = fetchByG_T(groupId, templateKey);
5165    
5166                    if (ddmTemplate == null) {
5167                            StringBundler msg = new StringBundler(6);
5168    
5169                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5170    
5171                            msg.append("groupId=");
5172                            msg.append(groupId);
5173    
5174                            msg.append(", templateKey=");
5175                            msg.append(templateKey);
5176    
5177                            msg.append(StringPool.CLOSE_CURLY_BRACE);
5178    
5179                            if (_log.isWarnEnabled()) {
5180                                    _log.warn(msg.toString());
5181                            }
5182    
5183                            throw new NoSuchTemplateException(msg.toString());
5184                    }
5185    
5186                    return ddmTemplate;
5187            }
5188    
5189            /**
5190             * Returns the d d m template where groupId = &#63; and templateKey = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
5191             *
5192             * @param groupId the group ID
5193             * @param templateKey the template key
5194             * @return the matching d d m template, or <code>null</code> if a matching d d m template could not be found
5195             * @throws SystemException if a system exception occurred
5196             */
5197            public DDMTemplate fetchByG_T(long groupId, String templateKey)
5198                    throws SystemException {
5199                    return fetchByG_T(groupId, templateKey, true);
5200            }
5201    
5202            /**
5203             * Returns the d d m template where groupId = &#63; and templateKey = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
5204             *
5205             * @param groupId the group ID
5206             * @param templateKey the template key
5207             * @param retrieveFromCache whether to use the finder cache
5208             * @return the matching d d m template, or <code>null</code> if a matching d d m template could not be found
5209             * @throws SystemException if a system exception occurred
5210             */
5211            public DDMTemplate fetchByG_T(long groupId, String templateKey,
5212                    boolean retrieveFromCache) throws SystemException {
5213                    Object[] finderArgs = new Object[] { groupId, templateKey };
5214    
5215                    Object result = null;
5216    
5217                    if (retrieveFromCache) {
5218                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_T,
5219                                            finderArgs, this);
5220                    }
5221    
5222                    if (result instanceof DDMTemplate) {
5223                            DDMTemplate ddmTemplate = (DDMTemplate)result;
5224    
5225                            if ((groupId != ddmTemplate.getGroupId()) ||
5226                                            !Validator.equals(templateKey, ddmTemplate.getTemplateKey())) {
5227                                    result = null;
5228                            }
5229                    }
5230    
5231                    if (result == null) {
5232                            StringBundler query = new StringBundler(4);
5233    
5234                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
5235    
5236                            query.append(_FINDER_COLUMN_G_T_GROUPID_2);
5237    
5238                            if (templateKey == null) {
5239                                    query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_1);
5240                            }
5241                            else {
5242                                    if (templateKey.equals(StringPool.BLANK)) {
5243                                            query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_3);
5244                                    }
5245                                    else {
5246                                            query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_2);
5247                                    }
5248                            }
5249    
5250                            String sql = query.toString();
5251    
5252                            Session session = null;
5253    
5254                            try {
5255                                    session = openSession();
5256    
5257                                    Query q = session.createQuery(sql);
5258    
5259                                    QueryPos qPos = QueryPos.getInstance(q);
5260    
5261                                    qPos.add(groupId);
5262    
5263                                    if (templateKey != null) {
5264                                            qPos.add(templateKey);
5265                                    }
5266    
5267                                    List<DDMTemplate> list = q.list();
5268    
5269                                    if (list.isEmpty()) {
5270                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_T,
5271                                                    finderArgs, list);
5272                                    }
5273                                    else {
5274                                            DDMTemplate ddmTemplate = list.get(0);
5275    
5276                                            result = ddmTemplate;
5277    
5278                                            cacheResult(ddmTemplate);
5279    
5280                                            if ((ddmTemplate.getGroupId() != groupId) ||
5281                                                            (ddmTemplate.getTemplateKey() == null) ||
5282                                                            !ddmTemplate.getTemplateKey().equals(templateKey)) {
5283                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_T,
5284                                                            finderArgs, ddmTemplate);
5285                                            }
5286                                    }
5287                            }
5288                            catch (Exception e) {
5289                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_T,
5290                                            finderArgs);
5291    
5292                                    throw processException(e);
5293                            }
5294                            finally {
5295                                    closeSession(session);
5296                            }
5297                    }
5298    
5299                    if (result instanceof List<?>) {
5300                            return null;
5301                    }
5302                    else {
5303                            return (DDMTemplate)result;
5304                    }
5305            }
5306    
5307            /**
5308             * Removes the d d m template where groupId = &#63; and templateKey = &#63; from the database.
5309             *
5310             * @param groupId the group ID
5311             * @param templateKey the template key
5312             * @return the d d m template that was removed
5313             * @throws SystemException if a system exception occurred
5314             */
5315            public DDMTemplate removeByG_T(long groupId, String templateKey)
5316                    throws NoSuchTemplateException, SystemException {
5317                    DDMTemplate ddmTemplate = findByG_T(groupId, templateKey);
5318    
5319                    return remove(ddmTemplate);
5320            }
5321    
5322            /**
5323             * Returns the number of d d m templates where groupId = &#63; and templateKey = &#63;.
5324             *
5325             * @param groupId the group ID
5326             * @param templateKey the template key
5327             * @return the number of matching d d m templates
5328             * @throws SystemException if a system exception occurred
5329             */
5330            public int countByG_T(long groupId, String templateKey)
5331                    throws SystemException {
5332                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_T;
5333    
5334                    Object[] finderArgs = new Object[] { groupId, templateKey };
5335    
5336                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
5337                                    this);
5338    
5339                    if (count == null) {
5340                            StringBundler query = new StringBundler(3);
5341    
5342                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
5343    
5344                            query.append(_FINDER_COLUMN_G_T_GROUPID_2);
5345    
5346                            if (templateKey == null) {
5347                                    query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_1);
5348                            }
5349                            else {
5350                                    if (templateKey.equals(StringPool.BLANK)) {
5351                                            query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_3);
5352                                    }
5353                                    else {
5354                                            query.append(_FINDER_COLUMN_G_T_TEMPLATEKEY_2);
5355                                    }
5356                            }
5357    
5358                            String sql = query.toString();
5359    
5360                            Session session = null;
5361    
5362                            try {
5363                                    session = openSession();
5364    
5365                                    Query q = session.createQuery(sql);
5366    
5367                                    QueryPos qPos = QueryPos.getInstance(q);
5368    
5369                                    qPos.add(groupId);
5370    
5371                                    if (templateKey != null) {
5372                                            qPos.add(templateKey);
5373                                    }
5374    
5375                                    count = (Long)q.uniqueResult();
5376    
5377                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
5378                            }
5379                            catch (Exception e) {
5380                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
5381    
5382                                    throw processException(e);
5383                            }
5384                            finally {
5385                                    closeSession(session);
5386                            }
5387                    }
5388    
5389                    return count.intValue();
5390            }
5391    
5392            private static final String _FINDER_COLUMN_G_T_GROUPID_2 = "ddmTemplate.groupId = ? AND ";
5393            private static final String _FINDER_COLUMN_G_T_TEMPLATEKEY_1 = "ddmTemplate.templateKey IS NULL";
5394            private static final String _FINDER_COLUMN_G_T_TEMPLATEKEY_2 = "ddmTemplate.templateKey = ?";
5395            private static final String _FINDER_COLUMN_G_T_TEMPLATEKEY_3 = "(ddmTemplate.templateKey IS NULL OR ddmTemplate.templateKey = ?)";
5396            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
5397                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
5398                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByG_C_C",
5399                            new String[] {
5400                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
5401                                    
5402                            Integer.class.getName(), Integer.class.getName(),
5403                                    OrderByComparator.class.getName()
5404                            });
5405            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
5406                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
5407                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByG_C_C",
5408                            new String[] {
5409                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
5410                            },
5411                            DDMTemplateModelImpl.GROUPID_COLUMN_BITMASK |
5412                            DDMTemplateModelImpl.CLASSNAMEID_COLUMN_BITMASK |
5413                            DDMTemplateModelImpl.CLASSPK_COLUMN_BITMASK);
5414            public static final FinderPath FINDER_PATH_COUNT_BY_G_C_C = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
5415                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
5416                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_C_C",
5417                            new String[] {
5418                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
5419                            });
5420    
5421            /**
5422             * Returns all the d d m templates where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5423             *
5424             * @param groupId the group ID
5425             * @param classNameId the class name ID
5426             * @param classPK the class p k
5427             * @return the matching d d m templates
5428             * @throws SystemException if a system exception occurred
5429             */
5430            public List<DDMTemplate> findByG_C_C(long groupId, long classNameId,
5431                    long classPK) throws SystemException {
5432                    return findByG_C_C(groupId, classNameId, classPK, QueryUtil.ALL_POS,
5433                            QueryUtil.ALL_POS, null);
5434            }
5435    
5436            /**
5437             * Returns a range of all the d d m templates where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5438             *
5439             * <p>
5440             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
5441             * </p>
5442             *
5443             * @param groupId the group ID
5444             * @param classNameId the class name ID
5445             * @param classPK the class p k
5446             * @param start the lower bound of the range of d d m templates
5447             * @param end the upper bound of the range of d d m templates (not inclusive)
5448             * @return the range of matching d d m templates
5449             * @throws SystemException if a system exception occurred
5450             */
5451            public List<DDMTemplate> findByG_C_C(long groupId, long classNameId,
5452                    long classPK, int start, int end) throws SystemException {
5453                    return findByG_C_C(groupId, classNameId, classPK, start, end, null);
5454            }
5455    
5456            /**
5457             * Returns an ordered range of all the d d m templates where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5458             *
5459             * <p>
5460             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
5461             * </p>
5462             *
5463             * @param groupId the group ID
5464             * @param classNameId the class name ID
5465             * @param classPK the class p k
5466             * @param start the lower bound of the range of d d m templates
5467             * @param end the upper bound of the range of d d m templates (not inclusive)
5468             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5469             * @return the ordered range of matching d d m templates
5470             * @throws SystemException if a system exception occurred
5471             */
5472            public List<DDMTemplate> findByG_C_C(long groupId, long classNameId,
5473                    long classPK, int start, int end, OrderByComparator orderByComparator)
5474                    throws SystemException {
5475                    boolean pagination = true;
5476                    FinderPath finderPath = null;
5477                    Object[] finderArgs = null;
5478    
5479                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
5480                                    (orderByComparator == null)) {
5481                            pagination = false;
5482                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C;
5483                            finderArgs = new Object[] { groupId, classNameId, classPK };
5484                    }
5485                    else {
5486                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_G_C_C;
5487                            finderArgs = new Object[] {
5488                                            groupId, classNameId, classPK,
5489                                            
5490                                            start, end, orderByComparator
5491                                    };
5492                    }
5493    
5494                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
5495                                    finderArgs, this);
5496    
5497                    if ((list != null) && !list.isEmpty()) {
5498                            for (DDMTemplate ddmTemplate : list) {
5499                                    if ((groupId != ddmTemplate.getGroupId()) ||
5500                                                    (classNameId != ddmTemplate.getClassNameId()) ||
5501                                                    (classPK != ddmTemplate.getClassPK())) {
5502                                            list = null;
5503    
5504                                            break;
5505                                    }
5506                            }
5507                    }
5508    
5509                    if (list == null) {
5510                            StringBundler query = null;
5511    
5512                            if (orderByComparator != null) {
5513                                    query = new StringBundler(5 +
5514                                                    (orderByComparator.getOrderByFields().length * 3));
5515                            }
5516                            else {
5517                                    query = new StringBundler(5);
5518                            }
5519    
5520                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
5521    
5522                            query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
5523    
5524                            query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
5525    
5526                            query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
5527    
5528                            if (orderByComparator != null) {
5529                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5530                                            orderByComparator);
5531                            }
5532                            else
5533                             if (pagination) {
5534                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
5535                            }
5536    
5537                            String sql = query.toString();
5538    
5539                            Session session = null;
5540    
5541                            try {
5542                                    session = openSession();
5543    
5544                                    Query q = session.createQuery(sql);
5545    
5546                                    QueryPos qPos = QueryPos.getInstance(q);
5547    
5548                                    qPos.add(groupId);
5549    
5550                                    qPos.add(classNameId);
5551    
5552                                    qPos.add(classPK);
5553    
5554                                    if (!pagination) {
5555                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
5556                                                            start, end, false);
5557    
5558                                            Collections.sort(list);
5559    
5560                                            list = new UnmodifiableList<DDMTemplate>(list);
5561                                    }
5562                                    else {
5563                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
5564                                                            start, end);
5565                                    }
5566    
5567                                    cacheResult(list);
5568    
5569                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
5570                            }
5571                            catch (Exception e) {
5572                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
5573    
5574                                    throw processException(e);
5575                            }
5576                            finally {
5577                                    closeSession(session);
5578                            }
5579                    }
5580    
5581                    return list;
5582            }
5583    
5584            /**
5585             * Returns the first d d m template in the ordered set where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5586             *
5587             * @param groupId the group ID
5588             * @param classNameId the class name ID
5589             * @param classPK the class p k
5590             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5591             * @return the first matching d d m template
5592             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
5593             * @throws SystemException if a system exception occurred
5594             */
5595            public DDMTemplate findByG_C_C_First(long groupId, long classNameId,
5596                    long classPK, OrderByComparator orderByComparator)
5597                    throws NoSuchTemplateException, SystemException {
5598                    DDMTemplate ddmTemplate = fetchByG_C_C_First(groupId, classNameId,
5599                                    classPK, orderByComparator);
5600    
5601                    if (ddmTemplate != null) {
5602                            return ddmTemplate;
5603                    }
5604    
5605                    StringBundler msg = new StringBundler(8);
5606    
5607                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5608    
5609                    msg.append("groupId=");
5610                    msg.append(groupId);
5611    
5612                    msg.append(", classNameId=");
5613                    msg.append(classNameId);
5614    
5615                    msg.append(", classPK=");
5616                    msg.append(classPK);
5617    
5618                    msg.append(StringPool.CLOSE_CURLY_BRACE);
5619    
5620                    throw new NoSuchTemplateException(msg.toString());
5621            }
5622    
5623            /**
5624             * Returns the first d d m template in the ordered set where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5625             *
5626             * @param groupId the group ID
5627             * @param classNameId the class name ID
5628             * @param classPK the class p k
5629             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5630             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
5631             * @throws SystemException if a system exception occurred
5632             */
5633            public DDMTemplate fetchByG_C_C_First(long groupId, long classNameId,
5634                    long classPK, OrderByComparator orderByComparator)
5635                    throws SystemException {
5636                    List<DDMTemplate> list = findByG_C_C(groupId, classNameId, classPK, 0,
5637                                    1, orderByComparator);
5638    
5639                    if (!list.isEmpty()) {
5640                            return list.get(0);
5641                    }
5642    
5643                    return null;
5644            }
5645    
5646            /**
5647             * Returns the last d d m template in the ordered set where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5648             *
5649             * @param groupId the group ID
5650             * @param classNameId the class name ID
5651             * @param classPK the class p k
5652             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5653             * @return the last matching d d m template
5654             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
5655             * @throws SystemException if a system exception occurred
5656             */
5657            public DDMTemplate findByG_C_C_Last(long groupId, long classNameId,
5658                    long classPK, OrderByComparator orderByComparator)
5659                    throws NoSuchTemplateException, SystemException {
5660                    DDMTemplate ddmTemplate = fetchByG_C_C_Last(groupId, classNameId,
5661                                    classPK, orderByComparator);
5662    
5663                    if (ddmTemplate != null) {
5664                            return ddmTemplate;
5665                    }
5666    
5667                    StringBundler msg = new StringBundler(8);
5668    
5669                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
5670    
5671                    msg.append("groupId=");
5672                    msg.append(groupId);
5673    
5674                    msg.append(", classNameId=");
5675                    msg.append(classNameId);
5676    
5677                    msg.append(", classPK=");
5678                    msg.append(classPK);
5679    
5680                    msg.append(StringPool.CLOSE_CURLY_BRACE);
5681    
5682                    throw new NoSuchTemplateException(msg.toString());
5683            }
5684    
5685            /**
5686             * Returns the last d d m template in the ordered set where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5687             *
5688             * @param groupId the group ID
5689             * @param classNameId the class name ID
5690             * @param classPK the class p k
5691             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5692             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
5693             * @throws SystemException if a system exception occurred
5694             */
5695            public DDMTemplate fetchByG_C_C_Last(long groupId, long classNameId,
5696                    long classPK, OrderByComparator orderByComparator)
5697                    throws SystemException {
5698                    int count = countByG_C_C(groupId, classNameId, classPK);
5699    
5700                    List<DDMTemplate> list = findByG_C_C(groupId, classNameId, classPK,
5701                                    count - 1, count, orderByComparator);
5702    
5703                    if (!list.isEmpty()) {
5704                            return list.get(0);
5705                    }
5706    
5707                    return null;
5708            }
5709    
5710            /**
5711             * Returns the d d m templates before and after the current d d m template in the ordered set where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5712             *
5713             * @param templateId the primary key of the current d d m template
5714             * @param groupId the group ID
5715             * @param classNameId the class name ID
5716             * @param classPK the class p k
5717             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
5718             * @return the previous, current, and next d d m template
5719             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
5720             * @throws SystemException if a system exception occurred
5721             */
5722            public DDMTemplate[] findByG_C_C_PrevAndNext(long templateId, long groupId,
5723                    long classNameId, long classPK, OrderByComparator orderByComparator)
5724                    throws NoSuchTemplateException, SystemException {
5725                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
5726    
5727                    Session session = null;
5728    
5729                    try {
5730                            session = openSession();
5731    
5732                            DDMTemplate[] array = new DDMTemplateImpl[3];
5733    
5734                            array[0] = getByG_C_C_PrevAndNext(session, ddmTemplate, groupId,
5735                                            classNameId, classPK, orderByComparator, true);
5736    
5737                            array[1] = ddmTemplate;
5738    
5739                            array[2] = getByG_C_C_PrevAndNext(session, ddmTemplate, groupId,
5740                                            classNameId, classPK, orderByComparator, false);
5741    
5742                            return array;
5743                    }
5744                    catch (Exception e) {
5745                            throw processException(e);
5746                    }
5747                    finally {
5748                            closeSession(session);
5749                    }
5750            }
5751    
5752            protected DDMTemplate getByG_C_C_PrevAndNext(Session session,
5753                    DDMTemplate ddmTemplate, long groupId, long classNameId, long classPK,
5754                    OrderByComparator orderByComparator, boolean previous) {
5755                    StringBundler query = null;
5756    
5757                    if (orderByComparator != null) {
5758                            query = new StringBundler(6 +
5759                                            (orderByComparator.getOrderByFields().length * 6));
5760                    }
5761                    else {
5762                            query = new StringBundler(3);
5763                    }
5764    
5765                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
5766    
5767                    query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
5768    
5769                    query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
5770    
5771                    query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
5772    
5773                    if (orderByComparator != null) {
5774                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
5775    
5776                            if (orderByConditionFields.length > 0) {
5777                                    query.append(WHERE_AND);
5778                            }
5779    
5780                            for (int i = 0; i < orderByConditionFields.length; i++) {
5781                                    query.append(_ORDER_BY_ENTITY_ALIAS);
5782                                    query.append(orderByConditionFields[i]);
5783    
5784                                    if ((i + 1) < orderByConditionFields.length) {
5785                                            if (orderByComparator.isAscending() ^ previous) {
5786                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
5787                                            }
5788                                            else {
5789                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
5790                                            }
5791                                    }
5792                                    else {
5793                                            if (orderByComparator.isAscending() ^ previous) {
5794                                                    query.append(WHERE_GREATER_THAN);
5795                                            }
5796                                            else {
5797                                                    query.append(WHERE_LESSER_THAN);
5798                                            }
5799                                    }
5800                            }
5801    
5802                            query.append(ORDER_BY_CLAUSE);
5803    
5804                            String[] orderByFields = orderByComparator.getOrderByFields();
5805    
5806                            for (int i = 0; i < orderByFields.length; i++) {
5807                                    query.append(_ORDER_BY_ENTITY_ALIAS);
5808                                    query.append(orderByFields[i]);
5809    
5810                                    if ((i + 1) < orderByFields.length) {
5811                                            if (orderByComparator.isAscending() ^ previous) {
5812                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
5813                                            }
5814                                            else {
5815                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
5816                                            }
5817                                    }
5818                                    else {
5819                                            if (orderByComparator.isAscending() ^ previous) {
5820                                                    query.append(ORDER_BY_ASC);
5821                                            }
5822                                            else {
5823                                                    query.append(ORDER_BY_DESC);
5824                                            }
5825                                    }
5826                            }
5827                    }
5828                    else {
5829                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
5830                    }
5831    
5832                    String sql = query.toString();
5833    
5834                    Query q = session.createQuery(sql);
5835    
5836                    q.setFirstResult(0);
5837                    q.setMaxResults(2);
5838    
5839                    QueryPos qPos = QueryPos.getInstance(q);
5840    
5841                    qPos.add(groupId);
5842    
5843                    qPos.add(classNameId);
5844    
5845                    qPos.add(classPK);
5846    
5847                    if (orderByComparator != null) {
5848                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
5849    
5850                            for (Object value : values) {
5851                                    qPos.add(value);
5852                            }
5853                    }
5854    
5855                    List<DDMTemplate> list = q.list();
5856    
5857                    if (list.size() == 2) {
5858                            return list.get(1);
5859                    }
5860                    else {
5861                            return null;
5862                    }
5863            }
5864    
5865            /**
5866             * Returns all the d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5867             *
5868             * @param groupId the group ID
5869             * @param classNameId the class name ID
5870             * @param classPK the class p k
5871             * @return the matching d d m templates that the user has permission to view
5872             * @throws SystemException if a system exception occurred
5873             */
5874            public List<DDMTemplate> filterFindByG_C_C(long groupId, long classNameId,
5875                    long classPK) throws SystemException {
5876                    return filterFindByG_C_C(groupId, classNameId, classPK,
5877                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
5878            }
5879    
5880            /**
5881             * Returns a range of all the d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5882             *
5883             * <p>
5884             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
5885             * </p>
5886             *
5887             * @param groupId the group ID
5888             * @param classNameId the class name ID
5889             * @param classPK the class p k
5890             * @param start the lower bound of the range of d d m templates
5891             * @param end the upper bound of the range of d d m templates (not inclusive)
5892             * @return the range of matching d d m templates that the user has permission to view
5893             * @throws SystemException if a system exception occurred
5894             */
5895            public List<DDMTemplate> filterFindByG_C_C(long groupId, long classNameId,
5896                    long classPK, int start, int end) throws SystemException {
5897                    return filterFindByG_C_C(groupId, classNameId, classPK, start, end, null);
5898            }
5899    
5900            /**
5901             * Returns an ordered range of all the d d m templates that the user has permissions to view where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
5902             *
5903             * <p>
5904             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
5905             * </p>
5906             *
5907             * @param groupId the group ID
5908             * @param classNameId the class name ID
5909             * @param classPK the class p k
5910             * @param start the lower bound of the range of d d m templates
5911             * @param end the upper bound of the range of d d m templates (not inclusive)
5912             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5913             * @return the ordered range of matching d d m templates that the user has permission to view
5914             * @throws SystemException if a system exception occurred
5915             */
5916            public List<DDMTemplate> filterFindByG_C_C(long groupId, long classNameId,
5917                    long classPK, int start, int end, OrderByComparator orderByComparator)
5918                    throws SystemException {
5919                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
5920                            return findByG_C_C(groupId, classNameId, classPK, start, end,
5921                                    orderByComparator);
5922                    }
5923    
5924                    StringBundler query = null;
5925    
5926                    if (orderByComparator != null) {
5927                            query = new StringBundler(5 +
5928                                            (orderByComparator.getOrderByFields().length * 3));
5929                    }
5930                    else {
5931                            query = new StringBundler(5);
5932                    }
5933    
5934                    if (getDB().isSupportsInlineDistinct()) {
5935                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
5936                    }
5937                    else {
5938                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
5939                    }
5940    
5941                    query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
5942    
5943                    query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
5944    
5945                    query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
5946    
5947                    if (!getDB().isSupportsInlineDistinct()) {
5948                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
5949                    }
5950    
5951                    if (orderByComparator != null) {
5952                            if (getDB().isSupportsInlineDistinct()) {
5953                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5954                                            orderByComparator);
5955                            }
5956                            else {
5957                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
5958                                            orderByComparator);
5959                            }
5960                    }
5961                    else {
5962                            if (getDB().isSupportsInlineDistinct()) {
5963                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
5964                            }
5965                            else {
5966                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
5967                            }
5968                    }
5969    
5970                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
5971                                    DDMTemplate.class.getName(),
5972                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
5973    
5974                    Session session = null;
5975    
5976                    try {
5977                            session = openSession();
5978    
5979                            SQLQuery q = session.createSQLQuery(sql);
5980    
5981                            if (getDB().isSupportsInlineDistinct()) {
5982                                    q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
5983                            }
5984                            else {
5985                                    q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
5986                            }
5987    
5988                            QueryPos qPos = QueryPos.getInstance(q);
5989    
5990                            qPos.add(groupId);
5991    
5992                            qPos.add(classNameId);
5993    
5994                            qPos.add(classPK);
5995    
5996                            return (List<DDMTemplate>)QueryUtil.list(q, getDialect(), start, end);
5997                    }
5998                    catch (Exception e) {
5999                            throw processException(e);
6000                    }
6001                    finally {
6002                            closeSession(session);
6003                    }
6004            }
6005    
6006            /**
6007             * Returns the d d m templates before and after the current d d m template in the ordered set of d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
6008             *
6009             * @param templateId the primary key of the current d d m template
6010             * @param groupId the group ID
6011             * @param classNameId the class name ID
6012             * @param classPK the class p k
6013             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6014             * @return the previous, current, and next d d m template
6015             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
6016             * @throws SystemException if a system exception occurred
6017             */
6018            public DDMTemplate[] filterFindByG_C_C_PrevAndNext(long templateId,
6019                    long groupId, long classNameId, long classPK,
6020                    OrderByComparator orderByComparator)
6021                    throws NoSuchTemplateException, SystemException {
6022                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
6023                            return findByG_C_C_PrevAndNext(templateId, groupId, classNameId,
6024                                    classPK, orderByComparator);
6025                    }
6026    
6027                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
6028    
6029                    Session session = null;
6030    
6031                    try {
6032                            session = openSession();
6033    
6034                            DDMTemplate[] array = new DDMTemplateImpl[3];
6035    
6036                            array[0] = filterGetByG_C_C_PrevAndNext(session, ddmTemplate,
6037                                            groupId, classNameId, classPK, orderByComparator, true);
6038    
6039                            array[1] = ddmTemplate;
6040    
6041                            array[2] = filterGetByG_C_C_PrevAndNext(session, ddmTemplate,
6042                                            groupId, classNameId, classPK, orderByComparator, false);
6043    
6044                            return array;
6045                    }
6046                    catch (Exception e) {
6047                            throw processException(e);
6048                    }
6049                    finally {
6050                            closeSession(session);
6051                    }
6052            }
6053    
6054            protected DDMTemplate filterGetByG_C_C_PrevAndNext(Session session,
6055                    DDMTemplate ddmTemplate, long groupId, long classNameId, long classPK,
6056                    OrderByComparator orderByComparator, boolean previous) {
6057                    StringBundler query = null;
6058    
6059                    if (orderByComparator != null) {
6060                            query = new StringBundler(6 +
6061                                            (orderByComparator.getOrderByFields().length * 6));
6062                    }
6063                    else {
6064                            query = new StringBundler(3);
6065                    }
6066    
6067                    if (getDB().isSupportsInlineDistinct()) {
6068                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_WHERE);
6069                    }
6070                    else {
6071                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1);
6072                    }
6073    
6074                    query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
6075    
6076                    query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
6077    
6078                    query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
6079    
6080                    if (!getDB().isSupportsInlineDistinct()) {
6081                            query.append(_FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2);
6082                    }
6083    
6084                    if (orderByComparator != null) {
6085                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
6086    
6087                            if (orderByConditionFields.length > 0) {
6088                                    query.append(WHERE_AND);
6089                            }
6090    
6091                            for (int i = 0; i < orderByConditionFields.length; i++) {
6092                                    if (getDB().isSupportsInlineDistinct()) {
6093                                            query.append(_ORDER_BY_ENTITY_ALIAS);
6094                                    }
6095                                    else {
6096                                            query.append(_ORDER_BY_ENTITY_TABLE);
6097                                    }
6098    
6099                                    query.append(orderByConditionFields[i]);
6100    
6101                                    if ((i + 1) < orderByConditionFields.length) {
6102                                            if (orderByComparator.isAscending() ^ previous) {
6103                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
6104                                            }
6105                                            else {
6106                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
6107                                            }
6108                                    }
6109                                    else {
6110                                            if (orderByComparator.isAscending() ^ previous) {
6111                                                    query.append(WHERE_GREATER_THAN);
6112                                            }
6113                                            else {
6114                                                    query.append(WHERE_LESSER_THAN);
6115                                            }
6116                                    }
6117                            }
6118    
6119                            query.append(ORDER_BY_CLAUSE);
6120    
6121                            String[] orderByFields = orderByComparator.getOrderByFields();
6122    
6123                            for (int i = 0; i < orderByFields.length; i++) {
6124                                    if (getDB().isSupportsInlineDistinct()) {
6125                                            query.append(_ORDER_BY_ENTITY_ALIAS);
6126                                    }
6127                                    else {
6128                                            query.append(_ORDER_BY_ENTITY_TABLE);
6129                                    }
6130    
6131                                    query.append(orderByFields[i]);
6132    
6133                                    if ((i + 1) < orderByFields.length) {
6134                                            if (orderByComparator.isAscending() ^ previous) {
6135                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
6136                                            }
6137                                            else {
6138                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
6139                                            }
6140                                    }
6141                                    else {
6142                                            if (orderByComparator.isAscending() ^ previous) {
6143                                                    query.append(ORDER_BY_ASC);
6144                                            }
6145                                            else {
6146                                                    query.append(ORDER_BY_DESC);
6147                                            }
6148                                    }
6149                            }
6150                    }
6151                    else {
6152                            if (getDB().isSupportsInlineDistinct()) {
6153                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
6154                            }
6155                            else {
6156                                    query.append(DDMTemplateModelImpl.ORDER_BY_SQL);
6157                            }
6158                    }
6159    
6160                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
6161                                    DDMTemplate.class.getName(),
6162                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
6163    
6164                    SQLQuery q = session.createSQLQuery(sql);
6165    
6166                    q.setFirstResult(0);
6167                    q.setMaxResults(2);
6168    
6169                    if (getDB().isSupportsInlineDistinct()) {
6170                            q.addEntity(_FILTER_ENTITY_ALIAS, DDMTemplateImpl.class);
6171                    }
6172                    else {
6173                            q.addEntity(_FILTER_ENTITY_TABLE, DDMTemplateImpl.class);
6174                    }
6175    
6176                    QueryPos qPos = QueryPos.getInstance(q);
6177    
6178                    qPos.add(groupId);
6179    
6180                    qPos.add(classNameId);
6181    
6182                    qPos.add(classPK);
6183    
6184                    if (orderByComparator != null) {
6185                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
6186    
6187                            for (Object value : values) {
6188                                    qPos.add(value);
6189                            }
6190                    }
6191    
6192                    List<DDMTemplate> list = q.list();
6193    
6194                    if (list.size() == 2) {
6195                            return list.get(1);
6196                    }
6197                    else {
6198                            return null;
6199                    }
6200            }
6201    
6202            /**
6203             * Removes all the d d m templates where groupId = &#63; and classNameId = &#63; and classPK = &#63; from the database.
6204             *
6205             * @param groupId the group ID
6206             * @param classNameId the class name ID
6207             * @param classPK the class p k
6208             * @throws SystemException if a system exception occurred
6209             */
6210            public void removeByG_C_C(long groupId, long classNameId, long classPK)
6211                    throws SystemException {
6212                    for (DDMTemplate ddmTemplate : findByG_C_C(groupId, classNameId,
6213                                    classPK, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
6214                            remove(ddmTemplate);
6215                    }
6216            }
6217    
6218            /**
6219             * Returns the number of d d m templates where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
6220             *
6221             * @param groupId the group ID
6222             * @param classNameId the class name ID
6223             * @param classPK the class p k
6224             * @return the number of matching d d m templates
6225             * @throws SystemException if a system exception occurred
6226             */
6227            public int countByG_C_C(long groupId, long classNameId, long classPK)
6228                    throws SystemException {
6229                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_C_C;
6230    
6231                    Object[] finderArgs = new Object[] { groupId, classNameId, classPK };
6232    
6233                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
6234                                    this);
6235    
6236                    if (count == null) {
6237                            StringBundler query = new StringBundler(4);
6238    
6239                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
6240    
6241                            query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
6242    
6243                            query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
6244    
6245                            query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
6246    
6247                            String sql = query.toString();
6248    
6249                            Session session = null;
6250    
6251                            try {
6252                                    session = openSession();
6253    
6254                                    Query q = session.createQuery(sql);
6255    
6256                                    QueryPos qPos = QueryPos.getInstance(q);
6257    
6258                                    qPos.add(groupId);
6259    
6260                                    qPos.add(classNameId);
6261    
6262                                    qPos.add(classPK);
6263    
6264                                    count = (Long)q.uniqueResult();
6265    
6266                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
6267                            }
6268                            catch (Exception e) {
6269                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
6270    
6271                                    throw processException(e);
6272                            }
6273                            finally {
6274                                    closeSession(session);
6275                            }
6276                    }
6277    
6278                    return count.intValue();
6279            }
6280    
6281            /**
6282             * Returns the number of d d m templates that the user has permission to view where groupId = &#63; and classNameId = &#63; and classPK = &#63;.
6283             *
6284             * @param groupId the group ID
6285             * @param classNameId the class name ID
6286             * @param classPK the class p k
6287             * @return the number of matching d d m templates that the user has permission to view
6288             * @throws SystemException if a system exception occurred
6289             */
6290            public int filterCountByG_C_C(long groupId, long classNameId, long classPK)
6291                    throws SystemException {
6292                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
6293                            return countByG_C_C(groupId, classNameId, classPK);
6294                    }
6295    
6296                    StringBundler query = new StringBundler(4);
6297    
6298                    query.append(_FILTER_SQL_COUNT_DDMTEMPLATE_WHERE);
6299    
6300                    query.append(_FINDER_COLUMN_G_C_C_GROUPID_2);
6301    
6302                    query.append(_FINDER_COLUMN_G_C_C_CLASSNAMEID_2);
6303    
6304                    query.append(_FINDER_COLUMN_G_C_C_CLASSPK_2);
6305    
6306                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
6307                                    DDMTemplate.class.getName(),
6308                                    _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN, groupId);
6309    
6310                    Session session = null;
6311    
6312                    try {
6313                            session = openSession();
6314    
6315                            SQLQuery q = session.createSQLQuery(sql);
6316    
6317                            q.addScalar(COUNT_COLUMN_NAME,
6318                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
6319    
6320                            QueryPos qPos = QueryPos.getInstance(q);
6321    
6322                            qPos.add(groupId);
6323    
6324                            qPos.add(classNameId);
6325    
6326                            qPos.add(classPK);
6327    
6328                            Long count = (Long)q.uniqueResult();
6329    
6330                            return count.intValue();
6331                    }
6332                    catch (Exception e) {
6333                            throw processException(e);
6334                    }
6335                    finally {
6336                            closeSession(session);
6337                    }
6338            }
6339    
6340            private static final String _FINDER_COLUMN_G_C_C_GROUPID_2 = "ddmTemplate.groupId = ? AND ";
6341            private static final String _FINDER_COLUMN_G_C_C_CLASSNAMEID_2 = "ddmTemplate.classNameId = ? AND ";
6342            private static final String _FINDER_COLUMN_G_C_C_CLASSPK_2 = "ddmTemplate.classPK = ?";
6343            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6344                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
6345                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_T",
6346                            new String[] {
6347                                    Long.class.getName(), Long.class.getName(),
6348                                    String.class.getName(),
6349                                    
6350                            Integer.class.getName(), Integer.class.getName(),
6351                                    OrderByComparator.class.getName()
6352                            });
6353            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6354                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
6355                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_T",
6356                            new String[] {
6357                                    Long.class.getName(), Long.class.getName(),
6358                                    String.class.getName()
6359                            },
6360                            DDMTemplateModelImpl.CLASSNAMEID_COLUMN_BITMASK |
6361                            DDMTemplateModelImpl.CLASSPK_COLUMN_BITMASK |
6362                            DDMTemplateModelImpl.TYPE_COLUMN_BITMASK);
6363            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_T = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6364                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
6365                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_T",
6366                            new String[] {
6367                                    Long.class.getName(), Long.class.getName(),
6368                                    String.class.getName()
6369                            });
6370    
6371            /**
6372             * Returns all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63;.
6373             *
6374             * @param classNameId the class name ID
6375             * @param classPK the class p k
6376             * @param type the type
6377             * @return the matching d d m templates
6378             * @throws SystemException if a system exception occurred
6379             */
6380            public List<DDMTemplate> findByC_C_T(long classNameId, long classPK,
6381                    String type) throws SystemException {
6382                    return findByC_C_T(classNameId, classPK, type, QueryUtil.ALL_POS,
6383                            QueryUtil.ALL_POS, null);
6384            }
6385    
6386            /**
6387             * Returns a range of all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63;.
6388             *
6389             * <p>
6390             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
6391             * </p>
6392             *
6393             * @param classNameId the class name ID
6394             * @param classPK the class p k
6395             * @param type the type
6396             * @param start the lower bound of the range of d d m templates
6397             * @param end the upper bound of the range of d d m templates (not inclusive)
6398             * @return the range of matching d d m templates
6399             * @throws SystemException if a system exception occurred
6400             */
6401            public List<DDMTemplate> findByC_C_T(long classNameId, long classPK,
6402                    String type, int start, int end) throws SystemException {
6403                    return findByC_C_T(classNameId, classPK, type, start, end, null);
6404            }
6405    
6406            /**
6407             * Returns an ordered range of all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63;.
6408             *
6409             * <p>
6410             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
6411             * </p>
6412             *
6413             * @param classNameId the class name ID
6414             * @param classPK the class p k
6415             * @param type the type
6416             * @param start the lower bound of the range of d d m templates
6417             * @param end the upper bound of the range of d d m templates (not inclusive)
6418             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
6419             * @return the ordered range of matching d d m templates
6420             * @throws SystemException if a system exception occurred
6421             */
6422            public List<DDMTemplate> findByC_C_T(long classNameId, long classPK,
6423                    String type, int start, int end, OrderByComparator orderByComparator)
6424                    throws SystemException {
6425                    boolean pagination = true;
6426                    FinderPath finderPath = null;
6427                    Object[] finderArgs = null;
6428    
6429                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
6430                                    (orderByComparator == null)) {
6431                            pagination = false;
6432                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T;
6433                            finderArgs = new Object[] { classNameId, classPK, type };
6434                    }
6435                    else {
6436                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T;
6437                            finderArgs = new Object[] {
6438                                            classNameId, classPK, type,
6439                                            
6440                                            start, end, orderByComparator
6441                                    };
6442                    }
6443    
6444                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
6445                                    finderArgs, this);
6446    
6447                    if ((list != null) && !list.isEmpty()) {
6448                            for (DDMTemplate ddmTemplate : list) {
6449                                    if ((classNameId != ddmTemplate.getClassNameId()) ||
6450                                                    (classPK != ddmTemplate.getClassPK()) ||
6451                                                    !Validator.equals(type, ddmTemplate.getType())) {
6452                                            list = null;
6453    
6454                                            break;
6455                                    }
6456                            }
6457                    }
6458    
6459                    if (list == null) {
6460                            StringBundler query = null;
6461    
6462                            if (orderByComparator != null) {
6463                                    query = new StringBundler(5 +
6464                                                    (orderByComparator.getOrderByFields().length * 3));
6465                            }
6466                            else {
6467                                    query = new StringBundler(5);
6468                            }
6469    
6470                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
6471    
6472                            query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
6473    
6474                            query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
6475    
6476                            if (type == null) {
6477                                    query.append(_FINDER_COLUMN_C_C_T_TYPE_1);
6478                            }
6479                            else {
6480                                    if (type.equals(StringPool.BLANK)) {
6481                                            query.append(_FINDER_COLUMN_C_C_T_TYPE_3);
6482                                    }
6483                                    else {
6484                                            query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
6485                                    }
6486                            }
6487    
6488                            if (orderByComparator != null) {
6489                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
6490                                            orderByComparator);
6491                            }
6492                            else
6493                             if (pagination) {
6494                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
6495                            }
6496    
6497                            String sql = query.toString();
6498    
6499                            Session session = null;
6500    
6501                            try {
6502                                    session = openSession();
6503    
6504                                    Query q = session.createQuery(sql);
6505    
6506                                    QueryPos qPos = QueryPos.getInstance(q);
6507    
6508                                    qPos.add(classNameId);
6509    
6510                                    qPos.add(classPK);
6511    
6512                                    if (type != null) {
6513                                            qPos.add(type);
6514                                    }
6515    
6516                                    if (!pagination) {
6517                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
6518                                                            start, end, false);
6519    
6520                                            Collections.sort(list);
6521    
6522                                            list = new UnmodifiableList<DDMTemplate>(list);
6523                                    }
6524                                    else {
6525                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
6526                                                            start, end);
6527                                    }
6528    
6529                                    cacheResult(list);
6530    
6531                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
6532                            }
6533                            catch (Exception e) {
6534                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
6535    
6536                                    throw processException(e);
6537                            }
6538                            finally {
6539                                    closeSession(session);
6540                            }
6541                    }
6542    
6543                    return list;
6544            }
6545    
6546            /**
6547             * Returns the first d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
6548             *
6549             * @param classNameId the class name ID
6550             * @param classPK the class p k
6551             * @param type the type
6552             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6553             * @return the first matching d d m template
6554             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
6555             * @throws SystemException if a system exception occurred
6556             */
6557            public DDMTemplate findByC_C_T_First(long classNameId, long classPK,
6558                    String type, OrderByComparator orderByComparator)
6559                    throws NoSuchTemplateException, SystemException {
6560                    DDMTemplate ddmTemplate = fetchByC_C_T_First(classNameId, classPK,
6561                                    type, orderByComparator);
6562    
6563                    if (ddmTemplate != null) {
6564                            return ddmTemplate;
6565                    }
6566    
6567                    StringBundler msg = new StringBundler(8);
6568    
6569                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6570    
6571                    msg.append("classNameId=");
6572                    msg.append(classNameId);
6573    
6574                    msg.append(", classPK=");
6575                    msg.append(classPK);
6576    
6577                    msg.append(", type=");
6578                    msg.append(type);
6579    
6580                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6581    
6582                    throw new NoSuchTemplateException(msg.toString());
6583            }
6584    
6585            /**
6586             * Returns the first d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
6587             *
6588             * @param classNameId the class name ID
6589             * @param classPK the class p k
6590             * @param type the type
6591             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6592             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
6593             * @throws SystemException if a system exception occurred
6594             */
6595            public DDMTemplate fetchByC_C_T_First(long classNameId, long classPK,
6596                    String type, OrderByComparator orderByComparator)
6597                    throws SystemException {
6598                    List<DDMTemplate> list = findByC_C_T(classNameId, classPK, type, 0, 1,
6599                                    orderByComparator);
6600    
6601                    if (!list.isEmpty()) {
6602                            return list.get(0);
6603                    }
6604    
6605                    return null;
6606            }
6607    
6608            /**
6609             * Returns the last d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
6610             *
6611             * @param classNameId the class name ID
6612             * @param classPK the class p k
6613             * @param type the type
6614             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6615             * @return the last matching d d m template
6616             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
6617             * @throws SystemException if a system exception occurred
6618             */
6619            public DDMTemplate findByC_C_T_Last(long classNameId, long classPK,
6620                    String type, OrderByComparator orderByComparator)
6621                    throws NoSuchTemplateException, SystemException {
6622                    DDMTemplate ddmTemplate = fetchByC_C_T_Last(classNameId, classPK, type,
6623                                    orderByComparator);
6624    
6625                    if (ddmTemplate != null) {
6626                            return ddmTemplate;
6627                    }
6628    
6629                    StringBundler msg = new StringBundler(8);
6630    
6631                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
6632    
6633                    msg.append("classNameId=");
6634                    msg.append(classNameId);
6635    
6636                    msg.append(", classPK=");
6637                    msg.append(classPK);
6638    
6639                    msg.append(", type=");
6640                    msg.append(type);
6641    
6642                    msg.append(StringPool.CLOSE_CURLY_BRACE);
6643    
6644                    throw new NoSuchTemplateException(msg.toString());
6645            }
6646    
6647            /**
6648             * Returns the last d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
6649             *
6650             * @param classNameId the class name ID
6651             * @param classPK the class p k
6652             * @param type the type
6653             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6654             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
6655             * @throws SystemException if a system exception occurred
6656             */
6657            public DDMTemplate fetchByC_C_T_Last(long classNameId, long classPK,
6658                    String type, OrderByComparator orderByComparator)
6659                    throws SystemException {
6660                    int count = countByC_C_T(classNameId, classPK, type);
6661    
6662                    List<DDMTemplate> list = findByC_C_T(classNameId, classPK, type,
6663                                    count - 1, count, orderByComparator);
6664    
6665                    if (!list.isEmpty()) {
6666                            return list.get(0);
6667                    }
6668    
6669                    return null;
6670            }
6671    
6672            /**
6673             * Returns the d d m templates before and after the current d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
6674             *
6675             * @param templateId the primary key of the current d d m template
6676             * @param classNameId the class name ID
6677             * @param classPK the class p k
6678             * @param type the type
6679             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
6680             * @return the previous, current, and next d d m template
6681             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
6682             * @throws SystemException if a system exception occurred
6683             */
6684            public DDMTemplate[] findByC_C_T_PrevAndNext(long templateId,
6685                    long classNameId, long classPK, String type,
6686                    OrderByComparator orderByComparator)
6687                    throws NoSuchTemplateException, SystemException {
6688                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
6689    
6690                    Session session = null;
6691    
6692                    try {
6693                            session = openSession();
6694    
6695                            DDMTemplate[] array = new DDMTemplateImpl[3];
6696    
6697                            array[0] = getByC_C_T_PrevAndNext(session, ddmTemplate,
6698                                            classNameId, classPK, type, orderByComparator, true);
6699    
6700                            array[1] = ddmTemplate;
6701    
6702                            array[2] = getByC_C_T_PrevAndNext(session, ddmTemplate,
6703                                            classNameId, classPK, type, orderByComparator, false);
6704    
6705                            return array;
6706                    }
6707                    catch (Exception e) {
6708                            throw processException(e);
6709                    }
6710                    finally {
6711                            closeSession(session);
6712                    }
6713            }
6714    
6715            protected DDMTemplate getByC_C_T_PrevAndNext(Session session,
6716                    DDMTemplate ddmTemplate, long classNameId, long classPK, String type,
6717                    OrderByComparator orderByComparator, boolean previous) {
6718                    StringBundler query = null;
6719    
6720                    if (orderByComparator != null) {
6721                            query = new StringBundler(6 +
6722                                            (orderByComparator.getOrderByFields().length * 6));
6723                    }
6724                    else {
6725                            query = new StringBundler(3);
6726                    }
6727    
6728                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
6729    
6730                    query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
6731    
6732                    query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
6733    
6734                    if (type == null) {
6735                            query.append(_FINDER_COLUMN_C_C_T_TYPE_1);
6736                    }
6737                    else {
6738                            if (type.equals(StringPool.BLANK)) {
6739                                    query.append(_FINDER_COLUMN_C_C_T_TYPE_3);
6740                            }
6741                            else {
6742                                    query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
6743                            }
6744                    }
6745    
6746                    if (orderByComparator != null) {
6747                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
6748    
6749                            if (orderByConditionFields.length > 0) {
6750                                    query.append(WHERE_AND);
6751                            }
6752    
6753                            for (int i = 0; i < orderByConditionFields.length; i++) {
6754                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6755                                    query.append(orderByConditionFields[i]);
6756    
6757                                    if ((i + 1) < orderByConditionFields.length) {
6758                                            if (orderByComparator.isAscending() ^ previous) {
6759                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
6760                                            }
6761                                            else {
6762                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
6763                                            }
6764                                    }
6765                                    else {
6766                                            if (orderByComparator.isAscending() ^ previous) {
6767                                                    query.append(WHERE_GREATER_THAN);
6768                                            }
6769                                            else {
6770                                                    query.append(WHERE_LESSER_THAN);
6771                                            }
6772                                    }
6773                            }
6774    
6775                            query.append(ORDER_BY_CLAUSE);
6776    
6777                            String[] orderByFields = orderByComparator.getOrderByFields();
6778    
6779                            for (int i = 0; i < orderByFields.length; i++) {
6780                                    query.append(_ORDER_BY_ENTITY_ALIAS);
6781                                    query.append(orderByFields[i]);
6782    
6783                                    if ((i + 1) < orderByFields.length) {
6784                                            if (orderByComparator.isAscending() ^ previous) {
6785                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
6786                                            }
6787                                            else {
6788                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
6789                                            }
6790                                    }
6791                                    else {
6792                                            if (orderByComparator.isAscending() ^ previous) {
6793                                                    query.append(ORDER_BY_ASC);
6794                                            }
6795                                            else {
6796                                                    query.append(ORDER_BY_DESC);
6797                                            }
6798                                    }
6799                            }
6800                    }
6801                    else {
6802                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
6803                    }
6804    
6805                    String sql = query.toString();
6806    
6807                    Query q = session.createQuery(sql);
6808    
6809                    q.setFirstResult(0);
6810                    q.setMaxResults(2);
6811    
6812                    QueryPos qPos = QueryPos.getInstance(q);
6813    
6814                    qPos.add(classNameId);
6815    
6816                    qPos.add(classPK);
6817    
6818                    if (type != null) {
6819                            qPos.add(type);
6820                    }
6821    
6822                    if (orderByComparator != null) {
6823                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
6824    
6825                            for (Object value : values) {
6826                                    qPos.add(value);
6827                            }
6828                    }
6829    
6830                    List<DDMTemplate> list = q.list();
6831    
6832                    if (list.size() == 2) {
6833                            return list.get(1);
6834                    }
6835                    else {
6836                            return null;
6837                    }
6838            }
6839    
6840            /**
6841             * Removes all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; from the database.
6842             *
6843             * @param classNameId the class name ID
6844             * @param classPK the class p k
6845             * @param type the type
6846             * @throws SystemException if a system exception occurred
6847             */
6848            public void removeByC_C_T(long classNameId, long classPK, String type)
6849                    throws SystemException {
6850                    for (DDMTemplate ddmTemplate : findByC_C_T(classNameId, classPK, type,
6851                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
6852                            remove(ddmTemplate);
6853                    }
6854            }
6855    
6856            /**
6857             * Returns the number of d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63;.
6858             *
6859             * @param classNameId the class name ID
6860             * @param classPK the class p k
6861             * @param type the type
6862             * @return the number of matching d d m templates
6863             * @throws SystemException if a system exception occurred
6864             */
6865            public int countByC_C_T(long classNameId, long classPK, String type)
6866                    throws SystemException {
6867                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_T;
6868    
6869                    Object[] finderArgs = new Object[] { classNameId, classPK, type };
6870    
6871                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
6872                                    this);
6873    
6874                    if (count == null) {
6875                            StringBundler query = new StringBundler(4);
6876    
6877                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
6878    
6879                            query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
6880    
6881                            query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
6882    
6883                            if (type == null) {
6884                                    query.append(_FINDER_COLUMN_C_C_T_TYPE_1);
6885                            }
6886                            else {
6887                                    if (type.equals(StringPool.BLANK)) {
6888                                            query.append(_FINDER_COLUMN_C_C_T_TYPE_3);
6889                                    }
6890                                    else {
6891                                            query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
6892                                    }
6893                            }
6894    
6895                            String sql = query.toString();
6896    
6897                            Session session = null;
6898    
6899                            try {
6900                                    session = openSession();
6901    
6902                                    Query q = session.createQuery(sql);
6903    
6904                                    QueryPos qPos = QueryPos.getInstance(q);
6905    
6906                                    qPos.add(classNameId);
6907    
6908                                    qPos.add(classPK);
6909    
6910                                    if (type != null) {
6911                                            qPos.add(type);
6912                                    }
6913    
6914                                    count = (Long)q.uniqueResult();
6915    
6916                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
6917                            }
6918                            catch (Exception e) {
6919                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
6920    
6921                                    throw processException(e);
6922                            }
6923                            finally {
6924                                    closeSession(session);
6925                            }
6926                    }
6927    
6928                    return count.intValue();
6929            }
6930    
6931            private static final String _FINDER_COLUMN_C_C_T_CLASSNAMEID_2 = "ddmTemplate.classNameId = ? AND ";
6932            private static final String _FINDER_COLUMN_C_C_T_CLASSPK_2 = "ddmTemplate.classPK = ? AND ";
6933            private static final String _FINDER_COLUMN_C_C_T_TYPE_1 = "ddmTemplate.type IS NULL";
6934            private static final String _FINDER_COLUMN_C_C_T_TYPE_2 = "ddmTemplate.type = ?";
6935            private static final String _FINDER_COLUMN_C_C_T_TYPE_3 = "(ddmTemplate.type IS NULL OR ddmTemplate.type = ?)";
6936            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T_M = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6937                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
6938                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_T_M",
6939                            new String[] {
6940                                    Long.class.getName(), Long.class.getName(),
6941                                    String.class.getName(), String.class.getName(),
6942                                    
6943                            Integer.class.getName(), Integer.class.getName(),
6944                                    OrderByComparator.class.getName()
6945                            });
6946            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T_M =
6947                    new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6948                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, DDMTemplateImpl.class,
6949                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_T_M",
6950                            new String[] {
6951                                    Long.class.getName(), Long.class.getName(),
6952                                    String.class.getName(), String.class.getName()
6953                            },
6954                            DDMTemplateModelImpl.CLASSNAMEID_COLUMN_BITMASK |
6955                            DDMTemplateModelImpl.CLASSPK_COLUMN_BITMASK |
6956                            DDMTemplateModelImpl.TYPE_COLUMN_BITMASK |
6957                            DDMTemplateModelImpl.MODE_COLUMN_BITMASK);
6958            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_T_M = new FinderPath(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
6959                            DDMTemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
6960                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_T_M",
6961                            new String[] {
6962                                    Long.class.getName(), Long.class.getName(),
6963                                    String.class.getName(), String.class.getName()
6964                            });
6965    
6966            /**
6967             * Returns all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
6968             *
6969             * @param classNameId the class name ID
6970             * @param classPK the class p k
6971             * @param type the type
6972             * @param mode the mode
6973             * @return the matching d d m templates
6974             * @throws SystemException if a system exception occurred
6975             */
6976            public List<DDMTemplate> findByC_C_T_M(long classNameId, long classPK,
6977                    String type, String mode) throws SystemException {
6978                    return findByC_C_T_M(classNameId, classPK, type, mode,
6979                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
6980            }
6981    
6982            /**
6983             * Returns a range of all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
6984             *
6985             * <p>
6986             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
6987             * </p>
6988             *
6989             * @param classNameId the class name ID
6990             * @param classPK the class p k
6991             * @param type the type
6992             * @param mode the mode
6993             * @param start the lower bound of the range of d d m templates
6994             * @param end the upper bound of the range of d d m templates (not inclusive)
6995             * @return the range of matching d d m templates
6996             * @throws SystemException if a system exception occurred
6997             */
6998            public List<DDMTemplate> findByC_C_T_M(long classNameId, long classPK,
6999                    String type, String mode, int start, int end) throws SystemException {
7000                    return findByC_C_T_M(classNameId, classPK, type, mode, start, end, null);
7001            }
7002    
7003            /**
7004             * Returns an ordered range of all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7005             *
7006             * <p>
7007             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
7008             * </p>
7009             *
7010             * @param classNameId the class name ID
7011             * @param classPK the class p k
7012             * @param type the type
7013             * @param mode the mode
7014             * @param start the lower bound of the range of d d m templates
7015             * @param end the upper bound of the range of d d m templates (not inclusive)
7016             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
7017             * @return the ordered range of matching d d m templates
7018             * @throws SystemException if a system exception occurred
7019             */
7020            public List<DDMTemplate> findByC_C_T_M(long classNameId, long classPK,
7021                    String type, String mode, int start, int end,
7022                    OrderByComparator orderByComparator) throws SystemException {
7023                    boolean pagination = true;
7024                    FinderPath finderPath = null;
7025                    Object[] finderArgs = null;
7026    
7027                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
7028                                    (orderByComparator == null)) {
7029                            pagination = false;
7030                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T_M;
7031                            finderArgs = new Object[] { classNameId, classPK, type, mode };
7032                    }
7033                    else {
7034                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T_M;
7035                            finderArgs = new Object[] {
7036                                            classNameId, classPK, type, mode,
7037                                            
7038                                            start, end, orderByComparator
7039                                    };
7040                    }
7041    
7042                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
7043                                    finderArgs, this);
7044    
7045                    if ((list != null) && !list.isEmpty()) {
7046                            for (DDMTemplate ddmTemplate : list) {
7047                                    if ((classNameId != ddmTemplate.getClassNameId()) ||
7048                                                    (classPK != ddmTemplate.getClassPK()) ||
7049                                                    !Validator.equals(type, ddmTemplate.getType()) ||
7050                                                    !Validator.equals(mode, ddmTemplate.getMode())) {
7051                                            list = null;
7052    
7053                                            break;
7054                                    }
7055                            }
7056                    }
7057    
7058                    if (list == null) {
7059                            StringBundler query = null;
7060    
7061                            if (orderByComparator != null) {
7062                                    query = new StringBundler(6 +
7063                                                    (orderByComparator.getOrderByFields().length * 3));
7064                            }
7065                            else {
7066                                    query = new StringBundler(6);
7067                            }
7068    
7069                            query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
7070    
7071                            query.append(_FINDER_COLUMN_C_C_T_M_CLASSNAMEID_2);
7072    
7073                            query.append(_FINDER_COLUMN_C_C_T_M_CLASSPK_2);
7074    
7075                            if (type == null) {
7076                                    query.append(_FINDER_COLUMN_C_C_T_M_TYPE_1);
7077                            }
7078                            else {
7079                                    if (type.equals(StringPool.BLANK)) {
7080                                            query.append(_FINDER_COLUMN_C_C_T_M_TYPE_3);
7081                                    }
7082                                    else {
7083                                            query.append(_FINDER_COLUMN_C_C_T_M_TYPE_2);
7084                                    }
7085                            }
7086    
7087                            if (mode == null) {
7088                                    query.append(_FINDER_COLUMN_C_C_T_M_MODE_1);
7089                            }
7090                            else {
7091                                    if (mode.equals(StringPool.BLANK)) {
7092                                            query.append(_FINDER_COLUMN_C_C_T_M_MODE_3);
7093                                    }
7094                                    else {
7095                                            query.append(_FINDER_COLUMN_C_C_T_M_MODE_2);
7096                                    }
7097                            }
7098    
7099                            if (orderByComparator != null) {
7100                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
7101                                            orderByComparator);
7102                            }
7103                            else
7104                             if (pagination) {
7105                                    query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
7106                            }
7107    
7108                            String sql = query.toString();
7109    
7110                            Session session = null;
7111    
7112                            try {
7113                                    session = openSession();
7114    
7115                                    Query q = session.createQuery(sql);
7116    
7117                                    QueryPos qPos = QueryPos.getInstance(q);
7118    
7119                                    qPos.add(classNameId);
7120    
7121                                    qPos.add(classPK);
7122    
7123                                    if (type != null) {
7124                                            qPos.add(type);
7125                                    }
7126    
7127                                    if (mode != null) {
7128                                            qPos.add(mode);
7129                                    }
7130    
7131                                    if (!pagination) {
7132                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
7133                                                            start, end, false);
7134    
7135                                            Collections.sort(list);
7136    
7137                                            list = new UnmodifiableList<DDMTemplate>(list);
7138                                    }
7139                                    else {
7140                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
7141                                                            start, end);
7142                                    }
7143    
7144                                    cacheResult(list);
7145    
7146                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
7147                            }
7148                            catch (Exception e) {
7149                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
7150    
7151                                    throw processException(e);
7152                            }
7153                            finally {
7154                                    closeSession(session);
7155                            }
7156                    }
7157    
7158                    return list;
7159            }
7160    
7161            /**
7162             * Returns the first d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7163             *
7164             * @param classNameId the class name ID
7165             * @param classPK the class p k
7166             * @param type the type
7167             * @param mode the mode
7168             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7169             * @return the first matching d d m template
7170             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
7171             * @throws SystemException if a system exception occurred
7172             */
7173            public DDMTemplate findByC_C_T_M_First(long classNameId, long classPK,
7174                    String type, String mode, OrderByComparator orderByComparator)
7175                    throws NoSuchTemplateException, SystemException {
7176                    DDMTemplate ddmTemplate = fetchByC_C_T_M_First(classNameId, classPK,
7177                                    type, mode, orderByComparator);
7178    
7179                    if (ddmTemplate != null) {
7180                            return ddmTemplate;
7181                    }
7182    
7183                    StringBundler msg = new StringBundler(10);
7184    
7185                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
7186    
7187                    msg.append("classNameId=");
7188                    msg.append(classNameId);
7189    
7190                    msg.append(", classPK=");
7191                    msg.append(classPK);
7192    
7193                    msg.append(", type=");
7194                    msg.append(type);
7195    
7196                    msg.append(", mode=");
7197                    msg.append(mode);
7198    
7199                    msg.append(StringPool.CLOSE_CURLY_BRACE);
7200    
7201                    throw new NoSuchTemplateException(msg.toString());
7202            }
7203    
7204            /**
7205             * Returns the first d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7206             *
7207             * @param classNameId the class name ID
7208             * @param classPK the class p k
7209             * @param type the type
7210             * @param mode the mode
7211             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7212             * @return the first matching d d m template, or <code>null</code> if a matching d d m template could not be found
7213             * @throws SystemException if a system exception occurred
7214             */
7215            public DDMTemplate fetchByC_C_T_M_First(long classNameId, long classPK,
7216                    String type, String mode, OrderByComparator orderByComparator)
7217                    throws SystemException {
7218                    List<DDMTemplate> list = findByC_C_T_M(classNameId, classPK, type,
7219                                    mode, 0, 1, orderByComparator);
7220    
7221                    if (!list.isEmpty()) {
7222                            return list.get(0);
7223                    }
7224    
7225                    return null;
7226            }
7227    
7228            /**
7229             * Returns the last d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7230             *
7231             * @param classNameId the class name ID
7232             * @param classPK the class p k
7233             * @param type the type
7234             * @param mode the mode
7235             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7236             * @return the last matching d d m template
7237             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a matching d d m template could not be found
7238             * @throws SystemException if a system exception occurred
7239             */
7240            public DDMTemplate findByC_C_T_M_Last(long classNameId, long classPK,
7241                    String type, String mode, OrderByComparator orderByComparator)
7242                    throws NoSuchTemplateException, SystemException {
7243                    DDMTemplate ddmTemplate = fetchByC_C_T_M_Last(classNameId, classPK,
7244                                    type, mode, orderByComparator);
7245    
7246                    if (ddmTemplate != null) {
7247                            return ddmTemplate;
7248                    }
7249    
7250                    StringBundler msg = new StringBundler(10);
7251    
7252                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
7253    
7254                    msg.append("classNameId=");
7255                    msg.append(classNameId);
7256    
7257                    msg.append(", classPK=");
7258                    msg.append(classPK);
7259    
7260                    msg.append(", type=");
7261                    msg.append(type);
7262    
7263                    msg.append(", mode=");
7264                    msg.append(mode);
7265    
7266                    msg.append(StringPool.CLOSE_CURLY_BRACE);
7267    
7268                    throw new NoSuchTemplateException(msg.toString());
7269            }
7270    
7271            /**
7272             * Returns the last d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7273             *
7274             * @param classNameId the class name ID
7275             * @param classPK the class p k
7276             * @param type the type
7277             * @param mode the mode
7278             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7279             * @return the last matching d d m template, or <code>null</code> if a matching d d m template could not be found
7280             * @throws SystemException if a system exception occurred
7281             */
7282            public DDMTemplate fetchByC_C_T_M_Last(long classNameId, long classPK,
7283                    String type, String mode, OrderByComparator orderByComparator)
7284                    throws SystemException {
7285                    int count = countByC_C_T_M(classNameId, classPK, type, mode);
7286    
7287                    List<DDMTemplate> list = findByC_C_T_M(classNameId, classPK, type,
7288                                    mode, count - 1, count, orderByComparator);
7289    
7290                    if (!list.isEmpty()) {
7291                            return list.get(0);
7292                    }
7293    
7294                    return null;
7295            }
7296    
7297            /**
7298             * Returns the d d m templates before and after the current d d m template in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7299             *
7300             * @param templateId the primary key of the current d d m template
7301             * @param classNameId the class name ID
7302             * @param classPK the class p k
7303             * @param type the type
7304             * @param mode the mode
7305             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
7306             * @return the previous, current, and next d d m template
7307             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
7308             * @throws SystemException if a system exception occurred
7309             */
7310            public DDMTemplate[] findByC_C_T_M_PrevAndNext(long templateId,
7311                    long classNameId, long classPK, String type, String mode,
7312                    OrderByComparator orderByComparator)
7313                    throws NoSuchTemplateException, SystemException {
7314                    DDMTemplate ddmTemplate = findByPrimaryKey(templateId);
7315    
7316                    Session session = null;
7317    
7318                    try {
7319                            session = openSession();
7320    
7321                            DDMTemplate[] array = new DDMTemplateImpl[3];
7322    
7323                            array[0] = getByC_C_T_M_PrevAndNext(session, ddmTemplate,
7324                                            classNameId, classPK, type, mode, orderByComparator, true);
7325    
7326                            array[1] = ddmTemplate;
7327    
7328                            array[2] = getByC_C_T_M_PrevAndNext(session, ddmTemplate,
7329                                            classNameId, classPK, type, mode, orderByComparator, false);
7330    
7331                            return array;
7332                    }
7333                    catch (Exception e) {
7334                            throw processException(e);
7335                    }
7336                    finally {
7337                            closeSession(session);
7338                    }
7339            }
7340    
7341            protected DDMTemplate getByC_C_T_M_PrevAndNext(Session session,
7342                    DDMTemplate ddmTemplate, long classNameId, long classPK, String type,
7343                    String mode, OrderByComparator orderByComparator, boolean previous) {
7344                    StringBundler query = null;
7345    
7346                    if (orderByComparator != null) {
7347                            query = new StringBundler(6 +
7348                                            (orderByComparator.getOrderByFields().length * 6));
7349                    }
7350                    else {
7351                            query = new StringBundler(3);
7352                    }
7353    
7354                    query.append(_SQL_SELECT_DDMTEMPLATE_WHERE);
7355    
7356                    query.append(_FINDER_COLUMN_C_C_T_M_CLASSNAMEID_2);
7357    
7358                    query.append(_FINDER_COLUMN_C_C_T_M_CLASSPK_2);
7359    
7360                    if (type == null) {
7361                            query.append(_FINDER_COLUMN_C_C_T_M_TYPE_1);
7362                    }
7363                    else {
7364                            if (type.equals(StringPool.BLANK)) {
7365                                    query.append(_FINDER_COLUMN_C_C_T_M_TYPE_3);
7366                            }
7367                            else {
7368                                    query.append(_FINDER_COLUMN_C_C_T_M_TYPE_2);
7369                            }
7370                    }
7371    
7372                    if (mode == null) {
7373                            query.append(_FINDER_COLUMN_C_C_T_M_MODE_1);
7374                    }
7375                    else {
7376                            if (mode.equals(StringPool.BLANK)) {
7377                                    query.append(_FINDER_COLUMN_C_C_T_M_MODE_3);
7378                            }
7379                            else {
7380                                    query.append(_FINDER_COLUMN_C_C_T_M_MODE_2);
7381                            }
7382                    }
7383    
7384                    if (orderByComparator != null) {
7385                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
7386    
7387                            if (orderByConditionFields.length > 0) {
7388                                    query.append(WHERE_AND);
7389                            }
7390    
7391                            for (int i = 0; i < orderByConditionFields.length; i++) {
7392                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7393                                    query.append(orderByConditionFields[i]);
7394    
7395                                    if ((i + 1) < orderByConditionFields.length) {
7396                                            if (orderByComparator.isAscending() ^ previous) {
7397                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
7398                                            }
7399                                            else {
7400                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
7401                                            }
7402                                    }
7403                                    else {
7404                                            if (orderByComparator.isAscending() ^ previous) {
7405                                                    query.append(WHERE_GREATER_THAN);
7406                                            }
7407                                            else {
7408                                                    query.append(WHERE_LESSER_THAN);
7409                                            }
7410                                    }
7411                            }
7412    
7413                            query.append(ORDER_BY_CLAUSE);
7414    
7415                            String[] orderByFields = orderByComparator.getOrderByFields();
7416    
7417                            for (int i = 0; i < orderByFields.length; i++) {
7418                                    query.append(_ORDER_BY_ENTITY_ALIAS);
7419                                    query.append(orderByFields[i]);
7420    
7421                                    if ((i + 1) < orderByFields.length) {
7422                                            if (orderByComparator.isAscending() ^ previous) {
7423                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
7424                                            }
7425                                            else {
7426                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
7427                                            }
7428                                    }
7429                                    else {
7430                                            if (orderByComparator.isAscending() ^ previous) {
7431                                                    query.append(ORDER_BY_ASC);
7432                                            }
7433                                            else {
7434                                                    query.append(ORDER_BY_DESC);
7435                                            }
7436                                    }
7437                            }
7438                    }
7439                    else {
7440                            query.append(DDMTemplateModelImpl.ORDER_BY_JPQL);
7441                    }
7442    
7443                    String sql = query.toString();
7444    
7445                    Query q = session.createQuery(sql);
7446    
7447                    q.setFirstResult(0);
7448                    q.setMaxResults(2);
7449    
7450                    QueryPos qPos = QueryPos.getInstance(q);
7451    
7452                    qPos.add(classNameId);
7453    
7454                    qPos.add(classPK);
7455    
7456                    if (type != null) {
7457                            qPos.add(type);
7458                    }
7459    
7460                    if (mode != null) {
7461                            qPos.add(mode);
7462                    }
7463    
7464                    if (orderByComparator != null) {
7465                            Object[] values = orderByComparator.getOrderByConditionValues(ddmTemplate);
7466    
7467                            for (Object value : values) {
7468                                    qPos.add(value);
7469                            }
7470                    }
7471    
7472                    List<DDMTemplate> list = q.list();
7473    
7474                    if (list.size() == 2) {
7475                            return list.get(1);
7476                    }
7477                    else {
7478                            return null;
7479                    }
7480            }
7481    
7482            /**
7483             * Removes all the d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63; from the database.
7484             *
7485             * @param classNameId the class name ID
7486             * @param classPK the class p k
7487             * @param type the type
7488             * @param mode the mode
7489             * @throws SystemException if a system exception occurred
7490             */
7491            public void removeByC_C_T_M(long classNameId, long classPK, String type,
7492                    String mode) throws SystemException {
7493                    for (DDMTemplate ddmTemplate : findByC_C_T_M(classNameId, classPK,
7494                                    type, mode, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
7495                            remove(ddmTemplate);
7496                    }
7497            }
7498    
7499            /**
7500             * Returns the number of d d m templates where classNameId = &#63; and classPK = &#63; and type = &#63; and mode = &#63;.
7501             *
7502             * @param classNameId the class name ID
7503             * @param classPK the class p k
7504             * @param type the type
7505             * @param mode the mode
7506             * @return the number of matching d d m templates
7507             * @throws SystemException if a system exception occurred
7508             */
7509            public int countByC_C_T_M(long classNameId, long classPK, String type,
7510                    String mode) throws SystemException {
7511                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_T_M;
7512    
7513                    Object[] finderArgs = new Object[] { classNameId, classPK, type, mode };
7514    
7515                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
7516                                    this);
7517    
7518                    if (count == null) {
7519                            StringBundler query = new StringBundler(5);
7520    
7521                            query.append(_SQL_COUNT_DDMTEMPLATE_WHERE);
7522    
7523                            query.append(_FINDER_COLUMN_C_C_T_M_CLASSNAMEID_2);
7524    
7525                            query.append(_FINDER_COLUMN_C_C_T_M_CLASSPK_2);
7526    
7527                            if (type == null) {
7528                                    query.append(_FINDER_COLUMN_C_C_T_M_TYPE_1);
7529                            }
7530                            else {
7531                                    if (type.equals(StringPool.BLANK)) {
7532                                            query.append(_FINDER_COLUMN_C_C_T_M_TYPE_3);
7533                                    }
7534                                    else {
7535                                            query.append(_FINDER_COLUMN_C_C_T_M_TYPE_2);
7536                                    }
7537                            }
7538    
7539                            if (mode == null) {
7540                                    query.append(_FINDER_COLUMN_C_C_T_M_MODE_1);
7541                            }
7542                            else {
7543                                    if (mode.equals(StringPool.BLANK)) {
7544                                            query.append(_FINDER_COLUMN_C_C_T_M_MODE_3);
7545                                    }
7546                                    else {
7547                                            query.append(_FINDER_COLUMN_C_C_T_M_MODE_2);
7548                                    }
7549                            }
7550    
7551                            String sql = query.toString();
7552    
7553                            Session session = null;
7554    
7555                            try {
7556                                    session = openSession();
7557    
7558                                    Query q = session.createQuery(sql);
7559    
7560                                    QueryPos qPos = QueryPos.getInstance(q);
7561    
7562                                    qPos.add(classNameId);
7563    
7564                                    qPos.add(classPK);
7565    
7566                                    if (type != null) {
7567                                            qPos.add(type);
7568                                    }
7569    
7570                                    if (mode != null) {
7571                                            qPos.add(mode);
7572                                    }
7573    
7574                                    count = (Long)q.uniqueResult();
7575    
7576                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
7577                            }
7578                            catch (Exception e) {
7579                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
7580    
7581                                    throw processException(e);
7582                            }
7583                            finally {
7584                                    closeSession(session);
7585                            }
7586                    }
7587    
7588                    return count.intValue();
7589            }
7590    
7591            private static final String _FINDER_COLUMN_C_C_T_M_CLASSNAMEID_2 = "ddmTemplate.classNameId = ? AND ";
7592            private static final String _FINDER_COLUMN_C_C_T_M_CLASSPK_2 = "ddmTemplate.classPK = ? AND ";
7593            private static final String _FINDER_COLUMN_C_C_T_M_TYPE_1 = "ddmTemplate.type IS NULL AND ";
7594            private static final String _FINDER_COLUMN_C_C_T_M_TYPE_2 = "ddmTemplate.type = ? AND ";
7595            private static final String _FINDER_COLUMN_C_C_T_M_TYPE_3 = "(ddmTemplate.type IS NULL OR ddmTemplate.type = ?) AND ";
7596            private static final String _FINDER_COLUMN_C_C_T_M_MODE_1 = "ddmTemplate.mode IS NULL";
7597            private static final String _FINDER_COLUMN_C_C_T_M_MODE_2 = "ddmTemplate.mode = ?";
7598            private static final String _FINDER_COLUMN_C_C_T_M_MODE_3 = "(ddmTemplate.mode IS NULL OR ddmTemplate.mode = ?)";
7599    
7600            /**
7601             * Caches the d d m template in the entity cache if it is enabled.
7602             *
7603             * @param ddmTemplate the d d m template
7604             */
7605            public void cacheResult(DDMTemplate ddmTemplate) {
7606                    EntityCacheUtil.putResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
7607                            DDMTemplateImpl.class, ddmTemplate.getPrimaryKey(), ddmTemplate);
7608    
7609                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
7610                            new Object[] {
7611                                    ddmTemplate.getUuid(), Long.valueOf(ddmTemplate.getGroupId())
7612                            }, ddmTemplate);
7613    
7614                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_T,
7615                            new Object[] {
7616                                    Long.valueOf(ddmTemplate.getGroupId()),
7617                                    
7618                            ddmTemplate.getTemplateKey()
7619                            }, ddmTemplate);
7620    
7621                    ddmTemplate.resetOriginalValues();
7622            }
7623    
7624            /**
7625             * Caches the d d m templates in the entity cache if it is enabled.
7626             *
7627             * @param ddmTemplates the d d m templates
7628             */
7629            public void cacheResult(List<DDMTemplate> ddmTemplates) {
7630                    for (DDMTemplate ddmTemplate : ddmTemplates) {
7631                            if (EntityCacheUtil.getResult(
7632                                                    DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
7633                                                    DDMTemplateImpl.class, ddmTemplate.getPrimaryKey()) == null) {
7634                                    cacheResult(ddmTemplate);
7635                            }
7636                            else {
7637                                    ddmTemplate.resetOriginalValues();
7638                            }
7639                    }
7640            }
7641    
7642            /**
7643             * Clears the cache for all d d m templates.
7644             *
7645             * <p>
7646             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
7647             * </p>
7648             */
7649            @Override
7650            public void clearCache() {
7651                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
7652                            CacheRegistryUtil.clear(DDMTemplateImpl.class.getName());
7653                    }
7654    
7655                    EntityCacheUtil.clearCache(DDMTemplateImpl.class.getName());
7656    
7657                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
7658                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7659                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7660            }
7661    
7662            /**
7663             * Clears the cache for the d d m template.
7664             *
7665             * <p>
7666             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
7667             * </p>
7668             */
7669            @Override
7670            public void clearCache(DDMTemplate ddmTemplate) {
7671                    EntityCacheUtil.removeResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
7672                            DDMTemplateImpl.class, ddmTemplate.getPrimaryKey());
7673    
7674                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7675                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7676    
7677                    clearUniqueFindersCache(ddmTemplate);
7678            }
7679    
7680            @Override
7681            public void clearCache(List<DDMTemplate> ddmTemplates) {
7682                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7683                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7684    
7685                    for (DDMTemplate ddmTemplate : ddmTemplates) {
7686                            EntityCacheUtil.removeResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
7687                                    DDMTemplateImpl.class, ddmTemplate.getPrimaryKey());
7688    
7689                            clearUniqueFindersCache(ddmTemplate);
7690                    }
7691            }
7692    
7693            protected void cacheUniqueFindersCache(DDMTemplate ddmTemplate) {
7694                    if (ddmTemplate.isNew()) {
7695                            Object[] args = new Object[] {
7696                                            ddmTemplate.getUuid(),
7697                                            Long.valueOf(ddmTemplate.getGroupId())
7698                                    };
7699    
7700                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
7701                                    Long.valueOf(1));
7702                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
7703                                    ddmTemplate);
7704    
7705                            args = new Object[] {
7706                                            Long.valueOf(ddmTemplate.getGroupId()),
7707                                            
7708                                            ddmTemplate.getTemplateKey()
7709                                    };
7710    
7711                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_T, args,
7712                                    Long.valueOf(1));
7713                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_T, args,
7714                                    ddmTemplate);
7715                    }
7716                    else {
7717                            DDMTemplateModelImpl ddmTemplateModelImpl = (DDMTemplateModelImpl)ddmTemplate;
7718    
7719                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7720                                            FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
7721                                    Object[] args = new Object[] {
7722                                                    ddmTemplate.getUuid(),
7723                                                    Long.valueOf(ddmTemplate.getGroupId())
7724                                            };
7725    
7726                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
7727                                            Long.valueOf(1));
7728                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
7729                                            ddmTemplate);
7730                            }
7731    
7732                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7733                                            FINDER_PATH_FETCH_BY_G_T.getColumnBitmask()) != 0) {
7734                                    Object[] args = new Object[] {
7735                                                    Long.valueOf(ddmTemplate.getGroupId()),
7736                                                    
7737                                                    ddmTemplate.getTemplateKey()
7738                                            };
7739    
7740                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_T, args,
7741                                            Long.valueOf(1));
7742                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_T, args,
7743                                            ddmTemplate);
7744                            }
7745                    }
7746            }
7747    
7748            protected void clearUniqueFindersCache(DDMTemplate ddmTemplate) {
7749                    DDMTemplateModelImpl ddmTemplateModelImpl = (DDMTemplateModelImpl)ddmTemplate;
7750    
7751                    Object[] args = new Object[] {
7752                                    ddmTemplate.getUuid(), Long.valueOf(ddmTemplate.getGroupId())
7753                            };
7754    
7755                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
7756                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);
7757    
7758                    if ((ddmTemplateModelImpl.getColumnBitmask() &
7759                                    FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
7760                            args = new Object[] {
7761                                            ddmTemplateModelImpl.getOriginalUuid(),
7762                                            Long.valueOf(ddmTemplateModelImpl.getOriginalGroupId())
7763                                    };
7764    
7765                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
7766                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);
7767                    }
7768    
7769                    args = new Object[] {
7770                                    Long.valueOf(ddmTemplate.getGroupId()),
7771                                    
7772                                    ddmTemplate.getTemplateKey()
7773                            };
7774    
7775                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_T, args);
7776                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_T, args);
7777    
7778                    if ((ddmTemplateModelImpl.getColumnBitmask() &
7779                                    FINDER_PATH_FETCH_BY_G_T.getColumnBitmask()) != 0) {
7780                            args = new Object[] {
7781                                            Long.valueOf(ddmTemplateModelImpl.getOriginalGroupId()),
7782                                            
7783                                            ddmTemplateModelImpl.getOriginalTemplateKey()
7784                                    };
7785    
7786                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_T, args);
7787                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_T, args);
7788                    }
7789            }
7790    
7791            /**
7792             * Creates a new d d m template with the primary key. Does not add the d d m template to the database.
7793             *
7794             * @param templateId the primary key for the new d d m template
7795             * @return the new d d m template
7796             */
7797            public DDMTemplate create(long templateId) {
7798                    DDMTemplate ddmTemplate = new DDMTemplateImpl();
7799    
7800                    ddmTemplate.setNew(true);
7801                    ddmTemplate.setPrimaryKey(templateId);
7802    
7803                    String uuid = PortalUUIDUtil.generate();
7804    
7805                    ddmTemplate.setUuid(uuid);
7806    
7807                    return ddmTemplate;
7808            }
7809    
7810            /**
7811             * Removes the d d m template with the primary key from the database. Also notifies the appropriate model listeners.
7812             *
7813             * @param templateId the primary key of the d d m template
7814             * @return the d d m template that was removed
7815             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
7816             * @throws SystemException if a system exception occurred
7817             */
7818            public DDMTemplate remove(long templateId)
7819                    throws NoSuchTemplateException, SystemException {
7820                    return remove(Long.valueOf(templateId));
7821            }
7822    
7823            /**
7824             * Removes the d d m template with the primary key from the database. Also notifies the appropriate model listeners.
7825             *
7826             * @param primaryKey the primary key of the d d m template
7827             * @return the d d m template that was removed
7828             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
7829             * @throws SystemException if a system exception occurred
7830             */
7831            @Override
7832            public DDMTemplate remove(Serializable primaryKey)
7833                    throws NoSuchTemplateException, SystemException {
7834                    Session session = null;
7835    
7836                    try {
7837                            session = openSession();
7838    
7839                            DDMTemplate ddmTemplate = (DDMTemplate)session.get(DDMTemplateImpl.class,
7840                                            primaryKey);
7841    
7842                            if (ddmTemplate == null) {
7843                                    if (_log.isWarnEnabled()) {
7844                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
7845                                    }
7846    
7847                                    throw new NoSuchTemplateException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
7848                                            primaryKey);
7849                            }
7850    
7851                            return remove(ddmTemplate);
7852                    }
7853                    catch (NoSuchTemplateException nsee) {
7854                            throw nsee;
7855                    }
7856                    catch (Exception e) {
7857                            throw processException(e);
7858                    }
7859                    finally {
7860                            closeSession(session);
7861                    }
7862            }
7863    
7864            @Override
7865            protected DDMTemplate removeImpl(DDMTemplate ddmTemplate)
7866                    throws SystemException {
7867                    ddmTemplate = toUnwrappedModel(ddmTemplate);
7868    
7869                    Session session = null;
7870    
7871                    try {
7872                            session = openSession();
7873    
7874                            if (!session.contains(ddmTemplate)) {
7875                                    ddmTemplate = (DDMTemplate)session.get(DDMTemplateImpl.class,
7876                                                    ddmTemplate.getPrimaryKeyObj());
7877                            }
7878    
7879                            if (ddmTemplate != null) {
7880                                    session.delete(ddmTemplate);
7881                            }
7882                    }
7883                    catch (Exception e) {
7884                            throw processException(e);
7885                    }
7886                    finally {
7887                            closeSession(session);
7888                    }
7889    
7890                    if (ddmTemplate != null) {
7891                            clearCache(ddmTemplate);
7892                    }
7893    
7894                    return ddmTemplate;
7895            }
7896    
7897            @Override
7898            public DDMTemplate updateImpl(
7899                    com.liferay.portlet.dynamicdatamapping.model.DDMTemplate ddmTemplate)
7900                    throws SystemException {
7901                    ddmTemplate = toUnwrappedModel(ddmTemplate);
7902    
7903                    boolean isNew = ddmTemplate.isNew();
7904    
7905                    DDMTemplateModelImpl ddmTemplateModelImpl = (DDMTemplateModelImpl)ddmTemplate;
7906    
7907                    if (Validator.isNull(ddmTemplate.getUuid())) {
7908                            String uuid = PortalUUIDUtil.generate();
7909    
7910                            ddmTemplate.setUuid(uuid);
7911                    }
7912    
7913                    Session session = null;
7914    
7915                    try {
7916                            session = openSession();
7917    
7918                            if (ddmTemplate.isNew()) {
7919                                    session.save(ddmTemplate);
7920    
7921                                    ddmTemplate.setNew(false);
7922                            }
7923                            else {
7924                                    session.merge(ddmTemplate);
7925                            }
7926                    }
7927                    catch (Exception e) {
7928                            throw processException(e);
7929                    }
7930                    finally {
7931                            closeSession(session);
7932                    }
7933    
7934                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
7935    
7936                    if (isNew || !DDMTemplateModelImpl.COLUMN_BITMASK_ENABLED) {
7937                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
7938                    }
7939    
7940                    else {
7941                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7942                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
7943                                    Object[] args = new Object[] {
7944                                                    ddmTemplateModelImpl.getOriginalUuid()
7945                                            };
7946    
7947                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7948                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7949                                            args);
7950    
7951                                    args = new Object[] { ddmTemplateModelImpl.getUuid() };
7952    
7953                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
7954                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
7955                                            args);
7956                            }
7957    
7958                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7959                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
7960                                    Object[] args = new Object[] {
7961                                                    ddmTemplateModelImpl.getOriginalUuid(),
7962                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalCompanyId())
7963                                            };
7964    
7965                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7966                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7967                                            args);
7968    
7969                                    args = new Object[] {
7970                                                    ddmTemplateModelImpl.getUuid(),
7971                                                    Long.valueOf(ddmTemplateModelImpl.getCompanyId())
7972                                            };
7973    
7974                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
7975                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
7976                                            args);
7977                            }
7978    
7979                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7980                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
7981                                    Object[] args = new Object[] {
7982                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalGroupId())
7983                                            };
7984    
7985                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
7986                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
7987                                            args);
7988    
7989                                    args = new Object[] {
7990                                                    Long.valueOf(ddmTemplateModelImpl.getGroupId())
7991                                            };
7992    
7993                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
7994                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
7995                                            args);
7996                            }
7997    
7998                            if ((ddmTemplateModelImpl.getColumnBitmask() &
7999                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK.getColumnBitmask()) != 0) {
8000                                    Object[] args = new Object[] {
8001                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassPK())
8002                                            };
8003    
8004                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSPK, args);
8005                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK,
8006                                            args);
8007    
8008                                    args = new Object[] {
8009                                                    Long.valueOf(ddmTemplateModelImpl.getClassPK())
8010                                            };
8011    
8012                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSPK, args);
8013                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK,
8014                                            args);
8015                            }
8016    
8017                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8018                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TEMPLATEKEY.getColumnBitmask()) != 0) {
8019                                    Object[] args = new Object[] {
8020                                                    ddmTemplateModelImpl.getOriginalTemplateKey()
8021                                            };
8022    
8023                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TEMPLATEKEY,
8024                                            args);
8025                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TEMPLATEKEY,
8026                                            args);
8027    
8028                                    args = new Object[] { ddmTemplateModelImpl.getTemplateKey() };
8029    
8030                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TEMPLATEKEY,
8031                                            args);
8032                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TEMPLATEKEY,
8033                                            args);
8034                            }
8035    
8036                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8037                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE.getColumnBitmask()) != 0) {
8038                                    Object[] args = new Object[] {
8039                                                    ddmTemplateModelImpl.getOriginalType()
8040                                            };
8041    
8042                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TYPE, args);
8043                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE,
8044                                            args);
8045    
8046                                    args = new Object[] { ddmTemplateModelImpl.getType() };
8047    
8048                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TYPE, args);
8049                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE,
8050                                            args);
8051                            }
8052    
8053                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8054                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LANGUAGE.getColumnBitmask()) != 0) {
8055                                    Object[] args = new Object[] {
8056                                                    ddmTemplateModelImpl.getOriginalLanguage()
8057                                            };
8058    
8059                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_LANGUAGE, args);
8060                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LANGUAGE,
8061                                            args);
8062    
8063                                    args = new Object[] { ddmTemplateModelImpl.getLanguage() };
8064    
8065                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_LANGUAGE, args);
8066                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LANGUAGE,
8067                                            args);
8068                            }
8069    
8070                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8071                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C.getColumnBitmask()) != 0) {
8072                                    Object[] args = new Object[] {
8073                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalGroupId()),
8074                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassNameId())
8075                                            };
8076    
8077                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C, args);
8078                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C,
8079                                            args);
8080    
8081                                    args = new Object[] {
8082                                                    Long.valueOf(ddmTemplateModelImpl.getGroupId()),
8083                                                    Long.valueOf(ddmTemplateModelImpl.getClassNameId())
8084                                            };
8085    
8086                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C, args);
8087                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C,
8088                                            args);
8089                            }
8090    
8091                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8092                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C.getColumnBitmask()) != 0) {
8093                                    Object[] args = new Object[] {
8094                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalGroupId()),
8095                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassNameId()),
8096                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassPK())
8097                                            };
8098    
8099                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C_C, args);
8100                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C,
8101                                            args);
8102    
8103                                    args = new Object[] {
8104                                                    Long.valueOf(ddmTemplateModelImpl.getGroupId()),
8105                                                    Long.valueOf(ddmTemplateModelImpl.getClassNameId()),
8106                                                    Long.valueOf(ddmTemplateModelImpl.getClassPK())
8107                                            };
8108    
8109                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_C_C, args);
8110                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_C_C,
8111                                            args);
8112                            }
8113    
8114                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8115                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T.getColumnBitmask()) != 0) {
8116                                    Object[] args = new Object[] {
8117                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassNameId()),
8118                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassPK()),
8119                                                    
8120                                                    ddmTemplateModelImpl.getOriginalType()
8121                                            };
8122    
8123                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
8124                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
8125                                            args);
8126    
8127                                    args = new Object[] {
8128                                                    Long.valueOf(ddmTemplateModelImpl.getClassNameId()),
8129                                                    Long.valueOf(ddmTemplateModelImpl.getClassPK()),
8130                                                    
8131                                                    ddmTemplateModelImpl.getType()
8132                                            };
8133    
8134                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
8135                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
8136                                            args);
8137                            }
8138    
8139                            if ((ddmTemplateModelImpl.getColumnBitmask() &
8140                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T_M.getColumnBitmask()) != 0) {
8141                                    Object[] args = new Object[] {
8142                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassNameId()),
8143                                                    Long.valueOf(ddmTemplateModelImpl.getOriginalClassPK()),
8144                                                    
8145                                                    ddmTemplateModelImpl.getOriginalType(),
8146                                                    
8147                                                    ddmTemplateModelImpl.getOriginalMode()
8148                                            };
8149    
8150                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_T_M, args);
8151                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T_M,
8152                                            args);
8153    
8154                                    args = new Object[] {
8155                                                    Long.valueOf(ddmTemplateModelImpl.getClassNameId()),
8156                                                    Long.valueOf(ddmTemplateModelImpl.getClassPK()),
8157                                                    
8158                                                    ddmTemplateModelImpl.getType(),
8159                                                    
8160                                                    ddmTemplateModelImpl.getMode()
8161                                            };
8162    
8163                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_T_M, args);
8164                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T_M,
8165                                            args);
8166                            }
8167                    }
8168    
8169                    EntityCacheUtil.putResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
8170                            DDMTemplateImpl.class, ddmTemplate.getPrimaryKey(), ddmTemplate);
8171    
8172                    clearUniqueFindersCache(ddmTemplate);
8173                    cacheUniqueFindersCache(ddmTemplate);
8174    
8175                    return ddmTemplate;
8176            }
8177    
8178            protected DDMTemplate toUnwrappedModel(DDMTemplate ddmTemplate) {
8179                    if (ddmTemplate instanceof DDMTemplateImpl) {
8180                            return ddmTemplate;
8181                    }
8182    
8183                    DDMTemplateImpl ddmTemplateImpl = new DDMTemplateImpl();
8184    
8185                    ddmTemplateImpl.setNew(ddmTemplate.isNew());
8186                    ddmTemplateImpl.setPrimaryKey(ddmTemplate.getPrimaryKey());
8187    
8188                    ddmTemplateImpl.setUuid(ddmTemplate.getUuid());
8189                    ddmTemplateImpl.setTemplateId(ddmTemplate.getTemplateId());
8190                    ddmTemplateImpl.setGroupId(ddmTemplate.getGroupId());
8191                    ddmTemplateImpl.setCompanyId(ddmTemplate.getCompanyId());
8192                    ddmTemplateImpl.setUserId(ddmTemplate.getUserId());
8193                    ddmTemplateImpl.setUserName(ddmTemplate.getUserName());
8194                    ddmTemplateImpl.setCreateDate(ddmTemplate.getCreateDate());
8195                    ddmTemplateImpl.setModifiedDate(ddmTemplate.getModifiedDate());
8196                    ddmTemplateImpl.setClassNameId(ddmTemplate.getClassNameId());
8197                    ddmTemplateImpl.setClassPK(ddmTemplate.getClassPK());
8198                    ddmTemplateImpl.setTemplateKey(ddmTemplate.getTemplateKey());
8199                    ddmTemplateImpl.setName(ddmTemplate.getName());
8200                    ddmTemplateImpl.setDescription(ddmTemplate.getDescription());
8201                    ddmTemplateImpl.setType(ddmTemplate.getType());
8202                    ddmTemplateImpl.setMode(ddmTemplate.getMode());
8203                    ddmTemplateImpl.setLanguage(ddmTemplate.getLanguage());
8204                    ddmTemplateImpl.setScript(ddmTemplate.getScript());
8205                    ddmTemplateImpl.setCacheable(ddmTemplate.isCacheable());
8206                    ddmTemplateImpl.setSmallImage(ddmTemplate.isSmallImage());
8207                    ddmTemplateImpl.setSmallImageId(ddmTemplate.getSmallImageId());
8208                    ddmTemplateImpl.setSmallImageURL(ddmTemplate.getSmallImageURL());
8209    
8210                    return ddmTemplateImpl;
8211            }
8212    
8213            /**
8214             * Returns the d d m template with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
8215             *
8216             * @param primaryKey the primary key of the d d m template
8217             * @return the d d m template
8218             * @throws com.liferay.portal.NoSuchModelException if a d d m template with the primary key could not be found
8219             * @throws SystemException if a system exception occurred
8220             */
8221            @Override
8222            public DDMTemplate findByPrimaryKey(Serializable primaryKey)
8223                    throws NoSuchModelException, SystemException {
8224                    return findByPrimaryKey(((Long)primaryKey).longValue());
8225            }
8226    
8227            /**
8228             * Returns the d d m template with the primary key or throws a {@link com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException} if it could not be found.
8229             *
8230             * @param templateId the primary key of the d d m template
8231             * @return the d d m template
8232             * @throws com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException if a d d m template with the primary key could not be found
8233             * @throws SystemException if a system exception occurred
8234             */
8235            public DDMTemplate findByPrimaryKey(long templateId)
8236                    throws NoSuchTemplateException, SystemException {
8237                    DDMTemplate ddmTemplate = fetchByPrimaryKey(templateId);
8238    
8239                    if (ddmTemplate == null) {
8240                            if (_log.isWarnEnabled()) {
8241                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + templateId);
8242                            }
8243    
8244                            throw new NoSuchTemplateException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
8245                                    templateId);
8246                    }
8247    
8248                    return ddmTemplate;
8249            }
8250    
8251            /**
8252             * Returns the d d m template with the primary key or returns <code>null</code> if it could not be found.
8253             *
8254             * @param primaryKey the primary key of the d d m template
8255             * @return the d d m template, or <code>null</code> if a d d m template with the primary key could not be found
8256             * @throws SystemException if a system exception occurred
8257             */
8258            @Override
8259            public DDMTemplate fetchByPrimaryKey(Serializable primaryKey)
8260                    throws SystemException {
8261                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
8262            }
8263    
8264            /**
8265             * Returns the d d m template with the primary key or returns <code>null</code> if it could not be found.
8266             *
8267             * @param templateId the primary key of the d d m template
8268             * @return the d d m template, or <code>null</code> if a d d m template with the primary key could not be found
8269             * @throws SystemException if a system exception occurred
8270             */
8271            public DDMTemplate fetchByPrimaryKey(long templateId)
8272                    throws SystemException {
8273                    DDMTemplate ddmTemplate = (DDMTemplate)EntityCacheUtil.getResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
8274                                    DDMTemplateImpl.class, templateId);
8275    
8276                    if (ddmTemplate == _nullDDMTemplate) {
8277                            return null;
8278                    }
8279    
8280                    if (ddmTemplate == null) {
8281                            Session session = null;
8282    
8283                            try {
8284                                    session = openSession();
8285    
8286                                    ddmTemplate = (DDMTemplate)session.get(DDMTemplateImpl.class,
8287                                                    Long.valueOf(templateId));
8288    
8289                                    if (ddmTemplate != null) {
8290                                            cacheResult(ddmTemplate);
8291                                    }
8292                                    else {
8293                                            EntityCacheUtil.putResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
8294                                                    DDMTemplateImpl.class, templateId, _nullDDMTemplate);
8295                                    }
8296                            }
8297                            catch (Exception e) {
8298                                    EntityCacheUtil.removeResult(DDMTemplateModelImpl.ENTITY_CACHE_ENABLED,
8299                                            DDMTemplateImpl.class, templateId);
8300    
8301                                    throw processException(e);
8302                            }
8303                            finally {
8304                                    closeSession(session);
8305                            }
8306                    }
8307    
8308                    return ddmTemplate;
8309            }
8310    
8311            /**
8312             * Returns all the d d m templates.
8313             *
8314             * @return the d d m templates
8315             * @throws SystemException if a system exception occurred
8316             */
8317            public List<DDMTemplate> findAll() throws SystemException {
8318                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
8319            }
8320    
8321            /**
8322             * Returns a range of all the d d m templates.
8323             *
8324             * <p>
8325             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
8326             * </p>
8327             *
8328             * @param start the lower bound of the range of d d m templates
8329             * @param end the upper bound of the range of d d m templates (not inclusive)
8330             * @return the range of d d m templates
8331             * @throws SystemException if a system exception occurred
8332             */
8333            public List<DDMTemplate> findAll(int start, int end)
8334                    throws SystemException {
8335                    return findAll(start, end, null);
8336            }
8337    
8338            /**
8339             * Returns an ordered range of all the d d m templates.
8340             *
8341             * <p>
8342             * 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.dynamicdatamapping.model.impl.DDMTemplateModelImpl}. 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.
8343             * </p>
8344             *
8345             * @param start the lower bound of the range of d d m templates
8346             * @param end the upper bound of the range of d d m templates (not inclusive)
8347             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
8348             * @return the ordered range of d d m templates
8349             * @throws SystemException if a system exception occurred
8350             */
8351            public List<DDMTemplate> findAll(int start, int end,
8352                    OrderByComparator orderByComparator) throws SystemException {
8353                    boolean pagination = true;
8354                    FinderPath finderPath = null;
8355                    Object[] finderArgs = null;
8356    
8357                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
8358                                    (orderByComparator == null)) {
8359                            pagination = false;
8360                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
8361                            finderArgs = FINDER_ARGS_EMPTY;
8362                    }
8363                    else {
8364                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
8365                            finderArgs = new Object[] { start, end, orderByComparator };
8366                    }
8367    
8368                    List<DDMTemplate> list = (List<DDMTemplate>)FinderCacheUtil.getResult(finderPath,
8369                                    finderArgs, this);
8370    
8371                    if (list == null) {
8372                            StringBundler query = null;
8373                            String sql = null;
8374    
8375                            if (orderByComparator != null) {
8376                                    query = new StringBundler(2 +
8377                                                    (orderByComparator.getOrderByFields().length * 3));
8378    
8379                                    query.append(_SQL_SELECT_DDMTEMPLATE);
8380    
8381                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
8382                                            orderByComparator);
8383    
8384                                    sql = query.toString();
8385                            }
8386                            else {
8387                                    sql = _SQL_SELECT_DDMTEMPLATE;
8388    
8389                                    if (pagination) {
8390                                            sql = sql.concat(DDMTemplateModelImpl.ORDER_BY_JPQL);
8391                                    }
8392                            }
8393    
8394                            Session session = null;
8395    
8396                            try {
8397                                    session = openSession();
8398    
8399                                    Query q = session.createQuery(sql);
8400    
8401                                    if (!pagination) {
8402                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
8403                                                            start, end, false);
8404    
8405                                            Collections.sort(list);
8406    
8407                                            list = new UnmodifiableList<DDMTemplate>(list);
8408                                    }
8409                                    else {
8410                                            list = (List<DDMTemplate>)QueryUtil.list(q, getDialect(),
8411                                                            start, end);
8412                                    }
8413    
8414                                    cacheResult(list);
8415    
8416                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
8417                            }
8418                            catch (Exception e) {
8419                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
8420    
8421                                    throw processException(e);
8422                            }
8423                            finally {
8424                                    closeSession(session);
8425                            }
8426                    }
8427    
8428                    return list;
8429            }
8430    
8431            /**
8432             * Removes all the d d m templates from the database.
8433             *
8434             * @throws SystemException if a system exception occurred
8435             */
8436            public void removeAll() throws SystemException {
8437                    for (DDMTemplate ddmTemplate : findAll()) {
8438                            remove(ddmTemplate);
8439                    }
8440            }
8441    
8442            /**
8443             * Returns the number of d d m templates.
8444             *
8445             * @return the number of d d m templates
8446             * @throws SystemException if a system exception occurred
8447             */
8448            public int countAll() throws SystemException {
8449                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
8450                                    FINDER_ARGS_EMPTY, this);
8451    
8452                    if (count == null) {
8453                            Session session = null;
8454    
8455                            try {
8456                                    session = openSession();
8457    
8458                                    Query q = session.createQuery(_SQL_COUNT_DDMTEMPLATE);
8459    
8460                                    count = (Long)q.uniqueResult();
8461    
8462                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
8463                                            FINDER_ARGS_EMPTY, count);
8464                            }
8465                            catch (Exception e) {
8466                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
8467                                            FINDER_ARGS_EMPTY);
8468    
8469                                    throw processException(e);
8470                            }
8471                            finally {
8472                                    closeSession(session);
8473                            }
8474                    }
8475    
8476                    return count.intValue();
8477            }
8478    
8479            /**
8480             * Initializes the d d m template persistence.
8481             */
8482            public void afterPropertiesSet() {
8483                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
8484                                            com.liferay.portal.util.PropsUtil.get(
8485                                                    "value.object.listener.com.liferay.portlet.dynamicdatamapping.model.DDMTemplate")));
8486    
8487                    if (listenerClassNames.length > 0) {
8488                            try {
8489                                    List<ModelListener<DDMTemplate>> listenersList = new ArrayList<ModelListener<DDMTemplate>>();
8490    
8491                                    for (String listenerClassName : listenerClassNames) {
8492                                            listenersList.add((ModelListener<DDMTemplate>)InstanceFactory.newInstance(
8493                                                            listenerClassName));
8494                                    }
8495    
8496                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
8497                            }
8498                            catch (Exception e) {
8499                                    _log.error(e);
8500                            }
8501                    }
8502            }
8503    
8504            public void destroy() {
8505                    EntityCacheUtil.removeCache(DDMTemplateImpl.class.getName());
8506                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
8507                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
8508                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
8509            }
8510    
8511            private static final String _SQL_SELECT_DDMTEMPLATE = "SELECT ddmTemplate FROM DDMTemplate ddmTemplate";
8512            private static final String _SQL_SELECT_DDMTEMPLATE_WHERE = "SELECT ddmTemplate FROM DDMTemplate ddmTemplate WHERE ";
8513            private static final String _SQL_COUNT_DDMTEMPLATE = "SELECT COUNT(ddmTemplate) FROM DDMTemplate ddmTemplate";
8514            private static final String _SQL_COUNT_DDMTEMPLATE_WHERE = "SELECT COUNT(ddmTemplate) FROM DDMTemplate ddmTemplate WHERE ";
8515            private static final String _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN = "ddmTemplate.templateId";
8516            private static final String _FILTER_SQL_SELECT_DDMTEMPLATE_WHERE = "SELECT DISTINCT {ddmTemplate.*} FROM DDMTemplate ddmTemplate WHERE ";
8517            private static final String _FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_1 =
8518                    "SELECT {DDMTemplate.*} FROM (SELECT DISTINCT ddmTemplate.templateId FROM DDMTemplate ddmTemplate WHERE ";
8519            private static final String _FILTER_SQL_SELECT_DDMTEMPLATE_NO_INLINE_DISTINCT_WHERE_2 =
8520                    ") TEMP_TABLE INNER JOIN DDMTemplate ON TEMP_TABLE.templateId = DDMTemplate.templateId";
8521            private static final String _FILTER_SQL_COUNT_DDMTEMPLATE_WHERE = "SELECT COUNT(DISTINCT ddmTemplate.templateId) AS COUNT_VALUE FROM DDMTemplate ddmTemplate WHERE ";
8522            private static final String _FILTER_ENTITY_ALIAS = "ddmTemplate";
8523            private static final String _FILTER_ENTITY_TABLE = "DDMTemplate";
8524            private static final String _ORDER_BY_ENTITY_ALIAS = "ddmTemplate.";
8525            private static final String _ORDER_BY_ENTITY_TABLE = "DDMTemplate.";
8526            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DDMTemplate exists with the primary key ";
8527            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DDMTemplate exists with the key {";
8528            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
8529            private static Log _log = LogFactoryUtil.getLog(DDMTemplatePersistenceImpl.class);
8530            private static DDMTemplate _nullDDMTemplate = new DDMTemplateImpl() {
8531                            @Override
8532                            public Object clone() {
8533                                    return this;
8534                            }
8535    
8536                            @Override
8537                            public CacheModel<DDMTemplate> toCacheModel() {
8538                                    return _nullDDMTemplateCacheModel;
8539                            }
8540                    };
8541    
8542            private static CacheModel<DDMTemplate> _nullDDMTemplateCacheModel = new CacheModel<DDMTemplate>() {
8543                            public DDMTemplate toEntityModel() {
8544                                    return _nullDDMTemplate;
8545                            }
8546                    };
8547    }