1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
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="AssetTagPropertyKeyFinderImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class AssetTagPropertyKeyFinderImpl
37      extends BasePersistenceImpl<Dummy> implements AssetTagPropertyKeyFinder {
38  
39      public static String COUNT_BY_GROUP_ID =
40          AssetTagPropertyKeyFinder.class.getName() + ".countByGroupId";
41  
42      public static String FIND_BY_GROUP_ID =
43          AssetTagPropertyKeyFinder.class.getName() + ".findByGroupId";
44  
45      public int countByGroupId(long groupId) throws SystemException {
46          Session session = null;
47  
48          try {
49              session = openSession();
50  
51              String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
52  
53              SQLQuery q = session.createSQLQuery(sql);
54  
55              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
56  
57              QueryPos qPos = QueryPos.getInstance(q);
58  
59              qPos.add(groupId);
60  
61              Iterator<Long> itr = q.list().iterator();
62  
63              if (itr.hasNext()) {
64                  Long count = itr.next();
65  
66                  if (count != null) {
67                      return count.intValue();
68                  }
69              }
70  
71              return 0;
72          }
73          catch (Exception e) {
74              throw new SystemException(e);
75          }
76          finally {
77              closeSession(session);
78          }
79      }
80  
81      public String[] findByGroupId(long groupId) throws SystemException {
82          return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
83      }
84  
85      public String[] findByGroupId(long groupId, int start, int end)
86          throws SystemException {
87  
88          Session session = null;
89  
90          try {
91              session = openSession();
92  
93              String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
94  
95              SQLQuery q = session.createSQLQuery(sql);
96  
97              q.addScalar("propertyKey", Type.STRING);
98  
99              QueryPos qPos = QueryPos.getInstance(q);
100 
101             qPos.add(groupId);
102 
103             List<String> list = (List<String>)QueryUtil.list(
104                 q, getDialect(), start, end);
105 
106             return list.toArray(new String[list.size()]);
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             closeSession(session);
113         }
114     }
115 
116 }