001    /**
002     * Copyright (c) 2000-present 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.impl;
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.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
031    import com.liferay.portlet.mobiledevicerules.model.impl.MDRRuleGroupImpl;
032    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleGroupFinder;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.Iterator;
036    import java.util.LinkedHashMap;
037    import java.util.List;
038    import java.util.Map;
039    
040    /**
041     * @author Edward Han
042     * @author Eduardo Lundgren
043     * @author Manuel de la Pe??a
044     */
045    public class MDRRuleGroupFinderImpl extends BasePersistenceImpl<MDRRuleGroup>
046            implements MDRRuleGroupFinder {
047    
048            public static final String COUNT_BY_G_N =
049                    MDRRuleGroupFinder.class.getName() + ".countByG_N";
050    
051            public static final String FIND_BY_G_N =
052                    MDRRuleGroupFinder.class.getName() + ".findByG_N";
053    
054            @Override
055            public int countByKeywords(
056                    long groupId, String keywords, LinkedHashMap<String, Object> params) {
057    
058                    String[] names = null;
059                    boolean andOperator = false;
060    
061                    if (Validator.isNotNull(keywords)) {
062                            names = CustomSQLUtil.keywords(keywords);
063                    }
064                    else {
065                            andOperator = true;
066                    }
067    
068                    return countByG_N(groupId, names, params, andOperator);
069            }
070    
071            @Override
072            public int countByG_N(
073                    long groupId, String name, LinkedHashMap<String, Object> params,
074                    boolean andOperator) {
075    
076                    String[] names = CustomSQLUtil.keywords(name);
077    
078                    return countByG_N(groupId, names, params, andOperator);
079            }
080    
081            @Override
082            public int countByG_N(
083                    long groupId, String[] names, LinkedHashMap<String, Object> params,
084                    boolean andOperator) {
085    
086                    names = CustomSQLUtil.keywords(names);
087    
088                    if (params == null) {
089                            params = _emptyLinkedHashMap;
090                    }
091    
092                    Session session = null;
093    
094                    try {
095                            session = openSession();
096    
097                            String sql = CustomSQLUtil.get(COUNT_BY_G_N);
098    
099                            sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupIds(params));
100                            sql = CustomSQLUtil.replaceKeywords(
101                                    sql, "lower(name)", StringPool.LIKE, true, names);
102                            sql = CustomSQLUtil.replaceAndOperator(sql, true);
103    
104                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
105    
106                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
107    
108                            QueryPos qPos = QueryPos.getInstance(q);
109    
110                            setGroupIds(qPos, groupId, params);
111    
112                            qPos.add(names, 2);
113    
114                            Iterator<Long> itr = q.iterate();
115    
116                            if (itr.hasNext()) {
117                                    Long count = itr.next();
118    
119                                    if (count != null) {
120                                            return count.intValue();
121                                    }
122                            }
123    
124                            return 0;
125                    }
126                    catch (Exception e) {
127                            throw new SystemException(e);
128                    }
129                    finally {
130                            closeSession(session);
131                    }
132            }
133    
134            @Override
135            public List<MDRRuleGroup> findByKeywords(
136                    long groupId, String keywords, LinkedHashMap<String, Object> params,
137                    int start, int end) {
138    
139                    String[] names = null;
140                    boolean andOperator = false;
141    
142                    if (Validator.isNotNull(keywords)) {
143                            names = CustomSQLUtil.keywords(keywords);
144                    }
145                    else {
146                            andOperator = true;
147                    }
148    
149                    return findByG_N(groupId, names, params, andOperator, start, end);
150            }
151    
152            @Override
153            public List<MDRRuleGroup> findByG_N(
154                    long groupId, String name, LinkedHashMap<String, Object> params,
155                    boolean andOperator) {
156    
157                    return findByG_N(
158                            groupId, name, params, andOperator, QueryUtil.ALL_POS,
159                            QueryUtil.ALL_POS);
160            }
161    
162            @Override
163            public List<MDRRuleGroup> findByG_N(
164                    long groupId, String name, LinkedHashMap<String, Object> params,
165                    boolean andOperator, int start, int end) {
166    
167                    String[] names = CustomSQLUtil.keywords(name);
168    
169                    return findByG_N(groupId, names, params, andOperator, start, end);
170            }
171    
172            @Override
173            public List<MDRRuleGroup> findByG_N(
174                    long groupId, String[] names, LinkedHashMap<String, Object> params,
175                    boolean andOperator, int start, int end) {
176    
177                    names = CustomSQLUtil.keywords(names);
178    
179                    if (params == null) {
180                            params = _emptyLinkedHashMap;
181                    }
182    
183                    Session session = null;
184    
185                    try {
186                            session = openSession();
187    
188                            String sql = CustomSQLUtil.get(FIND_BY_G_N);
189    
190                            sql = StringUtil.replace(sql, "[$GROUP_ID$]", getGroupIds(params));
191                            sql = CustomSQLUtil.replaceKeywords(
192                                    sql, "lower(name)", StringPool.LIKE, true, names);
193                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
194    
195                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
196    
197                            q.addEntity("MDRRuleGroup", MDRRuleGroupImpl.class);
198    
199                            QueryPos qPos = QueryPos.getInstance(q);
200    
201                            setGroupIds(qPos, groupId, params);
202    
203                            qPos.add(names, 2);
204    
205                            return (List<MDRRuleGroup>)QueryUtil.list(
206                                    q, getDialect(), start, end);
207                    }
208                    catch (Exception e) {
209                            throw new SystemException(e);
210                    }
211                    finally {
212                            closeSession(session);
213                    }
214            }
215    
216            protected String getGroupIds(Map<String, Object> params) {
217                    Boolean includeGlobalScope = (Boolean)params.get("includeGlobalScope");
218    
219                    if ((includeGlobalScope != null) && includeGlobalScope) {
220                            return "((groupId = ?) OR (groupId = ?))";
221                    }
222                    else {
223                            return "(groupId = ?)";
224                    }
225            }
226    
227            protected void setGroupIds(
228                            QueryPos qPos, long groupId, Map<String, Object> params)
229                    throws PortalException {
230    
231                    Boolean includeGlobalScope = (Boolean)params.get("includeGlobalScope");
232    
233                    if ((includeGlobalScope != null) && includeGlobalScope) {
234                            qPos.add(groupId);
235    
236                            Group group = GroupLocalServiceUtil.getGroup(groupId);
237    
238                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
239                                    group.getCompanyId());
240    
241                            qPos.add(companyGroup.getGroupId());
242                    }
243                    else {
244                            qPos.add(groupId);
245                    }
246            }
247    
248            private final LinkedHashMap<String, Object> _emptyLinkedHashMap =
249                    new LinkedHashMap<String, Object>(0);
250    
251    }