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