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.messageboards.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.portal.util.PortalUtil;
029: import com.liferay.portlet.messageboards.model.MBThread;
030: import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
031: import com.liferay.util.dao.hibernate.QueryPos;
032: import com.liferay.util.dao.hibernate.QueryUtil;
033:
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="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class MBThreadFinderImpl implements MBThreadFinder {
048:
049: public static String COUNT_BY_CATEGORY_IDS = MBThreadFinder.class
050: .getName()
051: + ".countByCategoryIds";
052:
053: public static String COUNT_BY_GROUP_ID = MBThreadFinder.class
054: .getName()
055: + ".countByGroupId";
056:
057: public static String COUNT_BY_G_U = MBThreadFinder.class.getName()
058: + ".countByG_U";
059:
060: public static String COUNT_BY_S_G_U = MBThreadFinder.class
061: .getName()
062: + ".countByS_G_U";
063:
064: public static String FIND_BY_GROUP_ID = MBThreadFinder.class
065: .getName()
066: + ".findByGroupId";
067:
068: public static String FIND_BY_G_U = MBThreadFinder.class.getName()
069: + ".findByG_U";
070:
071: public static String FIND_BY_S_G_U = MBThreadFinder.class.getName()
072: + ".findByS_G_U";
073:
074: public int countByCategoryIds(List categoryIds)
075: throws SystemException {
076: Session session = null;
077:
078: try {
079: session = HibernateUtil.openSession();
080:
081: String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
082:
083: sql = StringUtil.replace(sql, "[$CATEGORY_ID$]",
084: getCategoryIds(categoryIds));
085:
086: SQLQuery q = session.createSQLQuery(sql);
087:
088: q.addScalar(HibernateUtil.getCountColumnName(),
089: Hibernate.INTEGER);
090:
091: QueryPos qPos = QueryPos.getInstance(q);
092:
093: for (int i = 0; i < categoryIds.size(); i++) {
094: Long categoryId = (Long) categoryIds.get(i);
095:
096: qPos.add(categoryId);
097: }
098:
099: Iterator itr = q.list().iterator();
100:
101: if (itr.hasNext()) {
102: Integer count = (Integer) itr.next();
103:
104: if (count != null) {
105: return count.intValue();
106: }
107: }
108:
109: return 0;
110: } catch (Exception e) {
111: throw new SystemException(e);
112: } finally {
113: HibernateUtil.closeSession(session);
114: }
115: }
116:
117: public int countByGroupId(long groupId) throws SystemException {
118: Session session = null;
119:
120: try {
121: session = HibernateUtil.openSession();
122:
123: String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
124:
125: SQLQuery q = session.createSQLQuery(sql);
126:
127: q.addScalar(HibernateUtil.getCountColumnName(),
128: Hibernate.LONG);
129:
130: QueryPos qPos = QueryPos.getInstance(q);
131:
132: qPos.add(groupId);
133:
134: Iterator itr = q.list().iterator();
135:
136: if (itr.hasNext()) {
137: Long count = (Long) itr.next();
138:
139: if (count != null) {
140: return count.intValue();
141: }
142: }
143:
144: return 0;
145: } catch (Exception e) {
146: throw new SystemException(e);
147: } finally {
148: HibernateUtil.closeSession(session);
149: }
150: }
151:
152: public int countByG_U(long groupId, long userId)
153: throws SystemException {
154: Session session = null;
155:
156: try {
157: session = HibernateUtil.openSession();
158:
159: String sql = CustomSQLUtil.get(COUNT_BY_G_U);
160:
161: SQLQuery q = session.createSQLQuery(sql);
162:
163: q.addScalar(HibernateUtil.getCountColumnName(),
164: Hibernate.LONG);
165:
166: QueryPos qPos = QueryPos.getInstance(q);
167:
168: qPos.add(groupId);
169: qPos.add(userId);
170:
171: Iterator itr = q.list().iterator();
172:
173: if (itr.hasNext()) {
174: Long count = (Long) itr.next();
175:
176: if (count != null) {
177: return count.intValue();
178: }
179: }
180:
181: return 0;
182: } catch (Exception e) {
183: throw new SystemException(e);
184: } finally {
185: HibernateUtil.closeSession(session);
186: }
187: }
188:
189: public int countByS_G_U(long groupId, long userId)
190: throws SystemException {
191: Session session = null;
192:
193: try {
194: session = HibernateUtil.openSession();
195:
196: String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
197:
198: SQLQuery q = session.createSQLQuery(sql);
199:
200: q.addScalar(HibernateUtil.getCountColumnName(),
201: Hibernate.LONG);
202:
203: QueryPos qPos = QueryPos.getInstance(q);
204:
205: qPos.add(PortalUtil
206: .getClassNameId(MBThread.class.getName()));
207: qPos.add(groupId);
208: qPos.add(userId);
209:
210: Iterator itr = q.list().iterator();
211:
212: if (itr.hasNext()) {
213: Long count = (Long) itr.next();
214:
215: if (count != null) {
216: return count.intValue();
217: }
218: }
219:
220: return 0;
221: } catch (Exception e) {
222: throw new SystemException(e);
223: } finally {
224: HibernateUtil.closeSession(session);
225: }
226: }
227:
228: public List findByGroupId(long groupId, int begin, int end)
229: throws SystemException {
230:
231: Session session = null;
232:
233: try {
234: session = HibernateUtil.openSession();
235:
236: String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
237:
238: SQLQuery q = session.createSQLQuery(sql);
239:
240: q.addEntity("MBThread", MBThreadImpl.class);
241:
242: QueryPos qPos = QueryPos.getInstance(q);
243:
244: qPos.add(groupId);
245:
246: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
247: end);
248: } catch (Exception e) {
249: throw new SystemException(e);
250: } finally {
251: HibernateUtil.closeSession(session);
252: }
253: }
254:
255: public List findByG_U(long groupId, long userId, int begin, int end)
256: throws SystemException {
257:
258: Session session = null;
259:
260: try {
261: session = HibernateUtil.openSession();
262:
263: String sql = CustomSQLUtil.get(FIND_BY_G_U);
264:
265: SQLQuery q = session.createSQLQuery(sql);
266:
267: q.addEntity("MBThread", MBThreadImpl.class);
268:
269: QueryPos qPos = QueryPos.getInstance(q);
270:
271: qPos.add(groupId);
272: qPos.add(userId);
273:
274: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
275: end);
276: } catch (Exception e) {
277: throw new SystemException(e);
278: } finally {
279: HibernateUtil.closeSession(session);
280: }
281: }
282:
283: public List findByS_G_U(long groupId, long userId, int begin,
284: int end) throws SystemException {
285:
286: Session session = null;
287:
288: try {
289: session = HibernateUtil.openSession();
290:
291: String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
292:
293: SQLQuery q = session.createSQLQuery(sql);
294:
295: q.addEntity("MBThread", MBThreadImpl.class);
296:
297: QueryPos qPos = QueryPos.getInstance(q);
298:
299: qPos.add(PortalUtil
300: .getClassNameId(MBThread.class.getName()));
301: qPos.add(groupId);
302: qPos.add(userId);
303:
304: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
305: end);
306: } catch (Exception e) {
307: throw new SystemException(e);
308: } finally {
309: HibernateUtil.closeSession(session);
310: }
311: }
312:
313: protected String getCategoryIds(List categoryIds) {
314: StringMaker sm = new StringMaker();
315:
316: for (int i = 0; i < categoryIds.size(); i++) {
317: sm.append("categoryId = ? ");
318:
319: if ((i + 1) != categoryIds.size()) {
320: sm.append("OR ");
321: }
322: }
323:
324: return sm.toString();
325: }
326:
327: }
|