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