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