1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.documentlibrary.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, InitializingBean {
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             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         if (_listeners.length > 0) {
175             for (ModelListener listener : _listeners) {
176                 if (isNew) {
177                     listener.onBeforeCreate(dlFileRank);
178                 }
179                 else {
180                     listener.onBeforeUpdate(dlFileRank);
181                 }
182             }
183         }
184 
185         dlFileRank = updateImpl(dlFileRank, merge);
186 
187         if (_listeners.length > 0) {
188             for (ModelListener listener : _listeners) {
189                 if (isNew) {
190                     listener.onAfterCreate(dlFileRank);
191                 }
192                 else {
193                     listener.onAfterUpdate(dlFileRank);
194                 }
195             }
196         }
197 
198         return dlFileRank;
199     }
200 
201     public DLFileRank updateImpl(
202         com.liferay.portlet.documentlibrary.model.DLFileRank dlFileRank,
203         boolean merge) throws SystemException {
204         Session session = null;
205 
206         try {
207             session = openSession();
208 
209             if (merge) {
210                 session.merge(dlFileRank);
211             }
212             else {
213                 if (dlFileRank.isNew()) {
214                     session.save(dlFileRank);
215                 }
216             }
217 
218             session.flush();
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 = (List<DLFileRank>)QueryUtil.list(q,
1028                        getDialect(), start, end);
1029
1030                if (obc == null) {
1031                    Collections.sort(list);
1032                }
1033
1034                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1035                    finderClassName, finderMethodName, finderParams,
1036                    finderArgs, list);
1037
1038                return list;
1039            }
1040            catch (Exception e) {
1041                throw processException(e);
1042            }
1043            finally {
1044                closeSession(session);
1045            }
1046        }
1047        else {
1048            return (List<DLFileRank>)result;
1049        }
1050    }
1051
1052    public void removeByUserId(long userId) throws SystemException {
1053        for (DLFileRank dlFileRank : findByUserId(userId)) {
1054            remove(dlFileRank);
1055        }
1056    }
1057
1058    public void removeByF_N(long folderId, String name)
1059        throws SystemException {
1060        for (DLFileRank dlFileRank : findByF_N(folderId, name)) {
1061            remove(dlFileRank);
1062        }
1063    }
1064
1065    public void removeByC_U_F_N(long companyId, long userId, long folderId,
1066        String name) throws NoSuchFileRankException, SystemException {
1067        DLFileRank dlFileRank = findByC_U_F_N(companyId, userId, folderId, name);
1068
1069        remove(dlFileRank);
1070    }
1071
1072    public void removeAll() throws SystemException {
1073        for (DLFileRank dlFileRank : findAll()) {
1074            remove(dlFileRank);
1075        }
1076    }
1077
1078    public int countByUserId(long userId) throws SystemException {
1079        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1080        String finderClassName = DLFileRank.class.getName();
1081        String finderMethodName = "countByUserId";
1082        String[] finderParams = new String[] { Long.class.getName() };
1083        Object[] finderArgs = new Object[] { new Long(userId) };
1084
1085        Object result = null;
1086
1087        if (finderClassNameCacheEnabled) {
1088            result = FinderCacheUtil.getResult(finderClassName,
1089                    finderMethodName, finderParams, finderArgs, this);
1090        }
1091
1092        if (result == null) {
1093            Session session = null;
1094
1095            try {
1096                session = openSession();
1097
1098                StringBuilder query = new StringBuilder();
1099
1100                query.append("SELECT COUNT(*) ");
1101                query.append(
1102                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1103
1104                query.append("userId = ?");
1105
1106                query.append(" ");
1107
1108                Query q = session.createQuery(query.toString());
1109
1110                QueryPos qPos = QueryPos.getInstance(q);
1111
1112                qPos.add(userId);
1113
1114                Long count = null;
1115
1116                Iterator<Long> itr = q.list().iterator();
1117
1118                if (itr.hasNext()) {
1119                    count = itr.next();
1120                }
1121
1122                if (count == null) {
1123                    count = new Long(0);
1124                }
1125
1126                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1127                    finderClassName, finderMethodName, finderParams,
1128                    finderArgs, count);
1129
1130                return count.intValue();
1131            }
1132            catch (Exception e) {
1133                throw processException(e);
1134            }
1135            finally {
1136                closeSession(session);
1137            }
1138        }
1139        else {
1140            return ((Long)result).intValue();
1141        }
1142    }
1143
1144    public int countByF_N(long folderId, String name) throws SystemException {
1145        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1146        String finderClassName = DLFileRank.class.getName();
1147        String finderMethodName = "countByF_N";
1148        String[] finderParams = new String[] {
1149                Long.class.getName(), String.class.getName()
1150            };
1151        Object[] finderArgs = new Object[] { new Long(folderId), name };
1152
1153        Object result = null;
1154
1155        if (finderClassNameCacheEnabled) {
1156            result = FinderCacheUtil.getResult(finderClassName,
1157                    finderMethodName, finderParams, finderArgs, this);
1158        }
1159
1160        if (result == null) {
1161            Session session = null;
1162
1163            try {
1164                session = openSession();
1165
1166                StringBuilder query = new StringBuilder();
1167
1168                query.append("SELECT COUNT(*) ");
1169                query.append(
1170                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1171
1172                query.append("folderId = ?");
1173
1174                query.append(" AND ");
1175
1176                if (name == null) {
1177                    query.append("name IS NULL");
1178                }
1179                else {
1180                    query.append("name = ?");
1181                }
1182
1183                query.append(" ");
1184
1185                Query q = session.createQuery(query.toString());
1186
1187                QueryPos qPos = QueryPos.getInstance(q);
1188
1189                qPos.add(folderId);
1190
1191                if (name != null) {
1192                    qPos.add(name);
1193                }
1194
1195                Long count = null;
1196
1197                Iterator<Long> itr = q.list().iterator();
1198
1199                if (itr.hasNext()) {
1200                    count = itr.next();
1201                }
1202
1203                if (count == null) {
1204                    count = new Long(0);
1205                }
1206
1207                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1208                    finderClassName, finderMethodName, finderParams,
1209                    finderArgs, count);
1210
1211                return count.intValue();
1212            }
1213            catch (Exception e) {
1214                throw processException(e);
1215            }
1216            finally {
1217                closeSession(session);
1218            }
1219        }
1220        else {
1221            return ((Long)result).intValue();
1222        }
1223    }
1224
1225    public int countByC_U_F_N(long companyId, long userId, long folderId,
1226        String name) throws SystemException {
1227        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1228        String finderClassName = DLFileRank.class.getName();
1229        String finderMethodName = "countByC_U_F_N";
1230        String[] finderParams = new String[] {
1231                Long.class.getName(), Long.class.getName(), Long.class.getName(),
1232                String.class.getName()
1233            };
1234        Object[] finderArgs = new Object[] {
1235                new Long(companyId), new Long(userId), new Long(folderId),
1236                
1237                name
1238            };
1239
1240        Object result = null;
1241
1242        if (finderClassNameCacheEnabled) {
1243            result = FinderCacheUtil.getResult(finderClassName,
1244                    finderMethodName, finderParams, finderArgs, this);
1245        }
1246
1247        if (result == null) {
1248            Session session = null;
1249
1250            try {
1251                session = openSession();
1252
1253                StringBuilder query = new StringBuilder();
1254
1255                query.append("SELECT COUNT(*) ");
1256                query.append(
1257                    "FROM com.liferay.portlet.documentlibrary.model.DLFileRank WHERE ");
1258
1259                query.append("companyId = ?");
1260
1261                query.append(" AND ");
1262
1263                query.append("userId = ?");
1264
1265                query.append(" AND ");
1266
1267                query.append("folderId = ?");
1268
1269                query.append(" AND ");
1270
1271                if (name == null) {
1272                    query.append("name IS NULL");
1273                }
1274                else {
1275                    query.append("name = ?");
1276                }
1277
1278                query.append(" ");
1279
1280                Query q = session.createQuery(query.toString());
1281
1282                QueryPos qPos = QueryPos.getInstance(q);
1283
1284                qPos.add(companyId);
1285
1286                qPos.add(userId);
1287
1288                qPos.add(folderId);
1289
1290                if (name != null) {
1291                    qPos.add(name);
1292                }
1293
1294                Long count = null;
1295
1296                Iterator<Long> itr = q.list().iterator();
1297
1298                if (itr.hasNext()) {
1299                    count = itr.next();
1300                }
1301
1302                if (count == null) {
1303                    count = new Long(0);
1304                }
1305
1306                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1307                    finderClassName, finderMethodName, finderParams,
1308                    finderArgs, count);
1309
1310                return count.intValue();
1311            }
1312            catch (Exception e) {
1313                throw processException(e);
1314            }
1315            finally {
1316                closeSession(session);
1317            }
1318        }
1319        else {
1320            return ((Long)result).intValue();
1321        }
1322    }
1323
1324    public int countAll() throws SystemException {
1325        boolean finderClassNameCacheEnabled = DLFileRankModelImpl.CACHE_ENABLED;
1326        String finderClassName = DLFileRank.class.getName();
1327        String finderMethodName = "countAll";
1328        String[] finderParams = new String[] {  };
1329        Object[] finderArgs = new Object[] {  };
1330
1331        Object result = null;
1332
1333        if (finderClassNameCacheEnabled) {
1334            result = FinderCacheUtil.getResult(finderClassName,
1335                    finderMethodName, finderParams, finderArgs, this);
1336        }
1337
1338        if (result == null) {
1339            Session session = null;
1340
1341            try {
1342                session = openSession();
1343
1344                Query q = session.createQuery(
1345                        "SELECT COUNT(*) FROM com.liferay.portlet.documentlibrary.model.DLFileRank");
1346
1347                Long count = null;
1348
1349                Iterator<Long> itr = q.list().iterator();
1350
1351                if (itr.hasNext()) {
1352                    count = itr.next();
1353                }
1354
1355                if (count == null) {
1356                    count = new Long(0);
1357                }
1358
1359                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1360                    finderClassName, finderMethodName, finderParams,
1361                    finderArgs, count);
1362
1363                return count.intValue();
1364            }
1365            catch (Exception e) {
1366                throw processException(e);
1367            }
1368            finally {
1369                closeSession(session);
1370            }
1371        }
1372        else {
1373            return ((Long)result).intValue();
1374        }
1375    }
1376
1377    public void registerListener(ModelListener listener) {
1378        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1379
1380        listeners.add(listener);
1381
1382        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1383    }
1384
1385    public void unregisterListener(ModelListener listener) {
1386        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1387
1388        listeners.remove(listener);
1389
1390        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1391    }
1392
1393    public void afterPropertiesSet() {
1394        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1395                    com.liferay.portal.util.PropsUtil.get(
1396                        "value.object.listener.com.liferay.portlet.documentlibrary.model.DLFileRank")));
1397
1398        if (listenerClassNames.length > 0) {
1399            try {
1400                List<ModelListener> listeners = new ArrayList<ModelListener>();
1401
1402                for (String listenerClassName : listenerClassNames) {
1403                    listeners.add((ModelListener)Class.forName(
1404                            listenerClassName).newInstance());
1405                }
1406
1407                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1408            }
1409            catch (Exception e) {
1410                _log.error(e);
1411            }
1412        }
1413    }
1414
1415    private static Log _log = LogFactory.getLog(DLFileRankPersistenceImpl.class);
1416    private ModelListener[] _listeners = new ModelListener[0];
1417}