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