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.wiki.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringPool;
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.wiki.NoSuchPageException;
030: import com.liferay.portlet.wiki.model.WikiPage;
031: import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
032: import com.liferay.util.dao.hibernate.QueryPos;
033: import com.liferay.util.dao.hibernate.QueryUtil;
034:
035: import java.sql.Timestamp;
036:
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: import org.hibernate.Hibernate;
042: import org.hibernate.SQLQuery;
043: import org.hibernate.Session;
044:
045: /**
046: * <a href="WikiPageFinderImpl.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class WikiPageFinderImpl implements WikiPageFinder {
052:
053: public static String COUNT_BY_CREATEDATE = WikiPageFinder.class
054: .getName()
055: + ".countByCreateDate";
056:
057: public static String FIND_BY_CREATEDATE = WikiPageFinder.class
058: .getName()
059: + ".findByCreateDate";
060:
061: public static String FIND_BY_NO_ASSETS = WikiPageFinder.class
062: .getName()
063: + ".findByNoAssets";
064:
065: public static String FIND_BY_UUID_G = WikiPageFinder.class
066: .getName()
067: + ".findByUuid_G";
068:
069: public int countByCreateDate(long nodeId, Date createDate,
070: boolean before) throws SystemException {
071:
072: return countByCreateDate(nodeId, new Timestamp(createDate
073: .getTime()), before);
074: }
075:
076: public int countByCreateDate(long nodeId, Timestamp createDate,
077: boolean before) throws SystemException {
078:
079: Session session = null;
080:
081: try {
082: session = HibernateUtil.openSession();
083:
084: String createDateComparator = StringPool.GREATER_THAN;
085:
086: if (before) {
087: createDateComparator = StringPool.LESS_THAN;
088: }
089:
090: String sql = CustomSQLUtil.get(COUNT_BY_CREATEDATE);
091:
092: sql = StringUtil.replace(sql, "[$CREATE_DATE_COMPARATOR$]",
093: createDateComparator);
094:
095: SQLQuery q = session.createSQLQuery(sql);
096:
097: q.addScalar(HibernateUtil.getCountColumnName(),
098: Hibernate.LONG);
099:
100: QueryPos qPos = QueryPos.getInstance(q);
101:
102: qPos.add(nodeId);
103: qPos.add(createDate);
104: qPos.add(true);
105:
106: Iterator itr = q.list().iterator();
107:
108: if (itr.hasNext()) {
109: Long count = (Long) itr.next();
110:
111: if (count != null) {
112: return count.intValue();
113: }
114: }
115:
116: return 0;
117: } catch (Exception e) {
118: throw new SystemException(e);
119: } finally {
120: HibernateUtil.closeSession(session);
121: }
122: }
123:
124: public List findByCreateDate(long nodeId, Date createDate,
125: boolean before, int begin, int end) throws SystemException {
126:
127: return findByCreateDate(nodeId, new Timestamp(createDate
128: .getTime()), before, begin, end);
129: }
130:
131: public List findByCreateDate(long nodeId, Timestamp createDate,
132: boolean before, int begin, int end) throws SystemException {
133:
134: Session session = null;
135:
136: try {
137: session = HibernateUtil.openSession();
138:
139: String createDateComparator = StringPool.GREATER_THAN;
140:
141: if (before) {
142: createDateComparator = StringPool.LESS_THAN;
143: }
144:
145: String sql = CustomSQLUtil.get(FIND_BY_CREATEDATE);
146:
147: sql = StringUtil.replace(sql, "[$CREATE_DATE_COMPARATOR$]",
148: createDateComparator);
149:
150: SQLQuery q = session.createSQLQuery(sql);
151:
152: q.addEntity("WikiPage", WikiPageImpl.class);
153:
154: QueryPos qPos = QueryPos.getInstance(q);
155:
156: qPos.add(nodeId);
157: qPos.add(createDate);
158: qPos.add(true);
159:
160: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
161: end);
162: } catch (Exception e) {
163: throw new SystemException(e);
164: } finally {
165: HibernateUtil.closeSession(session);
166: }
167: }
168:
169: public List findByNoAssets() throws SystemException {
170: Session session = null;
171:
172: try {
173: session = HibernateUtil.openSession();
174:
175: String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
176:
177: SQLQuery q = session.createSQLQuery(sql);
178:
179: q.addEntity("WikiPage", WikiPageImpl.class);
180:
181: return q.list();
182: } catch (Exception e) {
183: throw new SystemException(e);
184: } finally {
185: HibernateUtil.closeSession(session);
186: }
187: }
188:
189: public WikiPage findByUuid_G(String uuid, long groupId)
190: throws NoSuchPageException, SystemException {
191:
192: Session session = null;
193:
194: try {
195: session = HibernateUtil.openSession();
196:
197: String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
198:
199: SQLQuery q = session.createSQLQuery(sql);
200:
201: q.addEntity("WikiPage", WikiPageImpl.class);
202:
203: QueryPos qPos = QueryPos.getInstance(q);
204:
205: qPos.add(uuid);
206: qPos.add(groupId);
207:
208: List list = q.list();
209:
210: if (list.size() == 0) {
211: StringMaker sm = new StringMaker();
212:
213: sm.append("No WikiPage exists with the key {uuid=");
214: sm.append(uuid);
215: sm.append(", groupId=");
216: sm.append(groupId);
217: sm.append("}");
218:
219: throw new NoSuchPageException(sm.toString());
220: } else {
221: return (WikiPage) list.get(0);
222: }
223: } catch (NoSuchPageException nspe) {
224: throw nspe;
225: } catch (Exception e) {
226: throw new SystemException(e);
227: } finally {
228: HibernateUtil.closeSession(session);
229: }
230: }
231:
232: }
|