001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.blogs.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.OrderByComparator;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
028: import com.liferay.portal.spring.hibernate.HibernateUtil;
029: import com.liferay.portlet.blogs.model.impl.BlogsStatsUserImpl;
030: import com.liferay.util.dao.hibernate.QueryPos;
031: import com.liferay.util.dao.hibernate.QueryUtil;
032:
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: import org.hibernate.Hibernate;
038: import org.hibernate.SQLQuery;
039: import org.hibernate.Session;
040:
041: /**
042: * <a href="BlogsStatsUserFinderImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class BlogsStatsUserFinderImpl implements BlogsStatsUserFinder {
048:
049: public static String COUNT_BY_ORGANIZATION_IDS = BlogsStatsUserFinder.class
050: .getName()
051: + ".countByOrganizationIds";
052:
053: public static String FIND_BY_ORGANIZATION_IDS = BlogsStatsUserFinder.class
054: .getName()
055: + ".findByOrganizationIds";
056:
057: public int countByOrganizationId(long organizationId)
058: throws SystemException {
059:
060: List organizationIds = new ArrayList();
061:
062: organizationIds.add(new Long(organizationId));
063:
064: return countByOrganizationIds(organizationIds);
065: }
066:
067: public int countByOrganizationIds(List organizationIds)
068: throws SystemException {
069:
070: Session session = null;
071:
072: try {
073: session = HibernateUtil.openSession();
074:
075: String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
076:
077: sql = StringUtil.replace(sql, "[$ORGANIZATION_ID$]",
078: getOrganizationIds(organizationIds));
079:
080: SQLQuery q = session.createSQLQuery(sql);
081:
082: q.addScalar(HibernateUtil.getCountColumnName(),
083: Hibernate.LONG);
084:
085: QueryPos qPos = QueryPos.getInstance(q);
086:
087: for (int i = 0; i < organizationIds.size(); i++) {
088: Long organizationId = (Long) organizationIds.get(i);
089:
090: qPos.add(organizationId);
091: }
092:
093: Iterator itr = q.list().iterator();
094:
095: if (itr.hasNext()) {
096: Long count = (Long) itr.next();
097:
098: if (count != null) {
099: return count.intValue();
100: }
101: }
102:
103: return 0;
104: } catch (Exception e) {
105: throw new SystemException(e);
106: } finally {
107: HibernateUtil.closeSession(session);
108: }
109: }
110:
111: public List findByOrganizationId(long organizationId, int begin,
112: int end, OrderByComparator obc) throws SystemException {
113:
114: List organizationIds = new ArrayList();
115:
116: organizationIds.add(new Long(organizationId));
117:
118: return findByOrganizationIds(organizationIds, begin, end, obc);
119: }
120:
121: public List findByOrganizationIds(List organizationIds, int begin,
122: int end, OrderByComparator obc) throws SystemException {
123:
124: Session session = null;
125:
126: try {
127: session = HibernateUtil.openSession();
128:
129: String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
130:
131: sql = StringUtil.replace(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 = (Long) organizationIds.get(i);
143:
144: qPos.add(organizationId);
145: }
146:
147: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
148: end);
149: } catch (Exception e) {
150: throw new SystemException(e);
151: } finally {
152: HibernateUtil.closeSession(session);
153: }
154: }
155:
156: protected String getOrganizationIds(List organizationIds) {
157: StringMaker sm = new StringMaker();
158:
159: for (int i = 0; i < organizationIds.size(); i++) {
160: sm.append("Users_Orgs.organizationId = ? ");
161:
162: if ((i + 1) != organizationIds.size()) {
163: sm.append("OR ");
164: }
165: }
166:
167: return sm.toString();
168: }
169:
170: }
|