1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.model.Dummy;
24  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
25  import com.liferay.util.dao.orm.CustomSQLUtil;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * <a href="TagsPropertyKeyFinderImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class TagsPropertyKeyFinderImpl
36      extends BasePersistenceImpl<Dummy> implements TagsPropertyKeyFinder {
37  
38      public static String COUNT_BY_GROUP_ID =
39          TagsPropertyKeyFinder.class.getName() + ".countByGroupId";
40  
41      public static String FIND_BY_GROUP_ID =
42          TagsPropertyKeyFinder.class.getName() + ".findByGroupId";
43  
44      public int countByGroupId(long groupId) throws SystemException {
45          Session session = null;
46  
47          try {
48              session = openSession();
49  
50              String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
51  
52              SQLQuery q = session.createSQLQuery(sql);
53  
54              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
55  
56              QueryPos qPos = QueryPos.getInstance(q);
57  
58              qPos.add(groupId);
59  
60              Iterator<Long> itr = q.list().iterator();
61  
62              if (itr.hasNext()) {
63                  Long count = itr.next();
64  
65                  if (count != null) {
66                      return count.intValue();
67                  }
68              }
69  
70              return 0;
71          }
72          catch (Exception e) {
73              throw new SystemException(e);
74          }
75          finally {
76              closeSession(session);
77          }
78      }
79  
80      public String[] findByGroupId(long groupId) throws SystemException {
81          return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
82      }
83  
84      public String[] findByGroupId(long groupId, int start, int end)
85          throws SystemException {
86  
87          Session session = null;
88  
89          try {
90              session = openSession();
91  
92              String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
93  
94              SQLQuery q = session.createSQLQuery(sql);
95  
96              q.addScalar("propertyKey", Type.STRING);
97  
98              QueryPos qPos = QueryPos.getInstance(q);
99  
100             qPos.add(groupId);
101 
102             List<String> list = (List<String>)QueryUtil.list(
103                 q, getDialect(), start, end);
104 
105             return list.toArray(new String[list.size()]);
106         }
107         catch (Exception e) {
108             throw new SystemException(e);
109         }
110         finally {
111             closeSession(session);
112         }
113     }
114 
115 }