1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.tags.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="TagsPropertyKeyFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class TagsPropertyKeyFinderImpl
41      extends BasePersistenceImpl implements TagsPropertyKeyFinder {
42  
43      public static String COUNT_BY_COMPANYID =
44          TagsPropertyKeyFinder.class.getName() + ".countByCompanyId";
45  
46      public static String FIND_BY_COMPANYID =
47          TagsPropertyKeyFinder.class.getName() + ".findByCompanyId";
48  
49      public int countByCompanyId(long companyId) throws SystemException {
50          Session session = null;
51  
52          try {
53              session = openSession();
54  
55              String sql = CustomSQLUtil.get(COUNT_BY_COMPANYID);
56  
57              SQLQuery q = session.createSQLQuery(sql);
58  
59              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
60  
61              QueryPos qPos = QueryPos.getInstance(q);
62  
63              qPos.add(companyId);
64  
65              Iterator<Long> itr = q.list().iterator();
66  
67              if (itr.hasNext()) {
68                  Long count = itr.next();
69  
70                  if (count != null) {
71                      return count.intValue();
72                  }
73              }
74  
75              return 0;
76          }
77          catch (Exception e) {
78              throw new SystemException(e);
79          }
80          finally {
81              closeSession(session);
82          }
83      }
84  
85      public String[] findByCompanyId(long companyId) throws SystemException {
86          return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
87      }
88  
89      public String[] findByCompanyId(long companyId, int start, int end)
90          throws SystemException {
91  
92          Session session = null;
93  
94          try {
95              session = openSession();
96  
97              String sql = CustomSQLUtil.get(FIND_BY_COMPANYID);
98  
99              SQLQuery q = session.createSQLQuery(sql);
100 
101             q.addScalar("propertyKey", Type.STRING);
102 
103             QueryPos qPos = QueryPos.getInstance(q);
104 
105             qPos.add(companyId);
106 
107             List<String> list = (List<String>)QueryUtil.list(
108                 q, getDialect(), start, end);
109 
110             return list.toArray(new String[list.size()]);
111         }
112         catch (Exception e) {
113             throw new SystemException(e);
114         }
115         finally {
116             closeSession(session);
117         }
118     }
119 
120 }