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.JournalTemplateImpl;
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="JournalTemplateFinderImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: * @author Bruno Farache
046: *
047: */
048: public class JournalTemplateFinderImpl implements JournalTemplateFinder {
049:
050: public static String COUNT_BY_C_G_T_S_N_D = JournalTemplateFinder.class
051: .getName()
052: + ".countByC_G_T_S_N_D";
053:
054: public static String FIND_BY_C_G_T_S_N_D = JournalTemplateFinder.class
055: .getName()
056: + ".findByC_G_T_S_N_D";
057:
058: public int countByKeywords(long companyId, long groupId,
059: String keywords, String structureId,
060: String structureIdComparator) throws SystemException {
061:
062: String[] templateIds = null;
063: String[] names = null;
064: String[] descriptions = null;
065: boolean andOperator = false;
066:
067: if (Validator.isNotNull(keywords)) {
068: templateIds = CustomSQLUtil.keywords(keywords, false);
069: names = CustomSQLUtil.keywords(keywords);
070: descriptions = CustomSQLUtil.keywords(keywords);
071: } else {
072: andOperator = true;
073: }
074:
075: return countByC_G_T_S_N_D(companyId, groupId, templateIds,
076: structureId, structureIdComparator, names,
077: descriptions, andOperator);
078: }
079:
080: public int countByC_G_T_S_N_D(long companyId, long groupId,
081: String templateId, String structureId,
082: String structureIdComparator, String name,
083: String description, boolean andOperator)
084: throws SystemException {
085:
086: return countByC_G_T_S_N_D(companyId, groupId,
087: new String[] { templateId }, structureId,
088: structureIdComparator, new String[] { name },
089: new String[] { description }, andOperator);
090: }
091:
092: public int countByC_G_T_S_N_D(long companyId, long groupId,
093: String[] templateIds, String structureId,
094: String structureIdComparator, String[] names,
095: String[] descriptions, boolean andOperator)
096: throws SystemException {
097:
098: templateIds = CustomSQLUtil.keywords(templateIds, false);
099: names = CustomSQLUtil.keywords(names);
100: descriptions = CustomSQLUtil.keywords(descriptions);
101:
102: Session session = null;
103:
104: try {
105: session = HibernateUtil.openSession();
106:
107: String sql = CustomSQLUtil.get(COUNT_BY_C_G_T_S_N_D);
108:
109: if (groupId <= 0) {
110: sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
111: }
112:
113: sql = CustomSQLUtil.replaceKeywords(sql, "templateId",
114: StringPool.LIKE, false, templateIds);
115:
116: if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
117: String replaceWith = "structureId != ? AND structureId IS NOT NULL";
118:
119: if (CustomSQLUtil.isVendorOracle()) {
120: replaceWith = "structureId IS NOT NULL";
121: }
122:
123: sql = StringUtil.replace(sql,
124: "structureId = ? [$AND_OR_NULL_CHECK$]",
125: replaceWith);
126: }
127:
128: sql = CustomSQLUtil.replaceKeywords(sql, "lower(name)",
129: StringPool.LIKE, false, names);
130: sql = CustomSQLUtil.replaceKeywords(sql,
131: "lower(description)", StringPool.LIKE, true,
132: descriptions);
133:
134: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
135:
136: SQLQuery q = session.createSQLQuery(sql);
137:
138: q.addScalar(HibernateUtil.getCountColumnName(),
139: Hibernate.LONG);
140:
141: QueryPos qPos = QueryPos.getInstance(q);
142:
143: qPos.add(companyId);
144:
145: if (groupId > 0) {
146: qPos.add(groupId);
147: }
148:
149: qPos.add(templateIds, 2);
150:
151: if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
152: if (CustomSQLUtil.isVendorOracle()) {
153: } else {
154: qPos.add(structureId);
155: }
156: } else {
157: qPos.add(structureId);
158: }
159:
160: if (structureIdComparator.equals(StringPool.EQUAL)) {
161: qPos.add(structureId);
162: }
163:
164: qPos.add(names, 2);
165: qPos.add(descriptions, 2);
166:
167: Iterator itr = q.list().iterator();
168:
169: if (itr.hasNext()) {
170: Long count = (Long) itr.next();
171:
172: if (count != null) {
173: return count.intValue();
174: }
175: }
176:
177: return 0;
178: } catch (Exception e) {
179: throw new SystemException(e);
180: } finally {
181: HibernateUtil.closeSession(session);
182: }
183: }
184:
185: public List findByKeywords(long companyId, long groupId,
186: String keywords, String structureId,
187: String structureIdComparator, int begin, int end,
188: OrderByComparator obc) throws SystemException {
189:
190: String[] templateIds = null;
191: String[] names = null;
192: String[] descriptions = null;
193: boolean andOperator = false;
194:
195: if (Validator.isNotNull(keywords)) {
196: templateIds = CustomSQLUtil.keywords(keywords, false);
197: names = CustomSQLUtil.keywords(keywords);
198: descriptions = CustomSQLUtil.keywords(keywords);
199: } else {
200: andOperator = true;
201: }
202:
203: return findByC_G_T_S_N_D(companyId, groupId, templateIds,
204: structureId, structureIdComparator, names,
205: descriptions, andOperator, begin, end, obc);
206: }
207:
208: public List findByC_G_T_S_N_D(long companyId, long groupId,
209: String templateId, String structureId,
210: String structureIdComparator, String name,
211: String description, boolean andOperator, int begin,
212: int end, OrderByComparator obc) throws SystemException {
213:
214: return findByC_G_T_S_N_D(companyId, groupId,
215: new String[] { templateId }, structureId,
216: structureIdComparator, new String[] { name },
217: new String[] { description }, andOperator, begin, end,
218: obc);
219: }
220:
221: public List findByC_G_T_S_N_D(long companyId, long groupId,
222: String[] templateIds, String structureId,
223: String structureIdComparator, String[] names,
224: String[] descriptions, boolean andOperator, int begin,
225: int end, OrderByComparator obc) throws SystemException {
226:
227: templateIds = CustomSQLUtil.keywords(templateIds, false);
228: names = CustomSQLUtil.keywords(names);
229: descriptions = CustomSQLUtil.keywords(descriptions);
230:
231: Session session = null;
232:
233: try {
234: session = HibernateUtil.openSession();
235:
236: String sql = CustomSQLUtil.get(FIND_BY_C_G_T_S_N_D);
237:
238: if (groupId <= 0) {
239: sql = StringUtil.replace(sql, "(groupId = ?) AND", "");
240: }
241:
242: sql = CustomSQLUtil.replaceKeywords(sql, "templateId",
243: StringPool.LIKE, false, templateIds);
244:
245: if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
246: String replaceWith = "structureId != ? AND structureId IS NOT NULL";
247:
248: if (CustomSQLUtil.isVendorOracle()) {
249: replaceWith = "structureId IS NOT NULL";
250: }
251:
252: sql = StringUtil.replace(sql,
253: "structureId = ? [$AND_OR_NULL_CHECK$]",
254: replaceWith);
255: }
256:
257: sql = CustomSQLUtil.replaceKeywords(sql, "lower(name)",
258: StringPool.LIKE, false, names);
259: sql = CustomSQLUtil.replaceKeywords(sql,
260: "lower(description)", StringPool.LIKE, true,
261: descriptions);
262:
263: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
264: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
265:
266: SQLQuery q = session.createSQLQuery(sql);
267:
268: q.addEntity("JournalTemplate", JournalTemplateImpl.class);
269:
270: QueryPos qPos = QueryPos.getInstance(q);
271:
272: qPos.add(companyId);
273:
274: if (groupId > 0) {
275: qPos.add(groupId);
276: }
277:
278: qPos.add(templateIds, 2);
279:
280: if (structureIdComparator.equals(StringPool.NOT_EQUAL)) {
281: if (CustomSQLUtil.isVendorOracle()) {
282: } else {
283: qPos.add(structureId);
284: }
285: } else {
286: qPos.add(structureId);
287: }
288:
289: if (structureIdComparator.equals(StringPool.EQUAL)) {
290: qPos.add(structureId);
291: }
292:
293: qPos.add(names, 2);
294: qPos.add(descriptions, 2);
295:
296: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
297: end);
298: } catch (Exception e) {
299: throw new SystemException(e);
300: } finally {
301: HibernateUtil.closeSession(session);
302: }
303: }
304:
305: }
|