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                     "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
415 
416                 query.append("folderId = ?");
417 
418                 query.append(" AND ");
419 
420                 if (name == null) {
421                     query.append("name IS NULL");
422                 }
423                 else {
424                     query.append("name = ?");
425                 }
426 
427                 query.append(" ");
428 
429                 query.append("ORDER BY ");
430 
431                 query.append("folderId DESC, ");
432                 query.append("name DESC, ");
433                 query.append("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                     "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
495 
496                 query.append("folderId = ?");
497 
498                 query.append(" AND ");
499 
500                 if (name == null) {
501                     query.append("name IS NULL");
502                 }
503                 else {
504                     query.append("name = ?");
505                 }
506 
507                 query.append(" ");
508 
509                 if (obc != null) {
510                     query.append("ORDER BY ");
511                     query.append(obc.getOrderBy());
512                 }
513 
514                 else {
515                     query.append("ORDER BY ");
516 
517                     query.append("folderId DESC, ");
518                     query.append("name DESC, ");
519                     query.append("version DESC");
520                 }
521 
522                 Query q = session.createQuery(query.toString());
523 
524                 QueryPos qPos = QueryPos.getInstance(q);
525 
526                 qPos.add(folderId);
527 
528                 if (name != null) {
529                     qPos.add(name);
530                 }
531 
532                 list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
533                         start, end);
534             }
535             catch (Exception e) {
536                 throw processException(e);
537             }
538             finally {
539                 if (list == null) {
540                     list = new ArrayList<DLFileVersion>();
541                 }
542 
543                 cacheResult(list);
544 
545                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_F_N,
546                     finderArgs, list);
547 
548                 closeSession(session);
549             }
550         }
551 
552         return list;
553     }
554 
555     public DLFileVersion findByF_N_First(long folderId, String name,
556         OrderByComparator obc)
557         throws NoSuchFileVersionException, SystemException {
558         List<DLFileVersion> list = findByF_N(folderId, name, 0, 1, obc);
559 
560         if (list.isEmpty()) {
561             StringBuilder msg = new StringBuilder();
562 
563             msg.append("No DLFileVersion exists with the key {");
564 
565             msg.append("folderId=" + folderId);
566 
567             msg.append(", ");
568             msg.append("name=" + name);
569 
570             msg.append(StringPool.CLOSE_CURLY_BRACE);
571 
572             throw new NoSuchFileVersionException(msg.toString());
573         }
574         else {
575             return list.get(0);
576         }
577     }
578 
579     public DLFileVersion findByF_N_Last(long folderId, String name,
580         OrderByComparator obc)
581         throws NoSuchFileVersionException, SystemException {
582         int count = countByF_N(folderId, name);
583 
584         List<DLFileVersion> list = findByF_N(folderId, name, count - 1, count,
585                 obc);
586 
587         if (list.isEmpty()) {
588             StringBuilder msg = new StringBuilder();
589 
590             msg.append("No DLFileVersion exists with the key {");
591 
592             msg.append("folderId=" + folderId);
593 
594             msg.append(", ");
595             msg.append("name=" + name);
596 
597             msg.append(StringPool.CLOSE_CURLY_BRACE);
598 
599             throw new NoSuchFileVersionException(msg.toString());
600         }
601         else {
602             return list.get(0);
603         }
604     }
605 
606     public DLFileVersion[] findByF_N_PrevAndNext(long fileVersionId,
607         long folderId, String name, OrderByComparator obc)
608         throws NoSuchFileVersionException, SystemException {
609         DLFileVersion dlFileVersion = findByPrimaryKey(fileVersionId);
610 
611         int count = countByF_N(folderId, name);
612 
613         Session session = null;
614 
615         try {
616             session = openSession();
617 
618             StringBuilder query = new StringBuilder();
619 
620             query.append(
621                 "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
622 
623             query.append("folderId = ?");
624 
625             query.append(" AND ");
626 
627             if (name == null) {
628                 query.append("name IS NULL");
629             }
630             else {
631                 query.append("name = ?");
632             }
633 
634             query.append(" ");
635 
636             if (obc != null) {
637                 query.append("ORDER BY ");
638                 query.append(obc.getOrderBy());
639             }
640 
641             else {
642                 query.append("ORDER BY ");
643 
644                 query.append("folderId DESC, ");
645                 query.append("name DESC, ");
646                 query.append("version DESC");
647             }
648 
649             Query q = session.createQuery(query.toString());
650 
651             QueryPos qPos = QueryPos.getInstance(q);
652 
653             qPos.add(folderId);
654 
655             if (name != null) {
656                 qPos.add(name);
657             }
658 
659             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
660                     dlFileVersion);
661 
662             DLFileVersion[] array = new DLFileVersionImpl[3];
663 
664             array[0] = (DLFileVersion)objArray[0];
665             array[1] = (DLFileVersion)objArray[1];
666             array[2] = (DLFileVersion)objArray[2];
667 
668             return array;
669         }
670         catch (Exception e) {
671             throw processException(e);
672         }
673         finally {
674             closeSession(session);
675         }
676     }
677 
678     public DLFileVersion findByF_N_V(long folderId, String name, double version)
679         throws NoSuchFileVersionException, SystemException {
680         DLFileVersion dlFileVersion = fetchByF_N_V(folderId, name, version);
681 
682         if (dlFileVersion == null) {
683             StringBuilder msg = new StringBuilder();
684 
685             msg.append("No DLFileVersion exists with the key {");
686 
687             msg.append("folderId=" + folderId);
688 
689             msg.append(", ");
690             msg.append("name=" + name);
691 
692             msg.append(", ");
693             msg.append("version=" + version);
694 
695             msg.append(StringPool.CLOSE_CURLY_BRACE);
696 
697             if (_log.isWarnEnabled()) {
698                 _log.warn(msg.toString());
699             }
700 
701             throw new NoSuchFileVersionException(msg.toString());
702         }
703 
704         return dlFileVersion;
705     }
706 
707     public DLFileVersion fetchByF_N_V(long folderId, String name, double version)
708         throws SystemException {
709         return fetchByF_N_V(folderId, name, version, true);
710     }
711 
712     public DLFileVersion fetchByF_N_V(long folderId, String name,
713         double version, boolean retrieveFromCache) throws SystemException {
714         Object[] finderArgs = new Object[] {
715                 new Long(folderId),
716                 
717                 name, new Double(version)
718             };
719 
720         Object result = null;
721 
722         if (retrieveFromCache) {
723             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_F_N_V,
724                     finderArgs, this);
725         }
726 
727         if (result == null) {
728             Session session = null;
729 
730             try {
731                 session = openSession();
732 
733                 StringBuilder query = new StringBuilder();
734 
735                 query.append(
736                     "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
737 
738                 query.append("folderId = ?");
739 
740                 query.append(" AND ");
741 
742                 if (name == null) {
743                     query.append("name IS NULL");
744                 }
745                 else {
746                     query.append("name = ?");
747                 }
748 
749                 query.append(" AND ");
750 
751                 query.append("version = ?");
752 
753                 query.append(" ");
754 
755                 query.append("ORDER BY ");
756 
757                 query.append("folderId DESC, ");
758                 query.append("name DESC, ");
759                 query.append("version DESC");
760 
761                 Query q = session.createQuery(query.toString());
762 
763                 QueryPos qPos = QueryPos.getInstance(q);
764 
765                 qPos.add(folderId);
766 
767                 if (name != null) {
768                     qPos.add(name);
769                 }
770 
771                 qPos.add(version);
772 
773                 List<DLFileVersion> list = q.list();
774 
775                 result = list;
776 
777                 DLFileVersion dlFileVersion = null;
778 
779                 if (list.isEmpty()) {
780                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
781                         finderArgs, list);
782                 }
783                 else {
784                     dlFileVersion = list.get(0);
785 
786                     cacheResult(dlFileVersion);
787 
788                     if ((dlFileVersion.getFolderId() != folderId) ||
789                             (dlFileVersion.getName() == null) ||
790                             !dlFileVersion.getName().equals(name) ||
791                             (dlFileVersion.getVersion() != version)) {
792                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
793                             finderArgs, dlFileVersion);
794                     }
795                 }
796 
797                 return dlFileVersion;
798             }
799             catch (Exception e) {
800                 throw processException(e);
801             }
802             finally {
803                 if (result == null) {
804                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_F_N_V,
805                         finderArgs, new ArrayList<DLFileVersion>());
806                 }
807 
808                 closeSession(session);
809             }
810         }
811         else {
812             if (result instanceof List) {
813                 return null;
814             }
815             else {
816                 return (DLFileVersion)result;
817             }
818         }
819     }
820 
821     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
822         throws SystemException {
823         Session session = null;
824 
825         try {
826             session = openSession();
827 
828             dynamicQuery.compile(session);
829 
830             return dynamicQuery.list();
831         }
832         catch (Exception e) {
833             throw processException(e);
834         }
835         finally {
836             closeSession(session);
837         }
838     }
839 
840     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
841         int start, int end) throws SystemException {
842         Session session = null;
843 
844         try {
845             session = openSession();
846 
847             dynamicQuery.setLimit(start, end);
848 
849             dynamicQuery.compile(session);
850 
851             return dynamicQuery.list();
852         }
853         catch (Exception e) {
854             throw processException(e);
855         }
856         finally {
857             closeSession(session);
858         }
859     }
860 
861     public List<DLFileVersion> findAll() throws SystemException {
862         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
863     }
864 
865     public List<DLFileVersion> findAll(int start, int end)
866         throws SystemException {
867         return findAll(start, end, null);
868     }
869 
870     public List<DLFileVersion> findAll(int start, int end, OrderByComparator obc)
871         throws SystemException {
872         Object[] finderArgs = new Object[] {
873                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
874             };
875 
876         List<DLFileVersion> list = (List<DLFileVersion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
877                 finderArgs, this);
878 
879         if (list == null) {
880             Session session = null;
881 
882             try {
883                 session = openSession();
884 
885                 StringBuilder query = new StringBuilder();
886 
887                 query.append(
888                     "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion ");
889 
890                 if (obc != null) {
891                     query.append("ORDER BY ");
892                     query.append(obc.getOrderBy());
893                 }
894 
895                 else {
896                     query.append("ORDER BY ");
897 
898                     query.append("folderId DESC, ");
899                     query.append("name DESC, ");
900                     query.append("version DESC");
901                 }
902 
903                 Query q = session.createQuery(query.toString());
904 
905                 if (obc == null) {
906                     list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
907                             start, end, false);
908 
909                     Collections.sort(list);
910                 }
911                 else {
912                     list = (List<DLFileVersion>)QueryUtil.list(q, getDialect(),
913                             start, end);
914                 }
915             }
916             catch (Exception e) {
917                 throw processException(e);
918             }
919             finally {
920                 if (list == null) {
921                     list = new ArrayList<DLFileVersion>();
922                 }
923 
924                 cacheResult(list);
925 
926                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
927 
928                 closeSession(session);
929             }
930         }
931 
932         return list;
933     }
934 
935     public void removeByF_N(long folderId, String name)
936         throws SystemException {
937         for (DLFileVersion dlFileVersion : findByF_N(folderId, name)) {
938             remove(dlFileVersion);
939         }
940     }
941 
942     public void removeByF_N_V(long folderId, String name, double version)
943         throws NoSuchFileVersionException, SystemException {
944         DLFileVersion dlFileVersion = findByF_N_V(folderId, name, version);
945 
946         remove(dlFileVersion);
947     }
948 
949     public void removeAll() throws SystemException {
950         for (DLFileVersion dlFileVersion : findAll()) {
951             remove(dlFileVersion);
952         }
953     }
954 
955     public int countByF_N(long folderId, String name) throws SystemException {
956         Object[] finderArgs = new Object[] { new Long(folderId), name };
957 
958         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_F_N,
959                 finderArgs, this);
960 
961         if (count == null) {
962             Session session = null;
963 
964             try {
965                 session = openSession();
966 
967                 StringBuilder query = new StringBuilder();
968 
969                 query.append("SELECT COUNT(*) ");
970                 query.append(
971                     "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
972 
973                 query.append("folderId = ?");
974 
975                 query.append(" AND ");
976 
977                 if (name == null) {
978                     query.append("name IS NULL");
979                 }
980                 else {
981                     query.append("name = ?");
982                 }
983 
984                 query.append(" ");
985 
986                 Query q = session.createQuery(query.toString());
987 
988                 QueryPos qPos = QueryPos.getInstance(q);
989 
990                 qPos.add(folderId);
991 
992                 if (name != null) {
993                     qPos.add(name);
994                 }
995 
996                 count = (Long)q.uniqueResult();
997             }
998             catch (Exception e) {
999                 throw processException(e);
1000            }
1001            finally {
1002                if (count == null) {
1003                    count = Long.valueOf(0);
1004                }
1005
1006                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_F_N, finderArgs,
1007                    count);
1008
1009                closeSession(session);
1010            }
1011        }
1012
1013        return count.intValue();
1014    }
1015
1016    public int countByF_N_V(long folderId, String name, double version)
1017        throws SystemException {
1018        Object[] finderArgs = new Object[] {
1019                new Long(folderId),
1020                
1021                name, new Double(version)
1022            };
1023
1024        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_F_N_V,
1025                finderArgs, this);
1026
1027        if (count == null) {
1028            Session session = null;
1029
1030            try {
1031                session = openSession();
1032
1033                StringBuilder query = new StringBuilder();
1034
1035                query.append("SELECT COUNT(*) ");
1036                query.append(
1037                    "FROM com.liferay.portlet.documentlibrary.model.DLFileVersion WHERE ");
1038
1039                query.append("folderId = ?");
1040
1041                query.append(" AND ");
1042
1043                if (name == null) {
1044                    query.append("name IS NULL");
1045                }
1046                else {
1047                    query.append("name = ?");
1048                }
1049
1050                query.append(" AND ");
1051
1052                query.append("version = ?");
1053
1054                query.append(" ");
1055
1056                Query q = session.createQuery(query.toString());
1057
1058                QueryPos qPos = QueryPos.getInstance(q);
1059
1060                qPos.add(folderId);
1061
1062                if (name != null) {
1063                    qPos.add(name);
1064                }
1065
1066                qPos.add(version);
1067
1068                count = (Long)q.uniqueResult();
1069            }
1070            catch (Exception e) {
1071                throw processException(e);
1072            }
1073            finally {
1074                if (count == null) {
1075                    count = Long.valueOf(0);
1076                }
1077
1078                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_F_N_V,
1079                    finderArgs, count);
1080
1081                closeSession(session);
1082            }
1083        }
1084
1085        return count.intValue();
1086    }
1087
1088    public int countAll() throws SystemException {
1089        Object[] finderArgs = new Object[0];
1090
1091        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1092                finderArgs, this);
1093
1094        if (count == null) {
1095            Session session = null;
1096
1097            try {
1098                session = openSession();
1099
1100                Query q = session.createQuery(
1101                        "SELECT COUNT(*) FROM com.liferay.portlet.documentlibrary.model.DLFileVersion");
1102
1103                count = (Long)q.uniqueResult();
1104            }
1105            catch (Exception e) {
1106                throw processException(e);
1107            }
1108            finally {
1109                if (count == null) {
1110                    count = Long.valueOf(0);
1111                }
1112
1113                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1114                    count);
1115
1116                closeSession(session);
1117            }
1118        }
1119
1120        return count.intValue();
1121    }
1122
1123    public void afterPropertiesSet() {
1124        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1125                    com.liferay.portal.util.PropsUtil.get(
1126                        "value.object.listener.com.liferay.portlet.documentlibrary.model.DLFileVersion")));
1127
1128        if (listenerClassNames.length > 0) {
1129            try {
1130                List<ModelListener<DLFileVersion>> listenersList = new ArrayList<ModelListener<DLFileVersion>>();
1131
1132                for (String listenerClassName : listenerClassNames) {
1133                    listenersList.add((ModelListener<DLFileVersion>)Class.forName(
1134                            listenerClassName).newInstance());
1135                }
1136
1137                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1138            }
1139            catch (Exception e) {
1140                _log.error(e);
1141            }
1142        }
1143    }
1144
1145    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryPersistence.impl")
1146    protected com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryPersistence dlFileEntryPersistence;
1147    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFileRankPersistence.impl")
1148    protected com.liferay.portlet.documentlibrary.service.persistence.DLFileRankPersistence dlFileRankPersistence;
1149    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutPersistence.impl")
1150    protected com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutPersistence dlFileShortcutPersistence;
1151    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFileVersionPersistence.impl")
1152    protected com.liferay.portlet.documentlibrary.service.persistence.DLFileVersionPersistence dlFileVersionPersistence;
1153    @BeanReference(name = "com.liferay.portlet.documentlibrary.service.persistence.DLFolderPersistence.impl")
1154    protected com.liferay.portlet.documentlibrary.service.persistence.DLFolderPersistence dlFolderPersistence;
1155    private static Log _log = LogFactoryUtil.getLog(DLFileVersionPersistenceImpl.class);
1156}