1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchUserGroupException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.dao.orm.QueryPos;
20  import com.liferay.portal.kernel.dao.orm.QueryUtil;
21  import com.liferay.portal.kernel.dao.orm.SQLQuery;
22  import com.liferay.portal.kernel.dao.orm.Session;
23  import com.liferay.portal.kernel.dao.orm.Type;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.UserGroup;
30  import com.liferay.portal.model.impl.UserGroupImpl;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.Iterator;
35  import java.util.LinkedHashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * <a href="UserGroupFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Charles May
43   */
44  public class UserGroupFinderImpl
45      extends BasePersistenceImpl<UserGroup> implements UserGroupFinder {
46  
47      public static String COUNT_BY_C_N_D =
48          UserGroupFinder.class.getName() + ".countByC_N_D";
49  
50      public static String FIND_BY_C_N =
51          UserGroupFinder.class.getName() + ".findByC_N";
52  
53      public static String FIND_BY_C_N_D =
54          UserGroupFinder.class.getName() + ".findByC_N_D";
55  
56      public static String JOIN_BY_GROUPS_PERMISSIONS =
57          UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
58  
59      public static String JOIN_BY_USER_GROUP_GROUP_ROLE =
60          UserGroupFinder.class.getName() + ".joinByUserGroupGroupRole";
61  
62      public static String JOIN_BY_USER_GROUPS_GROUPS =
63          UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
64  
65      public static String JOIN_BY_USER_GROUPS_ROLES =
66          UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
67  
68      public int countByC_N_D(
69              long companyId, String name, String description,
70              LinkedHashMap<String, Object> params)
71          throws SystemException {
72  
73          name = StringUtil.lowerCase(name);
74          description = StringUtil.lowerCase(description);
75  
76          Session session = null;
77  
78          try {
79              session = openSession();
80  
81              String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
82  
83              sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
84              sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
85  
86              SQLQuery q = session.createSQLQuery(sql);
87  
88              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
89  
90              QueryPos qPos = QueryPos.getInstance(q);
91  
92              setJoin(qPos, params);
93              qPos.add(companyId);
94              qPos.add(name);
95              qPos.add(name);
96              qPos.add(description);
97              qPos.add(description);
98  
99              Iterator<Long> itr = q.list().iterator();
100 
101             if (itr.hasNext()) {
102                 Long count = itr.next();
103 
104                 if (count != null) {
105                     return count.intValue();
106                 }
107             }
108 
109             return 0;
110         }
111         catch (Exception e) {
112             throw new SystemException(e);
113         }
114         finally {
115             closeSession(session);
116         }
117     }
118 
119     public UserGroup findByC_N(long companyId, String name)
120         throws NoSuchUserGroupException, SystemException {
121 
122         name = StringUtil.lowerCase(name);
123 
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             String sql = CustomSQLUtil.get(FIND_BY_C_N);
130 
131             SQLQuery q = session.createSQLQuery(sql);
132 
133             q.addEntity("UserGroup", UserGroupImpl.class);
134 
135             QueryPos qPos = QueryPos.getInstance(q);
136 
137             qPos.add(companyId);
138             qPos.add(name);
139 
140             List<UserGroup> list = q.list();
141 
142             if (!list.isEmpty()) {
143                 return list.get(0);
144             }
145         }
146         catch (Exception e) {
147             throw new SystemException(e);
148         }
149         finally {
150             closeSession(session);
151         }
152 
153         StringBundler sb = new StringBundler(5);
154 
155         sb.append("No UserGroup exists with the key {companyId=");
156         sb.append(companyId);
157         sb.append(", name=");
158         sb.append(name);
159         sb.append("}");
160 
161         throw new NoSuchUserGroupException(sb.toString());
162     }
163 
164     public List<UserGroup> findByC_N_D(
165             long companyId, String name, String description,
166             LinkedHashMap<String, Object> params, int start, int end,
167             OrderByComparator obc)
168         throws SystemException {
169 
170         name = StringUtil.lowerCase(name);
171         description = StringUtil.lowerCase(description);
172 
173         Session session = null;
174 
175         try {
176             session = openSession();
177 
178             String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
179 
180             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
181             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
182             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
183 
184             SQLQuery q = session.createSQLQuery(sql);
185 
186             q.addEntity("UserGroup", UserGroupImpl.class);
187 
188             QueryPos qPos = QueryPos.getInstance(q);
189 
190             setJoin(qPos, params);
191             qPos.add(companyId);
192             qPos.add(name);
193             qPos.add(name);
194             qPos.add(description);
195             qPos.add(description);
196 
197             return (List<UserGroup>)QueryUtil.list(
198                 q, getDialect(), start, end);
199         }
200         catch (Exception e) {
201             throw new SystemException(e);
202         }
203         finally {
204             closeSession(session);
205         }
206     }
207 
208     protected String getJoin(LinkedHashMap<String, Object> params) {
209         if ((params == null) || params.isEmpty()) {
210             return StringPool.BLANK;
211         }
212 
213         StringBundler sb = new StringBundler(params.size());
214 
215         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
216 
217         while (itr.hasNext()) {
218             Map.Entry<String, Object> entry = itr.next();
219 
220             String key = entry.getKey();
221             Object value = entry.getValue();
222 
223             if (Validator.isNotNull(value)) {
224                 sb.append(getJoin(key));
225             }
226         }
227 
228         return sb.toString();
229     }
230 
231     protected String getJoin(String key) {
232         String join = StringPool.BLANK;
233 
234         if (key.equals("permissionsResourceId")) {
235             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
236         }
237         else if (key.equals("userGroupGroupRole")) {
238             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
239         }
240         else if (key.equals("userGroupsGroups")) {
241             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
242         }
243         else if (key.equals("userGroupsRoles")) {
244             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
245         }
246 
247         if (Validator.isNotNull(join)) {
248             int pos = join.indexOf("WHERE");
249 
250             if (pos != -1) {
251                 join = join.substring(0, pos);
252             }
253         }
254 
255         return join;
256     }
257 
258     protected String getWhere(LinkedHashMap<String, Object> params) {
259         if ((params == null) || params.isEmpty()) {
260             return StringPool.BLANK;
261         }
262 
263         StringBundler sb = new StringBundler(params.size());
264 
265         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
266 
267         while (itr.hasNext()) {
268             Map.Entry<String, Object> entry = itr.next();
269 
270             String key = entry.getKey();
271             Object value = entry.getValue();
272 
273             if (Validator.isNotNull(value)) {
274                 sb.append(getWhere(key));
275             }
276         }
277 
278         return sb.toString();
279     }
280 
281     protected String getWhere(String key) {
282         String join = StringPool.BLANK;
283 
284         if (key.equals("permissionsResourceId")) {
285             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
286         }
287         else if (key.equals("userGroupGroupRole")) {
288             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
289         }
290         else if (key.equals("userGroupsGroups")) {
291             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
292         }
293         else if (key.equals("userGroupsRoles")) {
294             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
295         }
296 
297         if (Validator.isNotNull(join)) {
298             int pos = join.indexOf("WHERE");
299 
300             if (pos != -1) {
301                 join = join.substring(pos + 5, join.length()).concat(" AND ");
302             }
303             else {
304                 join = StringPool.BLANK;
305             }
306         }
307 
308         return join;
309     }
310 
311     protected void setJoin(
312         QueryPos qPos, LinkedHashMap<String, Object> params) {
313 
314         if (params != null) {
315             Iterator<Map.Entry<String, Object>> itr =
316                 params.entrySet().iterator();
317 
318             while (itr.hasNext()) {
319                 Map.Entry<String, Object> entry = itr.next();
320 
321                 Object value = entry.getValue();
322 
323                 if (value instanceof Long) {
324                     Long valueLong = (Long)value;
325 
326                     if (Validator.isNotNull(valueLong)) {
327                         qPos.add(valueLong);
328                     }
329                 }
330                 else if (value instanceof Long[]) {
331                     Long[] valueArray = (Long[])value;
332 
333                     for (int i = 0; i < valueArray.length; i++) {
334                         if (Validator.isNotNull(valueArray[i])) {
335                             qPos.add(valueArray[i]);
336                         }
337                     }
338                 }
339                 else if (value instanceof String) {
340                     String valueString = (String)value;
341 
342                     if (Validator.isNotNull(valueString)) {
343                         qPos.add(valueString);
344                     }
345                 }
346             }
347         }
348     }
349 
350 }