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.blogs.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.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.blogs.model.BlogsStatsUser;
32  import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
33  import com.liferay.util.dao.orm.CustomSQLUtil;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="BlogsStatsUserFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class BlogsStatsUserFinderImpl
46      extends BasePersistenceImpl implements BlogsStatsUserFinder {
47  
48      public static String COUNT_BY_ORGANIZATION_IDS =
49          BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
50  
51      public static String FIND_BY_ORGANIZATION_IDS =
52          BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
53  
54      public int countByOrganizationId(long organizationId)
55          throws SystemException {
56  
57          List<Long> organizationIds = new ArrayList<Long>();
58  
59          organizationIds.add(organizationId);
60  
61          return countByOrganizationIds(organizationIds);
62      }
63  
64      public int countByOrganizationIds(List<Long> organizationIds)
65          throws SystemException {
66  
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
73  
74              sql = StringUtil.replace(
75                  sql, "[$ORGANIZATION_ID$]",
76                  getOrganizationIds(organizationIds));
77  
78              SQLQuery q = session.createSQLQuery(sql);
79  
80              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
81  
82              QueryPos qPos = QueryPos.getInstance(q);
83  
84              for (int i = 0; i < organizationIds.size(); i++) {
85                  Long organizationId = organizationIds.get(i);
86  
87                  qPos.add(organizationId);
88              }
89  
90              Iterator<Long> itr = q.list().iterator();
91  
92              if (itr.hasNext()) {
93                  Long count = itr.next();
94  
95                  if (count != null) {
96                      return count.intValue();
97                  }
98              }
99  
100             return 0;
101         }
102         catch (Exception e) {
103             throw new SystemException(e);
104         }
105         finally {
106             closeSession(session);
107         }
108     }
109 
110     public List<BlogsStatsUser> findByOrganizationId(
111             long organizationId, int start, int end, OrderByComparator obc)
112         throws SystemException {
113 
114         List<Long> organizationIds = new ArrayList<Long>();
115 
116         organizationIds.add(organizationId);
117 
118         return findByOrganizationIds(organizationIds, start, end, obc);
119     }
120 
121     public List<BlogsStatsUser> findByOrganizationIds(
122             List<Long> organizationIds, int start, int end,
123             OrderByComparator obc)
124         throws SystemException {
125 
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
132 
133             sql = StringUtil.replace(
134                 sql, "[$ORGANIZATION_ID$]",
135                 getOrganizationIds(organizationIds));
136             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
137 
138             SQLQuery q = session.createSQLQuery(sql);
139 
140             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
141 
142             QueryPos qPos = QueryPos.getInstance(q);
143 
144             for (int i = 0; i < organizationIds.size(); i++) {
145                 Long organizationId = organizationIds.get(i);
146 
147                 qPos.add(organizationId);
148             }
149 
150             return (List<BlogsStatsUser>)QueryUtil.list(
151                 q, getDialect(), start, end);
152         }
153         catch (Exception e) {
154             throw new SystemException(e);
155         }
156         finally {
157             closeSession(session);
158         }
159     }
160 
161     protected String getOrganizationIds(List<Long> organizationIds) {
162         StringBuilder sb = new StringBuilder();
163 
164         for (int i = 0; i < organizationIds.size(); i++) {
165             sb.append("Users_Orgs.organizationId = ? ");
166 
167             if ((i + 1) != organizationIds.size()) {
168                 sb.append("OR ");
169             }
170         }
171 
172         return sb.toString();
173     }
174 
175 }