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