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