1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.blogs.model.BlogsStatsUser;
29  import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="BlogsStatsUserFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class BlogsStatsUserFinderImpl
42      extends BasePersistenceImpl<BlogsStatsUser>
43      implements BlogsStatsUserFinder {
44  
45      public static String COUNT_BY_ORGANIZATION_IDS =
46          BlogsStatsUserFinder.class.getName() + ".countByOrganizationIds";
47  
48      public static String FIND_BY_ORGANIZATION_IDS =
49          BlogsStatsUserFinder.class.getName() + ".findByOrganizationIds";
50  
51      public int countByOrganizationId(long organizationId)
52          throws SystemException {
53  
54          List<Long> organizationIds = new ArrayList<Long>();
55  
56          organizationIds.add(organizationId);
57  
58          return countByOrganizationIds(organizationIds);
59      }
60  
61      public int countByOrganizationIds(List<Long> organizationIds)
62          throws SystemException {
63  
64          Session session = null;
65  
66          try {
67              session = openSession();
68  
69              String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
70  
71              sql = StringUtil.replace(
72                  sql, "[$ORGANIZATION_ID$]",
73                  getOrganizationIds(organizationIds));
74  
75              SQLQuery q = session.createSQLQuery(sql);
76  
77              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
78  
79              QueryPos qPos = QueryPos.getInstance(q);
80  
81              for (int i = 0; i < organizationIds.size(); i++) {
82                  Long organizationId = organizationIds.get(i);
83  
84                  qPos.add(organizationId);
85              }
86  
87              Iterator<Long> itr = q.list().iterator();
88  
89              if (itr.hasNext()) {
90                  Long count = 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             closeSession(session);
104         }
105     }
106 
107     public List<BlogsStatsUser> findByOrganizationId(
108             long organizationId, int start, int end, OrderByComparator obc)
109         throws SystemException {
110 
111         List<Long> organizationIds = new ArrayList<Long>();
112 
113         organizationIds.add(organizationId);
114 
115         return findByOrganizationIds(organizationIds, start, end, obc);
116     }
117 
118     public List<BlogsStatsUser> findByOrganizationIds(
119             List<Long> organizationIds, int start, int end,
120             OrderByComparator obc)
121         throws SystemException {
122 
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
129 
130             sql = StringUtil.replace(
131                 sql, "[$ORGANIZATION_ID$]",
132                 getOrganizationIds(organizationIds));
133             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
134 
135             SQLQuery q = session.createSQLQuery(sql);
136 
137             q.addEntity("BlogsStatsUser", BlogsStatsUserImpl.class);
138 
139             QueryPos qPos = QueryPos.getInstance(q);
140 
141             for (int i = 0; i < organizationIds.size(); i++) {
142                 Long organizationId = organizationIds.get(i);
143 
144                 qPos.add(organizationId);
145             }
146 
147             return (List<BlogsStatsUser>)QueryUtil.list(
148                 q, getDialect(), start, end);
149         }
150         catch (Exception e) {
151             throw new SystemException(e);
152         }
153         finally {
154             closeSession(session);
155         }
156     }
157 
158     protected String getOrganizationIds(List<Long> organizationIds) {
159         if (organizationIds.isEmpty()) {
160             return StringPool.BLANK;
161         }
162 
163         StringBundler sb = new StringBundler(organizationIds.size() * 2 - 1);
164 
165         for (int i = 0; i < organizationIds.size(); i++) {
166             sb.append("Users_Orgs.organizationId = ? ");
167 
168             if ((i + 1) != organizationIds.size()) {
169                 sb.append("OR ");
170             }
171         }
172 
173         return sb.toString();
174     }
175 
176 }