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