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