1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
35  import com.liferay.portlet.tags.NoSuchEntryException;
36  import com.liferay.portlet.tags.model.TagsEntry;
37  import com.liferay.portlet.tags.model.impl.TagsEntryImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="TagsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Bruno Farache
48   *
49   */
50  public class TagsEntryFinderImpl
51      extends BasePersistenceImpl implements TagsEntryFinder {
52  
53      public static String COUNT_BY_G_C_N_F =
54          TagsEntryFinder.class.getName() + ".countByG_C_N_F";
55  
56      public static String COUNT_BY_G_N_F_P =
57          TagsEntryFinder.class.getName() + ".countByG_N_F_P";
58  
59      public static String FIND_BY_FOLKSONOMY =
60          TagsEntryFinder.class.getName() + ".findByFolksonomy";
61  
62      public static String FIND_BY_A_F =
63          TagsEntryFinder.class.getName() + ".findByA_F";
64  
65      public static String FIND_BY_G_N_F =
66          TagsEntryFinder.class.getName() + ".findByG_N_F";
67  
68      public static String FIND_BY_G_C_N_F =
69          TagsEntryFinder.class.getName() + ".findByG_C_N_F";
70  
71      public static String FIND_BY_G_N_F_P =
72          TagsEntryFinder.class.getName() + ".findByG_N_F_P";
73  
74      public int countByG_C_N_F(
75              long groupId, long classNameId, String name, boolean folksonomy)
76          throws SystemException {
77  
78          Session session = null;
79  
80          try {
81              session = openSession();
82  
83              String sql = CustomSQLUtil.get(COUNT_BY_G_C_N_F);
84  
85              SQLQuery q = session.createSQLQuery(sql);
86  
87              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
88  
89              QueryPos qPos = QueryPos.getInstance(q);
90  
91              qPos.add(groupId);
92              qPos.add(classNameId);
93              qPos.add(name);
94              qPos.add(name);
95              qPos.add(folksonomy);
96  
97              Iterator<Long> itr = q.list().iterator();
98  
99              if (itr.hasNext()) {
100                 Long count = itr.next();
101 
102                 if (count != null) {
103                     return count.intValue();
104                 }
105             }
106 
107             return 0;
108         }
109         catch (Exception e) {
110             throw new SystemException(e);
111         }
112         finally {
113             closeSession(session);
114         }
115     }
116 
117     public int countByG_N_F_P(
118             long groupId, String name, boolean folksonomy, String[] properties)
119         throws SystemException {
120 
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             String sql = CustomSQLUtil.get(COUNT_BY_G_N_F_P);
127 
128             SQLQuery q = session.createSQLQuery(sql);
129 
130             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
131 
132             QueryPos qPos = QueryPos.getInstance(q);
133 
134             setJoin(qPos, properties);
135             qPos.add(groupId);
136             qPos.add(name);
137             qPos.add(name);
138             qPos.add(folksonomy);
139 
140             Iterator<Long> itr = q.list().iterator();
141 
142             if (itr.hasNext()) {
143                 Long count = itr.next();
144 
145                 if (count != null) {
146                     return count.intValue();
147                 }
148             }
149 
150             return 0;
151         }
152         catch (Exception e) {
153             throw new SystemException(e);
154         }
155         finally {
156             closeSession(session);
157         }
158     }
159 
160     public List<TagsEntry> findByFolksonomy(boolean folksonomy)
161         throws SystemException {
162 
163         Session session = null;
164 
165         try {
166             session = openSession();
167 
168             String sql = CustomSQLUtil.get(FIND_BY_FOLKSONOMY);
169 
170             SQLQuery q = session.createSQLQuery(sql);
171 
172             q.addEntity("TagsEntry", TagsEntryImpl.class);
173 
174             QueryPos qPos = QueryPos.getInstance(q);
175 
176             qPos.add(folksonomy);
177 
178             return (List<TagsEntry>) QueryUtil.list(
179                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
180         }
181         catch (Exception e) {
182             throw new SystemException(e);
183         }
184         finally {
185             closeSession(session);
186         }
187     }
188 
189     public List<TagsEntry> findByA_F(long assetId, boolean folksonomy)
190         throws SystemException {
191 
192         Session session = null;
193 
194         try {
195             session = openSession();
196 
197             String sql = CustomSQLUtil.get(FIND_BY_A_F);
198 
199             SQLQuery q = session.createSQLQuery(sql);
200 
201             q.addEntity("TagsEntry", TagsEntryImpl.class);
202 
203             QueryPos qPos = QueryPos.getInstance(q);
204 
205             qPos.add(assetId);
206             qPos.add(folksonomy);
207 
208             return (List<TagsEntry>) QueryUtil.list(
209                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
210         }
211         catch (Exception e) {
212             throw new SystemException(e);
213         }
214         finally {
215             closeSession(session);
216         }
217     }
218 
219     public TagsEntry findByG_N_F(long groupId, String name, boolean folksonomy)
220         throws NoSuchEntryException, SystemException {
221 
222         name = name.trim().toLowerCase();
223 
224         Session session = null;
225 
226         try {
227             session = openSession();
228 
229             String sql = CustomSQLUtil.get(FIND_BY_G_N_F);
230 
231             SQLQuery q = session.createSQLQuery(sql);
232 
233             q.addEntity("TagsEntry", TagsEntryImpl.class);
234 
235             QueryPos qPos = QueryPos.getInstance(q);
236 
237             qPos.add(groupId);
238             qPos.add(name);
239             qPos.add(folksonomy);
240 
241             List<TagsEntry> list = q.list();
242 
243             if (list.size() == 0) {
244                 StringBuilder sb = new StringBuilder();
245 
246                 sb.append("No TagsEntry exists with the key ");
247                 sb.append("{groupId=");
248                 sb.append(groupId);
249                 sb.append(", name=");
250                 sb.append(name);
251                 sb.append(", folksonomy=");
252                 sb.append(folksonomy);
253                 sb.append("}");
254 
255                 throw new NoSuchEntryException(sb.toString());
256             }
257             else {
258                 return list.get(0);
259             }
260         }
261         catch (NoSuchEntryException nsee) {
262             throw nsee;
263         }
264         catch (Exception e) {
265             throw new SystemException(e);
266         }
267         finally {
268             closeSession(session);
269         }
270     }
271 
272     public List<TagsEntry> findByG_C_N_F(
273             long groupId, long classNameId, String name, boolean folksonomy)
274         throws SystemException {
275 
276         return findByG_C_N_F(
277             groupId, classNameId, name, folksonomy, QueryUtil.ALL_POS,
278             QueryUtil.ALL_POS);
279     }
280 
281     public List<TagsEntry> findByG_C_N_F(
282             long groupId, long classNameId, String name, boolean folksonomy,
283             int start, int end)
284         throws SystemException {
285 
286         Session session = null;
287 
288         try {
289             session = openSession();
290 
291             String sql = CustomSQLUtil.get(FIND_BY_G_C_N_F);
292 
293             SQLQuery q = session.createSQLQuery(sql);
294 
295             q.addEntity("TagsEntry", TagsEntryImpl.class);
296 
297             QueryPos qPos = QueryPos.getInstance(q);
298 
299             qPos.add(groupId);
300             qPos.add(classNameId);
301             qPos.add(name);
302             qPos.add(name);
303             qPos.add(folksonomy);
304 
305             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
306         }
307         catch (Exception e) {
308             throw new SystemException(e);
309         }
310         finally {
311             closeSession(session);
312         }
313     }
314 
315     public List<TagsEntry> findByG_N_F_P(
316             long groupId, String name, boolean folksonomy, String[] properties)
317         throws SystemException {
318 
319         return findByG_N_F_P(
320             groupId, name, folksonomy, properties, QueryUtil.ALL_POS,
321             QueryUtil.ALL_POS);
322     }
323 
324     public List<TagsEntry> findByG_N_F_P(
325             long groupId, String name, boolean folksonomy, String[] properties,
326             int start, int end)
327         throws SystemException {
328 
329         Session session = null;
330 
331         try {
332             session = openSession();
333 
334             String sql = CustomSQLUtil.get(FIND_BY_G_N_F_P);
335 
336             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
337 
338             SQLQuery q = session.createSQLQuery(sql);
339 
340             q.addEntity("TagsEntry", TagsEntryImpl.class);
341 
342             QueryPos qPos = QueryPos.getInstance(q);
343 
344             setJoin(qPos, properties);
345             qPos.add(groupId);
346             qPos.add(name);
347             qPos.add(name);
348             qPos.add(folksonomy);
349 
350             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
351         }
352         catch (Exception e) {
353             throw new SystemException(e);
354         }
355         finally {
356             closeSession(session);
357         }
358     }
359 
360     protected String getJoin(String[] properties) {
361         if (properties.length == 0) {
362             return StringPool.BLANK;
363         }
364         else {
365             StringBuilder sb = new StringBuilder();
366 
367             sb.append(" INNER JOIN TagsProperty ON ");
368             sb.append(" (TagsProperty.entryId = TagsEntry.entryId) AND ");
369 
370             for (int i = 0; i < properties.length; i++) {
371                 sb.append("(TagsProperty.key_ = ? AND ");
372                 sb.append("TagsProperty.value = ?) ");
373 
374                 if ((i + 1) < properties.length) {
375                     sb.append(" AND ");
376                 }
377             }
378 
379             return sb.toString();
380         }
381     }
382 
383     protected void setJoin(QueryPos qPos, String[] properties) {
384         for (int i = 0; i < properties.length; i++) {
385             String[] property = StringUtil.split(
386                 properties[i], StringPool.COLON);
387 
388             String key = StringPool.BLANK;
389 
390             if (property.length > 0) {
391                 key = GetterUtil.getString(property[0]);
392             }
393 
394             String value = StringPool.BLANK;
395 
396             if (property.length > 1) {
397                 value = GetterUtil.getString(property[1]);
398             }
399 
400             qPos.add(key);
401             qPos.add(value);
402         }
403     }
404 
405 }