1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.portlet.tags.NoSuchEntryException;
28  import com.liferay.portlet.tags.model.TagsEntry;
29  import com.liferay.portlet.tags.model.impl.TagsEntryImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="TagsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Bruno Farache
40   */
41  public class TagsEntryFinderImpl
42      extends BasePersistenceImpl<TagsEntry> implements TagsEntryFinder {
43  
44      public static String COUNT_BY_G_C_N_F =
45          TagsEntryFinder.class.getName() + ".countByG_C_N_F";
46  
47      public static String COUNT_BY_G_N_F_P =
48          TagsEntryFinder.class.getName() + ".countByG_N_F_P";
49  
50      public static String FIND_BY_FOLKSONOMY =
51          TagsEntryFinder.class.getName() + ".findByFolksonomy";
52  
53      public static String FIND_BY_A_F =
54          TagsEntryFinder.class.getName() + ".findByA_F";
55  
56      public static String FIND_BY_G_N_F =
57          TagsEntryFinder.class.getName() + ".findByG_N_F";
58  
59      public static String FIND_BY_C_C_F =
60          TagsEntryFinder.class.getName() + ".findByC_C_F";
61  
62      public static String FIND_BY_G_C_N_F =
63          TagsEntryFinder.class.getName() + ".findByG_C_N_F";
64  
65      public static String FIND_BY_G_N_F_P =
66          TagsEntryFinder.class.getName() + ".findByG_N_F_P";
67  
68      public int countByG_C_N_F(
69              long groupId, long classNameId, String name, boolean folksonomy)
70          throws SystemException {
71  
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              String sql = CustomSQLUtil.get(COUNT_BY_G_C_N_F);
78  
79              SQLQuery q = session.createSQLQuery(sql);
80  
81              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
82  
83              QueryPos qPos = QueryPos.getInstance(q);
84  
85              qPos.add(groupId);
86              qPos.add(classNameId);
87              qPos.add(name);
88              qPos.add(name);
89              qPos.add(folksonomy);
90  
91              Iterator<Long> itr = q.list().iterator();
92  
93              if (itr.hasNext()) {
94                  Long count = itr.next();
95  
96                  if (count != null) {
97                      return count.intValue();
98                  }
99              }
100 
101             return 0;
102         }
103         catch (Exception e) {
104             throw new SystemException(e);
105         }
106         finally {
107             closeSession(session);
108         }
109     }
110 
111     public int countByG_N_F_P(
112             long groupId, String name, boolean folksonomy, String[] properties)
113         throws SystemException {
114 
115         Session session = null;
116 
117         try {
118             session = openSession();
119 
120             String sql = CustomSQLUtil.get(COUNT_BY_G_N_F_P);
121 
122             SQLQuery q = session.createSQLQuery(sql);
123 
124             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
125 
126             QueryPos qPos = QueryPos.getInstance(q);
127 
128             setJoin(qPos, properties);
129             qPos.add(groupId);
130             qPos.add(name);
131             qPos.add(name);
132             qPos.add(folksonomy);
133 
134             Iterator<Long> itr = q.list().iterator();
135 
136             if (itr.hasNext()) {
137                 Long count = itr.next();
138 
139                 if (count != null) {
140                     return count.intValue();
141                 }
142             }
143 
144             return 0;
145         }
146         catch (Exception e) {
147             throw new SystemException(e);
148         }
149         finally {
150             closeSession(session);
151         }
152     }
153 
154     public List<TagsEntry> findByFolksonomy(boolean folksonomy)
155         throws SystemException {
156 
157         Session session = null;
158 
159         try {
160             session = openSession();
161 
162             String sql = CustomSQLUtil.get(FIND_BY_FOLKSONOMY);
163 
164             SQLQuery q = session.createSQLQuery(sql);
165 
166             q.addEntity("TagsEntry", TagsEntryImpl.class);
167 
168             QueryPos qPos = QueryPos.getInstance(q);
169 
170             qPos.add(folksonomy);
171 
172             return (List<TagsEntry>) QueryUtil.list(
173                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
174         }
175         catch (Exception e) {
176             throw new SystemException(e);
177         }
178         finally {
179             closeSession(session);
180         }
181     }
182 
183     public List<TagsEntry> findByA_F(long assetId, boolean folksonomy)
184         throws SystemException {
185 
186         Session session = null;
187 
188         try {
189             session = openSession();
190 
191             String sql = CustomSQLUtil.get(FIND_BY_A_F);
192 
193             SQLQuery q = session.createSQLQuery(sql);
194 
195             q.addEntity("TagsEntry", TagsEntryImpl.class);
196 
197             QueryPos qPos = QueryPos.getInstance(q);
198 
199             qPos.add(assetId);
200             qPos.add(folksonomy);
201 
202             return (List<TagsEntry>) QueryUtil.list(
203                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
204         }
205         catch (Exception e) {
206             throw new SystemException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     public TagsEntry findByG_N_F(long groupId, String name, boolean folksonomy)
214         throws NoSuchEntryException, SystemException {
215 
216         name = name.trim().toLowerCase();
217 
218         Session session = null;
219 
220         try {
221             session = openSession();
222 
223             String sql = CustomSQLUtil.get(FIND_BY_G_N_F);
224 
225             SQLQuery q = session.createSQLQuery(sql);
226 
227             q.addEntity("TagsEntry", TagsEntryImpl.class);
228 
229             QueryPos qPos = QueryPos.getInstance(q);
230 
231             qPos.add(groupId);
232             qPos.add(name);
233             qPos.add(folksonomy);
234 
235             List<TagsEntry> list = q.list();
236 
237             if (list.size() == 0) {
238                 StringBuilder sb = new StringBuilder();
239 
240                 sb.append("No TagsEntry exists with the key ");
241                 sb.append("{groupId=");
242                 sb.append(groupId);
243                 sb.append(", name=");
244                 sb.append(name);
245                 sb.append(", folksonomy=");
246                 sb.append(folksonomy);
247                 sb.append("}");
248 
249                 throw new NoSuchEntryException(sb.toString());
250             }
251             else {
252                 return list.get(0);
253             }
254         }
255         catch (NoSuchEntryException nsee) {
256             throw nsee;
257         }
258         catch (Exception e) {
259             throw new SystemException(e);
260         }
261         finally {
262             closeSession(session);
263         }
264     }
265 
266     public List<TagsEntry> findByC_C_F(
267             long classNameId, long classPK, boolean folksonomy)
268         throws SystemException {
269 
270         Session session = null;
271 
272         try {
273             session = openSession();
274 
275             String sql = CustomSQLUtil.get(FIND_BY_C_C_F);
276 
277             SQLQuery q = session.createSQLQuery(sql);
278 
279             q.addEntity("TagsEntry", TagsEntryImpl.class);
280 
281             QueryPos qPos = QueryPos.getInstance(q);
282 
283             qPos.add(classNameId);
284             qPos.add(classPK);
285             qPos.add(folksonomy);
286 
287             return (List<TagsEntry>) QueryUtil.list(
288                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
289         }
290         catch (Exception e) {
291             throw new SystemException(e);
292         }
293         finally {
294             closeSession(session);
295         }
296     }
297 
298     public List<TagsEntry> findByG_C_N_F(
299             long groupId, long classNameId, String name, boolean folksonomy)
300         throws SystemException {
301 
302         return findByG_C_N_F(
303             groupId, classNameId, name, folksonomy, QueryUtil.ALL_POS,
304             QueryUtil.ALL_POS);
305     }
306 
307     public List<TagsEntry> findByG_C_N_F(
308             long groupId, long classNameId, String name, boolean folksonomy,
309             int start, int end)
310         throws SystemException {
311 
312         Session session = null;
313 
314         try {
315             session = openSession();
316 
317             String sql = CustomSQLUtil.get(FIND_BY_G_C_N_F);
318 
319             SQLQuery q = session.createSQLQuery(sql);
320 
321             q.addEntity("TagsEntry", TagsEntryImpl.class);
322 
323             QueryPos qPos = QueryPos.getInstance(q);
324 
325             qPos.add(groupId);
326             qPos.add(classNameId);
327             qPos.add(name);
328             qPos.add(name);
329             qPos.add(folksonomy);
330 
331             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
332         }
333         catch (Exception e) {
334             throw new SystemException(e);
335         }
336         finally {
337             closeSession(session);
338         }
339     }
340 
341     public List<TagsEntry> findByG_N_F_P(
342             long groupId, String name, boolean folksonomy, String[] properties)
343         throws SystemException {
344 
345         return findByG_N_F_P(
346             groupId, name, folksonomy, properties, QueryUtil.ALL_POS,
347             QueryUtil.ALL_POS);
348     }
349 
350     public List<TagsEntry> findByG_N_F_P(
351             long groupId, String name, boolean folksonomy, String[] properties,
352             int start, int end)
353         throws SystemException {
354 
355         Session session = null;
356 
357         try {
358             session = openSession();
359 
360             String sql = CustomSQLUtil.get(FIND_BY_G_N_F_P);
361 
362             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
363 
364             SQLQuery q = session.createSQLQuery(sql);
365 
366             q.addEntity("TagsEntry", TagsEntryImpl.class);
367 
368             QueryPos qPos = QueryPos.getInstance(q);
369 
370             setJoin(qPos, properties);
371             qPos.add(groupId);
372             qPos.add(name);
373             qPos.add(name);
374             qPos.add(folksonomy);
375 
376             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
377         }
378         catch (Exception e) {
379             throw new SystemException(e);
380         }
381         finally {
382             closeSession(session);
383         }
384     }
385 
386     protected String getJoin(String[] properties) {
387         if (properties.length == 0) {
388             return StringPool.BLANK;
389         }
390         else {
391             StringBuilder sb = new StringBuilder();
392 
393             sb.append(" INNER JOIN TagsProperty ON ");
394             sb.append(" (TagsProperty.entryId = TagsEntry.entryId) AND ");
395 
396             for (int i = 0; i < properties.length; i++) {
397                 sb.append("(TagsProperty.key_ = ? AND ");
398                 sb.append("TagsProperty.value = ?) ");
399 
400                 if ((i + 1) < properties.length) {
401                     sb.append(" AND ");
402                 }
403             }
404 
405             return sb.toString();
406         }
407     }
408 
409     protected void setJoin(QueryPos qPos, String[] properties) {
410         for (int i = 0; i < properties.length; i++) {
411             String[] property = StringUtil.split(
412                 properties[i], StringPool.COLON);
413 
414             String key = StringPool.BLANK;
415 
416             if (property.length > 0) {
417                 key = GetterUtil.getString(property[0]);
418             }
419 
420             String value = StringPool.BLANK;
421 
422             if (property.length > 1) {
423                 value = GetterUtil.getString(property[1]);
424             }
425 
426             qPos.add(key);
427             qPos.add(value);
428         }
429     }
430 
431 }