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.ExpandoColumn;
29  import com.liferay.portlet.expando.model.impl.ExpandoColumnImpl;
30  import com.liferay.util.dao.hibernate.QueryPos;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import org.hibernate.Hibernate;
36  import org.hibernate.SQLQuery;
37  import org.hibernate.Session;
38  
39  /**
40   * <a href="ExpandoColumnFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Raymond Augé
43   *
44   */
45  public class ExpandoColumnFinderImpl implements ExpandoColumnFinder {
46  
47      public static String COUNT_BY_TC_TN =
48          ExpandoColumnFinder.class.getName() + ".countByTC_TN";
49  
50      public static String FIND_BY_TC_TN =
51          ExpandoColumnFinder.class.getName() + ".findByTC_TN";
52  
53      public static String FIND_BY_TC_TN_CN =
54          ExpandoColumnFinder.class.getName() + ".findByTC_TN_CN";
55  
56      public int countByTC_TN(long classNameId, String tableName)
57          throws SystemException {
58  
59          Session session = null;
60  
61          try {
62              session = HibernateUtil.openSession();
63  
64              String sql = CustomSQLUtil.get(COUNT_BY_TC_TN);
65  
66              SQLQuery q = session.createSQLQuery(sql);
67  
68              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
69  
70              QueryPos qPos = QueryPos.getInstance(q);
71  
72              qPos.add(classNameId);
73              qPos.add(tableName);
74  
75              Iterator<Long> itr = q.list().iterator();
76  
77              if (itr.hasNext()) {
78                  Long count = itr.next();
79  
80                  if (count != null) {
81                      return count.intValue();
82                  }
83              }
84  
85              return 0;
86          }
87          catch (Exception e) {
88              throw new SystemException(e);
89          }
90          finally {
91              HibernateUtil.closeSession(session);
92          }
93      }
94  
95      public List<ExpandoColumn> findByTC_TN(long classNameId, String tableName)
96          throws SystemException {
97  
98          Session session = null;
99  
100         try {
101             session = HibernateUtil.openSession();
102 
103             String sql = CustomSQLUtil.get(FIND_BY_TC_TN);
104 
105             SQLQuery q = session.createSQLQuery(sql);
106 
107             q.addEntity("ExpandoColumn", ExpandoColumnImpl.class);
108 
109             QueryPos qPos = QueryPos.getInstance(q);
110 
111             qPos.add(classNameId);
112             qPos.add(tableName);
113 
114             return (List<ExpandoColumn>)q.list();
115         }
116         catch (Exception e) {
117             throw new SystemException(e);
118         }
119         finally {
120             HibernateUtil.closeSession(session);
121         }
122     }
123 
124     public ExpandoColumn findByTC_TN_CN(
125             long classNameId, String tableName, String name)
126         throws SystemException {
127 
128         Session session = null;
129 
130         try {
131             session = HibernateUtil.openSession();
132 
133             String sql = CustomSQLUtil.get(FIND_BY_TC_TN_CN);
134 
135             SQLQuery q = session.createSQLQuery(sql);
136 
137             q.addEntity("ExpandoColumn", ExpandoColumnImpl.class);
138 
139             QueryPos qPos = QueryPos.getInstance(q);
140 
141             qPos.add(classNameId);
142             qPos.add(tableName);
143             qPos.add(name);
144 
145             return (ExpandoColumn)q.uniqueResult();
146         }
147         catch (Exception e) {
148             throw new SystemException(e);
149         }
150         finally {
151             HibernateUtil.closeSession(session);
152         }
153     }
154 
155 }