1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="DLFileEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class DLFileEntryPersistenceImpl extends BasePersistence
58      implements DLFileEntryPersistence {
59      public DLFileEntry create(long fileEntryId) {
60          DLFileEntry dlFileEntry = new DLFileEntryImpl();
61          dlFileEntry.setNew(true);
62          dlFileEntry.setPrimaryKey(fileEntryId);
63  
64          return dlFileEntry;
65      }
66  
67      public DLFileEntry remove(long fileEntryId)
68          throws NoSuchFileEntryException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              DLFileEntry dlFileEntry = (DLFileEntry)session.get(DLFileEntryImpl.class,
75                      new Long(fileEntryId));
76  
77              if (dlFileEntry == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No DLFileEntry exists with the primary key " +
80                          fileEntryId);
81                  }
82  
83                  throw new NoSuchFileEntryException(
84                      "No DLFileEntry exists with the primary key " +
85                      fileEntryId);
86              }
87  
88              return remove(dlFileEntry);
89          }
90          catch (NoSuchFileEntryException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public DLFileEntry remove(DLFileEntry dlFileEntry)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(dlFileEntry);
108             session.flush();
109 
110             return dlFileEntry;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(DLFileEntry.class.getName());
118         }
119     }
120 
121     public DLFileEntry update(
122         com.liferay.portlet.documentlibrary.model.DLFileEntry dlFileEntry)
123         throws SystemException {
124         return update(dlFileEntry, false);
125     }
126 
127     public DLFileEntry update(
128         com.liferay.portlet.documentlibrary.model.DLFileEntry dlFileEntry,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(dlFileEntry);
137             }
138             else {
139                 if (dlFileEntry.isNew()) {
140                     session.save(dlFileEntry);
141                 }
142             }
143 
144             session.flush();
145             dlFileEntry.setNew(false);
146 
147             return dlFileEntry;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(DLFileEntry.class.getName());
155         }
156     }
157 
158     public DLFileEntry findByPrimaryKey(long fileEntryId)
159         throws NoSuchFileEntryException, SystemException {
160         DLFileEntry dlFileEntry = fetchByPrimaryKey(fileEntryId);
161 
162         if (dlFileEntry == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No DLFileEntry exists with the primary key " +
165                     fileEntryId);
166             }
167 
168             throw new NoSuchFileEntryException(
169                 "No DLFileEntry exists with the primary key " + fileEntryId);
170         }
171 
172         return dlFileEntry;
173     }
174 
175     public DLFileEntry fetchByPrimaryKey(long fileEntryId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (DLFileEntry)session.get(DLFileEntryImpl.class,
183                 new Long(fileEntryId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByFolderId(long folderId) throws SystemException {
194         String finderClassName = DLFileEntry.class.getName();
195         String finderMethodName = "findByFolderId";
196         String[] finderParams = new String[] { Long.class.getName() };
197         Object[] finderArgs = new Object[] { new Long(folderId) };
198         Object result = FinderCache.getResult(finderClassName,
199                 finderMethodName, finderParams, finderArgs, getSessionFactory());
200 
201         if (result == null) {
202             Session session = null;
203 
204             try {
205                 session = openSession();
206 
207                 StringMaker query = new StringMaker();
208                 query.append(
209                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
210                 query.append("folderId = ?");
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("folderId ASC").append(", ");
214                 query.append("name ASC");
215 
216                 Query q = session.createQuery(query.toString());
217                 int queryPos = 0;
218                 q.setLong(queryPos++, folderId);
219 
220                 List list = q.list();
221                 FinderCache.putResult(finderClassName, finderMethodName,
222                     finderParams, finderArgs, list);
223 
224                 return list;
225             }
226             catch (Exception e) {
227                 throw HibernateUtil.processException(e);
228             }
229             finally {
230                 closeSession(session);
231             }
232         }
233         else {
234             return (List)result;
235         }
236     }
237 
238     public List findByFolderId(long folderId, int begin, int end)
239         throws SystemException {
240         return findByFolderId(folderId, begin, end, null);
241     }
242 
243     public List findByFolderId(long folderId, int begin, int end,
244         OrderByComparator obc) throws SystemException {
245         String finderClassName = DLFileEntry.class.getName();
246         String finderMethodName = "findByFolderId";
247         String[] finderParams = new String[] {
248                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
249                 "com.liferay.portal.kernel.util.OrderByComparator"
250             };
251         Object[] finderArgs = new Object[] {
252                 new Long(folderId), String.valueOf(begin), String.valueOf(end),
253                 String.valueOf(obc)
254             };
255         Object result = FinderCache.getResult(finderClassName,
256                 finderMethodName, finderParams, finderArgs, getSessionFactory());
257 
258         if (result == null) {
259             Session session = null;
260 
261             try {
262                 session = openSession();
263 
264                 StringMaker query = new StringMaker();
265                 query.append(
266                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
267                 query.append("folderId = ?");
268                 query.append(" ");
269 
270                 if (obc != null) {
271                     query.append("ORDER BY ");
272                     query.append(obc.getOrderBy());
273                 }
274                 else {
275                     query.append("ORDER BY ");
276                     query.append("folderId ASC").append(", ");
277                     query.append("name ASC");
278                 }
279 
280                 Query q = session.createQuery(query.toString());
281                 int queryPos = 0;
282                 q.setLong(queryPos++, folderId);
283 
284                 List list = QueryUtil.list(q, getDialect(), begin, end);
285                 FinderCache.putResult(finderClassName, finderMethodName,
286                     finderParams, finderArgs, list);
287 
288                 return list;
289             }
290             catch (Exception e) {
291                 throw HibernateUtil.processException(e);
292             }
293             finally {
294                 closeSession(session);
295             }
296         }
297         else {
298             return (List)result;
299         }
300     }
301 
302     public DLFileEntry findByFolderId_First(long folderId, OrderByComparator obc)
303         throws NoSuchFileEntryException, SystemException {
304         List list = findByFolderId(folderId, 0, 1, obc);
305 
306         if (list.size() == 0) {
307             StringMaker msg = new StringMaker();
308             msg.append("No DLFileEntry exists with the key ");
309             msg.append(StringPool.OPEN_CURLY_BRACE);
310             msg.append("folderId=");
311             msg.append(folderId);
312             msg.append(StringPool.CLOSE_CURLY_BRACE);
313             throw new NoSuchFileEntryException(msg.toString());
314         }
315         else {
316             return (DLFileEntry)list.get(0);
317         }
318     }
319 
320     public DLFileEntry findByFolderId_Last(long folderId, OrderByComparator obc)
321         throws NoSuchFileEntryException, SystemException {
322         int count = countByFolderId(folderId);
323         List list = findByFolderId(folderId, count - 1, count, obc);
324 
325         if (list.size() == 0) {
326             StringMaker msg = new StringMaker();
327             msg.append("No DLFileEntry exists with the key ");
328             msg.append(StringPool.OPEN_CURLY_BRACE);
329             msg.append("folderId=");
330             msg.append(folderId);
331             msg.append(StringPool.CLOSE_CURLY_BRACE);
332             throw new NoSuchFileEntryException(msg.toString());
333         }
334         else {
335             return (DLFileEntry)list.get(0);
336         }
337     }
338 
339     public DLFileEntry[] findByFolderId_PrevAndNext(long fileEntryId,
340         long folderId, OrderByComparator obc)
341         throws NoSuchFileEntryException, SystemException {
342         DLFileEntry dlFileEntry = findByPrimaryKey(fileEntryId);
343         int count = countByFolderId(folderId);
344         Session session = null;
345 
346         try {
347             session = openSession();
348 
349             StringMaker query = new StringMaker();
350             query.append(
351                 "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
352             query.append("folderId = ?");
353             query.append(" ");
354 
355             if (obc != null) {
356                 query.append("ORDER BY ");
357                 query.append(obc.getOrderBy());
358             }
359             else {
360                 query.append("ORDER BY ");
361                 query.append("folderId ASC").append(", ");
362                 query.append("name ASC");
363             }
364 
365             Query q = session.createQuery(query.toString());
366             int queryPos = 0;
367             q.setLong(queryPos++, folderId);
368 
369             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
370                     dlFileEntry);
371             DLFileEntry[] array = new DLFileEntryImpl[3];
372             array[0] = (DLFileEntry)objArray[0];
373             array[1] = (DLFileEntry)objArray[1];
374             array[2] = (DLFileEntry)objArray[2];
375 
376             return array;
377         }
378         catch (Exception e) {
379             throw HibernateUtil.processException(e);
380         }
381         finally {
382             closeSession(session);
383         }
384     }
385 
386     public DLFileEntry findByF_N(long folderId, String name)
387         throws NoSuchFileEntryException, SystemException {
388         DLFileEntry dlFileEntry = fetchByF_N(folderId, name);
389 
390         if (dlFileEntry == null) {
391             StringMaker msg = new StringMaker();
392             msg.append("No DLFileEntry exists with the key ");
393             msg.append(StringPool.OPEN_CURLY_BRACE);
394             msg.append("folderId=");
395             msg.append(folderId);
396             msg.append(", ");
397             msg.append("name=");
398             msg.append(name);
399             msg.append(StringPool.CLOSE_CURLY_BRACE);
400 
401             if (_log.isWarnEnabled()) {
402                 _log.warn(msg.toString());
403             }
404 
405             throw new NoSuchFileEntryException(msg.toString());
406         }
407 
408         return dlFileEntry;
409     }
410 
411     public DLFileEntry fetchByF_N(long folderId, String name)
412         throws SystemException {
413         String finderClassName = DLFileEntry.class.getName();
414         String finderMethodName = "fetchByF_N";
415         String[] finderParams = new String[] {
416                 Long.class.getName(), String.class.getName()
417             };
418         Object[] finderArgs = new Object[] { new Long(folderId), name };
419         Object result = FinderCache.getResult(finderClassName,
420                 finderMethodName, finderParams, finderArgs, getSessionFactory());
421 
422         if (result == null) {
423             Session session = null;
424 
425             try {
426                 session = openSession();
427 
428                 StringMaker query = new StringMaker();
429                 query.append(
430                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
431                 query.append("folderId = ?");
432                 query.append(" AND ");
433 
434                 if (name == null) {
435                     query.append("name IS NULL");
436                 }
437                 else {
438                     query.append("name = ?");
439                 }
440 
441                 query.append(" ");
442                 query.append("ORDER BY ");
443                 query.append("folderId ASC").append(", ");
444                 query.append("name ASC");
445 
446                 Query q = session.createQuery(query.toString());
447                 int queryPos = 0;
448                 q.setLong(queryPos++, folderId);
449 
450                 if (name != null) {
451                     q.setString(queryPos++, name);
452                 }
453 
454                 List list = q.list();
455                 FinderCache.putResult(finderClassName, finderMethodName,
456                     finderParams, finderArgs, list);
457 
458                 if (list.size() == 0) {
459                     return null;
460                 }
461                 else {
462                     return (DLFileEntry)list.get(0);
463                 }
464             }
465             catch (Exception e) {
466                 throw HibernateUtil.processException(e);
467             }
468             finally {
469                 closeSession(session);
470             }
471         }
472         else {
473             List list = (List)result;
474 
475             if (list.size() == 0) {
476                 return null;
477             }
478             else {
479                 return (DLFileEntry)list.get(0);
480             }
481         }
482     }
483 
484     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
485         throws SystemException {
486         Session session = null;
487 
488         try {
489             session = openSession();
490 
491             DynamicQuery query = queryInitializer.initialize(session);
492 
493             return query.list();
494         }
495         catch (Exception e) {
496             throw HibernateUtil.processException(e);
497         }
498         finally {
499             closeSession(session);
500         }
501     }
502 
503     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
504         int begin, int end) throws SystemException {
505         Session session = null;
506 
507         try {
508             session = openSession();
509 
510             DynamicQuery query = queryInitializer.initialize(session);
511             query.setLimit(begin, end);
512 
513             return query.list();
514         }
515         catch (Exception e) {
516             throw HibernateUtil.processException(e);
517         }
518         finally {
519             closeSession(session);
520         }
521     }
522 
523     public List findAll() throws SystemException {
524         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
525     }
526 
527     public List findAll(int begin, int end) throws SystemException {
528         return findAll(begin, end, null);
529     }
530 
531     public List findAll(int begin, int end, OrderByComparator obc)
532         throws SystemException {
533         String finderClassName = DLFileEntry.class.getName();
534         String finderMethodName = "findAll";
535         String[] finderParams = new String[] {
536                 "java.lang.Integer", "java.lang.Integer",
537                 "com.liferay.portal.kernel.util.OrderByComparator"
538             };
539         Object[] finderArgs = new Object[] {
540                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
541             };
542         Object result = FinderCache.getResult(finderClassName,
543                 finderMethodName, finderParams, finderArgs, getSessionFactory());
544 
545         if (result == null) {
546             Session session = null;
547 
548             try {
549                 session = openSession();
550 
551                 StringMaker query = new StringMaker();
552                 query.append(
553                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry ");
554 
555                 if (obc != null) {
556                     query.append("ORDER BY ");
557                     query.append(obc.getOrderBy());
558                 }
559                 else {
560                     query.append("ORDER BY ");
561                     query.append("folderId ASC").append(", ");
562                     query.append("name ASC");
563                 }
564 
565                 Query q = session.createQuery(query.toString());
566                 List list = QueryUtil.list(q, getDialect(), begin, end);
567 
568                 if (obc == null) {
569                     Collections.sort(list);
570                 }
571 
572                 FinderCache.putResult(finderClassName, finderMethodName,
573                     finderParams, finderArgs, list);
574 
575                 return list;
576             }
577             catch (Exception e) {
578                 throw HibernateUtil.processException(e);
579             }
580             finally {
581                 closeSession(session);
582             }
583         }
584         else {
585             return (List)result;
586         }
587     }
588 
589     public void removeByFolderId(long folderId) throws SystemException {
590         Iterator itr = findByFolderId(folderId).iterator();
591 
592         while (itr.hasNext()) {
593             DLFileEntry dlFileEntry = (DLFileEntry)itr.next();
594             remove(dlFileEntry);
595         }
596     }
597 
598     public void removeByF_N(long folderId, String name)
599         throws NoSuchFileEntryException, SystemException {
600         DLFileEntry dlFileEntry = findByF_N(folderId, name);
601         remove(dlFileEntry);
602     }
603 
604     public void removeAll() throws SystemException {
605         Iterator itr = findAll().iterator();
606 
607         while (itr.hasNext()) {
608             remove((DLFileEntry)itr.next());
609         }
610     }
611 
612     public int countByFolderId(long folderId) throws SystemException {
613         String finderClassName = DLFileEntry.class.getName();
614         String finderMethodName = "countByFolderId";
615         String[] finderParams = new String[] { Long.class.getName() };
616         Object[] finderArgs = new Object[] { new Long(folderId) };
617         Object result = FinderCache.getResult(finderClassName,
618                 finderMethodName, finderParams, finderArgs, getSessionFactory());
619 
620         if (result == null) {
621             Session session = null;
622 
623             try {
624                 session = openSession();
625 
626                 StringMaker query = new StringMaker();
627                 query.append("SELECT COUNT(*) ");
628                 query.append(
629                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
630                 query.append("folderId = ?");
631                 query.append(" ");
632 
633                 Query q = session.createQuery(query.toString());
634                 int queryPos = 0;
635                 q.setLong(queryPos++, folderId);
636 
637                 Long count = null;
638                 Iterator itr = q.list().iterator();
639 
640                 if (itr.hasNext()) {
641                     count = (Long)itr.next();
642                 }
643 
644                 if (count == null) {
645                     count = new Long(0);
646                 }
647 
648                 FinderCache.putResult(finderClassName, finderMethodName,
649                     finderParams, finderArgs, count);
650 
651                 return count.intValue();
652             }
653             catch (Exception e) {
654                 throw HibernateUtil.processException(e);
655             }
656             finally {
657                 closeSession(session);
658             }
659         }
660         else {
661             return ((Long)result).intValue();
662         }
663     }
664 
665     public int countByF_N(long folderId, String name) throws SystemException {
666         String finderClassName = DLFileEntry.class.getName();
667         String finderMethodName = "countByF_N";
668         String[] finderParams = new String[] {
669                 Long.class.getName(), String.class.getName()
670             };
671         Object[] finderArgs = new Object[] { new Long(folderId), name };
672         Object result = FinderCache.getResult(finderClassName,
673                 finderMethodName, finderParams, finderArgs, getSessionFactory());
674 
675         if (result == null) {
676             Session session = null;
677 
678             try {
679                 session = openSession();
680 
681                 StringMaker query = new StringMaker();
682                 query.append("SELECT COUNT(*) ");
683                 query.append(
684                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry WHERE ");
685                 query.append("folderId = ?");
686                 query.append(" AND ");
687 
688                 if (name == null) {
689                     query.append("name IS NULL");
690                 }
691                 else {
692                     query.append("name = ?");
693                 }
694 
695                 query.append(" ");
696 
697                 Query q = session.createQuery(query.toString());
698                 int queryPos = 0;
699                 q.setLong(queryPos++, folderId);
700 
701                 if (name != null) {
702                     q.setString(queryPos++, name);
703                 }
704 
705                 Long count = null;
706                 Iterator itr = q.list().iterator();
707 
708                 if (itr.hasNext()) {
709                     count = (Long)itr.next();
710                 }
711 
712                 if (count == null) {
713                     count = new Long(0);
714                 }
715 
716                 FinderCache.putResult(finderClassName, finderMethodName,
717                     finderParams, finderArgs, count);
718 
719                 return count.intValue();
720             }
721             catch (Exception e) {
722                 throw HibernateUtil.processException(e);
723             }
724             finally {
725                 closeSession(session);
726             }
727         }
728         else {
729             return ((Long)result).intValue();
730         }
731     }
732 
733     public int countAll() throws SystemException {
734         String finderClassName = DLFileEntry.class.getName();
735         String finderMethodName = "countAll";
736         String[] finderParams = new String[] {  };
737         Object[] finderArgs = new Object[] {  };
738         Object result = FinderCache.getResult(finderClassName,
739                 finderMethodName, finderParams, finderArgs, getSessionFactory());
740 
741         if (result == null) {
742             Session session = null;
743 
744             try {
745                 session = openSession();
746 
747                 StringMaker query = new StringMaker();
748                 query.append("SELECT COUNT(*) ");
749                 query.append(
750                     "FROM com.liferay.portlet.documentlibrary.model.DLFileEntry");
751 
752                 Query q = session.createQuery(query.toString());
753                 Long count = null;
754                 Iterator itr = q.list().iterator();
755 
756                 if (itr.hasNext()) {
757                     count = (Long)itr.next();
758                 }
759 
760                 if (count == null) {
761                     count = new Long(0);
762                 }
763 
764                 FinderCache.putResult(finderClassName, finderMethodName,
765                     finderParams, finderArgs, count);
766 
767                 return count.intValue();
768             }
769             catch (Exception e) {
770                 throw HibernateUtil.processException(e);
771             }
772             finally {
773                 closeSession(session);
774             }
775         }
776         else {
777             return ((Long)result).intValue();
778         }
779     }
780 
781     protected void initDao() {
782     }
783 
784     private static Log _log = LogFactory.getLog(DLFileEntryPersistenceImpl.class);
785 }