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.dao.orm.FinderCacheUtil;
28 import com.liferay.portal.kernel.dao.orm.QueryPos;
29 import com.liferay.portal.kernel.dao.orm.QueryUtil;
30 import com.liferay.portal.kernel.dao.orm.SQLQuery;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.dao.orm.Type;
33 import com.liferay.portal.kernel.util.ArrayUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.model.Group;
39 import com.liferay.portal.model.Role;
40 import com.liferay.portal.model.impl.GroupModelImpl;
41 import com.liferay.portal.model.impl.RoleImpl;
42 import com.liferay.portal.model.impl.RoleModelImpl;
43 import com.liferay.portal.model.impl.UserModelImpl;
44 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45 import com.liferay.util.dao.orm.CustomSQLUtil;
46
47 import java.util.ArrayList;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.LinkedHashMap;
51 import java.util.List;
52 import java.util.Map;
53
54
60 public class RoleFinderImpl extends BasePersistenceImpl implements RoleFinder {
61
62 public static String COUNT_BY_C_N_D_T =
63 RoleFinder.class.getName() + ".countByC_N_D_T";
64
65 public static String COUNT_BY_COMMUNITY =
66 RoleFinder.class.getName() + ".countByCommunity";
67
68 public static String COUNT_BY_ORGANIZATION =
69 RoleFinder.class.getName() + ".countByOrganization";
70
71 public static String COUNT_BY_USER =
72 RoleFinder.class.getName() + ".countByUser";
73
74 public static String COUNT_BY_USER_GROUP =
75 RoleFinder.class.getName() + ".countByUserGroup";
76
77 public static String FIND_BY_USER_GROUP_ROLE =
78 RoleFinder.class.getName() + ".findByUserGroupRole";
79
80 public static String FIND_BY_C_N =
81 RoleFinder.class.getName() + ".findByC_N";
82
83 public static String FIND_BY_U_G =
84 RoleFinder.class.getName() + ".findByU_G";
85
86 public static String FIND_BY_C_N_D_T =
87 RoleFinder.class.getName() + ".findByC_N_D_T";
88
89 public static String FIND_BY_C_N_S_P =
90 RoleFinder.class.getName() + ".findByC_N_S_P";
91
92 public static String JOIN_BY_ROLES_PERMISSIONS =
93 RoleFinder.class.getName() + ".joinByRolesPermissions";
94
95 public static String JOIN_BY_USERS_ROLES =
96 RoleFinder.class.getName() + ".joinByUsersRoles";
97
98 public int countByR_U(long roleId, long userId) throws SystemException {
99 String finderSQL = Role.class.getName();
100 boolean[] finderClassNamesCacheEnabled = new boolean[] {
101 GroupModelImpl.CACHE_ENABLED, RoleModelImpl.CACHE_ENABLED,
102 GroupModelImpl.CACHE_ENABLED_GROUPS_ROLES,
103 UserModelImpl.CACHE_ENABLED_USERS_GROUPS,
104 UserModelImpl.CACHE_ENABLED_USERS_ORGS,
105 UserModelImpl.CACHE_ENABLED_USERS_ROLES,
106 UserModelImpl.CACHE_ENABLED_USERS_USERGROUPS
107 };
108 String[] finderClassNames = new String[] {
109 Group.class.getName(), Role.class.getName(), "Groups_Roles",
110 "Users_Groups", "Users_Orgs", "Users_Roles", "Users_UserGroups"
111 };
112 String finderMethodName = "customCountByR_U";
113 String finderParams[] = new String[] {
114 Long.class.getName(), Long.class.getName()
115 };
116 Object finderArgs[] = new Object[] {new Long(roleId), new Long(userId)};
117
118 Object result = null;
119
120 if (!ArrayUtil.contains(finderClassNamesCacheEnabled, false)) {
121 result = FinderCacheUtil.getResult(
122 finderSQL, finderClassNames, finderMethodName, finderParams,
123 finderArgs, this);
124 }
125
126 if (result == null) {
127 Session session = null;
128
129 try {
130 session = openSession();
131
132 StringBuilder sb = new StringBuilder();
133
134 sb.append("(");
135 sb.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
136 sb.append(") UNION (");
137 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
138 sb.append(") UNION (");
139 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
140 sb.append(") UNION (");
141 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
142 sb.append(")");
143
144 SQLQuery q = session.createSQLQuery(sb.toString());
145
146 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
147
148 QueryPos qPos = QueryPos.getInstance(q);
149
150 for (int i = 0; i < 4; i++) {
151 qPos.add(roleId);
152 qPos.add(userId);
153 }
154
155 int count = 0;
156
157 Iterator<Long> itr = q.list().iterator();
158
159 while (itr.hasNext()) {
160 Long l = itr.next();
161
162 if (l != null) {
163 count += l.intValue();
164 }
165 }
166
167 FinderCacheUtil.putResult(
168 finderSQL, finderClassNamesCacheEnabled, finderClassNames,
169 finderMethodName, finderParams, finderArgs,
170 new Long(count));
171
172 return count;
173 }
174 catch (Exception e) {
175 throw new SystemException(e);
176 }
177 finally {
178 closeSession(session);
179 }
180 }
181 else {
182 return ((Long)result).intValue();
183 }
184 }
185
186 public int countByC_N_D_T(
187 long companyId, String name, String description, Integer type,
188 LinkedHashMap<String, Object> params)
189 throws SystemException {
190
191 name = StringUtil.lowerCase(name);
192 description = StringUtil.lowerCase(description);
193
194 Session session = null;
195
196 try {
197 session = openSession();
198
199 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
200
201 if (type == null) {
202 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
203 }
204
205 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
206 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
207
208 SQLQuery q = session.createSQLQuery(sql);
209
210 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
211
212 QueryPos qPos = QueryPos.getInstance(q);
213
214 setJoin(qPos, params);
215 qPos.add(companyId);
216 qPos.add(name);
217 qPos.add(name);
218 qPos.add(description);
219 qPos.add(description);
220
221 if (type != null) {
222 qPos.add(type);
223 }
224
225 Iterator<Long> itr = q.list().iterator();
226
227 if (itr.hasNext()) {
228 Long count = itr.next();
229
230 if (count != null) {
231 return count.intValue();
232 }
233 }
234
235 return 0;
236 }
237 catch (Exception e) {
238 throw new SystemException(e);
239 }
240 finally {
241 closeSession(session);
242 }
243 }
244
245 public List<Role> findByUserGroupRole(long userId, long groupId)
246 throws SystemException {
247
248 Session session = null;
249
250 try {
251 session = openSession();
252
253 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
254
255 SQLQuery q = session.createSQLQuery(sql);
256
257 q.addEntity("Role_", RoleImpl.class);
258
259 QueryPos qPos = QueryPos.getInstance(q);
260
261 qPos.add(userId);
262 qPos.add(groupId);
263
264 return q.list();
265 }
266 catch (Exception e) {
267 throw new SystemException(e);
268 }
269 finally {
270 closeSession(session);
271 }
272 }
273
274 public Role findByC_N(long companyId, String name)
275 throws NoSuchRoleException, SystemException {
276
277 name = StringUtil.lowerCase(name);
278
279 boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
280 String finderClassName = Role.class.getName();
281 String finderMethodName = "customFindByC_N";
282 String finderParams[] = new String[] {
283 Long.class.getName(), String.class.getName()
284 };
285 Object finderArgs[] = new Object[] {new Long(companyId), name};
286
287 Object result = FinderCacheUtil.getResult(
288 finderClassName, finderMethodName, finderParams, finderArgs, this);
289
290 if (result == null) {
291 Session session = null;
292
293 try {
294 session = openSession();
295
296 String sql = CustomSQLUtil.get(FIND_BY_C_N);
297
298 SQLQuery q = session.createSQLQuery(sql);
299
300 q.addEntity("Role_", RoleImpl.class);
301
302 QueryPos qPos = QueryPos.getInstance(q);
303
304 qPos.add(companyId);
305 qPos.add(name);
306
307 Iterator<Role> itr = q.list().iterator();
308
309 if (itr.hasNext()) {
310 Role role = itr.next();
311
312 FinderCacheUtil.putResult(
313 finderClassNameCacheEnabled, finderClassName,
314 finderMethodName, finderParams, finderArgs, role);
315
316 return role;
317 }
318 }
319 catch (Exception e) {
320 throw new SystemException(e);
321 }
322 finally {
323 closeSession(session);
324 }
325
326 throw new NoSuchRoleException(
327 "No Role exists with the key {companyId=" + companyId +
328 ", name=" + name + "}");
329 }
330 else {
331 return (Role)result;
332 }
333 }
334
335 public List<Role> findByU_G(long userId, long groupId)
336 throws SystemException {
337
338 return findByU_G(userId, new long[] {groupId});
339 }
340
341 public List<Role> findByU_G(long userId, long[] groupIds)
342 throws SystemException {
343
344 Session session = null;
345
346 try {
347 session = openSession();
348
349 String sql = CustomSQLUtil.get(FIND_BY_U_G);
350
351 sql = StringUtil.replace(
352 sql, "[$GROUP_IDS$]", getGroupIds(groupIds, "Groups_Roles"));
353
354 SQLQuery q = session.createSQLQuery(sql);
355
356 q.addEntity("Role_", RoleImpl.class);
357
358 QueryPos qPos = QueryPos.getInstance(q);
359
360 qPos.add(userId);
361 setGroupIds(qPos, groupIds);
362
363 return q.list();
364 }
365 catch (Exception e) {
366 throw new SystemException(e);
367 }
368 finally {
369 closeSession(session);
370 }
371 }
372
373 public List<Role> findByU_G(long userId, List<Group> groups)
374 throws SystemException {
375
376 long[] groupIds = new long[groups.size()];
377
378 for (int i = 0; i < groups.size(); i++) {
379 Group group = groups.get(i);
380
381 groupIds[i] = group.getGroupId();
382 }
383
384 return findByU_G(userId, groupIds);
385 }
386
387 public List<Role> findByC_N_D_T(
388 long companyId, String name, String description, Integer type,
389 LinkedHashMap<String, Object> params, int start, int end,
390 OrderByComparator obc)
391 throws SystemException {
392
393 name = StringUtil.lowerCase(name);
394 description = StringUtil.lowerCase(description);
395
396 Session session = null;
397
398 try {
399 session = openSession();
400
401 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
402
403 if (type == null) {
404 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
405 }
406
407 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
408 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
409 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
410
411 SQLQuery q = session.createSQLQuery(sql);
412
413 q.addEntity("Role_", RoleImpl.class);
414
415 QueryPos qPos = QueryPos.getInstance(q);
416
417 setJoin(qPos, params);
418 qPos.add(companyId);
419 qPos.add(name);
420 qPos.add(name);
421 qPos.add(description);
422 qPos.add(description);
423
424 if (type != null) {
425 qPos.add(type);
426 }
427
428 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
429 }
430 catch (Exception e) {
431 throw new SystemException(e);
432 }
433 finally {
434 closeSession(session);
435 }
436 }
437
438 public Map<String, List<String>> findByC_N_S_P(
439 long companyId, String name, int scope, String primKey)
440 throws SystemException {
441
442 Session session = null;
443
444 try {
445 session = openSession();
446
447 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
448
449 SQLQuery q = session.createSQLQuery(sql);
450
451 q.addScalar("roleName", Type.STRING);
452 q.addScalar("actionId", Type.STRING);
453
454 QueryPos qPos = QueryPos.getInstance(q);
455
456 qPos.add(companyId);
457 qPos.add(name);
458 qPos.add(scope);
459 qPos.add(primKey);
460
461 Map<String, List<String>> roleMap =
462 new HashMap<String, List<String>>();
463
464 Iterator<Object[]> itr = q.list().iterator();
465
466 while (itr.hasNext()) {
467 Object[] array = itr.next();
468
469 String roleName = (String)array[0];
470 String actionId = (String)array[1];
471
472 List<String> roleList = roleMap.get(roleName);
473
474 if (roleList == null) {
475 roleList = new ArrayList<String>();
476 }
477
478 roleList.add(actionId);
479
480 roleMap.put(roleName, roleList);
481 }
482
483 return roleMap;
484 }
485 catch (Exception e) {
486 throw new SystemException(e);
487 }
488 finally {
489 closeSession(session);
490 }
491 }
492
493 protected String getGroupIds(long[] groupIds, String table) {
494 StringBuilder sb = new StringBuilder();
495
496 for (int i = 0; i < groupIds.length; i++) {
497 sb.append(table);
498 sb.append(".groupId = ?");
499
500 if ((i + 1) < groupIds.length) {
501 sb.append(" OR ");
502 }
503 }
504
505 return sb.toString();
506 }
507
508 protected void setGroupIds(QueryPos qPos, long[] groupIds) {
509 for (int i = 0; i < groupIds.length; i++) {
510 qPos.add(groupIds[i]);
511 }
512 }
513
514 protected String getJoin(LinkedHashMap<String, Object> params) {
515 if (params == null) {
516 return StringPool.BLANK;
517 }
518
519 StringBuilder sb = new StringBuilder();
520
521 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
522
523 while (itr.hasNext()) {
524 Map.Entry<String, Object> entry = itr.next();
525
526 String key = entry.getKey();
527 Object value = entry.getValue();
528
529 if (Validator.isNotNull(value)) {
530 sb.append(getJoin(key));
531 }
532 }
533
534 return sb.toString();
535 }
536
537 protected String getJoin(String key) {
538 String join = StringPool.BLANK;
539
540 if (key.equals("permissionsResourceId")) {
541 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
542 }
543 else if (key.equals("usersRoles")) {
544 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
545 }
546
547 if (Validator.isNotNull(join)) {
548 int pos = join.indexOf("WHERE");
549
550 if (pos != -1) {
551 join = join.substring(0, pos);
552 }
553 }
554
555 return join;
556 }
557
558 protected String getWhere(LinkedHashMap<String, Object> params) {
559 if (params == null) {
560 return StringPool.BLANK;
561 }
562
563 StringBuilder sb = new StringBuilder();
564
565 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
566
567 while (itr.hasNext()) {
568 Map.Entry<String, Object> entry = itr.next();
569
570 String key = entry.getKey();
571 Object value = entry.getValue();
572
573 if (Validator.isNotNull(value)) {
574 sb.append(getWhere(key));
575 }
576 }
577
578 return sb.toString();
579 }
580
581 protected String getWhere(String key) {
582 String join = StringPool.BLANK;
583
584 if (key.equals("permissionsResourceId")) {
585 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
586 }
587 else if (key.equals("usersRoles")) {
588 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
589 }
590
591 if (Validator.isNotNull(join)) {
592 int pos = join.indexOf("WHERE");
593
594 if (pos != -1) {
595 StringBuilder sb = new StringBuilder();
596
597 sb.append(join.substring(pos + 5, join.length()));
598 sb.append(" AND ");
599
600 join = sb.toString();
601 }
602 else {
603 join = StringPool.BLANK;
604 }
605 }
606
607 return join;
608 }
609
610 protected void setJoin(
611 QueryPos qPos, LinkedHashMap<String, Object> params) {
612
613 if (params != null) {
614 Iterator<Map.Entry<String, Object>> itr =
615 params.entrySet().iterator();
616
617 while (itr.hasNext()) {
618 Map.Entry<String, Object> entry = itr.next();
619
620 Object value = entry.getValue();
621
622 if (value instanceof Long) {
623 Long valueLong = (Long)value;
624
625 if (Validator.isNotNull(valueLong)) {
626 qPos.add(valueLong);
627 }
628 }
629 else if (value instanceof String) {
630 String valueString = (String)value;
631
632 if (Validator.isNotNull(valueString)) {
633 qPos.add(valueString);
634 }
635 }
636 }
637 }
638 }
639
640 }