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.portal.service.persistence.impl;
016    
017    import com.liferay.portal.exception.NoSuchUserGroupException;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.UserGroup;
030    import com.liferay.portal.model.impl.UserGroupImpl;
031    import com.liferay.portal.service.persistence.UserGroupFinder;
032    import com.liferay.portal.service.persistence.UserGroupUtil;
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 Charles May
042     */
043    public class UserGroupFinderImpl
044            extends UserGroupFinderBaseImpl implements UserGroupFinder {
045    
046            public static final String COUNT_BY_C_N_D =
047                    UserGroupFinder.class.getName() + ".countByC_N_D";
048    
049            public static final String FIND_BY_C_N_D =
050                    UserGroupFinder.class.getName() + ".findByC_N_D";
051    
052            public static final String JOIN_BY_USER_GROUP_GROUP_ROLE =
053                    UserGroupFinder.class.getName() + ".joinByUserGroupGroupRole";
054    
055            public static final String JOIN_BY_USER_GROUPS_GROUPS =
056                    UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
057    
058            public static final String JOIN_BY_USER_GROUPS_ROLES =
059                    UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
060    
061            public static final String JOIN_BY_USER_GROUPS_TEAMS =
062                    UserGroupFinder.class.getName() + ".joinByUserGroupsTeams";
063    
064            public static final String JOIN_BY_USER_GROUPS_USERS =
065                    UserGroupFinder.class.getName() + ".joinByUserGroupsUsers";
066    
067            @Override
068            public int countByKeywords(
069                    long companyId, String keywords, LinkedHashMap<String, Object> params) {
070    
071                    String[] names = null;
072                    String[] descriptions = null;
073                    boolean andOperator = false;
074    
075                    if (Validator.isNotNull(keywords)) {
076                            names = CustomSQLUtil.keywords(keywords);
077                            descriptions = CustomSQLUtil.keywords(keywords);
078                    }
079                    else {
080                            andOperator = true;
081                    }
082    
083                    return countByC_N_D(
084                            companyId, names, descriptions, params, andOperator);
085            }
086    
087            @Override
088            public int countByC_N_D(
089                    long companyId, String name, String description,
090                    LinkedHashMap<String, Object> params, boolean andOperator) {
091    
092                    String[] names = CustomSQLUtil.keywords(name);
093                    String[] descriptions = CustomSQLUtil.keywords(description);
094    
095                    return countByC_N_D(
096                            companyId, names, descriptions, params, andOperator);
097            }
098    
099            @Override
100            public int countByC_N_D(
101                    long companyId, String[] names, String[] descriptions,
102                    LinkedHashMap<String, Object> params, boolean andOperator) {
103    
104                    names = CustomSQLUtil.keywords(names);
105                    descriptions = CustomSQLUtil.keywords(descriptions);
106    
107                    Session session = null;
108    
109                    try {
110                            session = openSession();
111    
112                            String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
113    
114                            sql = CustomSQLUtil.replaceKeywords(
115                                    sql, "lower(UserGroup.name)", StringPool.LIKE, false, names);
116                            sql = CustomSQLUtil.replaceKeywords(
117                                    sql, "lower(UserGroup.description)", StringPool.LIKE, true,
118                                    descriptions);
119                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
120                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
121                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
122    
123                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
124    
125                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
126    
127                            QueryPos qPos = QueryPos.getInstance(q);
128    
129                            setJoin(qPos, params);
130    
131                            qPos.add(companyId);
132                            qPos.add(names, 2);
133                            qPos.add(descriptions, 2);
134    
135                            Iterator<Long> itr = q.iterate();
136    
137                            if (itr.hasNext()) {
138                                    Long count = itr.next();
139    
140                                    if (count != null) {
141                                            return count.intValue();
142                                    }
143                            }
144    
145                            return 0;
146                    }
147                    catch (Exception e) {
148                            throw new SystemException(e);
149                    }
150                    finally {
151                            closeSession(session);
152                    }
153            }
154    
155            @Override
156            public List<UserGroup> findByKeywords(
157                    long companyId, String keywords, LinkedHashMap<String, Object> params,
158                    int start, int end, OrderByComparator<UserGroup> obc) {
159    
160                    String[] names = null;
161                    String[] descriptions = null;
162                    boolean andOperator = false;
163    
164                    if (Validator.isNotNull(keywords)) {
165                            names = CustomSQLUtil.keywords(keywords);
166                            descriptions = CustomSQLUtil.keywords(keywords);
167                    }
168                    else {
169                            andOperator = true;
170                    }
171    
172                    return findByC_N_D(
173                            companyId, names, descriptions, params, andOperator, start, end,
174                            obc);
175            }
176    
177            /**
178             * @deprecated As of 7.0.0, with no direct replacement
179             */
180            @Deprecated
181            @Override
182            public UserGroup findByC_N(long companyId, String name)
183                    throws NoSuchUserGroupException {
184    
185                    return UserGroupUtil.findByC_N(companyId, name);
186            }
187    
188            @Override
189            public List<UserGroup> findByC_N_D(
190                    long companyId, String name, String description,
191                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
192                    int end, OrderByComparator<UserGroup> obc) {
193    
194                    String[] names = CustomSQLUtil.keywords(name);
195                    String[] descriptions = CustomSQLUtil.keywords(description);
196    
197                    return findByC_N_D(
198                            companyId, names, descriptions, params, andOperator, start, end,
199                            obc);
200            }
201    
202            @Override
203            public List<UserGroup> findByC_N_D(
204                    long companyId, String[] names, String[] descriptions,
205                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
206                    int end, OrderByComparator<UserGroup> obc) {
207    
208                    names = CustomSQLUtil.keywords(names);
209                    descriptions = CustomSQLUtil.keywords(descriptions);
210    
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
217    
218                            sql = CustomSQLUtil.replaceKeywords(
219                                    sql, "lower(UserGroup.name)", StringPool.LIKE, false, names);
220                            sql = CustomSQLUtil.replaceKeywords(
221                                    sql, "lower(UserGroup.description)", StringPool.LIKE, true,
222                                    descriptions);
223    
224                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
225                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
226                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
227                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
228    
229                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
230    
231                            q.addEntity("UserGroup", UserGroupImpl.class);
232    
233                            QueryPos qPos = QueryPos.getInstance(q);
234    
235                            setJoin(qPos, params);
236    
237                            qPos.add(companyId);
238                            qPos.add(names, 2);
239                            qPos.add(descriptions, 2);
240    
241                            return (List<UserGroup>)QueryUtil.list(q, getDialect(), start, end);
242                    }
243                    catch (Exception e) {
244                            throw new SystemException(e);
245                    }
246                    finally {
247                            closeSession(session);
248                    }
249            }
250    
251            protected String getJoin(LinkedHashMap<String, Object> params) {
252                    if ((params == null) || params.isEmpty()) {
253                            return StringPool.BLANK;
254                    }
255    
256                    StringBundler sb = new StringBundler(params.size());
257    
258                    for (Map.Entry<String, Object> entry : params.entrySet()) {
259                            String key = entry.getKey();
260                            Object value = entry.getValue();
261    
262                            if (Validator.isNotNull(value)) {
263                                    sb.append(getJoin(key));
264                            }
265                    }
266    
267                    return sb.toString();
268            }
269    
270            protected String getJoin(String key) {
271                    String join = StringPool.BLANK;
272    
273                    if (key.equals("userGroupGroupRole")) {
274                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
275                    }
276                    else if (key.equals("userGroupsGroups")) {
277                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
278                    }
279                    else if (key.equals("userGroupsRoles")) {
280                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
281                    }
282                    else if (key.equals("userGroupsTeams")) {
283                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
284                    }
285                    else if (key.equals("userGroupsUsers")) {
286                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
287                    }
288    
289                    if (Validator.isNotNull(join)) {
290                            int pos = join.indexOf("WHERE");
291    
292                            if (pos != -1) {
293                                    join = join.substring(0, pos);
294                            }
295                    }
296    
297                    return join;
298            }
299    
300            protected String getWhere(LinkedHashMap<String, Object> params) {
301                    if ((params == null) || params.isEmpty()) {
302                            return StringPool.BLANK;
303                    }
304    
305                    StringBundler sb = new StringBundler(params.size());
306    
307                    for (Map.Entry<String, Object> entry : params.entrySet()) {
308                            String key = entry.getKey();
309                            Object value = entry.getValue();
310    
311                            if (Validator.isNotNull(value)) {
312                                    sb.append(getWhere(key, value));
313                            }
314                    }
315    
316                    return sb.toString();
317            }
318    
319            protected String getWhere(String key, Object value) {
320                    String join = StringPool.BLANK;
321    
322                    if (key.equals("userGroupGroupRole")) {
323                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
324                    }
325                    else if (key.equals("userGroupsGroups")) {
326                            if (value instanceof Long) {
327                                    join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
328                            }
329                            else if (value instanceof Long[]) {
330                                    Long[] userGroupIds = (Long[])value;
331    
332                                    if (userGroupIds.length == 0) {
333                                            join = "WHERE (Groups_UserGroups.groupId = -1)";
334                                    }
335                                    else {
336                                            StringBundler sb = new StringBundler(
337                                                    userGroupIds.length * 2 + 1);
338    
339                                            sb.append("WHERE (");
340    
341                                            for (int i = 0; i < userGroupIds.length; i++) {
342                                                    sb.append("(Groups_UserGroups.groupId = ?) ");
343    
344                                                    if ((i + 1) < userGroupIds.length) {
345                                                            sb.append("OR ");
346                                                    }
347                                            }
348    
349                                            sb.append(StringPool.CLOSE_PARENTHESIS);
350    
351                                            join = sb.toString();
352                                    }
353                            }
354                    }
355                    else if (key.equals("userGroupsRoles")) {
356                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
357                    }
358                    else if (key.equals("userGroupsTeams")) {
359                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
360                    }
361                    else if (key.equals("userGroupsUsers")) {
362                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
363                    }
364    
365                    if (Validator.isNotNull(join)) {
366                            int pos = join.indexOf("WHERE");
367    
368                            if (pos != -1) {
369                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
370                            }
371                            else {
372                                    join = StringPool.BLANK;
373                            }
374                    }
375    
376                    return join;
377            }
378    
379            protected void setJoin(
380                    QueryPos qPos, LinkedHashMap<String, Object> params) {
381    
382                    if (params == null) {
383                            return;
384                    }
385    
386                    for (Map.Entry<String, Object> entry : params.entrySet()) {
387                            Object value = entry.getValue();
388    
389                            if (value instanceof Long) {
390                                    Long valueLong = (Long)value;
391    
392                                    if (Validator.isNotNull(valueLong)) {
393                                            qPos.add(valueLong);
394                                    }
395                            }
396                            else if (value instanceof Long[]) {
397                                    Long[] valueArray = (Long[])value;
398    
399                                    for (int i = 0; i < valueArray.length; i++) {
400                                            if (Validator.isNotNull(valueArray[i])) {
401                                                    qPos.add(valueArray[i]);
402                                            }
403                                    }
404                            }
405                            else if (value instanceof String) {
406                                    String valueString = (String)value;
407    
408                                    if (Validator.isNotNull(valueString)) {
409                                            qPos.add(valueString);
410                                    }
411                            }
412                    }
413            }
414    
415    }