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