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