001    /**
002     * Copyright (c) 2000-2013 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            @Override
047            public int countByKeywords(long groupId, String keywords)
048                    throws SystemException {
049    
050                    String[] names = null;
051                    boolean andOperator = false;
052    
053                    if (Validator.isNotNull(keywords)) {
054                            names = CustomSQLUtil.keywords(keywords);
055                    }
056                    else {
057                            andOperator = true;
058                    }
059    
060                    return countByG_N(groupId, names, andOperator);
061            }
062    
063            @Override
064            public int countByG_N(long groupId, String name, boolean andOperator)
065                    throws SystemException {
066    
067                    String[] names = CustomSQLUtil.keywords(name);
068    
069                    return countByG_N(groupId, names, andOperator);
070            }
071    
072            @Override
073            public int countByG_N(long groupId, String[] names, boolean andOperator)
074                    throws SystemException {
075    
076                    names = CustomSQLUtil.keywords(names);
077    
078                    Session session = null;
079    
080                    try {
081                            session = openSession();
082    
083                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
084    
085                            sql = CustomSQLUtil.replaceKeywords(
086                                    sql, "lower(name)", StringPool.LIKE, true, names);
087                            sql = CustomSQLUtil.replaceAndOperator(sql, false);
088    
089                            SQLQuery q = session.createSQLQuery(sql);
090    
091                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
092    
093                            QueryPos qPos = QueryPos.getInstance(q);
094    
095                            qPos.add(groupId);
096                            qPos.add(names, 2);
097    
098                            Iterator<Long> itr = q.iterate();
099    
100                            if (itr.hasNext()) {
101                                    Long count = itr.next();
102    
103                                    if (count != null) {
104                                            return count.intValue();
105                                    }
106                            }
107    
108                            return 0;
109                    }
110                    catch (Exception e) {
111                            throw new SystemException(e);
112                    }
113                    finally {
114                            closeSession(session);
115                    }
116            }
117    
118            @Override
119            public List<MDRRuleGroup> findByKeywords(
120                            long groupId, String keywords, int start, int end)
121                    throws SystemException {
122    
123                    String[] names = null;
124                    boolean andOperator = false;
125    
126                    if (Validator.isNotNull(keywords)) {
127                            names = CustomSQLUtil.keywords(keywords);
128                    }
129                    else {
130                            andOperator = true;
131                    }
132    
133                    return findByG_N(groupId, names, andOperator, start, end);
134            }
135    
136            @Override
137            public List<MDRRuleGroup> findByG_N(
138                            long groupId, String name, boolean andOperator)
139                    throws SystemException {
140    
141                    return findByG_N(
142                            groupId, name, andOperator, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
143            }
144    
145            @Override
146            public List<MDRRuleGroup> findByG_N(
147                            long groupId, String name, boolean andOperator, int start, int end)
148                    throws SystemException {
149    
150                    String[] names = CustomSQLUtil.keywords(name);
151    
152                    return findByG_N(groupId, names, andOperator, start, end);
153            }
154    
155            @Override
156            public List<MDRRuleGroup> findByG_N(
157                            long groupId, String[] names, boolean andOperator, int start,
158                            int end)
159                    throws SystemException {
160    
161                    names = CustomSQLUtil.keywords(names);
162    
163                    Session session = null;
164    
165                    try {
166                            session = openSession();
167    
168                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
169    
170                            sql = CustomSQLUtil.replaceKeywords(
171                                    sql, "lower(name)", StringPool.LIKE, true, names);
172                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
173    
174                            SQLQuery q = session.createSQLQuery(sql);
175    
176                            q.addEntity("MDRRuleGroup", MDRRuleGroupImpl.class);
177    
178                            QueryPos qPos = QueryPos.getInstance(q);
179    
180                            qPos.add(groupId);
181                            qPos.add(names, 2);
182    
183                            return (List<MDRRuleGroup>)QueryUtil.list(
184                                    q, getDialect(), start, end);
185                    }
186                    catch (Exception e) {
187                            throw new SystemException(e);
188                    }
189                    finally {
190                            closeSession(session);
191                    }
192            }
193    
194    }