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