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