1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.ResourcePersistence;
40  import com.liferay.portal.service.persistence.UserPersistence;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
44  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
45  import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
46  import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionModelImpl;
47  
48  import java.io.Serializable;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="DLFileVersionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * <p>
58   * ServiceBuilder generated this class. Modifications in this class will be
59   * overwritten the next time is generated.
60   * </p>
61   *
62   * @author    Brian Wing Shun Chan
63   * @see       DLFileVersionPersistence
64   * @see       DLFileVersionUtil
65   * @generated
66   */
67  public class DLFileVersionPersistenceImpl extends BasePersistenceImpl<DLFileVersion>
68      implements DLFileVersionPersistence {
69      public static final String FINDER_CLASS_NAME_ENTITY = DLFileVersionImpl.class.getName();
70      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
71          ".List";
72      public static final FinderPath FINDER_PATH_FIND_BY_F_N = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
73              DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
74              FINDER_CLASS_NAME_LIST, "findByF_N",
75              new String[] {
76                  Long.class.getName(), String.class.getName(),
77                  
78              "java.lang.Integer", "java.lang.Integer",
79                  "com.liferay.portal.kernel.util.OrderByComparator"
80              });
81      public static final FinderPath FINDER_PATH_COUNT_BY_F_N = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
82              DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
83              FINDER_CLASS_NAME_LIST, "countByF_N",
84              new String[] { Long.class.getName(), String.class.getName() });
85      public static final FinderPath FINDER_PATH_FETCH_BY_F_N_V = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
86              DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
87              FINDER_CLASS_NAME_ENTITY, "fetchByF_N_V",
88              new String[] {
89                  Long.class.getName(), String.class.getName(),
90                  Double.class.getName()
91              });
92      public static final FinderPath FINDER_PATH_COUNT_BY_F_N_V = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
93              DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
94              FINDER_CLASS_NAME_LIST, "countByF_N_V",
95              new String[] {
96                  Long.class.getName(), String.class.getName(),
97                  Double.class.getName()
98              });
99      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
100             DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
101             FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
102     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
103             DLFileVersionModelImpl.FINDER_CACHE_ENABLED,
104             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
105 
106     public void cacheResult(DLFileVersion dlFileVersion) {
107         EntityCacheUtil.putResult(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
108             DLFileVersionImpl.class, dlFileVersion.getPrimaryKey(),
109             dlFileVersion);
110 
111         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
112             new Object[] {
113                 new Long(dlFileVersion.getFolderId()),
114                 
115             dlFileVersion.getName(), new Double(dlFileVersion.getVersion())
116             }, dlFileVersion);
117     }
118 
119     public void cacheResult(List<DLFileVersion> dlFileVersions) {
120         for (DLFileVersion dlFileVersion : dlFileVersions) {
121             if (EntityCacheUtil.getResult(
122                         DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
123                         DLFileVersionImpl.class, dlFileVersion.getPrimaryKey(),
124                         this) == null) {
125                 cacheResult(dlFileVersion);
126             }
127         }
128     }
129 
130     public void clearCache() {
131         CacheRegistry.clear(DLFileVersionImpl.class.getName());
132         EntityCacheUtil.clearCache(DLFileVersionImpl.class.getName());
133         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
135     }
136 
137     public void clearCache(DLFileVersion dlFileVersion) {
138         EntityCacheUtil.removeResult(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
139             DLFileVersionImpl.class, dlFileVersion.getPrimaryKey());
140 
141         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_F_N_V,
142             new Object[] {
143                 new Long(dlFileVersion.getFolderId()),
144                 
145             dlFileVersion.getName(), new Double(dlFileVersion.getVersion())
146             });
147     }
148 
149     public DLFileVersion create(long fileVersionId) {
150         DLFileVersion dlFileVersion = new DLFileVersionImpl();
151 
152         dlFileVersion.setNew(true);
153         dlFileVersion.setPrimaryKey(fileVersionId);
154 
155         return dlFileVersion;
156     }
157 
158     public DLFileVersion remove(Serializable primaryKey)
159         throws NoSuchModelException, SystemException {
160         return remove(((Long)primaryKey).longValue());
161     }
162 
163     public DLFileVersion remove(long fileVersionId)
164         throws NoSuchFileVersionException, SystemException {
165         Session session = null;
166 
167         try {
168             session = openSession();
169 
170             DLFileVersion dlFileVersion = (DLFileVersion)session.get(DLFileVersionImpl.class,
171                     new Long(fileVersionId));
172 
173             if (dlFileVersion == null) {
174                 if (_log.isWarnEnabled()) {
175                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + fileVersionId);
176                 }
177 
178                 throw new NoSuchFileVersionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
179                     fileVersionId);
180             }
181 
182             return remove(dlFileVersion);
183         }
184         catch (NoSuchFileVersionException nsee) {
185             throw nsee;
186         }
187         catch (Exception e) {
188             throw processException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193     }
194 
195     protected DLFileVersion removeImpl(DLFileVersion dlFileVersion)
196         throws SystemException {
197         dlFileVersion = toUnwrappedModel(dlFileVersion);
198 
199         Session session = null;
200 
201         try {
202             session = openSession();
203 
204             BatchSessionUtil.delete(session, dlFileVersion);
205         }
206         catch (Exception e) {
207             throw processException(e);
208         }
209         finally {
210             closeSession(session);
211         }
212 
213         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
214 
215         DLFileVersionModelImpl dlFileVersionModelImpl = (DLFileVersionModelImpl)dlFileVersion;
216 
217         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_F_N_V,
218             new Object[] {
219                 new Long(dlFileVersionModelImpl.getOriginalFolderId()),
220                 
221             dlFileVersionModelImpl.getOriginalName(),
222                 new Double(dlFileVersionModelImpl.getOriginalVersion())
223             });
224 
225         EntityCacheUtil.removeResult(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
226             DLFileVersionImpl.class, dlFileVersion.getPrimaryKey());
227 
228         return dlFileVersion;
229     }
230 
231     /**
232      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
233      */
234     public DLFileVersion update(DLFileVersion dlFileVersion)
235         throws SystemException {
236         if (_log.isWarnEnabled()) {
237             _log.warn(
238                 "Using the deprecated update(DLFileVersion dlFileVersion) method. Use update(DLFileVersion dlFileVersion, boolean merge) instead.");
239         }
240 
241         return update(dlFileVersion, false);
242     }
243 
244     public DLFileVersion updateImpl(
245         com.liferay.portlet.documentlibrary.model.DLFileVersion dlFileVersion,
246         boolean merge) throws SystemException {
247         dlFileVersion = toUnwrappedModel(dlFileVersion);
248 
249         boolean isNew = dlFileVersion.isNew();
250 
251         DLFileVersionModelImpl dlFileVersionModelImpl = (DLFileVersionModelImpl)dlFileVersion;
252 
253         Session session = null;
254 
255         try {
256             session = openSession();
257 
258             BatchSessionUtil.update(session, dlFileVersion, merge);
259 
260             dlFileVersion.setNew(false);
261         }
262         catch (Exception e) {
263             throw processException(e);
264         }
265         finally {
266             closeSession(session);
267         }
268 
269         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
270 
271         EntityCacheUtil.putResult(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
272             DLFileVersionImpl.class, dlFileVersion.getPrimaryKey(),
273             dlFileVersion);
274 
275         if (!isNew &&
276                 ((dlFileVersion.getFolderId() != dlFileVersionModelImpl.getOriginalFolderId()) ||
277                 !Validator.equals(dlFileVersion.getName(),
278                     dlFileVersionModelImpl.getOriginalName()) ||
279                 (dlFileVersion.getVersion() != dlFileVersionModelImpl.getOriginalVersion()))) {
280             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_F_N_V,
281                 new Object[] {
282                     new Long(dlFileVersionModelImpl.getOriginalFolderId()),
283                     
284                 dlFileVersionModelImpl.getOriginalName(),
285                     new Double(dlFileVersionModelImpl.getOriginalVersion())
286                 });
287         }
288 
289         if (isNew ||
290                 ((dlFileVersion.getFolderId() != dlFileVersionModelImpl.getOriginalFolderId()) ||
291                 !Validator.equals(dlFileVersion.getName(),
292                     dlFileVersionModelImpl.getOriginalName()) ||
293                 (dlFileVersion.getVersion() != dlFileVersionModelImpl.getOriginalVersion()))) {
294             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
295                 new Object[] {
296                     new Long(dlFileVersion.getFolderId()),
297                     
298                 dlFileVersion.getName(), new Double(dlFileVersion.getVersion())
299                 }, dlFileVersion);
300         }
301 
302         return dlFileVersion;
303     }
304 
305     protected DLFileVersion toUnwrappedModel(DLFileVersion dlFileVersion) {
306         if (dlFileVersion instanceof DLFileVersionImpl) {
307             return dlFileVersion;
308         }
309 
310         DLFileVersionImpl dlFileVersionImpl = new DLFileVersionImpl();
311 
312         dlFileVersionImpl.setNew(dlFileVersion.isNew());
313         dlFileVersionImpl.setPrimaryKey(dlFileVersion.getPrimaryKey());
314 
315         dlFileVersionImpl.setFileVersionId(dlFileVersion.getFileVersionId());
316         dlFileVersionImpl.setGroupId(dlFileVersion.getGroupId());
317         dlFileVersionImpl.setCompanyId(dlFileVersion.getCompanyId());
318         dlFileVersionImpl.setUserId(dlFileVersion.getUserId());
319         dlFileVersionImpl.setUserName(dlFileVersion.getUserName());
320         dlFileVersionImpl.setCreateDate(dlFileVersion.getCreateDate());
321         dlFileVersionImpl.setFolderId(dlFileVersion.getFolderId());
322         dlFileVersionImpl.setName(dlFileVersion.getName());
323         dlFileVersionImpl.setDescription(dlFileVersion.getDescription());
324         dlFileVersionImpl.setVersion(dlFileVersion.getVersion());
325         dlFileVersionImpl.setSize(dlFileVersion.getSize());
326 
327         return dlFileVersionImpl;
328     }
329 
330     public DLFileVersion findByPrimaryKey(Serializable primaryKey)
331         throws NoSuchModelException, SystemException {
332         return findByPrimaryKey(((Long)primaryKey).longValue());
333     }
334 
335     public DLFileVersion findByPrimaryKey(long fileVersionId)
336         throws NoSuchFileVersionException, SystemException {
337         DLFileVersion dlFileVersion = fetchByPrimaryKey(fileVersionId);
338 
339         if (dlFileVersion == null) {
340             if (_log.isWarnEnabled()) {
341                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + fileVersionId);
342             }
343 
344             throw new NoSuchFileVersionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
345                 fileVersionId);
346         }
347 
348         return dlFileVersion;
349     }
350 
351     public DLFileVersion fetchByPrimaryKey(Serializable primaryKey)
352         throws SystemException {
353         return fetchByPrimaryKey(((Long)primaryKey).longValue());
354     }
355 
356     public DLFileVersion fetchByPrimaryKey(long fileVersionId)
357         throws SystemException {
358         DLFileVersion dlFileVersion = (DLFileVersion)EntityCacheUtil.getResult(DLFileVersionModelImpl.ENTITY_CACHE_ENABLED,
359                 DLFileVersionImpl.class, fileVersionId, this);
360 
361         if (dlFileVersion == null) {
362             Session session = null;
363 
364             try {
365                 session = openSession();
366 
367                 dlFileVersion = (DLFileVersion)session.get(DLFileVersionImpl.class,
368                         new Long(fileVersionId));
369             }
370             catch (Exception e) {
371                 throw processException(e);
372             }
373             finally {
374                 if (dlFileVersion != null) {
375                     cacheResult(dlFileVersion);
376                 }
377 
378                 closeSession(session);
379             }
380         }
381 
382         return dlFileVersion;
383     }
384 
385     public List<DLFileVersion> findByF_N(long folderId, String name)
386         throws SystemException {
387         return findByF_N(folderId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
388             null);
389     }
390 
391     public List<DLFileVersion> findByF_N(long folderId, String name, int start,
392         int end) throws SystemException {
393         return findByF_N(folderId, name, start, end, null);
394     }
395 
396     public List<DLFileVersion> findByF_N(long folderId, String name, int start,
397         int end, OrderByComparator orderByComparator) throws SystemException {
398         Object[] finderArgs = new Object[] {
399                 folderId, name,
400                 
401                 String.valueOf(start), String.valueOf(end),
402                 String.valueOf(orderByComparator)
403             };
404 
405         List<DLFileVersion> list = (List<DLFileVersion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_F_N,
406                 finderArgs, this);
407 
408         if (list == null) {
409             StringBundler query = null;
410 
411             if (orderByComparator != null) {
412                 query = new StringBundler(4 +
413                         (orderByComparator.getOrderByFields().length * 3));
414             }
415             else {
416                 query = new StringBundler(4);
417             }
418 
419             query.append(_SQL_SELECT_DLFILEVERSION_WHERE);
420 
421             query.append(_FINDER_COLUMN_F_N_FOLDERID_2);
422 
423             if (name == null) {
424                 query.append(_FINDER_COLUMN_F_N_NAME_1);
425             }
426             else {
427                 if (name.equals(StringPool.BLANK)) {
428                     query.append(_FINDER_COLUMN_F_N_NAME_3);
429                 }
430                 else {
431                     query.append(_FINDER_COLUMN_F_N_NAME_2);
432                 }
433             }
434 
435             if (orderByComparator != null) {
436                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
437                     orderByComparator);
438             }
439 
440             else {
441                 query.append(DLFileVersionModelImpl.ORDER_BY_JPQL);
442             }
443 
444             String sql = query.toString();
445 
446             Session session = null;
447 
448             try {
449                 session = openSession();
450 
451                 Query q = session.createQuery(sql);
452 
453                 QueryPos qPos = QueryPos.getInstance(q);
454 
455                 qPos.add(folderId);
456 
457                 if (name != null) {
458                     qPos.add(name);
459                 }
460 
461                 list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
462                         start, end);
463             }
464             catch (Exception e) {
465                 throw processException(e);
466             }
467             finally {
468                 if (list == null) {
469                     list = new ArrayList<DLFileVersion>();
470                 }
471 
472                 cacheResult(list);
473 
474                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_F_N, finderArgs,
475                     list);
476 
477                 closeSession(session);
478             }
479         }
480 
481         return list;
482     }
483 
484     public DLFileVersion findByF_N_First(long folderId, String name,
485         OrderByComparator orderByComparator)
486         throws NoSuchFileVersionException, SystemException {
487         List<DLFileVersion> list = findByF_N(folderId, name, 0, 1,
488                 orderByComparator);
489 
490         if (list.isEmpty()) {
491             StringBundler msg = new StringBundler(6);
492 
493             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
494 
495             msg.append("folderId=");
496             msg.append(folderId);
497 
498             msg.append(", name=");
499             msg.append(name);
500 
501             msg.append(StringPool.CLOSE_CURLY_BRACE);
502 
503             throw new NoSuchFileVersionException(msg.toString());
504         }
505         else {
506             return list.get(0);
507         }
508     }
509 
510     public DLFileVersion findByF_N_Last(long folderId, String name,
511         OrderByComparator orderByComparator)
512         throws NoSuchFileVersionException, SystemException {
513         int count = countByF_N(folderId, name);
514 
515         List<DLFileVersion> list = findByF_N(folderId, name, count - 1, count,
516                 orderByComparator);
517 
518         if (list.isEmpty()) {
519             StringBundler msg = new StringBundler(6);
520 
521             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
522 
523             msg.append("folderId=");
524             msg.append(folderId);
525 
526             msg.append(", name=");
527             msg.append(name);
528 
529             msg.append(StringPool.CLOSE_CURLY_BRACE);
530 
531             throw new NoSuchFileVersionException(msg.toString());
532         }
533         else {
534             return list.get(0);
535         }
536     }
537 
538     public DLFileVersion[] findByF_N_PrevAndNext(long fileVersionId,
539         long folderId, String name, OrderByComparator orderByComparator)
540         throws NoSuchFileVersionException, SystemException {
541         DLFileVersion dlFileVersion = findByPrimaryKey(fileVersionId);
542 
543         Session session = null;
544 
545         try {
546             session = openSession();
547 
548             DLFileVersion[] array = new DLFileVersionImpl[3];
549 
550             array[0] = getByF_N_PrevAndNext(session, dlFileVersion, folderId,
551                     name, orderByComparator, true);
552 
553             array[1] = dlFileVersion;
554 
555             array[2] = getByF_N_PrevAndNext(session, dlFileVersion, folderId,
556                     name, orderByComparator, false);
557 
558             return array;
559         }
560         catch (Exception e) {
561             throw processException(e);
562         }
563         finally {
564             closeSession(session);
565         }
566     }
567 
568     protected DLFileVersion getByF_N_PrevAndNext(Session session,
569         DLFileVersion dlFileVersion, long folderId, String name,
570         OrderByComparator orderByComparator, boolean previous) {
571         StringBundler query = null;
572 
573         if (orderByComparator != null) {
574             query = new StringBundler(6 +
575                     (orderByComparator.getOrderByFields().length * 6));
576         }
577         else {
578             query = new StringBundler(3);
579         }
580 
581         query.append(_SQL_SELECT_DLFILEVERSION_WHERE);
582 
583         query.append(_FINDER_COLUMN_F_N_FOLDERID_2);
584 
585         if (name == null) {
586             query.append(_FINDER_COLUMN_F_N_NAME_1);
587         }
588         else {
589             if (name.equals(StringPool.BLANK)) {
590                 query.append(_FINDER_COLUMN_F_N_NAME_3);
591             }
592             else {
593                 query.append(_FINDER_COLUMN_F_N_NAME_2);
594             }
595         }
596 
597         if (orderByComparator != null) {
598             String[] orderByFields = orderByComparator.getOrderByFields();
599 
600             if (orderByFields.length > 0) {
601                 query.append(WHERE_AND);
602             }
603 
604             for (int i = 0; i < orderByFields.length; i++) {
605                 query.append(_ORDER_BY_ENTITY_ALIAS);
606                 query.append(orderByFields[i]);
607 
608                 if ((i + 1) < orderByFields.length) {
609                     if (orderByComparator.isAscending() ^ previous) {
610                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
611                     }
612                     else {
613                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
614                     }
615                 }
616                 else {
617                     if (orderByComparator.isAscending() ^ previous) {
618                         query.append(WHERE_GREATER_THAN);
619                     }
620                     else {
621                         query.append(WHERE_LESSER_THAN);
622                     }
623                 }
624             }
625 
626             query.append(ORDER_BY_CLAUSE);
627 
628             for (int i = 0; i < orderByFields.length; i++) {
629                 query.append(_ORDER_BY_ENTITY_ALIAS);
630                 query.append(orderByFields[i]);
631 
632                 if ((i + 1) < orderByFields.length) {
633                     if (orderByComparator.isAscending() ^ previous) {
634                         query.append(ORDER_BY_ASC_HAS_NEXT);
635                     }
636                     else {
637                         query.append(ORDER_BY_DESC_HAS_NEXT);
638                     }
639                 }
640                 else {
641                     if (orderByComparator.isAscending() ^ previous) {
642                         query.append(ORDER_BY_ASC);
643                     }
644                     else {
645                         query.append(ORDER_BY_DESC);
646                     }
647                 }
648             }
649         }
650 
651         else {
652             query.append(DLFileVersionModelImpl.ORDER_BY_JPQL);
653         }
654 
655         String sql = query.toString();
656 
657         Query q = session.createQuery(sql);
658 
659         q.setFirstResult(0);
660         q.setMaxResults(2);
661 
662         QueryPos qPos = QueryPos.getInstance(q);
663 
664         qPos.add(folderId);
665 
666         if (name != null) {
667             qPos.add(name);
668         }
669 
670         if (orderByComparator != null) {
671             Object[] values = orderByComparator.getOrderByValues(dlFileVersion);
672 
673             for (Object value : values) {
674                 qPos.add(value);
675             }
676         }
677 
678         List<DLFileVersion> list = q.list();
679 
680         if (list.size() == 2) {
681             return list.get(1);
682         }
683         else {
684             return null;
685         }
686     }
687 
688     public DLFileVersion findByF_N_V(long folderId, String name, double version)
689         throws NoSuchFileVersionException, SystemException {
690         DLFileVersion dlFileVersion = fetchByF_N_V(folderId, name, version);
691 
692         if (dlFileVersion == null) {
693             StringBundler msg = new StringBundler(8);
694 
695             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
696 
697             msg.append("folderId=");
698             msg.append(folderId);
699 
700             msg.append(", name=");
701             msg.append(name);
702 
703             msg.append(", version=");
704             msg.append(version);
705 
706             msg.append(StringPool.CLOSE_CURLY_BRACE);
707 
708             if (_log.isWarnEnabled()) {
709                 _log.warn(msg.toString());
710             }
711 
712             throw new NoSuchFileVersionException(msg.toString());
713         }
714 
715         return dlFileVersion;
716     }
717 
718     public DLFileVersion fetchByF_N_V(long folderId, String name, double version)
719         throws SystemException {
720         return fetchByF_N_V(folderId, name, version, true);
721     }
722 
723     public DLFileVersion fetchByF_N_V(long folderId, String name,
724         double version, boolean retrieveFromCache) throws SystemException {
725         Object[] finderArgs = new Object[] { folderId, name, version };
726 
727         Object result = null;
728 
729         if (retrieveFromCache) {
730             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_F_N_V,
731                     finderArgs, this);
732         }
733 
734         if (result == null) {
735             StringBundler query = new StringBundler(5);
736 
737             query.append(_SQL_SELECT_DLFILEVERSION_WHERE);
738 
739             query.append(_FINDER_COLUMN_F_N_V_FOLDERID_2);
740 
741             if (name == null) {
742                 query.append(_FINDER_COLUMN_F_N_V_NAME_1);
743             }
744             else {
745                 if (name.equals(StringPool.BLANK)) {
746                     query.append(_FINDER_COLUMN_F_N_V_NAME_3);
747                 }
748                 else {
749                     query.append(_FINDER_COLUMN_F_N_V_NAME_2);
750                 }
751             }
752 
753             query.append(_FINDER_COLUMN_F_N_V_VERSION_2);
754 
755             query.append(DLFileVersionModelImpl.ORDER_BY_JPQL);
756 
757             String sql = query.toString();
758 
759             Session session = null;
760 
761             try {
762                 session = openSession();
763 
764                 Query q = session.createQuery(sql);
765 
766                 QueryPos qPos = QueryPos.getInstance(q);
767 
768                 qPos.add(folderId);
769 
770                 if (name != null) {
771                     qPos.add(name);
772                 }
773 
774                 qPos.add(version);
775 
776                 List<DLFileVersion> list = q.list();
777 
778                 result = list;
779 
780                 DLFileVersion dlFileVersion = null;
781 
782                 if (list.isEmpty()) {
783                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
784                         finderArgs, list);
785                 }
786                 else {
787                     dlFileVersion = list.get(0);
788 
789                     cacheResult(dlFileVersion);
790 
791                     if ((dlFileVersion.getFolderId() != folderId) ||
792                             (dlFileVersion.getName() == null) ||
793                             !dlFileVersion.getName().equals(name) ||
794                             (dlFileVersion.getVersion() != version)) {
795                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
796                             finderArgs, dlFileVersion);
797                     }
798                 }
799 
800                 return dlFileVersion;
801             }
802             catch (Exception e) {
803                 throw processException(e);
804             }
805             finally {
806                 if (result == null) {
807                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
808                         finderArgs, new ArrayList<DLFileVersion>());
809                 }
810 
811                 closeSession(session);
812             }
813         }
814         else {
815             if (result instanceof List<?>) {
816                 return null;
817             }
818             else {
819                 return (DLFileVersion)result;
820             }
821         }
822     }
823 
824     public List<DLFileVersion> findAll() throws SystemException {
825         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
826     }
827 
828     public List<DLFileVersion> findAll(int start, int end)
829         throws SystemException {
830         return findAll(start, end, null);
831     }
832 
833     public List<DLFileVersion> findAll(int start, int end,
834         OrderByComparator orderByComparator) throws SystemException {
835         Object[] finderArgs = new Object[] {
836                 String.valueOf(start), String.valueOf(end),
837                 String.valueOf(orderByComparator)
838             };
839 
840         List<DLFileVersion> list = (List<DLFileVersion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
841                 finderArgs, this);
842 
843         if (list == null) {
844             StringBundler query = null;
845             String sql = null;
846 
847             if (orderByComparator != null) {
848                 query = new StringBundler(2 +
849                         (orderByComparator.getOrderByFields().length * 3));
850 
851                 query.append(_SQL_SELECT_DLFILEVERSION);
852 
853                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
854                     orderByComparator);
855 
856                 sql = query.toString();
857             }
858             else {
859                 sql = _SQL_SELECT_DLFILEVERSION.concat(DLFileVersionModelImpl.ORDER_BY_JPQL);
860             }
861 
862             Session session = null;
863 
864             try {
865                 session = openSession();
866 
867                 Query q = session.createQuery(sql);
868 
869                 if (orderByComparator == null) {
870                     list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
871                             start, end, false);
872 
873                     Collections.sort(list);
874                 }
875                 else {
876                     list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
877                             start, end);
878                 }
879             }
880             catch (Exception e) {
881                 throw processException(e);
882             }
883             finally {
884                 if (list == null) {
885                     list = new ArrayList<DLFileVersion>();
886                 }
887 
888                 cacheResult(list);
889 
890                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
891 
892                 closeSession(session);
893             }
894         }
895 
896         return list;
897     }
898 
899     public void removeByF_N(long folderId, String name)
900         throws SystemException {
901         for (DLFileVersion dlFileVersion : findByF_N(folderId, name)) {
902             remove(dlFileVersion);
903         }
904     }
905 
906     public void removeByF_N_V(long folderId, String name, double version)
907         throws NoSuchFileVersionException, SystemException {
908         DLFileVersion dlFileVersion = findByF_N_V(folderId, name, version);
909 
910         remove(dlFileVersion);
911     }
912 
913     public void removeAll() throws SystemException {
914         for (DLFileVersion dlFileVersion : findAll()) {
915             remove(dlFileVersion);
916         }
917     }
918 
919     public int countByF_N(long folderId, String name) throws SystemException {
920         Object[] finderArgs = new Object[] { folderId, name };
921 
922         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_F_N,
923                 finderArgs, this);
924 
925         if (count == null) {
926             StringBundler query = new StringBundler(3);
927 
928             query.append(_SQL_COUNT_DLFILEVERSION_WHERE);
929 
930             query.append(_FINDER_COLUMN_F_N_FOLDERID_2);
931 
932             if (name == null) {
933                 query.append(_FINDER_COLUMN_F_N_NAME_1);
934             }
935             else {
936                 if (name.equals(StringPool.BLANK)) {
937                     query.append(_FINDER_COLUMN_F_N_NAME_3);
938                 }
939                 else {
940                     query.append(_FINDER_COLUMN_F_N_NAME_2);
941                 }
942             }
943 
944             String sql = query.toString();
945 
946             Session session = null;
947 
948             try {
949                 session = openSession();
950 
951                 Query q = session.createQuery(sql);
952 
953                 QueryPos qPos = QueryPos.getInstance(q);
954 
955                 qPos.add(folderId);
956 
957                 if (name != null) {
958                     qPos.add(name);
959                 }
960 
961                 count = (Long)q.uniqueResult();
962             }
963             catch (Exception e) {
964                 throw processException(e);
965             }
966             finally {
967                 if (count == null) {
968                     count = Long.valueOf(0);
969                 }
970 
971                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_F_N, finderArgs,
972                     count);
973 
974                 closeSession(session);
975             }
976         }
977 
978         return count.intValue();
979     }
980 
981     public int countByF_N_V(long folderId, String name, double version)
982         throws SystemException {
983         Object[] finderArgs = new Object[] { folderId, name, version };
984 
985         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_F_N_V,
986                 finderArgs, this);
987 
988         if (count == null) {
989             StringBundler query = new StringBundler(4);
990 
991             query.append(_SQL_COUNT_DLFILEVERSION_WHERE);
992 
993             query.append(_FINDER_COLUMN_F_N_V_FOLDERID_2);
994 
995             if (name == null) {
996                 query.append(_FINDER_COLUMN_F_N_V_NAME_1);
997             }
998             else {
999                 if (name.equals(StringPool.BLANK)) {
1000                    query.append(_FINDER_COLUMN_F_N_V_NAME_3);
1001                }
1002                else {
1003                    query.append(_FINDER_COLUMN_F_N_V_NAME_2);
1004                }
1005            }
1006
1007            query.append(_FINDER_COLUMN_F_N_V_VERSION_2);
1008
1009            String sql = query.toString();
1010
1011            Session session = null;
1012
1013            try {
1014                session = openSession();
1015
1016                Query q = session.createQuery(sql);
1017
1018                QueryPos qPos = QueryPos.getInstance(q);
1019
1020                qPos.add(folderId);
1021
1022                if (name != null) {
1023                    qPos.add(name);
1024                }
1025
1026                qPos.add(version);
1027
1028                count = (Long)q.uniqueResult();
1029            }
1030            catch (Exception e) {
1031                throw processException(e);
1032            }
1033            finally {
1034                if (count == null) {
1035                    count = Long.valueOf(0);
1036                }
1037
1038                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_F_N_V,
1039                    finderArgs, count);
1040
1041                closeSession(session);
1042            }
1043        }
1044
1045        return count.intValue();
1046    }
1047
1048    public int countAll() throws SystemException {
1049        Object[] finderArgs = new Object[0];
1050
1051        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1052                finderArgs, this);
1053
1054        if (count == null) {
1055            Session session = null;
1056
1057            try {
1058                session = openSession();
1059
1060                Query q = session.createQuery(_SQL_COUNT_DLFILEVERSION);
1061
1062                count = (Long)q.uniqueResult();
1063            }
1064            catch (Exception e) {
1065                throw processException(e);
1066            }
1067            finally {
1068                if (count == null) {
1069                    count = Long.valueOf(0);
1070                }
1071
1072                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1073                    count);
1074
1075                closeSession(session);
1076            }
1077        }
1078
1079        return count.intValue();
1080    }
1081
1082    public void afterPropertiesSet() {
1083        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1084                    com.liferay.portal.util.PropsUtil.get(
1085                        "value.object.listener.com.liferay.portlet.documentlibrary.model.DLFileVersion")));
1086
1087        if (listenerClassNames.length > 0) {
1088            try {
1089                List<ModelListener<DLFileVersion>> listenersList = new ArrayList<ModelListener<DLFileVersion>>();
1090
1091                for (String listenerClassName : listenerClassNames) {
1092                    listenersList.add((ModelListener<DLFileVersion>)InstanceFactory.newInstance(
1093                            listenerClassName));
1094                }
1095
1096                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1097            }
1098            catch (Exception e) {
1099                _log.error(e);
1100            }
1101        }
1102    }
1103
1104    public void destroy() {
1105        EntityCacheUtil.removeCache(DLFileVersionImpl.class.getName());
1106        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1107        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1108    }
1109
1110    @BeanReference(type = DLFileEntryPersistence.class)
1111    protected DLFileEntryPersistence dlFileEntryPersistence;
1112    @BeanReference(type = DLFileRankPersistence.class)
1113    protected DLFileRankPersistence dlFileRankPersistence;
1114    @BeanReference(type = DLFileShortcutPersistence.class)
1115    protected DLFileShortcutPersistence dlFileShortcutPersistence;
1116    @BeanReference(type = DLFileVersionPersistence.class)
1117    protected DLFileVersionPersistence dlFileVersionPersistence;
1118    @BeanReference(type = DLFolderPersistence.class)
1119    protected DLFolderPersistence dlFolderPersistence;
1120    @BeanReference(type = ResourcePersistence.class)
1121    protected ResourcePersistence resourcePersistence;
1122    @BeanReference(type = UserPersistence.class)
1123    protected UserPersistence userPersistence;
1124    private static final String _SQL_SELECT_DLFILEVERSION = "SELECT dlFileVersion FROM DLFileVersion dlFileVersion";
1125    private static final String _SQL_SELECT_DLFILEVERSION_WHERE = "SELECT dlFileVersion FROM DLFileVersion dlFileVersion WHERE ";
1126    private static final String _SQL_COUNT_DLFILEVERSION = "SELECT COUNT(dlFileVersion) FROM DLFileVersion dlFileVersion";
1127    private static final String _SQL_COUNT_DLFILEVERSION_WHERE = "SELECT COUNT(dlFileVersion) FROM DLFileVersion dlFileVersion WHERE ";
1128    private static final String _FINDER_COLUMN_F_N_FOLDERID_2 = "dlFileVersion.folderId = ? AND ";
1129    private static final String _FINDER_COLUMN_F_N_NAME_1 = "dlFileVersion.name IS NULL";
1130    private static final String _FINDER_COLUMN_F_N_NAME_2 = "dlFileVersion.name = ?";
1131    private static final String _FINDER_COLUMN_F_N_NAME_3 = "(dlFileVersion.name IS NULL OR dlFileVersion.name = ?)";
1132    private static final String _FINDER_COLUMN_F_N_V_FOLDERID_2 = "dlFileVersion.folderId = ? AND ";
1133    private static final String _FINDER_COLUMN_F_N_V_NAME_1 = "dlFileVersion.name IS NULL AND ";
1134    private static final String _FINDER_COLUMN_F_N_V_NAME_2 = "dlFileVersion.name = ? AND ";
1135    private static final String _FINDER_COLUMN_F_N_V_NAME_3 = "(dlFileVersion.name IS NULL OR dlFileVersion.name = ?) AND ";
1136    private static final String _FINDER_COLUMN_F_N_V_VERSION_2 = "dlFileVersion.version = ?";
1137    private static final String _ORDER_BY_ENTITY_ALIAS = "dlFileVersion.";
1138    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DLFileVersion exists with the primary key ";
1139    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DLFileVersion exists with the key {";
1140    private static Log _log = LogFactoryUtil.getLog(DLFileVersionPersistenceImpl.class);
1141}