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