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