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