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.model.TagsEntry;
28  import com.liferay.portlet.tags.model.impl.TagsEntryImpl;
29  import com.liferay.util.dao.orm.CustomSQLUtil;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="TagsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class TagsEntryFinderImpl
40      extends BasePersistenceImpl<TagsEntry> implements TagsEntryFinder {
41  
42      public static String COUNT_BY_C_N_P =
43          TagsEntryFinder.class.getName() + ".countByC_N_P";
44  
45      public static String COUNT_BY_G_C_C_N =
46          TagsEntryFinder.class.getName() + ".countByG_C_C_N";
47  
48      public static String FIND_BY_C_C =
49          TagsEntryFinder.class.getName() + ".findByC_C";
50  
51      public static String FIND_BY_C_N_P =
52          TagsEntryFinder.class.getName() + ".findByC_N_P";
53  
54      public static String FIND_BY_G_C_C_N =
55          TagsEntryFinder.class.getName() + ".findByG_C_C_N";
56  
57      public int countByC_N_P(long companyId, String name, String[] properties)
58          throws SystemException {
59  
60          Session session = null;
61  
62          try {
63              session = openSession();
64  
65              String sql = CustomSQLUtil.get(COUNT_BY_C_N_P);
66  
67              sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
68  
69              SQLQuery q = session.createSQLQuery(sql);
70  
71              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
72  
73              QueryPos qPos = QueryPos.getInstance(q);
74  
75              setJoin(qPos, properties);
76              qPos.add(companyId);
77              qPos.add(name);
78              qPos.add(name);
79  
80              Iterator<Long> itr = q.list().iterator();
81  
82              if (itr.hasNext()) {
83                  Long count = itr.next();
84  
85                  if (count != null) {
86                      return count.intValue();
87                  }
88              }
89  
90              return 0;
91          }
92          catch (Exception e) {
93              throw new SystemException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public int countByG_C_C_N(
101             long groupId, long companyId, long classNameId, String name)
102         throws SystemException {
103 
104         Session session = null;
105 
106         try {
107             session = openSession();
108 
109             String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_N);
110 
111             SQLQuery q = session.createSQLQuery(sql);
112 
113             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
114 
115             QueryPos qPos = QueryPos.getInstance(q);
116 
117             qPos.add(groupId);
118             qPos.add(companyId);
119             qPos.add(classNameId);
120             qPos.add(name);
121             qPos.add(name);
122 
123             Iterator<Long> itr = q.list().iterator();
124 
125             if (itr.hasNext()) {
126                 Long count = itr.next();
127 
128                 if (count != null) {
129                     return count.intValue();
130                 }
131             }
132 
133             return 0;
134         }
135         catch (Exception e) {
136             throw new SystemException(e);
137         }
138         finally {
139             closeSession(session);
140         }
141     }
142 
143     public List<TagsEntry> findByC_C(long classNameId, long classPK)
144         throws SystemException {
145 
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             String sql = CustomSQLUtil.get(FIND_BY_C_C);
152 
153             SQLQuery q = session.createSQLQuery(sql);
154 
155             q.addEntity("TagsEntry", TagsEntryImpl.class);
156 
157             QueryPos qPos = QueryPos.getInstance(q);
158 
159             qPos.add(classNameId);
160             qPos.add(classPK);
161 
162             return (List<TagsEntry>) QueryUtil.list(
163                 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
164         }
165         catch (Exception e) {
166             throw new SystemException(e);
167         }
168         finally {
169             closeSession(session);
170         }
171     }
172 
173     public List<TagsEntry> findByC_N_P(
174             long companyId, String name, String[] properties)
175         throws SystemException {
176 
177         return findByC_N_P(
178             companyId, name, properties, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
179     }
180 
181     public List<TagsEntry> findByC_N_P(
182             long companyId, String name, String[] properties, int start,
183             int end)
184         throws SystemException {
185 
186         Session session = null;
187 
188         try {
189             session = openSession();
190 
191             String sql = CustomSQLUtil.get(FIND_BY_C_N_P);
192 
193             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(properties));
194 
195             SQLQuery q = session.createSQLQuery(sql);
196 
197             q.addEntity("TagsEntry", TagsEntryImpl.class);
198 
199             QueryPos qPos = QueryPos.getInstance(q);
200 
201             setJoin(qPos, properties);
202             qPos.add(companyId);
203             qPos.add(name);
204             qPos.add(name);
205 
206             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
207         }
208         catch (Exception e) {
209             throw new SystemException(e);
210         }
211         finally {
212             closeSession(session);
213         }
214     }
215 
216     public List<TagsEntry> findByG_C_C_N(
217             long groupId, long companyId, long classNameId, String name)
218         throws SystemException {
219 
220         return findByG_C_C_N(
221             groupId, companyId, classNameId, name, QueryUtil.ALL_POS,
222             QueryUtil.ALL_POS);
223     }
224 
225     public List<TagsEntry> findByG_C_C_N(
226             long groupId, long companyId, long classNameId, String name,
227             int start, int end)
228         throws SystemException {
229 
230         Session session = null;
231 
232         try {
233             session = openSession();
234 
235             String sql = CustomSQLUtil.get(FIND_BY_G_C_C_N);
236 
237             SQLQuery q = session.createSQLQuery(sql);
238 
239             q.addEntity("TagsEntry", TagsEntryImpl.class);
240 
241             QueryPos qPos = QueryPos.getInstance(q);
242 
243             qPos.add(groupId);
244             qPos.add(companyId);
245             qPos.add(classNameId);
246             qPos.add(name);
247             qPos.add(name);
248 
249             return (List<TagsEntry>)QueryUtil.list(q, getDialect(), start, end);
250         }
251         catch (Exception e) {
252             throw new SystemException(e);
253         }
254         finally {
255             closeSession(session);
256         }
257     }
258 
259     protected String getJoin(String[] properties) {
260         if ((properties == null) || (properties.length == 0)) {
261             return StringPool.BLANK;
262         }
263         else {
264             StringBuilder sb = new StringBuilder();
265 
266             sb.append(" INNER JOIN TagsProperty ON ");
267             sb.append(" (TagsProperty.entryId = TagsEntry.entryId) AND ");
268 
269             for (int i = 0; i < properties.length; i++) {
270                 sb.append("(TagsProperty.key_ = ? AND ");
271                 sb.append("TagsProperty.value = ?) ");
272 
273                 if ((i + 1) < properties.length) {
274                     sb.append(" AND ");
275                 }
276             }
277 
278             return sb.toString();
279         }
280     }
281 
282     protected void setJoin(QueryPos qPos, String[] properties) {
283         if ((properties == null) || (properties.length == 0)) {
284             return;
285         }
286 
287         for (int i = 0; i < properties.length; i++) {
288             String[] property = StringUtil.split(
289                 properties[i], StringPool.COLON);
290 
291             String key = StringPool.BLANK;
292 
293             if (property.length > 0) {
294                 key = GetterUtil.getString(property[0]);
295             }
296 
297             String value = StringPool.BLANK;
298 
299             if (property.length > 1) {
300                 value = GetterUtil.getString(property[1]);
301             }
302 
303             qPos.add(key);
304             qPos.add(value);
305         }
306     }
307 
308 }