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.journal.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.OrderByComparator;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
029: import com.liferay.portal.spring.hibernate.HibernateUtil;
030: import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
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="JournalFeedFinderImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Raymond Augé
045: *
046: */
047: public class JournalFeedFinderImpl implements JournalFeedFinder {
048:
049: public static String COUNT_BY_C_G_F_N_D = JournalFeedFinder.class
050: .getName()
051: + ".countByC_G_F_N_D";
052:
053: public static String FIND_BY_C_G_F_N_D = JournalFeedFinder.class
054: .getName()
055: + ".findByC_G_F_N_D";
056:
057: public int countByKeywords(long companyId, long groupId,
058: String keywords) throws SystemException {
059:
060: String[] feedIds = null;
061: String[] names = null;
062: String[] descriptions = null;
063: boolean andOperator = false;
064:
065: if (Validator.isNotNull(keywords)) {
066: feedIds = CustomSQLUtil.keywords(keywords, false);
067: names = CustomSQLUtil.keywords(keywords);
068: descriptions = CustomSQLUtil.keywords(keywords);
069: } else {
070: andOperator = true;
071: }
072:
073: return countByC_G_F_N_D(companyId, groupId, feedIds, names,
074: descriptions, andOperator);
075: }
076:
077: public int countByC_G_F_N_D(long companyId, long groupId,
078: String feedId, String name, String description,
079: boolean andOperator) throws SystemException {
080:
081: return countByC_G_F_N_D(companyId, groupId,
082: new String[] { feedId }, new String[] { name },
083: new String[] { description }, andOperator);
084: }
085:
086: public int countByC_G_F_N_D(long companyId, long groupId,
087: String[] feedIds, String[] names, String[] descriptions,
088: boolean andOperator) throws SystemException {
089:
090: feedIds = CustomSQLUtil.keywords(feedIds, false);
091: names = CustomSQLUtil.keywords(names);
092: descriptions = CustomSQLUtil.keywords(descriptions);
093:
094: Session session = null;
095:
096: try {
097: session = HibernateUtil.openSession();
098:
099: String sql = CustomSQLUtil.get(COUNT_BY_C_G_F_N_D);
100:
101: if (groupId <= 0) {
102: sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
103: }
104:
105: sql = CustomSQLUtil.replaceKeywords(sql, "feedId",
106: StringPool.LIKE, false, feedIds);
107: sql = CustomSQLUtil.replaceKeywords(sql, "lower(name)",
108: StringPool.LIKE, false, names);
109: sql = CustomSQLUtil.replaceKeywords(sql,
110: "lower(description)", StringPool.LIKE, true,
111: descriptions);
112:
113: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
114:
115: SQLQuery q = session.createSQLQuery(sql);
116:
117: q.addScalar(HibernateUtil.getCountColumnName(),
118: Hibernate.LONG);
119:
120: QueryPos qPos = QueryPos.getInstance(q);
121:
122: qPos.add(companyId);
123:
124: if (groupId > 0) {
125: qPos.add(groupId);
126: }
127:
128: qPos.add(feedIds, 2);
129: qPos.add(names, 2);
130: qPos.add(descriptions, 2);
131:
132: Iterator itr = q.list().iterator();
133:
134: if (itr.hasNext()) {
135: Long count = (Long) itr.next();
136:
137: if (count != null) {
138: return count.intValue();
139: }
140: }
141:
142: return 0;
143: } catch (Exception e) {
144: throw new SystemException(e);
145: } finally {
146: HibernateUtil.closeSession(session);
147: }
148: }
149:
150: public List findByKeywords(long companyId, long groupId,
151: String keywords, int begin, int end, OrderByComparator obc)
152: throws SystemException {
153:
154: String[] feedIds = null;
155: String[] names = null;
156: String[] descriptions = null;
157: boolean andOperator = false;
158:
159: if (Validator.isNotNull(keywords)) {
160: feedIds = CustomSQLUtil.keywords(keywords, false);
161: names = CustomSQLUtil.keywords(keywords);
162: descriptions = CustomSQLUtil.keywords(keywords);
163: } else {
164: andOperator = true;
165: }
166:
167: return findByC_G_F_N_D(companyId, groupId, feedIds, names,
168: descriptions, andOperator, begin, end, obc);
169: }
170:
171: public List findByC_G_F_N_D(long companyId, long groupId,
172: String feedId, String name, String description,
173: boolean andOperator, int begin, int end,
174: OrderByComparator obc) throws SystemException {
175:
176: return findByC_G_F_N_D(companyId, groupId,
177: new String[] { feedId }, new String[] { name },
178: new String[] { description }, andOperator, begin, end,
179: obc);
180: }
181:
182: public List findByC_G_F_N_D(long companyId, long groupId,
183: String[] feedIds, String[] names, String[] descriptions,
184: boolean andOperator, int begin, int end,
185: OrderByComparator obc) throws SystemException {
186:
187: feedIds = CustomSQLUtil.keywords(feedIds, false);
188: names = CustomSQLUtil.keywords(names);
189: descriptions = CustomSQLUtil.keywords(descriptions);
190:
191: Session session = null;
192:
193: try {
194: session = HibernateUtil.openSession();
195:
196: String sql = CustomSQLUtil.get(FIND_BY_C_G_F_N_D);
197:
198: if (groupId <= 0) {
199: sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
200: }
201:
202: sql = CustomSQLUtil.replaceKeywords(sql, "feedId",
203: StringPool.LIKE, false, feedIds);
204: sql = CustomSQLUtil.replaceKeywords(sql, "lower(name)",
205: StringPool.LIKE, false, names);
206: sql = CustomSQLUtil.replaceKeywords(sql,
207: "lower(description)", StringPool.LIKE, true,
208: descriptions);
209:
210: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
211: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
212:
213: SQLQuery q = session.createSQLQuery(sql);
214:
215: q.addEntity("JournalFeed", JournalFeedImpl.class);
216:
217: QueryPos qPos = QueryPos.getInstance(q);
218:
219: qPos.add(companyId);
220:
221: if (groupId > 0) {
222: qPos.add(groupId);
223: }
224:
225: qPos.add(feedIds, 2);
226: qPos.add(names, 2);
227: qPos.add(descriptions, 2);
228:
229: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
230: end);
231: } catch (Exception e) {
232: throw new SystemException(e);
233: } finally {
234: HibernateUtil.closeSession(session);
235: }
236: }
237:
238: }
|