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