1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
27  import com.liferay.portal.spring.hibernate.HibernateUtil;
28  import com.liferay.portlet.expando.model.ExpandoValue;
29  import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
30  import com.liferay.util.dao.hibernate.QueryPos;
31  import com.liferay.util.dao.hibernate.QueryUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import org.hibernate.Hibernate;
37  import org.hibernate.SQLQuery;
38  import org.hibernate.Session;
39  
40  /**
41   * <a href="ExpandoValueFinderImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Raymond Augé
44   *
45   */
46  public class ExpandoValueFinderImpl implements ExpandoValueFinder {
47  
48      public static String COUNT_BY_TC_TN_CN =
49          ExpandoValueFinder.class.getName() + ".countByTC_TN_CN";
50  
51      public static String FIND_BY_TC_TN_CN =
52          ExpandoValueFinder.class.getName() + ".findByTC_TN_CN";
53  
54      public static String FIND_BY_TC_TN_CN_R =
55          ExpandoValueFinder.class.getName() + ".findByTC_TN_CN_R";
56  
57      public int countByTC_TN_CN(
58              long classNameId, String tableName, String columnName)
59          throws SystemException {
60  
61          Session session = null;
62  
63          try {
64              session = HibernateUtil.openSession();
65  
66              String sql = CustomSQLUtil.get(COUNT_BY_TC_TN_CN);
67  
68              SQLQuery q = session.createSQLQuery(sql);
69  
70              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
71  
72              QueryPos qPos = QueryPos.getInstance(q);
73  
74              qPos.add(classNameId);
75              qPos.add(tableName);
76              qPos.add(columnName);
77  
78              Iterator<Long> itr = q.list().iterator();
79  
80              if (itr.hasNext()) {
81                  Long count = itr.next();
82  
83                  if (count != null) {
84                      return count.intValue();
85                  }
86              }
87  
88              return 0;
89          }
90          catch (Exception e) {
91              throw new SystemException(e);
92          }
93          finally {
94              HibernateUtil.closeSession(session);
95          }
96      }
97  
98      public List<ExpandoValue> findByTC_TN_CN(
99              long classNameId, String tableName, String columnName, int begin,
100             int end)
101         throws SystemException {
102 
103         Session session = null;
104 
105         try {
106             session = HibernateUtil.openSession();
107 
108             String sql = CustomSQLUtil.get(FIND_BY_TC_TN_CN);
109 
110             SQLQuery q = session.createSQLQuery(sql);
111 
112             q.addEntity("ExpandoValue", ExpandoValueImpl.class);
113 
114             QueryPos qPos = QueryPos.getInstance(q);
115 
116             qPos.add(classNameId);
117             qPos.add(tableName);
118             qPos.add(columnName);
119 
120             return (List<ExpandoValue>)QueryUtil.list(
121                     q, HibernateUtil.getDialect(), begin, end);
122         }
123         catch (Exception e) {
124             throw new SystemException(e);
125         }
126         finally {
127             HibernateUtil.closeSession(session);
128         }
129     }
130 
131     public ExpandoValue findByTC_TN_N(
132             long classNameId, String tableName, String columnName, long rowId)
133         throws SystemException {
134 
135         Session session = null;
136 
137         try {
138             session = HibernateUtil.openSession();
139 
140             String sql = CustomSQLUtil.get(FIND_BY_TC_TN_CN_R);
141 
142             SQLQuery q = session.createSQLQuery(sql);
143 
144             q.addEntity("ExpandoValue", ExpandoValueImpl.class);
145 
146             QueryPos qPos = QueryPos.getInstance(q);
147 
148             qPos.add(classNameId);
149             qPos.add(tableName);
150             qPos.add(columnName);
151             qPos.add(rowId);
152 
153             return (ExpandoValue)q.uniqueResult();
154         }
155         catch (Exception e) {
156             throw new SystemException(e);
157         }
158         finally {
159             HibernateUtil.closeSession(session);
160         }
161     }
162 
163 }