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_GROUPS_GROUPS =
60          UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
61  
62      public static String JOIN_BY_USER_GROUPS_ROLES =
63          UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
64  
65      public int countByC_N_D(
66              long companyId, String name, String description,
67              LinkedHashMap<String, Object> params)
68          throws SystemException {
69  
70          name = StringUtil.lowerCase(name);
71          description = StringUtil.lowerCase(description);
72  
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
79  
80              sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
81              sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
82  
83              SQLQuery q = session.createSQLQuery(sql);
84  
85              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
86  
87              QueryPos qPos = QueryPos.getInstance(q);
88  
89              setJoin(qPos, params);
90              qPos.add(companyId);
91              qPos.add(name);
92              qPos.add(name);
93              qPos.add(description);
94              qPos.add(description);
95  
96              Iterator<Long> itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             closeSession(session);
113         }
114     }
115 
116     public UserGroup findByC_N(long companyId, String name)
117         throws NoSuchUserGroupException, SystemException {
118 
119         name = StringUtil.lowerCase(name);
120 
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             String sql = CustomSQLUtil.get(FIND_BY_C_N);
127 
128             SQLQuery q = session.createSQLQuery(sql);
129 
130             q.addEntity("UserGroup", UserGroupImpl.class);
131 
132             QueryPos qPos = QueryPos.getInstance(q);
133 
134             qPos.add(companyId);
135             qPos.add(name);
136 
137             List<UserGroup> list = q.list();
138 
139             if (!list.isEmpty()) {
140                 return list.get(0);
141             }
142         }
143         catch (Exception e) {
144             throw new SystemException(e);
145         }
146         finally {
147             closeSession(session);
148         }
149 
150         StringBundler sb = new StringBundler(5);
151 
152         sb.append("No UserGroup exists with the key {companyId=");
153         sb.append(companyId);
154         sb.append(", name=");
155         sb.append(name);
156         sb.append("}");
157 
158         throw new NoSuchUserGroupException(sb.toString());
159     }
160 
161     public List<UserGroup> findByC_N_D(
162             long companyId, String name, String description,
163             LinkedHashMap<String, Object> params, int start, int end,
164             OrderByComparator obc)
165         throws SystemException {
166 
167         name = StringUtil.lowerCase(name);
168         description = StringUtil.lowerCase(description);
169 
170         Session session = null;
171 
172         try {
173             session = openSession();
174 
175             String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
176 
177             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
178             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
179             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
180 
181             SQLQuery q = session.createSQLQuery(sql);
182 
183             q.addEntity("UserGroup", UserGroupImpl.class);
184 
185             QueryPos qPos = QueryPos.getInstance(q);
186 
187             setJoin(qPos, params);
188             qPos.add(companyId);
189             qPos.add(name);
190             qPos.add(name);
191             qPos.add(description);
192             qPos.add(description);
193 
194             return (List<UserGroup>)QueryUtil.list(
195                 q, getDialect(), start, end);
196         }
197         catch (Exception e) {
198             throw new SystemException(e);
199         }
200         finally {
201             closeSession(session);
202         }
203     }
204 
205     protected String getJoin(LinkedHashMap<String, Object> params) {
206         if ((params == null) || params.isEmpty()) {
207             return StringPool.BLANK;
208         }
209 
210         StringBundler sb = new StringBundler(params.size());
211 
212         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
213 
214         while (itr.hasNext()) {
215             Map.Entry<String, Object> entry = itr.next();
216 
217             String key = entry.getKey();
218             Object value = entry.getValue();
219 
220             if (Validator.isNotNull(value)) {
221                 sb.append(getJoin(key));
222             }
223         }
224 
225         return sb.toString();
226     }
227 
228     protected String getJoin(String key) {
229         String join = StringPool.BLANK;
230 
231         if (key.equals("permissionsResourceId")) {
232             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
233         }
234         else if (key.equals("userGroupsGroups")) {
235             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
236         }
237         else if (key.equals("userGroupsRoles")) {
238             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
239         }
240 
241         if (Validator.isNotNull(join)) {
242             int pos = join.indexOf("WHERE");
243 
244             if (pos != -1) {
245                 join = join.substring(0, pos);
246             }
247         }
248 
249         return join;
250     }
251 
252     protected String getWhere(LinkedHashMap<String, Object> params) {
253         if ((params == null) || params.isEmpty()) {
254             return StringPool.BLANK;
255         }
256 
257         StringBundler sb = new StringBundler(params.size());
258 
259         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
260 
261         while (itr.hasNext()) {
262             Map.Entry<String, Object> entry = itr.next();
263 
264             String key = entry.getKey();
265             Object value = entry.getValue();
266 
267             if (Validator.isNotNull(value)) {
268                 sb.append(getWhere(key));
269             }
270         }
271 
272         return sb.toString();
273     }
274 
275     protected String getWhere(String key) {
276         String join = StringPool.BLANK;
277 
278         if (key.equals("permissionsResourceId")) {
279             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
280         }
281         else if (key.equals("userGroupsGroups")) {
282             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
283         }
284         else if (key.equals("userGroupsRoles")) {
285             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
286         }
287 
288         if (Validator.isNotNull(join)) {
289             int pos = join.indexOf("WHERE");
290 
291             if (pos != -1) {
292                 join = join.substring(pos + 5, join.length()).concat(" AND ");
293             }
294             else {
295                 join = StringPool.BLANK;
296             }
297         }
298 
299         return join;
300     }
301 
302     protected void setJoin(
303         QueryPos qPos, LinkedHashMap<String, Object> params) {
304 
305         if (params != null) {
306             Iterator<Map.Entry<String, Object>> itr =
307                 params.entrySet().iterator();
308 
309             while (itr.hasNext()) {
310                 Map.Entry<String, Object> entry = itr.next();
311 
312                 Object value = entry.getValue();
313 
314                 if (value instanceof Long) {
315                     Long valueLong = (Long)value;
316 
317                     if (Validator.isNotNull(valueLong)) {
318                         qPos.add(valueLong);
319                     }
320                 }
321                 else if (value instanceof String) {
322                     String valueString = (String)value;
323 
324                     if (Validator.isNotNull(valueString)) {
325                         qPos.add(valueString);
326                     }
327                 }
328             }
329         }
330     }
331 
332 }