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