001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchRoleException;
018 import com.liferay.portal.kernel.dao.orm.QueryPos;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.util.OrderByComparator;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.model.Role;
031 import com.liferay.portal.model.impl.RoleImpl;
032 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
033 import com.liferay.util.dao.orm.CustomSQLUtil;
034
035 import java.util.ArrayList;
036 import java.util.HashMap;
037 import java.util.Iterator;
038 import java.util.LinkedHashMap;
039 import java.util.List;
040 import java.util.Map;
041
042
045 public class RoleFinderImpl
046 extends BasePersistenceImpl<Role> implements RoleFinder {
047
048 public static String COUNT_BY_COMMUNITY =
049 RoleFinder.class.getName() + ".countByCommunity";
050
051 public static String COUNT_BY_ORGANIZATION =
052 RoleFinder.class.getName() + ".countByOrganization";
053
054 public static String COUNT_BY_ORGANIZATION_COMMUNITY =
055 RoleFinder.class.getName() + ".countByOrganizationCommunity";
056
057 public static String COUNT_BY_USER =
058 RoleFinder.class.getName() + ".countByUser";
059
060 public static String COUNT_BY_USER_GROUP =
061 RoleFinder.class.getName() + ".countByUserGroup";
062
063 public static String COUNT_BY_USER_GROUP_COMMUNITY =
064 RoleFinder.class.getName() + ".countByUserGroupCommunity";
065
066 public static String COUNT_BY_U_G_R =
067 RoleFinder.class.getName() + ".countByU_G_R";
068
069 public static String COUNT_BY_C_N_D_T =
070 RoleFinder.class.getName() + ".countByC_N_D_T";
071
072 public static String FIND_BY_SYSTEM =
073 RoleFinder.class.getName() + ".findBySystem";
074
075 public static String FIND_BY_USER_GROUP_GROUP_ROLE =
076 RoleFinder.class.getName() + ".findByUserGroupGroupRole";
077
078 public static String FIND_BY_USER_GROUP_ROLE =
079 RoleFinder.class.getName() + ".findByUserGroupRole";
080
081 public static String FIND_BY_C_N =
082 RoleFinder.class.getName() + ".findByC_N";
083
084 public static String FIND_BY_U_G =
085 RoleFinder.class.getName() + ".findByU_G";
086
087 public static String FIND_BY_C_N_D_T =
088 RoleFinder.class.getName() + ".findByC_N_D_T";
089
090 public static String FIND_BY_C_N_S_P =
091 RoleFinder.class.getName() + ".findByC_N_S_P";
092
093 public static String JOIN_BY_ROLES_PERMISSIONS =
094 RoleFinder.class.getName() + ".joinByRolesPermissions";
095
096 public static String JOIN_BY_USERS_ROLES =
097 RoleFinder.class.getName() + ".joinByUsersRoles";
098
099 public int countByR_U(long roleId, long userId) throws SystemException {
100 Session session = null;
101
102 try {
103 session = openSession();
104
105 StringBundler sb = new StringBundler(13);
106
107 sb.append("(");
108 sb.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
109 sb.append(") UNION (");
110 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
111 sb.append(") UNION (");
112 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_COMMUNITY));
113 sb.append(") UNION (");
114 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
115 sb.append(") UNION (");
116 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
117 sb.append(") UNION (");
118 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_COMMUNITY));
119 sb.append(")");
120
121 SQLQuery q = session.createSQLQuery(sb.toString());
122
123 QueryPos qPos = QueryPos.getInstance(q);
124
125 for (int i = 0; i < 6; i++) {
126 qPos.add(roleId);
127 qPos.add(userId);
128 }
129
130 return q.list().size();
131 }
132 catch (Exception e) {
133 throw new SystemException(e);
134 }
135 finally {
136 closeSession(session);
137 }
138 }
139
140 public int countByU_G_R(long userId, long groupId, long roleId)
141 throws SystemException {
142
143 Session session = null;
144
145 try {
146 session = openSession();
147
148 String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
149
150 SQLQuery q = session.createSQLQuery(sql);
151
152 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
153
154 QueryPos qPos = QueryPos.getInstance(q);
155
156 qPos.add(roleId);
157 qPos.add(groupId);
158 qPos.add(userId);
159
160 Iterator<Long> itr = q.list().iterator();
161
162 if (itr.hasNext()) {
163 Long count = itr.next();
164
165 if (count != null) {
166 return count.intValue();
167 }
168 }
169
170 return 0;
171 }
172 catch (Exception e) {
173 throw new SystemException(e);
174 }
175 finally {
176 closeSession(session);
177 }
178 }
179
180 public int countByC_N_D_T(
181 long companyId, String name, String description, Integer[] types,
182 LinkedHashMap<String, Object> params)
183 throws SystemException {
184
185 name = StringUtil.lowerCase(name);
186 description = StringUtil.lowerCase(description);
187
188 if (types == null) {
189 types = new Integer[0];
190 }
191
192 Session session = null;
193
194 try {
195 session = openSession();
196
197 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
198
199 sql = StringUtil.replace(sql, " AND ([$TYPE$])", getTypes(types));
200 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
201 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
202
203 SQLQuery q = session.createSQLQuery(sql);
204
205 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
206
207 QueryPos qPos = QueryPos.getInstance(q);
208
209 setJoin(qPos, params);
210 qPos.add(companyId);
211 qPos.add(name);
212 qPos.add(name);
213 qPos.add(description);
214 qPos.add(description);
215 qPos.add(types);
216
217 Iterator<Long> itr = q.list().iterator();
218
219 if (itr.hasNext()) {
220 Long count = itr.next();
221
222 if (count != null) {
223 return count.intValue();
224 }
225 }
226
227 return 0;
228 }
229 catch (Exception e) {
230 throw new SystemException(e);
231 }
232 finally {
233 closeSession(session);
234 }
235 }
236
237 public List<Role> findBySystem(long companyId) throws SystemException {
238 Session session = null;
239
240 try {
241 session = openSession();
242
243 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
244
245 SQLQuery q = session.createSQLQuery(sql);
246
247 q.addEntity("Role_", RoleImpl.class);
248
249 QueryPos qPos = QueryPos.getInstance(q);
250
251 qPos.add(companyId);
252
253 return q.list();
254 }
255 catch (Exception e) {
256 throw new SystemException(e);
257 }
258 finally {
259 closeSession(session);
260 }
261 }
262
263 public List<Role> findByUserGroupGroupRole(long userId, long groupId)
264 throws SystemException {
265
266 Session session = null;
267
268 try {
269 session = openSession();
270
271 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
272
273 SQLQuery q = session.createSQLQuery(sql);
274
275 q.addEntity("Role_", RoleImpl.class);
276
277 QueryPos qPos = QueryPos.getInstance(q);
278
279 qPos.add(userId);
280 qPos.add(groupId);
281
282 return q.list();
283 }
284 catch (Exception e) {
285 throw new SystemException(e);
286 }
287 finally {
288 closeSession(session);
289 }
290 }
291
292 public List<Role> findByUserGroupRole(long userId, long groupId)
293 throws SystemException {
294
295 Session session = null;
296
297 try {
298 session = openSession();
299
300 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
301
302 SQLQuery q = session.createSQLQuery(sql);
303
304 q.addEntity("Role_", RoleImpl.class);
305
306 QueryPos qPos = QueryPos.getInstance(q);
307
308 qPos.add(userId);
309 qPos.add(groupId);
310
311 return q.list();
312 }
313 catch (Exception e) {
314 throw new SystemException(e);
315 }
316 finally {
317 closeSession(session);
318 }
319 }
320
321 public Role findByC_N(long companyId, String name)
322 throws NoSuchRoleException, SystemException {
323
324 name = StringUtil.lowerCase(name);
325
326 Session session = null;
327
328 try {
329 session = openSession();
330
331 String sql = CustomSQLUtil.get(FIND_BY_C_N);
332
333 SQLQuery q = session.createSQLQuery(sql);
334
335 q.addEntity("Role_", RoleImpl.class);
336
337 QueryPos qPos = QueryPos.getInstance(q);
338
339 qPos.add(companyId);
340 qPos.add(name);
341
342 List<Role> list = q.list();
343
344 if (!list.isEmpty()) {
345 return list.get(0);
346 }
347 }
348 catch (Exception e) {
349 throw new SystemException(e);
350 }
351 finally {
352 closeSession(session);
353 }
354
355 StringBundler sb = new StringBundler(5);
356
357 sb.append("No Role exists with the key {companyId=");
358 sb.append(companyId);
359 sb.append(", name=");
360 sb.append(name);
361 sb.append("}");
362
363 throw new NoSuchRoleException(sb.toString());
364 }
365
366 public List<Role> findByU_G(long userId, long groupId)
367 throws SystemException {
368
369 return findByU_G(userId, new long[] {groupId});
370 }
371
372 public List<Role> findByU_G(long userId, long[] groupIds)
373 throws SystemException {
374
375 Session session = null;
376
377 try {
378 session = openSession();
379
380 String sql = CustomSQLUtil.get(FIND_BY_U_G);
381
382 sql = StringUtil.replace(
383 sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
384
385 SQLQuery q = session.createSQLQuery(sql);
386
387 q.addEntity("Role_", RoleImpl.class);
388
389 QueryPos qPos = QueryPos.getInstance(q);
390
391 qPos.add(userId);
392 qPos.add(groupIds);
393
394 return q.list();
395 }
396 catch (Exception e) {
397 throw new SystemException(e);
398 }
399 finally {
400 closeSession(session);
401 }
402 }
403
404 public List<Role> findByU_G(long userId, List<Group> groups)
405 throws SystemException {
406
407 long[] groupIds = new long[groups.size()];
408
409 for (int i = 0; i < groups.size(); i++) {
410 Group group = groups.get(i);
411
412 groupIds[i] = group.getGroupId();
413 }
414
415 return findByU_G(userId, groupIds);
416 }
417
418 public List<Role> findByC_N_D_T(
419 long companyId, String name, String description, Integer[] types,
420 LinkedHashMap<String, Object> params, int start, int end,
421 OrderByComparator obc)
422 throws SystemException {
423
424 name = StringUtil.lowerCase(name);
425 description = StringUtil.lowerCase(description);
426
427 if (types == null) {
428 types = new Integer[0];
429 }
430
431 Session session = null;
432
433 try {
434 session = openSession();
435
436 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
437
438 sql = StringUtil.replace(sql, " AND ([$TYPE$])", getTypes(types));
439 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
440 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
441 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
442
443 SQLQuery q = session.createSQLQuery(sql);
444
445 q.addEntity("Role_", RoleImpl.class);
446
447 QueryPos qPos = QueryPos.getInstance(q);
448
449 setJoin(qPos, params);
450 qPos.add(companyId);
451 qPos.add(name);
452 qPos.add(name);
453 qPos.add(description);
454 qPos.add(description);
455 qPos.add(types);
456
457 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
458 }
459 catch (Exception e) {
460 throw new SystemException(e);
461 }
462 finally {
463 closeSession(session);
464 }
465 }
466
467 public Map<String, List<String>> findByC_N_S_P(
468 long companyId, String name, int scope, String primKey)
469 throws SystemException {
470
471 Session session = null;
472
473 try {
474 session = openSession();
475
476 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
477
478 SQLQuery q = session.createSQLQuery(sql);
479
480 q.addScalar("roleName", Type.STRING);
481 q.addScalar("actionId", Type.STRING);
482
483 QueryPos qPos = QueryPos.getInstance(q);
484
485 qPos.add(companyId);
486 qPos.add(name);
487 qPos.add(scope);
488 qPos.add(primKey);
489
490 Map<String, List<String>> roleMap =
491 new HashMap<String, List<String>>();
492
493 Iterator<Object[]> itr = q.list().iterator();
494
495 while (itr.hasNext()) {
496 Object[] array = itr.next();
497
498 String roleName = (String)array[0];
499 String actionId = (String)array[1];
500
501 List<String> roleList = roleMap.get(roleName);
502
503 if (roleList == null) {
504 roleList = new ArrayList<String>();
505 }
506
507 roleList.add(actionId);
508
509 roleMap.put(roleName, roleList);
510 }
511
512 return roleMap;
513 }
514 catch (Exception e) {
515 throw new SystemException(e);
516 }
517 finally {
518 closeSession(session);
519 }
520 }
521
522 protected String getGroupIds(long[] groupIds, String table) {
523 if (groupIds.length == 0) {
524 return StringPool.BLANK;
525 }
526
527 StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
528
529 for (int i = 0; i < groupIds.length; i++) {
530 sb.append(table);
531 sb.append(".groupId = ?");
532
533 if ((i + 1) < groupIds.length) {
534 sb.append(" OR ");
535 }
536 }
537
538 return sb.toString();
539 }
540
541 protected String getJoin(LinkedHashMap<String, Object> params) {
542 if ((params == null) || params.isEmpty()) {
543 return StringPool.BLANK;
544 }
545
546 StringBundler sb = new StringBundler(params.size());
547
548 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
549
550 while (itr.hasNext()) {
551 Map.Entry<String, Object> entry = itr.next();
552
553 String key = entry.getKey();
554 Object value = entry.getValue();
555
556 if (Validator.isNotNull(value)) {
557 sb.append(getJoin(key));
558 }
559 }
560
561 return sb.toString();
562 }
563
564 protected String getJoin(String key) {
565 String join = StringPool.BLANK;
566
567 if (key.equals("permissionsResourceId")) {
568 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
569 }
570 else if (key.equals("usersRoles")) {
571 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
572 }
573
574 if (Validator.isNotNull(join)) {
575 int pos = join.indexOf("WHERE");
576
577 if (pos != -1) {
578 join = join.substring(0, pos);
579 }
580 }
581
582 return join;
583 }
584
585 protected String getTypes(Integer[] types) {
586 if (types.length == 0) {
587 return StringPool.BLANK;
588 }
589
590 StringBundler sb = new StringBundler(types.length * 2);
591
592 sb.append(" AND ");
593
594 for (int i = 0; i < types.length; i++) {
595 sb.append("Role_.type_ = ?");
596
597 if ((i + 1) < types.length) {
598 sb.append(" OR ");
599 }
600 }
601
602 return sb.toString();
603 }
604
605 protected String getWhere(LinkedHashMap<String, Object> params) {
606 if ((params == null) || params.isEmpty()) {
607 return StringPool.BLANK;
608 }
609
610 StringBundler sb = new StringBundler(params.size());
611
612 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
613
614 while (itr.hasNext()) {
615 Map.Entry<String, Object> entry = itr.next();
616
617 String key = entry.getKey();
618 Object value = entry.getValue();
619
620 if (Validator.isNotNull(value)) {
621 sb.append(getWhere(key));
622 }
623 }
624
625 return sb.toString();
626 }
627
628 protected String getWhere(String key) {
629 String join = StringPool.BLANK;
630
631 if (key.equals("permissionsResourceId")) {
632 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
633 }
634 else if (key.equals("usersRoles")) {
635 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
636 }
637
638 if (Validator.isNotNull(join)) {
639 int pos = join.indexOf("WHERE");
640
641 if (pos != -1) {
642 join = join.substring(pos + 5, join.length()).concat(" AND ");
643 }
644 else {
645 join = StringPool.BLANK;
646 }
647 }
648
649 return join;
650 }
651
652 protected void setJoin(
653 QueryPos qPos, LinkedHashMap<String, Object> params) {
654
655 if (params != null) {
656 Iterator<Map.Entry<String, Object>> itr =
657 params.entrySet().iterator();
658
659 while (itr.hasNext()) {
660 Map.Entry<String, Object> entry = itr.next();
661
662 Object value = entry.getValue();
663
664 if (value instanceof Long) {
665 Long valueLong = (Long)value;
666
667 if (Validator.isNotNull(valueLong)) {
668 qPos.add(valueLong);
669 }
670 }
671 else if (value instanceof String) {
672 String valueString = (String)value;
673
674 if (Validator.isNotNull(valueString)) {
675 qPos.add(valueString);
676 }
677 }
678 }
679 }
680 }
681
682 }