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