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