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