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