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