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