1   /**
2    * Copyright (c) 2000-2007 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.blogs.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29  import com.liferay.portal.spring.hibernate.HibernateUtil;
30  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
31  import com.liferay.util.dao.hibernate.QueryPos;
32  import com.liferay.util.dao.hibernate.QueryUtil;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import org.hibernate.Hibernate;
39  import org.hibernate.SQLQuery;
40  import org.hibernate.Session;
41  
42  /**
43   * <a href="BlogsEntryFinder.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class BlogsEntryFinder {
49  
50      public static String COUNT_BY_CATEGORY_IDS =
51          BlogsEntryFinder.class.getName() + ".countByCategoryIds";
52  
53      public static String COUNT_BY_ORGANIZATION_IDS =
54          BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
55  
56      public static String FIND_BY_ORGANIZATION_IDS =
57          BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
58  
59      public static String FIND_BY_NO_ASSETS =
60          BlogsEntryFinder.class.getName() + ".findByNoAssets";
61  
62      public static int countByCategoryIds(List categoryIds)
63          throws SystemException {
64  
65          Session session = null;
66  
67          try {
68              session = HibernateUtil.openSession();
69  
70              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
71  
72              sql = StringUtil.replace(
73                  sql, "[$CATEGORY_ID$]", _getCategoryIds(categoryIds));
74  
75              SQLQuery q = session.createSQLQuery(sql);
76  
77              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
78  
79              QueryPos qPos = QueryPos.getInstance(q);
80  
81              for (int i = 0; i < categoryIds.size(); i++) {
82                  Long categoryId = (Long)categoryIds.get(i);
83  
84                  qPos.add(categoryId);
85              }
86  
87              Iterator itr = q.list().iterator();
88  
89              if (itr.hasNext()) {
90                  Long count = (Long)itr.next();
91  
92                  if (count != null) {
93                      return count.intValue();
94                  }
95              }
96  
97              return 0;
98          }
99          catch (Exception e) {
100             throw new SystemException(e);
101         }
102         finally {
103             HibernateUtil.closeSession(session);
104         }
105     }
106 
107     public static int countByOrganizationId(long organizationId)
108         throws SystemException {
109 
110         List organizationIds = new ArrayList();
111 
112         organizationIds.add(new Long(organizationId));
113 
114         return countByOrganizationIds(organizationIds);
115     }
116 
117     public static int countByOrganizationIds(List organizationIds)
118         throws SystemException {
119 
120         Session session = null;
121 
122         try {
123             session = HibernateUtil.openSession();
124 
125             String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
126 
127             sql = StringUtil.replace(
128                 sql, "[$ORGANIZATION_ID$]",
129                 _getOrganizationIds(organizationIds));
130 
131             SQLQuery q = session.createSQLQuery(sql);
132 
133             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
134 
135             QueryPos qPos = QueryPos.getInstance(q);
136 
137             for (int i = 0; i < organizationIds.size(); i++) {
138                 Long organizationId = (Long)organizationIds.get(i);
139 
140                 qPos.add(organizationId);
141             }
142 
143             Iterator itr = q.list().iterator();
144 
145             if (itr.hasNext()) {
146                 Long count = (Long)itr.next();
147 
148                 if (count != null) {
149                     return count.intValue();
150                 }
151             }
152 
153             return 0;
154         }
155         catch (Exception e) {
156             throw new SystemException(e);
157         }
158         finally {
159             HibernateUtil.closeSession(session);
160         }
161     }
162 
163     public static List findByOrganizationId(
164             long organizationId, int begin, int end)
165         throws SystemException {
166 
167         List organizationIds = new ArrayList();
168 
169         organizationIds.add(new Long(organizationId));
170 
171         return findByOrganizationIds(organizationIds, begin, end);
172     }
173 
174     public static List findByOrganizationIds(
175             List organizationIds, int begin, int end)
176         throws SystemException {
177 
178         Session session = null;
179 
180         try {
181             session = HibernateUtil.openSession();
182 
183             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
184 
185             sql = StringUtil.replace(
186                 sql, "[$ORGANIZATION_ID$]",
187                 _getOrganizationIds(organizationIds));
188 
189             SQLQuery q = session.createSQLQuery(sql);
190 
191             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
192 
193             QueryPos qPos = QueryPos.getInstance(q);
194 
195             for (int i = 0; i < organizationIds.size(); i++) {
196                 Long organizationId = (Long)organizationIds.get(i);
197 
198                 qPos.add(organizationId);
199             }
200 
201             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
202         }
203         catch (Exception e) {
204             throw new SystemException(e);
205         }
206         finally {
207             HibernateUtil.closeSession(session);
208         }
209     }
210 
211     public static List findByNoAssets() throws SystemException {
212         Session session = null;
213 
214         try {
215             session = HibernateUtil.openSession();
216 
217             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
218 
219             SQLQuery q = session.createSQLQuery(sql);
220 
221             q.addEntity("BlogsEntry", BlogsEntryImpl.class);
222 
223             return q.list();
224         }
225         catch (Exception e) {
226             throw new SystemException(e);
227         }
228         finally {
229             HibernateUtil.closeSession(session);
230         }
231     }
232 
233     private static String _getCategoryIds(List categoryIds) {
234         StringMaker sm = new StringMaker();
235 
236         for (int i = 0; i < categoryIds.size(); i++) {
237             sm.append("categoryId = ? ");
238 
239             if ((i + 1) != categoryIds.size()) {
240                 sm.append("OR ");
241             }
242         }
243 
244         return sm.toString();
245     }
246 
247     private static String _getOrganizationIds(List organizationIds) {
248         StringMaker sm = new StringMaker();
249 
250         for (int i = 0; i < organizationIds.size(); i++) {
251             sm.append("Users_Orgs.organizationId = ? ");
252 
253             if ((i + 1) != organizationIds.size()) {
254                 sm.append("OR ");
255             }
256         }
257 
258         return sm.toString();
259     }
260 
261 }