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.StringMaker;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
027: import com.liferay.portal.spring.hibernate.HibernateUtil;
028: import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
029: import com.liferay.util.dao.hibernate.QueryPos;
030: import com.liferay.util.dao.hibernate.QueryUtil;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: import org.hibernate.Hibernate;
037: import org.hibernate.SQLQuery;
038: import org.hibernate.Session;
039:
040: /**
041: * <a href="BlogsEntryFinderImpl.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class BlogsEntryFinderImpl implements BlogsEntryFinder {
047:
048: public static String COUNT_BY_ORGANIZATION_IDS = BlogsEntryFinder.class
049: .getName()
050: + ".countByOrganizationIds";
051:
052: public static String FIND_BY_ORGANIZATION_IDS = BlogsEntryFinder.class
053: .getName()
054: + ".findByOrganizationIds";
055:
056: public static String FIND_BY_NO_ASSETS = BlogsEntryFinder.class
057: .getName()
058: + ".findByNoAssets";
059:
060: public int countByOrganizationId(long organizationId)
061: throws SystemException {
062:
063: List organizationIds = new ArrayList();
064:
065: organizationIds.add(new Long(organizationId));
066:
067: return countByOrganizationIds(organizationIds);
068: }
069:
070: public int countByOrganizationIds(List organizationIds)
071: throws SystemException {
072:
073: Session session = null;
074:
075: try {
076: session = HibernateUtil.openSession();
077:
078: String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
079:
080: sql = StringUtil.replace(sql, "[$ORGANIZATION_ID$]",
081: getOrganizationIds(organizationIds));
082:
083: SQLQuery q = session.createSQLQuery(sql);
084:
085: q.addScalar(HibernateUtil.getCountColumnName(),
086: Hibernate.LONG);
087:
088: QueryPos qPos = QueryPos.getInstance(q);
089:
090: for (int i = 0; i < organizationIds.size(); i++) {
091: Long organizationId = (Long) organizationIds.get(i);
092:
093: qPos.add(organizationId);
094: }
095:
096: Iterator itr = q.list().iterator();
097:
098: if (itr.hasNext()) {
099: Long count = (Long) itr.next();
100:
101: if (count != null) {
102: return count.intValue();
103: }
104: }
105:
106: return 0;
107: } catch (Exception e) {
108: throw new SystemException(e);
109: } finally {
110: HibernateUtil.closeSession(session);
111: }
112: }
113:
114: public List findByOrganizationId(long organizationId, int begin,
115: int end) throws SystemException {
116:
117: List organizationIds = new ArrayList();
118:
119: organizationIds.add(new Long(organizationId));
120:
121: return findByOrganizationIds(organizationIds, begin, end);
122: }
123:
124: public List findByOrganizationIds(List organizationIds, int begin,
125: int end) throws SystemException {
126:
127: Session session = null;
128:
129: try {
130: session = HibernateUtil.openSession();
131:
132: String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
133:
134: sql = StringUtil.replace(sql, "[$ORGANIZATION_ID$]",
135: getOrganizationIds(organizationIds));
136:
137: SQLQuery q = session.createSQLQuery(sql);
138:
139: q.addEntity("BlogsEntry", BlogsEntryImpl.class);
140:
141: QueryPos qPos = QueryPos.getInstance(q);
142:
143: for (int i = 0; i < organizationIds.size(); i++) {
144: Long organizationId = (Long) organizationIds.get(i);
145:
146: qPos.add(organizationId);
147: }
148:
149: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
150: end);
151: } catch (Exception e) {
152: throw new SystemException(e);
153: } finally {
154: HibernateUtil.closeSession(session);
155: }
156: }
157:
158: public List findByNoAssets() throws SystemException {
159: Session session = null;
160:
161: try {
162: session = HibernateUtil.openSession();
163:
164: String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
165:
166: SQLQuery q = session.createSQLQuery(sql);
167:
168: q.addEntity("BlogsEntry", BlogsEntryImpl.class);
169:
170: return q.list();
171: } catch (Exception e) {
172: throw new SystemException(e);
173: } finally {
174: HibernateUtil.closeSession(session);
175: }
176: }
177:
178: protected String getOrganizationIds(List organizationIds) {
179: StringMaker sm = new StringMaker();
180:
181: for (int i = 0; i < organizationIds.size(); i++) {
182: sm.append("Users_Orgs.organizationId = ? ");
183:
184: if ((i + 1) != organizationIds.size()) {
185: sm.append("OR ");
186: }
187: }
188:
189: return sm.toString();
190: }
191:
192: }
|