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.NoSuchFileRankException;
40  import com.liferay.portlet.documentlibrary.model.DLFileRank;
41  import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
42  import com.liferay.portlet.documentlibrary.model.impl.DLFileRankModelImpl;
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="DLFileRankPersistenceImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class DLFileRankPersistenceImpl extends BasePersistence
64      implements DLFileRankPersistence {
65      public DLFileRank create(long fileRankId) {
66          DLFileRank dlFileRank = new DLFileRankImpl();
67  
68          dlFileRank.setNew(true);
69          dlFileRank.setPrimaryKey(fileRankId);
70  
71          return dlFileRank;
72      }
73  
74      public DLFileRank remove(long fileRankId)
75          throws NoSuchFileRankException, SystemException {
76          Session session = null;
77  
78          try {
79              session = openSession();
80  
81              DLFileRank dlFileRank = (DLFileRank)session.get(DLFileRankImpl.class,
82                      new Long(fileRankId));
83  
84              if (dlFileRank == null) {
85                  if (_log.isWarnEnabled()) {
86                      _log.warn("No DLFileRank exists with the primary key " +
87                          fileRankId);
88                  }
89  
90                  throw new NoSuchFileRankException(
91                      "No DLFileRank exists with the primary key " + fileRankId);
92              }
93  
94              return remove(dlFileRank);
95          }
96          catch (NoSuchFileRankException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw HibernateUtil.processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public DLFileRank remove(DLFileRank dlFileRank) throws SystemException {
108         if (_listeners != null) {
109             for (ModelListener listener : _listeners) {
110                 listener.onBeforeRemove(dlFileRank);
111             }
112         }
113 
114         dlFileRank = removeImpl(dlFileRank);
115 
116         if (_listeners != null) {
117             for (ModelListener listener : _listeners) {
118                 listener.onAfterRemove(dlFileRank);
119             }
120         }
121 
122         return dlFileRank;
123     }
124 
125     protected DLFileRank removeImpl(DLFileRank dlFileRank)
126         throws SystemException {
127         Session session = null;
128 
129         try {
130             session = openSession();
131 
132             session.delete(dlFileRank);
133 
134             session.flush();
135 
136             return dlFileRank;
137         }
138         catch (Exception e) {
139             throw HibernateUtil.processException(e);
140         }
141         finally {
142             closeSession(session);
143 
144             FinderCache.clearCache(DLFileRank.class.getName());
145         }
146     }
147 
148     /**
149      * @deprecated Use <code>update(DLFileRank dlFileRank, boolean merge)</code>.
150      */
151     public DLFileRank update(DLFileRank dlFileRank) throws SystemException {
152         if (_log.isWarnEnabled()) {
153             _log.warn(
154                 "Using the deprecated update(DLFileRank dlFileRank) method. Use update(DLFileRank dlFileRank, boolean merge) instead.");
155         }
156 
157         return update(dlFileRank, 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        dlFileRank 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 dlFileRank 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 DLFileRank update(DLFileRank dlFileRank, boolean merge)
174         throws SystemException {
175         boolean isNew = dlFileRank.isNew();
176 
177         if (_listeners != null) {
178             for (ModelListener listener : _listeners) {
179                 if (isNew) {
180                     listener.onBeforeCreate(dlFileRank);
181                 }
182                 else {
183                     listener.onBeforeUpdate(dlFileRank);
184                 }
185             }
186         }
187 
188         dlFileRank = updateImpl(dlFileRank, merge);
189 
190         if (_listeners != null) {
191             for (ModelListener listener : _listeners) {
192                 if (isNew) {
193                     listener.onAfterCreate(dlFileRank);
194                 }
195                 else {
196                     listener.onAfterUpdate(dlFileRank);
197                 }
198             }
199         }
200 
201         return dlFileRank;
202     }
203 
204     public DLFileRank updateImpl(
205         com.liferay.portlet.documentlibrary.model.DLFileRank dlFileRank,
206         boolean merge) throws SystemException {
207         Session session = null;
208 
209         try {
210             session = openSession();
211 
212             if (merge) {
213                 session.merge(dlFileRank);
214             }
215             else {
216                 if (dlFileRank.isNew()) {
217                     session.save(dlFileRank);
218                 }
219             }
220 
221             session.flush();
222 
223             dlFileRank.setNew(false);
224 
225             return dlFileRank;
226         }
227         catch (Exception e) {
228             throw HibernateUtil.processException(e);
229         }
230         finally {
231             closeSession(session);
232 
233             FinderCache.clearCache(DLFileRank.class.getName());
234         }
235     }
236 
237     public DLFileRank findByPrimaryKey(long fileRankId)
238         throws NoSuchFileRankException, SystemException {
239         DLFileRank dlFileRank = fetchByPrimaryKey(fileRankId);
240 
241         if (dlFileRank == null) {
242             if (_log.isWarnEnabled()) {
243                 _log.warn("No DLFileRank exists with the primary key " +
244                     fileRankId);
245             }
246 
247             throw new NoSuchFileRankException(
248                 "No DLFileRank exists with the primary key " + fileRankId);
249         }
250 
251         return dlFileRank;
252     }
253 
254     public DLFileRank fetchByPrimaryKey(long fileRankId)
255         throws SystemException {
256         Session session = null;
257 
258         try {
259             session = openSession();
260 
261             return (DLFileRank)session.get(DLFileRankImpl.class,
262                 new Long(fileRankId));
263         }
264         catch (Exception e) {
265             throw HibernateUtil.processException(e);
266         }
267         finally {
268             closeSession(session);
269         }
270     }
271 
272     public List<DLFileRank> findByUserId(long userId) throws SystemException {
273         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
274         String finderClassName = DLFileRank.class.getName();
275         String finderMethodName = "findByUserId";
276         String[] finderParams = new String[] { Long.class.getName() };
277         Object[] finderArgs = new Object[] { new Long(userId) };
278 
279         Object result = null;
280 
281         if (finderClassNameCacheEnabled) {
282             result = FinderCache.getResult(finderClassName, finderMethodName,
283                     finderParams, finderArgs, getSessionFactory());
284         }
285 
286         if (result == null) {
287             Session session = null;
288 
289             try {
290                 session = openSession();
291 
292                 StringMaker query = new StringMaker();
293 
294                 query.append(
295                     "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
296 
297                 query.append("userId = ?");
298 
299                 query.append(" ");
300 
301                 query.append("ORDER BY ");
302 
303                 query.append("createDate DESC");
304 
305                 Query q = session.createQuery(query.toString());
306 
307                 int queryPos = 0;
308 
309                 q.setLong(queryPos++, userId);
310 
311                 List<DLFileRank> list = q.list();
312 
313                 FinderCache.putResult(finderClassNameCacheEnabled,
314                     finderClassName, finderMethodName, finderParams,
315                     finderArgs, list);
316 
317                 return list;
318             }
319             catch (Exception e) {
320                 throw HibernateUtil.processException(e);
321             }
322             finally {
323                 closeSession(session);
324             }
325         }
326         else {
327             return (List<DLFileRank>)result;
328         }
329     }
330 
331     public List<DLFileRank> findByUserId(long userId, int begin, int end)
332         throws SystemException {
333         return findByUserId(userId, begin, end, null);
334     }
335 
336     public List<DLFileRank> findByUserId(long userId, int begin, int end,
337         OrderByComparator obc) throws SystemException {
338         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
339         String finderClassName = DLFileRank.class.getName();
340         String finderMethodName = "findByUserId";
341         String[] finderParams = new String[] {
342                 Long.class.getName(),
343                 
344                 "java.lang.Integer", "java.lang.Integer",
345                 "com.liferay.portal.kernel.util.OrderByComparator"
346             };
347         Object[] finderArgs = new Object[] {
348                 new Long(userId),
349                 
350                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
351             };
352 
353         Object result = null;
354 
355         if (finderClassNameCacheEnabled) {
356             result = FinderCache.getResult(finderClassName, finderMethodName,
357                     finderParams, finderArgs, getSessionFactory());
358         }
359 
360         if (result == null) {
361             Session session = null;
362 
363             try {
364                 session = openSession();
365 
366                 StringMaker query = new StringMaker();
367 
368                 query.append(
369                     "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
370 
371                 query.append("userId = ?");
372 
373                 query.append(" ");
374 
375                 if (obc != null) {
376                     query.append("ORDER BY ");
377                     query.append(obc.getOrderBy());
378                 }
379 
380                 else {
381                     query.append("ORDER BY ");
382 
383                     query.append("createDate DESC");
384                 }
385 
386                 Query q = session.createQuery(query.toString());
387 
388                 int queryPos = 0;
389 
390                 q.setLong(queryPos++, userId);
391 
392                 List<DLFileRank> list = (List<DLFileRank>)QueryUtil.list(q,
393                         getDialect(), begin, end);
394 
395                 FinderCache.putResult(finderClassNameCacheEnabled,
396                     finderClassName, finderMethodName, finderParams,
397                     finderArgs, list);
398 
399                 return list;
400             }
401             catch (Exception e) {
402                 throw HibernateUtil.processException(e);
403             }
404             finally {
405                 closeSession(session);
406             }
407         }
408         else {
409             return (List<DLFileRank>)result;
410         }
411     }
412 
413     public DLFileRank findByUserId_First(long userId, OrderByComparator obc)
414         throws NoSuchFileRankException, SystemException {
415         List<DLFileRank> list = findByUserId(userId, 0, 1, obc);
416 
417         if (list.size() == 0) {
418             StringMaker msg = new StringMaker();
419 
420             msg.append("No DLFileRank exists with the key {");
421 
422             msg.append("userId=" + userId);
423 
424             msg.append(StringPool.CLOSE_CURLY_BRACE);
425 
426             throw new NoSuchFileRankException(msg.toString());
427         }
428         else {
429             return list.get(0);
430         }
431     }
432 
433     public DLFileRank findByUserId_Last(long userId, OrderByComparator obc)
434         throws NoSuchFileRankException, SystemException {
435         int count = countByUserId(userId);
436 
437         List<DLFileRank> list = findByUserId(userId, count - 1, count, obc);
438 
439         if (list.size() == 0) {
440             StringMaker msg = new StringMaker();
441 
442             msg.append("No DLFileRank exists with the key {");
443 
444             msg.append("userId=" + userId);
445 
446             msg.append(StringPool.CLOSE_CURLY_BRACE);
447 
448             throw new NoSuchFileRankException(msg.toString());
449         }
450         else {
451             return list.get(0);
452         }
453     }
454 
455     public DLFileRank[] findByUserId_PrevAndNext(long fileRankId, long userId,
456         OrderByComparator obc) throws NoSuchFileRankException, SystemException {
457         DLFileRank dlFileRank = findByPrimaryKey(fileRankId);
458 
459         int count = countByUserId(userId);
460 
461         Session session = null;
462 
463         try {
464             session = openSession();
465 
466             StringMaker query = new StringMaker();
467 
468             query.append(
469                 "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
470 
471             query.append("userId = ?");
472 
473             query.append(" ");
474 
475             if (obc != null) {
476                 query.append("ORDER BY ");
477                 query.append(obc.getOrderBy());
478             }
479 
480             else {
481                 query.append("ORDER BY ");
482 
483                 query.append("createDate DESC");
484             }
485 
486             Query q = session.createQuery(query.toString());
487 
488             int queryPos = 0;
489 
490             q.setLong(queryPos++, userId);
491 
492             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
493                     dlFileRank);
494 
495             DLFileRank[] array = new DLFileRankImpl[3];
496 
497             array[0] = (DLFileRank)objArray[0];
498             array[1] = (DLFileRank)objArray[1];
499             array[2] = (DLFileRank)objArray[2];
500 
501             return array;
502         }
503         catch (Exception e) {
504             throw HibernateUtil.processException(e);
505         }
506         finally {
507             closeSession(session);
508         }
509     }
510 
511     public List<DLFileRank> findByF_N(long folderId, String name)
512         throws SystemException {
513         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
514         String finderClassName = DLFileRank.class.getName();
515         String finderMethodName = "findByF_N";
516         String[] finderParams = new String[] {
517                 Long.class.getName(), String.class.getName()
518             };
519         Object[] finderArgs = new Object[] { new Long(folderId), name };
520 
521         Object result = null;
522 
523         if (finderClassNameCacheEnabled) {
524             result = FinderCache.getResult(finderClassName, finderMethodName,
525                     finderParams, finderArgs, getSessionFactory());
526         }
527 
528         if (result == null) {
529             Session session = null;
530 
531             try {
532                 session = openSession();
533 
534                 StringMaker query = new StringMaker();
535 
536                 query.append(
537                     "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
538 
539                 query.append("folderId = ?");
540 
541                 query.append(" AND ");
542 
543                 if (name == null) {
544                     query.append("name IS NULL");
545                 }
546                 else {
547                     query.append("name = ?");
548                 }
549 
550                 query.append(" ");
551 
552                 query.append("ORDER BY ");
553 
554                 query.append("createDate DESC");
555 
556                 Query q = session.createQuery(query.toString());
557 
558                 int queryPos = 0;
559 
560                 q.setLong(queryPos++, folderId);
561 
562                 if (name != null) {
563                     q.setString(queryPos++, name);
564                 }
565 
566                 List<DLFileRank> list = q.list();
567 
568                 FinderCache.putResult(finderClassNameCacheEnabled,
569                     finderClassName, finderMethodName, finderParams,
570                     finderArgs, list);
571 
572                 return list;
573             }
574             catch (Exception e) {
575                 throw HibernateUtil.processException(e);
576             }
577             finally {
578                 closeSession(session);
579             }
580         }
581         else {
582             return (List<DLFileRank>)result;
583         }
584     }
585 
586     public List<DLFileRank> findByF_N(long folderId, String name, int begin,
587         int end) throws SystemException {
588         return findByF_N(folderId, name, begin, end, null);
589     }
590 
591     public List<DLFileRank> findByF_N(long folderId, String name, int begin,
592         int end, OrderByComparator obc) throws SystemException {
593         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
594         String finderClassName = DLFileRank.class.getName();
595         String finderMethodName = "findByF_N";
596         String[] finderParams = new String[] {
597                 Long.class.getName(), String.class.getName(),
598                 
599                 "java.lang.Integer", "java.lang.Integer",
600                 "com.liferay.portal.kernel.util.OrderByComparator"
601             };
602         Object[] finderArgs = new Object[] {
603                 new Long(folderId),
604                 
605                 name,
606                 
607                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
608             };
609 
610         Object result = null;
611 
612         if (finderClassNameCacheEnabled) {
613             result = FinderCache.getResult(finderClassName, finderMethodName,
614                     finderParams, finderArgs, getSessionFactory());
615         }
616 
617         if (result == null) {
618             Session session = null;
619 
620             try {
621                 session = openSession();
622 
623                 StringMaker query = new StringMaker();
624 
625                 query.append(
626                     "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
627 
628                 query.append("folderId = ?");
629 
630                 query.append(" AND ");
631 
632                 if (name == null) {
633                     query.append("name IS NULL");
634                 }
635                 else {
636                     query.append("name = ?");
637                 }
638 
639                 query.append(" ");
640 
641                 if (obc != null) {
642                     query.append("ORDER BY ");
643                     query.append(obc.getOrderBy());
644                 }
645 
646                 else {
647                     query.append("ORDER BY ");
648 
649                     query.append("createDate DESC");
650                 }
651 
652                 Query q = session.createQuery(query.toString());
653 
654                 int queryPos = 0;
655 
656                 q.setLong(queryPos++, folderId);
657 
658                 if (name != null) {
659                     q.setString(queryPos++, name);
660                 }
661 
662                 List<DLFileRank> list = (List<DLFileRank>)QueryUtil.list(q,
663                         getDialect(), begin, end);
664 
665                 FinderCache.putResult(finderClassNameCacheEnabled,
666                     finderClassName, finderMethodName, finderParams,
667                     finderArgs, list);
668 
669                 return list;
670             }
671             catch (Exception e) {
672                 throw HibernateUtil.processException(e);
673             }
674             finally {
675                 closeSession(session);
676             }
677         }
678         else {
679             return (List<DLFileRank>)result;
680         }
681     }
682 
683     public DLFileRank findByF_N_First(long folderId, String name,
684         OrderByComparator obc) throws NoSuchFileRankException, SystemException {
685         List<DLFileRank> list = findByF_N(folderId, name, 0, 1, obc);
686 
687         if (list.size() == 0) {
688             StringMaker msg = new StringMaker();
689 
690             msg.append("No DLFileRank exists with the key {");
691 
692             msg.append("folderId=" + folderId);
693 
694             msg.append(", ");
695             msg.append("name=" + name);
696 
697             msg.append(StringPool.CLOSE_CURLY_BRACE);
698 
699             throw new NoSuchFileRankException(msg.toString());
700         }
701         else {
702             return list.get(0);
703         }
704     }
705 
706     public DLFileRank findByF_N_Last(long folderId, String name,
707         OrderByComparator obc) throws NoSuchFileRankException, SystemException {
708         int count = countByF_N(folderId, name);
709 
710         List<DLFileRank> list = findByF_N(folderId, name, count - 1, count, obc);
711 
712         if (list.size() == 0) {
713             StringMaker msg = new StringMaker();
714 
715             msg.append("No DLFileRank exists with the key {");
716 
717             msg.append("folderId=" + folderId);
718 
719             msg.append(", ");
720             msg.append("name=" + name);
721 
722             msg.append(StringPool.CLOSE_CURLY_BRACE);
723 
724             throw new NoSuchFileRankException(msg.toString());
725         }
726         else {
727             return list.get(0);
728         }
729     }
730 
731     public DLFileRank[] findByF_N_PrevAndNext(long fileRankId, long folderId,
732         String name, OrderByComparator obc)
733         throws NoSuchFileRankException, SystemException {
734         DLFileRank dlFileRank = findByPrimaryKey(fileRankId);
735 
736         int count = countByF_N(folderId, name);
737 
738         Session session = null;
739 
740         try {
741             session = openSession();
742 
743             StringMaker query = new StringMaker();
744 
745             query.append(
746                 "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
747 
748             query.append("folderId = ?");
749 
750             query.append(" AND ");
751 
752             if (name == null) {
753                 query.append("name IS NULL");
754             }
755             else {
756                 query.append("name = ?");
757             }
758 
759             query.append(" ");
760 
761             if (obc != null) {
762                 query.append("ORDER BY ");
763                 query.append(obc.getOrderBy());
764             }
765 
766             else {
767                 query.append("ORDER BY ");
768 
769                 query.append("createDate DESC");
770             }
771 
772             Query q = session.createQuery(query.toString());
773 
774             int queryPos = 0;
775 
776             q.setLong(queryPos++, folderId);
777 
778             if (name != null) {
779                 q.setString(queryPos++, name);
780             }
781 
782             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
783                     dlFileRank);
784 
785             DLFileRank[] array = new DLFileRankImpl[3];
786 
787             array[0] = (DLFileRank)objArray[0];
788             array[1] = (DLFileRank)objArray[1];
789             array[2] = (DLFileRank)objArray[2];
790 
791             return array;
792         }
793         catch (Exception e) {
794             throw HibernateUtil.processException(e);
795         }
796         finally {
797             closeSession(session);
798         }
799     }
800 
801     public DLFileRank findByC_U_F_N(long companyId, long userId, long folderId,
802         String name) throws NoSuchFileRankException, SystemException {
803         DLFileRank dlFileRank = fetchByC_U_F_N(companyId, userId, folderId, name);
804 
805         if (dlFileRank == null) {
806             StringMaker msg = new StringMaker();
807 
808             msg.append("No DLFileRank exists with the key {");
809 
810             msg.append("companyId=" + companyId);
811 
812             msg.append(", ");
813             msg.append("userId=" + userId);
814 
815             msg.append(", ");
816             msg.append("folderId=" + folderId);
817 
818             msg.append(", ");
819             msg.append("name=" + name);
820 
821             msg.append(StringPool.CLOSE_CURLY_BRACE);
822 
823             if (_log.isWarnEnabled()) {
824                 _log.warn(msg.toString());
825             }
826 
827             throw new NoSuchFileRankException(msg.toString());
828         }
829 
830         return dlFileRank;
831     }
832 
833     public DLFileRank fetchByC_U_F_N(long companyId, long userId,
834         long folderId, String name) throws SystemException {
835         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
836         String finderClassName = DLFileRank.class.getName();
837         String finderMethodName = "fetchByC_U_F_N";
838         String[] finderParams = new String[] {
839                 Long.class.getName(), Long.class.getName(), Long.class.getName(),
840                 String.class.getName()
841             };
842         Object[] finderArgs = new Object[] {
843                 new Long(companyId), new Long(userId), new Long(folderId),
844                 
845                 name
846             };
847 
848         Object result = null;
849 
850         if (finderClassNameCacheEnabled) {
851             result = FinderCache.getResult(finderClassName, finderMethodName,
852                     finderParams, finderArgs, getSessionFactory());
853         }
854 
855         if (result == null) {
856             Session session = null;
857 
858             try {
859                 session = openSession();
860 
861                 StringMaker query = new StringMaker();
862 
863                 query.append(
864                     "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
865 
866                 query.append("companyId = ?");
867 
868                 query.append(" AND ");
869 
870                 query.append("userId = ?");
871 
872                 query.append(" AND ");
873 
874                 query.append("folderId = ?");
875 
876                 query.append(" AND ");
877 
878                 if (name == null) {
879                     query.append("name IS NULL");
880                 }
881                 else {
882                     query.append("name = ?");
883                 }
884 
885                 query.append(" ");
886 
887                 query.append("ORDER BY ");
888 
889                 query.append("createDate DESC");
890 
891                 Query q = session.createQuery(query.toString());
892 
893                 int queryPos = 0;
894 
895                 q.setLong(queryPos++, companyId);
896 
897                 q.setLong(queryPos++, userId);
898 
899                 q.setLong(queryPos++, folderId);
900 
901                 if (name != null) {
902                     q.setString(queryPos++, name);
903                 }
904 
905                 List<DLFileRank> list = q.list();
906 
907                 FinderCache.putResult(finderClassNameCacheEnabled,
908                     finderClassName, finderMethodName, finderParams,
909                     finderArgs, list);
910 
911                 if (list.size() == 0) {
912                     return null;
913                 }
914                 else {
915                     return list.get(0);
916                 }
917             }
918             catch (Exception e) {
919                 throw HibernateUtil.processException(e);
920             }
921             finally {
922                 closeSession(session);
923             }
924         }
925         else {
926             List<DLFileRank> list = (List<DLFileRank>)result;
927 
928             if (list.size() == 0) {
929                 return null;
930             }
931             else {
932                 return list.get(0);
933             }
934         }
935     }
936 
937     public List<DLFileRank> findWithDynamicQuery(
938         DynamicQueryInitializer queryInitializer) throws SystemException {
939         Session session = null;
940 
941         try {
942             session = openSession();
943 
944             DynamicQuery query = queryInitializer.initialize(session);
945 
946             return query.list();
947         }
948         catch (Exception e) {
949             throw HibernateUtil.processException(e);
950         }
951         finally {
952             closeSession(session);
953         }
954     }
955 
956     public List<DLFileRank> findWithDynamicQuery(
957         DynamicQueryInitializer queryInitializer, int begin, int end)
958         throws SystemException {
959         Session session = null;
960 
961         try {
962             session = openSession();
963 
964             DynamicQuery query = queryInitializer.initialize(session);
965 
966             query.setLimit(begin, end);
967 
968             return query.list();
969         }
970         catch (Exception e) {
971             throw HibernateUtil.processException(e);
972         }
973         finally {
974             closeSession(session);
975         }
976     }
977 
978     public List<DLFileRank> findAll() throws SystemException {
979         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
980     }
981 
982     public List<DLFileRank> findAll(int begin, int end)
983         throws SystemException {
984         return findAll(begin, end, null);
985     }
986 
987     public List<DLFileRank> findAll(int begin, int end, OrderByComparator obc)
988         throws SystemException {
989         boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
990         String finderClassName = DLFileRank.class.getName();
991         String finderMethodName = "findAll";
992         String[] finderParams = new String[] {
993                 "java.lang.Integer", "java.lang.Integer",
994                 "com.liferay.portal.kernel.util.OrderByComparator"
995             };
996         Object[] finderArgs = new Object[] {
997                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
998             };
999 
1000        Object result = null;
1001
1002        if (finderClassNameCacheEnabled) {
1003            result = FinderCache.getResult(finderClassName, finderMethodName,
1004                    finderParams, finderArgs, getSessionFactory());
1005        }
1006
1007        if (result == null) {
1008            Session session = null;
1009
1010            try {
1011                session = openSession();
1012
1013                StringMaker query = new StringMaker();
1014
1015                query.append(
1016                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank ");
1017
1018                if (obc != null) {
1019                    query.append("ORDER BY ");
1020                    query.append(obc.getOrderBy());
1021                }
1022
1023                else {
1024                    query.append("ORDER BY ");
1025
1026                    query.append("createDate DESC");
1027                }
1028
1029                Query q = session.createQuery(query.toString());
1030
1031                List<DLFileRank> list = (List<DLFileRank>)QueryUtil.list(q,
1032                        getDialect(), begin, end);
1033
1034                if (obc == null) {
1035                    Collections.sort(list);
1036                }
1037
1038                FinderCache.putResult(finderClassNameCacheEnabled,
1039                    finderClassName, finderMethodName, finderParams,
1040                    finderArgs, list);
1041
1042                return list;
1043            }
1044            catch (Exception e) {
1045                throw HibernateUtil.processException(e);
1046            }
1047            finally {
1048                closeSession(session);
1049            }
1050        }
1051        else {
1052            return (List<DLFileRank>)result;
1053        }
1054    }
1055
1056    public void removeByUserId(long userId) throws SystemException {
1057        for (DLFileRank dlFileRank : findByUserId(userId)) {
1058            remove(dlFileRank);
1059        }
1060    }
1061
1062    public void removeByF_N(long folderId, String name)
1063        throws SystemException {
1064        for (DLFileRank dlFileRank : findByF_N(folderId, name)) {
1065            remove(dlFileRank);
1066        }
1067    }
1068
1069    public void removeByC_U_F_N(long companyId, long userId, long folderId,
1070        String name) throws NoSuchFileRankException, SystemException {
1071        DLFileRank dlFileRank = findByC_U_F_N(companyId, userId, folderId, name);
1072
1073        remove(dlFileRank);
1074    }
1075
1076    public void removeAll() throws SystemException {
1077        for (DLFileRank dlFileRank : findAll()) {
1078            remove(dlFileRank);
1079        }
1080    }
1081
1082    public int countByUserId(long userId) throws SystemException {
1083        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1084        String finderClassName = DLFileRank.class.getName();
1085        String finderMethodName = "countByUserId";
1086        String[] finderParams = new String[] { Long.class.getName() };
1087        Object[] finderArgs = new Object[] { new Long(userId) };
1088
1089        Object result = null;
1090
1091        if (finderClassNameCacheEnabled) {
1092            result = FinderCache.getResult(finderClassName, finderMethodName,
1093                    finderParams, finderArgs, getSessionFactory());
1094        }
1095
1096        if (result == null) {
1097            Session session = null;
1098
1099            try {
1100                session = openSession();
1101
1102                StringMaker query = new StringMaker();
1103
1104                query.append("SELECT COUNT(*) ");
1105                query.append(
1106                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1107
1108                query.append("userId = ?");
1109
1110                query.append(" ");
1111
1112                Query q = session.createQuery(query.toString());
1113
1114                int queryPos = 0;
1115
1116                q.setLong(queryPos++, userId);
1117
1118                Long count = null;
1119
1120                Iterator<Long> itr = q.list().iterator();
1121
1122                if (itr.hasNext()) {
1123                    count = itr.next();
1124                }
1125
1126                if (count == null) {
1127                    count = new Long(0);
1128                }
1129
1130                FinderCache.putResult(finderClassNameCacheEnabled,
1131                    finderClassName, finderMethodName, finderParams,
1132                    finderArgs, count);
1133
1134                return count.intValue();
1135            }
1136            catch (Exception e) {
1137                throw HibernateUtil.processException(e);
1138            }
1139            finally {
1140                closeSession(session);
1141            }
1142        }
1143        else {
1144            return ((Long)result).intValue();
1145        }
1146    }
1147
1148    public int countByF_N(long folderId, String name) throws SystemException {
1149        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1150        String finderClassName = DLFileRank.class.getName();
1151        String finderMethodName = "countByF_N";
1152        String[] finderParams = new String[] {
1153                Long.class.getName(), String.class.getName()
1154            };
1155        Object[] finderArgs = new Object[] { new Long(folderId), name };
1156
1157        Object result = null;
1158
1159        if (finderClassNameCacheEnabled) {
1160            result = FinderCache.getResult(finderClassName, finderMethodName,
1161                    finderParams, finderArgs, getSessionFactory());
1162        }
1163
1164        if (result == null) {
1165            Session session = null;
1166
1167            try {
1168                session = openSession();
1169
1170                StringMaker query = new StringMaker();
1171
1172                query.append("SELECT COUNT(*) ");
1173                query.append(
1174                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1175
1176                query.append("folderId = ?");
1177
1178                query.append(" AND ");
1179
1180                if (name == null) {
1181                    query.append("name IS NULL");
1182                }
1183                else {
1184                    query.append("name = ?");
1185                }
1186
1187                query.append(" ");
1188
1189                Query q = session.createQuery(query.toString());
1190
1191                int queryPos = 0;
1192
1193                q.setLong(queryPos++, folderId);
1194
1195                if (name != null) {
1196                    q.setString(queryPos++, name);
1197                }
1198
1199                Long count = null;
1200
1201                Iterator<Long> itr = q.list().iterator();
1202
1203                if (itr.hasNext()) {
1204                    count = itr.next();
1205                }
1206
1207                if (count == null) {
1208                    count = new Long(0);
1209                }
1210
1211                FinderCache.putResult(finderClassNameCacheEnabled,
1212                    finderClassName, finderMethodName, finderParams,
1213                    finderArgs, count);
1214
1215                return count.intValue();
1216            }
1217            catch (Exception e) {
1218                throw HibernateUtil.processException(e);
1219            }
1220            finally {
1221                closeSession(session);
1222            }
1223        }
1224        else {
1225            return ((Long)result).intValue();
1226        }
1227    }
1228
1229    public int countByC_U_F_N(long companyId, long userId, long folderId,
1230        String name) throws SystemException {
1231        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1232        String finderClassName = DLFileRank.class.getName();
1233        String finderMethodName = "countByC_U_F_N";
1234        String[] finderParams = new String[] {
1235                Long.class.getName(), Long.class.getName(), Long.class.getName(),
1236                String.class.getName()
1237            };
1238        Object[] finderArgs = new Object[] {
1239                new Long(companyId), new Long(userId), new Long(folderId),
1240                
1241                name
1242            };
1243
1244        Object result = null;
1245
1246        if (finderClassNameCacheEnabled) {
1247            result = FinderCache.getResult(finderClassName, finderMethodName,
1248                    finderParams, finderArgs, getSessionFactory());
1249        }
1250
1251        if (result == null) {
1252            Session session = null;
1253
1254            try {
1255                session = openSession();
1256
1257                StringMaker query = new StringMaker();
1258
1259                query.append("SELECT COUNT(*) ");
1260                query.append(
1261                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1262
1263                query.append("companyId = ?");
1264
1265                query.append(" AND ");
1266
1267                query.append("userId = ?");
1268
1269                query.append(" AND ");
1270
1271                query.append("folderId = ?");
1272
1273                query.append(" AND ");
1274
1275                if (name == null) {
1276                    query.append("name IS NULL");
1277                }
1278                else {
1279                    query.append("name = ?");
1280                }
1281
1282                query.append(" ");
1283
1284                Query q = session.createQuery(query.toString());
1285
1286                int queryPos = 0;
1287
1288                q.setLong(queryPos++, companyId);
1289
1290                q.setLong(queryPos++, userId);
1291
1292                q.setLong(queryPos++, folderId);
1293
1294                if (name != null) {
1295                    q.setString(queryPos++, name);
1296                }
1297
1298                Long count = null;
1299
1300                Iterator<Long> itr = q.list().iterator();
1301
1302                if (itr.hasNext()) {
1303                    count = itr.next();
1304                }
1305
1306                if (count == null) {
1307                    count = new Long(0);
1308                }
1309
1310                FinderCache.putResult(finderClassNameCacheEnabled,
1311                    finderClassName, finderMethodName, finderParams,
1312                    finderArgs, count);
1313
1314                return count.intValue();
1315            }
1316            catch (Exception e) {
1317                throw HibernateUtil.processException(e);
1318            }
1319            finally {
1320                closeSession(session);
1321            }
1322        }
1323        else {
1324            return ((Long)result).intValue();
1325        }
1326    }
1327
1328    public int countAll() throws SystemException {
1329        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1330        String finderClassName = DLFileRank.class.getName();
1331        String finderMethodName = "countAll";
1332        String[] finderParams = new String[] {  };
1333        Object[] finderArgs = new Object[] {  };
1334
1335        Object result = null;
1336
1337        if (finderClassNameCacheEnabled) {
1338            result = FinderCache.getResult(finderClassName, finderMethodName,
1339                    finderParams, finderArgs, getSessionFactory());
1340        }
1341
1342        if (result == null) {
1343            Session session = null;
1344
1345            try {
1346                session = openSession();
1347
1348                Query q = session.createQuery(
1349                        "SELECT COUNT(*) FROM com.liferay.portlet.documentlibrary.model.DLFileRank");
1350
1351                Long count = null;
1352
1353                Iterator<Long> itr = q.list().iterator();
1354
1355                if (itr.hasNext()) {
1356                    count = itr.next();
1357                }
1358
1359                if (count == null) {
1360                    count = new Long(0);
1361                }
1362
1363                FinderCache.putResult(finderClassNameCacheEnabled,
1364                    finderClassName, finderMethodName, finderParams,
1365                    finderArgs, count);
1366
1367                return count.intValue();
1368            }
1369            catch (Exception e) {
1370                throw HibernateUtil.processException(e);
1371            }
1372            finally {
1373                closeSession(session);
1374            }
1375        }
1376        else {
1377            return ((Long)result).intValue();
1378        }
1379    }
1380
1381    protected void initDao() {
1382        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1383                    PropsUtil.get(
1384                        "value.object.listener.com.liferay.portlet.documentlibrary.model.DLFileRank")));
1385
1386        if (listenerClassNames.length > 0) {
1387            try {
1388                List<ModelListener> listeners = new ArrayList<ModelListener>();
1389
1390                for (String listenerClassName : listenerClassNames) {
1391                    listeners.add((ModelListener)Class.forName(
1392                            listenerClassName).newInstance());
1393                }
1394
1395                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1396            }
1397            catch (Exception e) {
1398                _log.error(e);
1399            }
1400        }
1401    }
1402
1403    private static Log _log = LogFactory.getLog(DLFileRankPersistenceImpl.class);
1404    private ModelListener[] _listeners;
1405}