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