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