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