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