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