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.shopping.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
026: import com.liferay.portal.spring.hibernate.HibernateUtil;
027: import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
028: import com.liferay.util.dao.hibernate.QueryPos;
029: import com.liferay.util.dao.hibernate.QueryUtil;
030:
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.hibernate.Hibernate;
035: import org.hibernate.SQLQuery;
036: import org.hibernate.Session;
037:
038: /**
039: * <a href="ShoppingCouponFinderImpl.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class ShoppingCouponFinderImpl implements ShoppingCouponFinder {
045:
046: public static String COUNT_BY_G_C_C_A_DT = ShoppingCouponFinder.class
047: .getName()
048: + ".countByG_C_C_A_DT";
049:
050: public static String FIND_BY_G_C_C_A_DT = ShoppingCouponFinder.class
051: .getName()
052: + ".findByG_C_C_A_DT";
053:
054: public int countByG_C_C_A_DT(long groupId, long companyId,
055: String code, boolean active, String discountType,
056: boolean andOperator) throws SystemException {
057:
058: code = StringUtil.upperCase(code);
059:
060: Session session = null;
061:
062: try {
063: session = HibernateUtil.openSession();
064:
065: String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_A_DT);
066:
067: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
068:
069: SQLQuery q = session.createSQLQuery(sql);
070:
071: q.addScalar(HibernateUtil.getCountColumnName(),
072: Hibernate.LONG);
073:
074: QueryPos qPos = QueryPos.getInstance(q);
075:
076: qPos.add(groupId);
077: qPos.add(companyId);
078: qPos.add(code);
079: qPos.add(code);
080: qPos.add(active);
081: qPos.add(discountType);
082: qPos.add(discountType);
083:
084: Iterator itr = q.list().iterator();
085:
086: if (itr.hasNext()) {
087: Long count = (Long) itr.next();
088:
089: if (count != null) {
090: return count.intValue();
091: }
092: }
093:
094: return 0;
095: } catch (Exception e) {
096: throw new SystemException(e);
097: } finally {
098: HibernateUtil.closeSession(session);
099: }
100: }
101:
102: public List findByG_C_C_A_DT(long groupId, long companyId,
103: String code, boolean active, String discountType,
104: boolean andOperator, int begin, int end)
105: throws SystemException {
106:
107: code = StringUtil.upperCase(code);
108:
109: Session session = null;
110:
111: try {
112: session = HibernateUtil.openSession();
113:
114: String sql = CustomSQLUtil.get(FIND_BY_G_C_C_A_DT);
115:
116: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
117:
118: SQLQuery q = session.createSQLQuery(sql);
119:
120: q.addEntity("ShoppingCoupon", ShoppingCouponImpl.class);
121:
122: QueryPos qPos = QueryPos.getInstance(q);
123:
124: qPos.add(groupId);
125: qPos.add(companyId);
126: qPos.add(code);
127: qPos.add(code);
128: qPos.add(active);
129: qPos.add(discountType);
130: qPos.add(discountType);
131:
132: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
133: end);
134: } catch (Exception e) {
135: throw new SystemException(e);
136: } finally {
137: HibernateUtil.closeSession(session);
138: }
139: }
140:
141: }
|