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