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.portlet.tags.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.tags.model.TagsProperty;
29  import com.liferay.portlet.tags.model.impl.TagsPropertyImpl;
30  import com.liferay.util.dao.hibernate.QueryPos;
31  import com.liferay.util.dao.hibernate.QueryUtil;
32  
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.hibernate.Hibernate;
38  import org.hibernate.SQLQuery;
39  import org.hibernate.Session;
40  
41  /**
42   * <a href="TagsPropertyFinder.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class TagsPropertyFinder {
48  
49      public static String COUNT_BY_C_K =
50          TagsPropertyFinder.class.getName() + ".countByC_K";
51  
52      public static String FIND_BY_C_K =
53          TagsPropertyFinder.class.getName() + ".findByC_K";
54  
55      public static int countByC_K(long companyId, String key)
56          throws SystemException {
57  
58          Session session = null;
59  
60          try {
61              session = HibernateUtil.openSession();
62  
63              String sql = CustomSQLUtil.get(COUNT_BY_C_K);
64  
65              SQLQuery q = session.createSQLQuery(sql);
66  
67              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
68  
69              QueryPos qPos = QueryPos.getInstance(q);
70  
71              qPos.add(companyId);
72              qPos.add(key);
73  
74              Iterator itr = q.list().iterator();
75  
76              if (itr.hasNext()) {
77                  Long count = (Long)itr.next();
78  
79                  if (count != null) {
80                      return count.intValue();
81                  }
82              }
83  
84              return 0;
85          }
86          catch (Exception e) {
87              throw new SystemException(e);
88          }
89          finally {
90              HibernateUtil.closeSession(session);
91          }
92      }
93  
94      public static List findByC_K(long companyId, String key)
95          throws SystemException {
96  
97          return findByC_K(companyId, key, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
98      }
99  
100     public static List findByC_K(
101             long companyId, String key, int begin, int end)
102         throws SystemException {
103 
104         Session session = null;
105 
106         try {
107             session = HibernateUtil.openSession();
108 
109             String sql = CustomSQLUtil.get(FIND_BY_C_K);
110 
111             SQLQuery q = session.createSQLQuery(sql);
112 
113             q.addScalar("propertyValue", Hibernate.STRING);
114 
115             QueryPos qPos = QueryPos.getInstance(q);
116 
117             qPos.add(companyId);
118             qPos.add(key);
119 
120             List list = new ArrayList();
121 
122             Iterator itr = QueryUtil.iterate(
123                 q, HibernateUtil.getDialect(), begin, end);
124 
125             while (itr.hasNext()) {
126                 String value = (String)itr.next();
127 
128                 TagsProperty property = new TagsPropertyImpl();
129 
130                 property.setKey(key);
131                 property.setValue(value);
132 
133                 list.add(property);
134             }
135 
136             return list;
137         }
138         catch (Exception e) {
139             throw new SystemException(e);
140         }
141         finally {
142             HibernateUtil.closeSession(session);
143         }
144     }
145 
146 }