001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
026    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
027    import com.liferay.portlet.mobiledevicerules.model.impl.MDRRuleGroupImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Edward Han
035     * @author Eduardo Lundgren
036     */
037    public class MDRRuleGroupFinderImpl extends BasePersistenceImpl<MDRRuleGroup>
038            implements MDRRuleGroupFinder {
039    
040            public static final String COUNT_BY_G_N =
041                    MDRRuleGroupFinder.class.getName() + ".countByG_N";
042    
043            public static final String FIND_BY_G_N =
044                    MDRRuleGroupFinder.class.getName() + ".findByG_N";
045    
046            public int countByKeywords(long groupId, String keywords)
047                    throws SystemException {
048    
049                    String[] names = null;
050                    boolean andOperator = false;
051    
052                    if (Validator.isNotNull(keywords)) {
053                            names = CustomSQLUtil.keywords(keywords);
054                    }
055                    else {
056                            andOperator = true;
057                    }
058    
059                    return countByG_N(groupId, names, andOperator);
060            }
061    
062            public int countByG_N(long groupId, String name, boolean andOperator)
063                    throws SystemException {
064    
065                    String[] names = CustomSQLUtil.keywords(name);
066    
067                    return countByG_N(groupId, names, andOperator);
068            }
069    
070            public int countByG_N(long groupId, String[] names, boolean andOperator)
071                    throws SystemException {
072    
073                    names = CustomSQLUtil.keywords(names);
074    
075                    Session session = null;
076    
077                    try {
078                            session = openSession();
079    
080                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
081    
082                            sql = CustomSQLUtil.replaceKeywords(
083                                    sql, "lower(name)", StringPool.LIKE, true, names);
084                            sql = CustomSQLUtil.replaceAndOperator(sql, false);
085    
086                            SQLQuery q = session.createSQLQuery(sql);
087    
088                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
089    
090                            QueryPos qPos = QueryPos.getInstance(q);
091    
092                            qPos.add(groupId);
093                            qPos.add(names, 2);
094    
095                            Iterator<Long> itr = q.iterate();
096    
097                            if (itr.hasNext()) {
098                                    Long count = itr.next();
099    
100                                    if (count != null) {
101                                            return count.intValue();
102                                    }
103                            }
104    
105                            return 0;
106                    }
107                    catch (Exception e) {
108                            throw new SystemException(e);
109                    }
110                    finally {
111                            closeSession(session);
112                    }
113            }
114    
115            public List<MDRRuleGroup> findByKeywords(
116                            long groupId, String keywords, int start, int end)
117                    throws SystemException {
118    
119                    String[] names = null;
120                    boolean andOperator = false;
121    
122                    if (Validator.isNotNull(keywords)) {
123                            names = CustomSQLUtil.keywords(keywords);
124                    }
125                    else {
126                            andOperator = true;
127                    }
128    
129                    return findByG_N(groupId, names, andOperator, start, end);
130            }
131    
132            public List<MDRRuleGroup> findByG_N(
133                            long groupId, String name, boolean andOperator)
134                    throws SystemException {
135    
136                    return findByG_N(
137                            groupId, name, andOperator, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
138            }
139    
140            public List<MDRRuleGroup> findByG_N(
141                            long groupId, String name, boolean andOperator, int start, int end)
142                    throws SystemException {
143    
144                    String[] names = CustomSQLUtil.keywords(name);
145    
146                    return findByG_N(groupId, names, andOperator, start, end);
147            }
148    
149            public List<MDRRuleGroup> findByG_N(
150                            long groupId, String[] names, boolean andOperator, int start,
151                            int end)
152                    throws SystemException {
153    
154                    names = CustomSQLUtil.keywords(names);
155    
156                    Session session = null;
157    
158                    try {
159                            session = openSession();
160    
161                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
162    
163                            sql = CustomSQLUtil.replaceKeywords(
164                                    sql, "lower(name)", StringPool.LIKE, true, names);
165                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
166    
167                            SQLQuery q = session.createSQLQuery(sql);
168    
169                            q.addEntity("MDRRuleGroup", MDRRuleGroupImpl.class);
170    
171                            QueryPos qPos = QueryPos.getInstance(q);
172    
173                            qPos.add(groupId);
174                            qPos.add(names, 2);
175    
176                            return (List<MDRRuleGroup>) QueryUtil.list(
177                                    q, getDialect(), start, end);
178                    }
179                    catch (Exception e) {
180                            throw new SystemException(e);
181                    }
182                    finally {
183                            closeSession(session);
184                    }
185            }
186    
187    }