001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
204                                    q, getDialect(), start, end);
205                    }
206                    catch (Exception e) {
207                            throw new SystemException(e);
208                    }
209                    finally {
210                            closeSession(session);
211                    }
212            }
213    
214            protected String getJoin(LinkedHashMap<String, Object> params) {
215                    if ((params == null) || params.isEmpty()) {
216                            return StringPool.BLANK;
217                    }
218    
219                    StringBundler sb = new StringBundler(params.size());
220    
221                    Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
222    
223                    while (itr.hasNext()) {
224                            Map.Entry<String, Object> entry = itr.next();
225    
226                            String key = entry.getKey();
227                            Object value = entry.getValue();
228    
229                            if (Validator.isNotNull(value)) {
230                                    sb.append(getJoin(key));
231                            }
232                    }
233    
234                    return sb.toString();
235            }
236    
237            protected String getJoin(String key) {
238                    String join = StringPool.BLANK;
239    
240                    if (key.equals("permissionsResourceId")) {
241                            join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
242                    }
243                    else if (key.equals("userGroupGroupRole")) {
244                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
245                    }
246                    else if (key.equals("userGroupsGroups")) {
247                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
248                    }
249                    else if (key.equals("userGroupsRoles")) {
250                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
251                    }
252                    else if (key.equals("userGroupsTeams")) {
253                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
254                    }
255                    else if (key.equals("userGroupsUsers")) {
256                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
257                    }
258    
259                    if (Validator.isNotNull(join)) {
260                            int pos = join.indexOf("WHERE");
261    
262                            if (pos != -1) {
263                                    join = join.substring(0, pos);
264                            }
265                    }
266    
267                    return join;
268            }
269    
270            protected String getWhere(LinkedHashMap<String, Object> params) {
271                    if ((params == null) || params.isEmpty()) {
272                            return StringPool.BLANK;
273                    }
274    
275                    StringBundler sb = new StringBundler(params.size());
276    
277                    Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
278    
279                    while (itr.hasNext()) {
280                            Map.Entry<String, Object> entry = itr.next();
281    
282                            String key = entry.getKey();
283                            Object value = entry.getValue();
284    
285                            if (Validator.isNotNull(value)) {
286                                    sb.append(getWhere(key));
287                            }
288                    }
289    
290                    return sb.toString();
291            }
292    
293            protected String getWhere(String key) {
294                    String join = StringPool.BLANK;
295    
296                    if (key.equals("permissionsResourceId")) {
297                            join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
298                    }
299                    else if (key.equals("userGroupGroupRole")) {
300                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
301                    }
302                    else if (key.equals("userGroupsGroups")) {
303                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
304                    }
305                    else if (key.equals("userGroupsRoles")) {
306                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
307                    }
308                    else if (key.equals("userGroupsTeams")) {
309                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
310                    }
311                    else if (key.equals("userGroupsUsers")) {
312                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
313                    }
314    
315                    if (Validator.isNotNull(join)) {
316                            int pos = join.indexOf("WHERE");
317    
318                            if (pos != -1) {
319                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
320                            }
321                            else {
322                                    join = StringPool.BLANK;
323                            }
324                    }
325    
326                    return join;
327            }
328    
329            protected void setJoin(
330                    QueryPos qPos, LinkedHashMap<String, Object> params) {
331    
332                    if (params == null) {
333                            return;
334                    }
335    
336                    for (Map.Entry<String, Object> entry : params.entrySet()) {
337                            Object value = entry.getValue();
338    
339                            if (value instanceof Long) {
340                                    Long valueLong = (Long)value;
341    
342                                    if (Validator.isNotNull(valueLong)) {
343                                            qPos.add(valueLong);
344                                    }
345                            }
346                            else if (value instanceof Long[]) {
347                                    Long[] valueArray = (Long[])value;
348    
349                                    for (int i = 0; i < valueArray.length; i++) {
350                                            if (Validator.isNotNull(valueArray[i])) {
351                                                    qPos.add(valueArray[i]);
352                                            }
353                                    }
354                            }
355                            else if (value instanceof String) {
356                                    String valueString = (String)value;
357    
358                                    if (Validator.isNotNull(valueString)) {
359                                            qPos.add(valueString);
360                                    }
361                            }
362                    }
363            }
364    
365    }