1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchRoleException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.model.Group;
30 import com.liferay.portal.model.Role;
31 import com.liferay.portal.model.impl.RoleImpl;
32 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
33 import com.liferay.portal.spring.hibernate.FinderCache;
34 import com.liferay.portal.spring.hibernate.HibernateUtil;
35 import com.liferay.util.dao.hibernate.QueryPos;
36 import com.liferay.util.dao.hibernate.QueryUtil;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Map;
43
44 import org.hibernate.Hibernate;
45 import org.hibernate.SQLQuery;
46 import org.hibernate.Session;
47
48
54 public class RoleFinder {
55
56 public static String COUNT_BY_C_N_D_T =
57 RoleFinder.class.getName() + ".countByC_N_D_T";
58
59 public static String COUNT_BY_COMMUNITY =
60 RoleFinder.class.getName() + ".countByCommunity";
61
62 public static String COUNT_BY_ORGANIZATION =
63 RoleFinder.class.getName() + ".countByOrganization";
64
65 public static String COUNT_BY_USER =
66 RoleFinder.class.getName() + ".countByUser";
67
68 public static String COUNT_BY_USER_GROUP =
69 RoleFinder.class.getName() + ".countByUserGroup";
70
71 public static String FIND_BY_USER_GROUP_ROLE =
72 RoleFinder.class.getName() + ".findByUserGroupRole";
73
74 public static String FIND_BY_C_N =
75 RoleFinder.class.getName() + ".findByC_N";
76
77 public static String FIND_BY_U_G =
78 RoleFinder.class.getName() + ".findByU_G";
79
80 public static String FIND_BY_C_N_D_T =
81 RoleFinder.class.getName() + ".findByC_N_D_T";
82
83 public static String FIND_BY_C_N_S_P =
84 RoleFinder.class.getName() + ".findByC_N_S_P";
85
86 public static int countByR_U(long roleId, long userId)
87 throws SystemException {
88
89 String finderSQL = Role.class.getName();
90 String[] finderClassNames = new String[] {
91 Group.class.getName(), Role.class.getName(), "Groups_Roles",
92 "Users_Groups", "Users_Orgs", "Users_Roles", "Users_UserGroups"
93 };
94 String finderMethodName = "customCountByR_U";
95 String finderParams[] = new String[] {
96 Long.class.getName(), Long.class.getName()
97 };
98 Object finderArgs[] = new Object[] {new Long(roleId), new Long(userId)};
99
100 Object result = FinderCache.getResult(
101 finderSQL, finderClassNames, finderMethodName, finderParams,
102 finderArgs);
103
104 if (result == null) {
105 Session session = null;
106
107 try {
108 session = HibernateUtil.openSession();
109
110 StringMaker sm = new StringMaker();
111
112 sm.append("(");
113 sm.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
114 sm.append(") UNION (");
115 sm.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
116 sm.append(") UNION (");
117 sm.append(CustomSQLUtil.get(COUNT_BY_USER));
118 sm.append(") UNION (");
119 sm.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
120 sm.append(")");
121
122 SQLQuery q = session.createSQLQuery(sm.toString());
123
124 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
125
126 QueryPos qPos = QueryPos.getInstance(q);
127
128 for (int i = 0; i < 4; i++) {
129 qPos.add(roleId);
130 qPos.add(userId);
131 }
132
133 int count = 0;
134
135 Iterator itr = q.list().iterator();
136
137 while (itr.hasNext()) {
138 Long l = (Long)itr.next();
139
140 if (l != null) {
141 count += l.intValue();
142 }
143 }
144
145 FinderCache.putResult(
146 finderSQL, finderClassNames, finderMethodName, finderParams,
147 finderArgs, new Long(count));
148
149 return count;
150 }
151 catch (Exception e) {
152 throw new SystemException(e);
153 }
154 finally {
155 HibernateUtil.closeSession(session);
156 }
157 }
158 else {
159 return ((Long)result).intValue();
160 }
161 }
162
163 public static int countByC_N_D_T(
164 long companyId, String name, String description, Integer type)
165 throws SystemException {
166
167 name = StringUtil.lowerCase(name);
168 description = StringUtil.lowerCase(description);
169
170 Session session = null;
171
172 try {
173 session = HibernateUtil.openSession();
174
175 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
176
177 if (type == null) {
178 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
179 }
180
181 SQLQuery q = session.createSQLQuery(sql);
182
183 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
184
185 QueryPos qPos = QueryPos.getInstance(q);
186
187 qPos.add(companyId);
188 qPos.add(name);
189 qPos.add(name);
190 qPos.add(description);
191 qPos.add(description);
192
193 if (type != null) {
194 qPos.add(type);
195 }
196
197 Iterator itr = q.list().iterator();
198
199 if (itr.hasNext()) {
200 Long count = (Long)itr.next();
201
202 if (count != null) {
203 return count.intValue();
204 }
205 }
206
207 return 0;
208 }
209 catch (Exception e) {
210 throw new SystemException(e);
211 }
212 finally {
213 HibernateUtil.closeSession(session);
214 }
215 }
216
217 public static List findByUserGroupRole(long userId, long groupId)
218 throws SystemException {
219
220 Session session = null;
221
222 try {
223 session = HibernateUtil.openSession();
224
225 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
226
227 SQLQuery q = session.createSQLQuery(sql);
228
229 q.addEntity("Role_", RoleImpl.class);
230
231 QueryPos qPos = QueryPos.getInstance(q);
232
233 qPos.add(userId);
234 qPos.add(groupId);
235
236 return q.list();
237 }
238 catch (Exception e) {
239 throw new SystemException(e);
240 }
241 finally {
242 HibernateUtil.closeSession(session);
243 }
244 }
245
246 public static Role findByC_N(long companyId, String name)
247 throws NoSuchRoleException, SystemException {
248
249 name = StringUtil.lowerCase(name);
250
251 String finderClassName = Role.class.getName();
252 String finderMethodName = "customFindByC_N";
253 String finderParams[] = new String[] {
254 Long.class.getName(), String.class.getName()
255 };
256 Object finderArgs[] = new Object[] {new Long(companyId), name};
257
258 Object result = FinderCache.getResult(
259 finderClassName, finderMethodName, finderParams, finderArgs);
260
261 if (result == null) {
262 Session session = null;
263
264 try {
265 session = HibernateUtil.openSession();
266
267 String sql = CustomSQLUtil.get(FIND_BY_C_N);
268
269 SQLQuery q = session.createSQLQuery(sql);
270
271 q.addEntity("Role_", RoleImpl.class);
272
273 QueryPos qPos = QueryPos.getInstance(q);
274
275 qPos.add(companyId);
276 qPos.add(name);
277
278 Iterator itr = q.list().iterator();
279
280 if (itr.hasNext()) {
281 Role role = (Role)itr.next();
282
283 FinderCache.putResult(
284 finderClassName, finderMethodName, finderParams,
285 finderArgs, role);
286
287 return role;
288 }
289 }
290 catch (Exception e) {
291 throw new SystemException(e);
292 }
293 finally {
294 HibernateUtil.closeSession(session);
295 }
296
297 throw new NoSuchRoleException(
298 "No Role exists with the key {companyId=" + companyId +
299 ", name=" + name + "}");
300 }
301 else {
302 return (Role)result;
303 }
304 }
305
306 public static List findByU_G(long userId, long groupId)
307 throws SystemException {
308
309 return findByU_G(userId, new long[] {groupId});
310 }
311
312 public static List findByU_G(long userId, long[] groupIds)
313 throws SystemException {
314
315 Session session = null;
316
317 try {
318 session = HibernateUtil.openSession();
319
320 String sql = CustomSQLUtil.get(FIND_BY_U_G);
321
322 sql = StringUtil.replace(
323 sql, "[$GROUP_IDS$]", _getGroupIds(groupIds, "Groups_Roles"));
324
325 SQLQuery q = session.createSQLQuery(sql);
326
327 q.addEntity("Role_", RoleImpl.class);
328
329 QueryPos qPos = QueryPos.getInstance(q);
330
331 qPos.add(userId);
332 _setGroupIds(qPos, groupIds);
333
334 return q.list();
335 }
336 catch (Exception e) {
337 throw new SystemException(e);
338 }
339 finally {
340 HibernateUtil.closeSession(session);
341 }
342 }
343
344 public static List findByU_G(long userId, List groups)
345 throws SystemException {
346
347 long[] groupIds = new long[groups.size()];
348
349 for (int i = 0; i < groups.size(); i++) {
350 Group group = (Group)groups.get(i);
351
352 groupIds[i] = group.getGroupId();
353 }
354
355 return findByU_G(userId, groupIds);
356 }
357
358 public static List findByC_N_D_T(
359 long companyId, String name, String description, Integer type,
360 int begin, int end)
361 throws SystemException {
362
363 name = StringUtil.lowerCase(name);
364 description = StringUtil.lowerCase(description);
365
366 Session session = null;
367
368 try {
369 session = HibernateUtil.openSession();
370
371 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
372
373 if (type == null) {
374 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
375 }
376
377 SQLQuery q = session.createSQLQuery(sql);
378
379 q.addEntity("Role_", RoleImpl.class);
380
381 QueryPos qPos = QueryPos.getInstance(q);
382
383 qPos.add(companyId);
384 qPos.add(name);
385 qPos.add(name);
386 qPos.add(description);
387 qPos.add(description);
388
389 if (type != null) {
390 qPos.add(type);
391 }
392
393 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
394 }
395 catch (Exception e) {
396 throw new SystemException(e);
397 }
398 finally {
399 HibernateUtil.closeSession(session);
400 }
401 }
402
403 public static Map findByC_N_S_P(
404 long companyId, String name, int scope, String primKey)
405 throws SystemException {
406
407 Session session = null;
408
409 try {
410 session = HibernateUtil.openSession();
411
412 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
413
414 SQLQuery q = session.createSQLQuery(sql);
415
416 q.addScalar("roleName", Hibernate.STRING);
417 q.addScalar("actionId", Hibernate.STRING);
418
419 QueryPos qPos = QueryPos.getInstance(q);
420
421 qPos.add(companyId);
422 qPos.add(name);
423 qPos.add(scope);
424 qPos.add(primKey);
425
426 Map roleMap = new HashMap();
427
428 Iterator itr = q.list().iterator();
429
430 while (itr.hasNext()) {
431 Object[] array = (Object[])itr.next();
432
433 String roleName = (String)array[0];
434 String actionId = (String)array[1];
435
436 List roleList = (List)roleMap.get(roleName);
437
438 if (roleList == null) {
439 roleList = new ArrayList();
440 }
441
442 roleList.add(actionId);
443
444 roleMap.put(roleName, roleList);
445 }
446
447 return roleMap;
448 }
449 catch (Exception e) {
450 throw new SystemException(e);
451 }
452 finally {
453 HibernateUtil.closeSession(session);
454 }
455 }
456
457 private static String _getGroupIds(long[] groupIds, String table) {
458 StringMaker sm = new StringMaker();
459
460 for (int i = 0; i < groupIds.length; i++) {
461 sm.append(table);
462 sm.append(".groupId = ?");
463
464 if ((i + 1) < groupIds.length) {
465 sm.append(" OR ");
466 }
467 }
468
469 return sm.toString();
470 }
471
472 private static void _setGroupIds(QueryPos qPos, long[] groupIds) {
473 for (int i = 0; i < groupIds.length; i++) {
474 qPos.add(groupIds[i]);
475 }
476 }
477
478 }