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