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