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