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