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.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.asset.NoSuchCategoryException;
29  import com.liferay.portlet.asset.model.AssetCategory;
30  import com.liferay.portlet.asset.model.impl.AssetCategoryImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="AssetCategoryFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Bruno Farache
41   * @author Jorge Ferrer
42   */
43  public class AssetCategoryFinderImpl
44      extends BasePersistenceImpl<AssetCategory> implements AssetCategoryFinder {
45  
46      public static String COUNT_BY_G_C_N =
47          AssetCategoryFinder.class.getName() + ".countByG_C_N";
48  
49      public static String COUNT_BY_G_N_P =
50          AssetCategoryFinder.class.getName() + ".countByG_N_P";
51  
52      public static String FIND_BY_ENTRY_ID =
53          AssetCategoryFinder.class.getName() + ".findByEntryId";
54  
55      public static String FIND_BY_G_N =
56          AssetCategoryFinder.class.getName() + ".findByG_N";
57  
58      public static String FIND_BY_C_C =
59          AssetCategoryFinder.class.getName() + ".findByC_C";
60  
61      public static String FIND_BY_G_N_P =
62          AssetCategoryFinder.class.getName() + ".findByG_N_P";
63  
64      public int countByG_C_N(long groupId, long classNameId, String name)
65          throws SystemException {
66  
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              String sql = CustomSQLUtil.get(COUNT_BY_G_C_N);
73  
74              SQLQuery q = session.createSQLQuery(sql);
75  
76              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
77  
78              QueryPos qPos = QueryPos.getInstance(q);
79  
80              qPos.add(groupId);
81              qPos.add(classNameId);
82              qPos.add(name);
83              qPos.add(name);
84  
85              Iterator<Long> itr = q.list().iterator();
86  
87              if (itr.hasNext()) {
88                  Long count = itr.next();
89  
90                  if (count != null) {
91                      return count.intValue();
92                  }
93              }
94  
95              return 0;
96          }
97          catch (Exception e) {
98              throw new SystemException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public int countByG_N_P(
106             long groupId, String name, String[] categoryProperties)
107         throws SystemException {
108 
109         Session session = null;
110 
111         try {
112             session = openSession();
113 
114             String sql = CustomSQLUtil.get(COUNT_BY_G_N_P);
115 
116             SQLQuery q = session.createSQLQuery(sql);
117 
118             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
119 
120             QueryPos qPos = QueryPos.getInstance(q);
121 
122             setJoin(qPos, categoryProperties);
123             qPos.add(groupId);
124             qPos.add(name);
125             qPos.add(name);
126 
127             Iterator<Long> itr = q.list().iterator();
128 
129             if (itr.hasNext()) {
130                 Long count = itr.next();
131 
132                 if (count != null) {
133                     return count.intValue();
134                 }
135             }
136 
137             return 0;
138         }
139         catch (Exception e) {
140             throw new SystemException(e);
141         }
142         finally {
143             closeSession(session);
144         }
145     }
146 
147     public List<AssetCategory> findByEntryId(long entryId)
148         throws SystemException {
149 
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             String sql = CustomSQLUtil.get(FIND_BY_ENTRY_ID);
156 
157             SQLQuery q = session.createSQLQuery(sql);
158 
159             q.addEntity("AssetCategory", AssetCategoryImpl.class);
160 
161             QueryPos qPos = QueryPos.getInstance(q);
162 
163             qPos.add(entryId);
164 
165             return (List<AssetCategory>) QueryUtil.list(
166                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
167         }
168         catch (Exception e) {
169             throw new SystemException(e);
170         }
171         finally {
172             closeSession(session);
173         }
174     }
175 
176     public AssetCategory findByG_N(long groupId, String name)
177         throws NoSuchCategoryException, SystemException {
178 
179         name = name.trim().toLowerCase();
180 
181         Session session = null;
182 
183         try {
184             session = openSession();
185 
186             String sql = CustomSQLUtil.get(FIND_BY_G_N);
187 
188             SQLQuery q = session.createSQLQuery(sql);
189 
190             q.addEntity("AssetCategory", AssetCategoryImpl.class);
191 
192             QueryPos qPos = QueryPos.getInstance(q);
193 
194             qPos.add(groupId);
195             qPos.add(name);
196 
197             List<AssetCategory> list = q.list();
198 
199             if (list.size() == 0) {
200                 StringBundler sb = new StringBundler(6);
201 
202                 sb.append("No AssetCategory exists with the key ");
203                 sb.append("{groupId=");
204                 sb.append(groupId);
205                 sb.append(", name=");
206                 sb.append(name);
207                 sb.append("}");
208 
209                 throw new NoSuchCategoryException(sb.toString());
210             }
211             else {
212                 return list.get(0);
213             }
214         }
215         catch (NoSuchCategoryException nsee) {
216             throw nsee;
217         }
218         catch (Exception e) {
219             throw new SystemException(e);
220         }
221         finally {
222             closeSession(session);
223         }
224     }
225 
226     public List<AssetCategory> findByC_C(long classNameId, long classPK)
227         throws SystemException {
228 
229         Session session = null;
230 
231         try {
232             session = openSession();
233 
234             String sql = CustomSQLUtil.get(FIND_BY_C_C);
235 
236             SQLQuery q = session.createSQLQuery(sql);
237 
238             q.addEntity("AssetCategory", AssetCategoryImpl.class);
239 
240             QueryPos qPos = QueryPos.getInstance(q);
241 
242             qPos.add(classNameId);
243             qPos.add(classPK);
244 
245             return (List<AssetCategory>)QueryUtil.list(
246                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
247         }
248         catch (Exception e) {
249             throw new SystemException(e);
250         }
251         finally {
252             closeSession(session);
253         }
254     }
255 
256     public List<AssetCategory> findByG_N_P(
257             long groupId, String name, String[] categoryProperties)
258         throws SystemException {
259 
260         return findByG_N_P(
261             groupId, name, categoryProperties, QueryUtil.ALL_POS,
262             QueryUtil.ALL_POS);
263     }
264 
265     public List<AssetCategory> findByG_N_P(
266             long groupId, String name, String[] categoryProperties, int start,
267             int end)
268         throws SystemException {
269 
270         Session session = null;
271 
272         try {
273             session = openSession();
274 
275             String sql = CustomSQLUtil.get(FIND_BY_G_N_P);
276 
277             sql = StringUtil.replace(
278                 sql, "[$JOIN$]", getJoin(categoryProperties));
279 
280             SQLQuery q = session.createSQLQuery(sql);
281 
282             q.addEntity("AssetCategory", AssetCategoryImpl.class);
283 
284             QueryPos qPos = QueryPos.getInstance(q);
285 
286             setJoin(qPos, categoryProperties);
287             qPos.add(groupId);
288             qPos.add(name);
289             qPos.add(name);
290 
291             return (List<AssetCategory>)QueryUtil.list(
292                 q, getDialect(), start, end);
293         }
294         catch (Exception e) {
295             throw new SystemException(e);
296         }
297         finally {
298             closeSession(session);
299         }
300     }
301 
302     protected String getJoin(String[] categoryProperties) {
303         if (categoryProperties.length == 0) {
304             return StringPool.BLANK;
305         }
306         else {
307             StringBundler sb = new StringBundler(
308                 categoryProperties.length * 3 + 2);
309 
310             sb.append(" INNER JOIN AssetCategoryProperty ON ");
311             sb.append(" (AssetCategoryProperty.categoryId = ");
312             sb.append(" AssetCategory.categoryId) AND ");
313 
314             for (int i = 0; i < categoryProperties.length; i++) {
315                 sb.append("(AssetCategoryProperty.key_ = ? AND ");
316                 sb.append("AssetCategoryProperty.value = ?) ");
317 
318                 if ((i + 1) < categoryProperties.length) {
319                     sb.append(" AND ");
320                 }
321             }
322 
323             return sb.toString();
324         }
325     }
326 
327     protected void setJoin(QueryPos qPos, String[] categoryProperties) {
328         for (int i = 0; i < categoryProperties.length; i++) {
329             String[] categoryProperty = StringUtil.split(
330                 categoryProperties[i], StringPool.COLON);
331 
332             String key = StringPool.BLANK;
333 
334             if (categoryProperty.length > 0) {
335                 key = GetterUtil.getString(categoryProperty[0]);
336             }
337 
338             String value = StringPool.BLANK;
339 
340             if (categoryProperty.length > 1) {
341                 value = GetterUtil.getString(categoryProperty[1]);
342             }
343 
344             qPos.add(key);
345             qPos.add(value);
346         }
347     }
348 
349 }