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