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