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