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