001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.mobiledevicerules.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
024    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    
030    /**
031     * @author Edward Han
032     */
033    public class MDRRuleGroupFinderImpl extends BasePersistenceImpl<MDRRuleGroup>
034            implements MDRRuleGroupFinder {
035    
036            public static String COUNT_BY_G_N =
037                    MDRRuleGroupFinder.class.getName() + ".countByG_N";
038    
039            public static String FIND_BY_G_N =
040                    MDRRuleGroupFinder.class.getName() + ".findByG_N";
041    
042            public int countByG_N(long groupId, String name) throws SystemException {
043                    String[] names = CustomSQLUtil.keywords(name);
044    
045                    Session session = null;
046    
047                    try {
048                            session = openSession();
049    
050                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
051    
052                            sql = CustomSQLUtil.replaceKeywords(
053                                    sql, "lower(name)", StringPool.LIKE, true, names);
054                            sql = CustomSQLUtil.replaceAndOperator(sql, false);
055    
056                            SQLQuery q = session.createSQLQuery(sql);
057    
058                            QueryPos qPos = QueryPos.getInstance(q);
059    
060                            qPos.add(groupId);
061                            qPos.add(names, 2);
062    
063                            Iterator<Long> itr = q.iterate();
064    
065                            if (itr.hasNext()) {
066                                    Long count = itr.next();
067    
068                                    if (count != null) {
069                                            return count.intValue();
070                                    }
071                            }
072    
073                            return 0;
074                    }
075                    catch (Exception e) {
076                            throw new SystemException(e);
077                    }
078                    finally {
079                            closeSession(session);
080                    }
081            }
082    
083            public List<MDRRuleGroup> findByG_N(long groupId, String name)
084                    throws SystemException {
085    
086                    return findByG_N(groupId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
087            }
088    
089            public List<MDRRuleGroup> findByG_N(
090                            long groupId, String name, int start, int end)
091                    throws SystemException {
092    
093                    String[] names = CustomSQLUtil.keywords(name);
094    
095                    Session session = null;
096    
097                    try {
098                            session = openSession();
099    
100                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
101    
102                            sql = CustomSQLUtil.replaceKeywords(
103                                    sql, "lower(name)", StringPool.LIKE, true, names);
104                            sql = CustomSQLUtil.replaceAndOperator(sql, false);
105    
106                            SQLQuery q = session.createSQLQuery(sql);
107    
108                            q.addEntity("MDRRuleGroup", MDRRuleGroup.class);
109    
110                            QueryPos qPos = QueryPos.getInstance(q);
111    
112                            qPos.add(groupId);
113                            qPos.add(names, 2);
114    
115                            return (List<MDRRuleGroup>) QueryUtil.list(
116                                    q, getDialect(), start, end);
117                    }
118                    catch (Exception e) {
119                            throw new SystemException(e);
120                    }
121                    finally {
122                            closeSession(session);
123                    }
124            }
125    
126    }