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
047 public class RoleFinderImpl
048 extends BasePersistenceImpl<Role> implements RoleFinder {
049
050 public static String COUNT_BY_ORGANIZATION =
051 RoleFinder.class.getName() + ".countByOrganization";
052
053 public static String COUNT_BY_ORGANIZATION_SITE =
054 RoleFinder.class.getName() + ".countByOrganizationSite";
055
056 public static String COUNT_BY_SITE =
057 RoleFinder.class.getName() + ".countBySite";
058
059 public static String COUNT_BY_USER =
060 RoleFinder.class.getName() + ".countByUser";
061
062 public static String COUNT_BY_USER_GROUP =
063 RoleFinder.class.getName() + ".countByUserGroup";
064
065 public static String COUNT_BY_USER_GROUP_SITE =
066 RoleFinder.class.getName() + ".countByUserGroupSite";
067
068 public static String COUNT_BY_U_G_R =
069 RoleFinder.class.getName() + ".countByU_G_R";
070
071 public static String COUNT_BY_C_N_D_T =
072 RoleFinder.class.getName() + ".countByC_N_D_T";
073
074 public static String FIND_BY_SYSTEM =
075 RoleFinder.class.getName() + ".findBySystem";
076
077 public static String FIND_BY_USER_GROUP_GROUP_ROLE =
078 RoleFinder.class.getName() + ".findByUserGroupGroupRole";
079
080 public static String FIND_BY_USER_GROUP_ROLE =
081 RoleFinder.class.getName() + ".findByUserGroupRole";
082
083 public static String FIND_BY_C_N =
084 RoleFinder.class.getName() + ".findByC_N";
085
086 public static String FIND_BY_U_G =
087 RoleFinder.class.getName() + ".findByU_G";
088
089 public static String FIND_BY_C_N_D_T =
090 RoleFinder.class.getName() + ".findByC_N_D_T";
091
092 public static String FIND_BY_C_N_S_P =
093 RoleFinder.class.getName() + ".findByC_N_S_P";
094
095 public static String FIND_BY_C_N_S_P_A =
096 RoleFinder.class.getName() + ".findByC_N_S_P_A";
097
098 public static String JOIN_BY_ROLES_PERMISSIONS =
099 RoleFinder.class.getName() + ".joinByRolesPermissions";
100
101 public static String JOIN_BY_USERS_ROLES =
102 RoleFinder.class.getName() + ".joinByUsersRoles";
103
104 public int countByR_U(long roleId, long userId) throws SystemException {
105 Session session = null;
106
107 try {
108 session = openSession();
109
110 SQLQuery q = session.createSQLQuery(getCountByR_U_SQL());
111
112 QueryPos qPos = QueryPos.getInstance(q);
113
114 for (int i = 0; i < 6; i++) {
115 qPos.add(roleId);
116 qPos.add(userId);
117 }
118
119 return q.list().size();
120 }
121 catch (Exception e) {
122 throw new SystemException(e);
123 }
124 finally {
125 closeSession(session);
126 }
127 }
128
129 public int countByU_G_R(long userId, long groupId, long roleId)
130 throws SystemException {
131
132 Session session = null;
133
134 try {
135 session = openSession();
136
137 String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
138
139 SQLQuery q = session.createSQLQuery(sql);
140
141 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
142
143 QueryPos qPos = QueryPos.getInstance(q);
144
145 qPos.add(roleId);
146 qPos.add(groupId);
147 qPos.add(userId);
148
149 Iterator<Long> itr = q.iterate();
150
151 if (itr.hasNext()) {
152 Long count = itr.next();
153
154 if (count != null) {
155 return count.intValue();
156 }
157 }
158
159 return 0;
160 }
161 catch (Exception e) {
162 throw new SystemException(e);
163 }
164 finally {
165 closeSession(session);
166 }
167 }
168
169 public int countByC_N_D_T(
170 long companyId, String name, String description, Integer[] types,
171 LinkedHashMap<String, Object> params, boolean andOperator)
172 throws SystemException {
173
174 String[] names = CustomSQLUtil.keywords(name);
175 String[] descriptions = CustomSQLUtil.keywords(description);
176
177 return countByC_N_D_T(
178 companyId, names, descriptions, types, params, andOperator);
179 }
180
181 public int countByC_N_D_T(
182 long companyId, String[] names, String[] descriptions,
183 Integer[] types, LinkedHashMap<String, Object> params,
184 boolean andOperator)
185 throws SystemException {
186
187 names = CustomSQLUtil.keywords(names, true);
188 descriptions = CustomSQLUtil.keywords(descriptions, true);
189
190 if (types == null) {
191 types = new Integer[0];
192 }
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 sql = CustomSQLUtil.replaceKeywords(
202 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
203 sql = CustomSQLUtil.replaceKeywords(
204 sql, "lower(Role_.description)", StringPool.LIKE, true,
205 descriptions);
206 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
207 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
208 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
209 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
210
211 SQLQuery q = session.createSQLQuery(sql);
212
213 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
214
215 QueryPos qPos = QueryPos.getInstance(q);
216
217 setJoin(qPos, params);
218
219 qPos.add(companyId);
220 qPos.add(names, 2);
221 qPos.add(descriptions, 2);
222 qPos.add(types);
223
224 Iterator<Long> itr = q.iterate();
225
226 if (itr.hasNext()) {
227 Long count = itr.next();
228
229 if (count != null) {
230 return count.intValue();
231 }
232 }
233
234 return 0;
235 }
236 catch (Exception e) {
237 throw new SystemException(e);
238 }
239 finally {
240 closeSession(session);
241 }
242 }
243
244 public int countByKeywords(
245 long companyId, String keywords, Integer[] types)
246 throws SystemException {
247
248 return countByKeywords(
249 companyId, keywords, types, new LinkedHashMap<String, Object>());
250 }
251
252 public int countByKeywords(
253 long companyId, String keywords, Integer[] types,
254 LinkedHashMap<String, Object> params)
255 throws SystemException {
256
257 String[] names = null;
258 String[] descriptions = null;
259 boolean andOperator = false;
260
261 if (Validator.isNotNull(keywords)) {
262 names = CustomSQLUtil.keywords(keywords);
263 descriptions = CustomSQLUtil.keywords(keywords);
264 }
265 else {
266 andOperator = true;
267 }
268
269 return countByC_N_D_T(
270 companyId, names, descriptions, types, params, andOperator);
271 }
272
273 public List<Role> findBySystem(long companyId) throws SystemException {
274 Session session = null;
275
276 try {
277 session = openSession();
278
279 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
280
281 SQLQuery q = session.createSQLQuery(sql);
282
283 q.addEntity("Role_", RoleImpl.class);
284
285 QueryPos qPos = QueryPos.getInstance(q);
286
287 qPos.add(companyId);
288
289 return q.list(true);
290 }
291 catch (Exception e) {
292 throw new SystemException(e);
293 }
294 finally {
295 closeSession(session);
296 }
297 }
298
299 public List<Role> findByUserGroupGroupRole(long userId, long groupId)
300 throws SystemException {
301
302 Session session = null;
303
304 try {
305 session = openSession();
306
307 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
308
309 SQLQuery q = session.createSQLQuery(sql);
310
311 q.addEntity("Role_", RoleImpl.class);
312
313 QueryPos qPos = QueryPos.getInstance(q);
314
315 qPos.add(userId);
316 qPos.add(groupId);
317
318 return q.list(true);
319 }
320 catch (Exception e) {
321 throw new SystemException(e);
322 }
323 finally {
324 closeSession(session);
325 }
326 }
327
328 public List<Role> findByUserGroupRole(long userId, long groupId)
329 throws SystemException {
330
331 Session session = null;
332
333 try {
334 session = openSession();
335
336 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
337
338 SQLQuery q = session.createSQLQuery(sql);
339
340 q.addEntity("Role_", RoleImpl.class);
341
342 QueryPos qPos = QueryPos.getInstance(q);
343
344 qPos.add(userId);
345 qPos.add(groupId);
346
347 return q.list(true);
348 }
349 catch (Exception e) {
350 throw new SystemException(e);
351 }
352 finally {
353 closeSession(session);
354 }
355 }
356
357 public Role findByC_N(long companyId, String name)
358 throws NoSuchRoleException, SystemException {
359
360 name = StringUtil.lowerCase(name);
361
362 Session session = null;
363
364 try {
365 session = openSession();
366
367 String sql = CustomSQLUtil.get(FIND_BY_C_N);
368
369 SQLQuery q = session.createSQLQuery(sql);
370
371 q.addEntity("Role_", RoleImpl.class);
372
373 QueryPos qPos = QueryPos.getInstance(q);
374
375 qPos.add(companyId);
376 qPos.add(name);
377
378 List<Role> roles = q.list();
379
380 if (!roles.isEmpty()) {
381 return roles.get(0);
382 }
383 }
384 catch (Exception e) {
385 throw new SystemException(e);
386 }
387 finally {
388 closeSession(session);
389 }
390
391 StringBundler sb = new StringBundler(5);
392
393 sb.append("No Role exists with the key {companyId=");
394 sb.append(companyId);
395 sb.append(", name=");
396 sb.append(name);
397 sb.append("}");
398
399 throw new NoSuchRoleException(sb.toString());
400 }
401
402 public List<Role> findByU_G(long userId, long groupId)
403 throws SystemException {
404
405 return findByU_G(userId, new long[] {groupId});
406 }
407
408 public List<Role> findByU_G(long userId, long[] groupIds)
409 throws SystemException {
410
411 Session session = null;
412
413 try {
414 session = openSession();
415
416 String sql = CustomSQLUtil.get(FIND_BY_U_G);
417
418 sql = StringUtil.replace(
419 sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
420
421 SQLQuery q = session.createSQLQuery(sql);
422
423 q.addEntity("Role_", RoleImpl.class);
424
425 QueryPos qPos = QueryPos.getInstance(q);
426
427 qPos.add(userId);
428 qPos.add(groupIds);
429
430 return q.list(true);
431 }
432 catch (Exception e) {
433 throw new SystemException(e);
434 }
435 finally {
436 closeSession(session);
437 }
438 }
439
440 public List<Role> findByU_G(long userId, List<Group> groups)
441 throws SystemException {
442
443 long[] groupIds = new long[groups.size()];
444
445 for (int i = 0; i < groups.size(); i++) {
446 Group group = groups.get(i);
447
448 groupIds[i] = group.getGroupId();
449 }
450
451 return findByU_G(userId, groupIds);
452 }
453
454 public List<Role> findByC_N_D_T(
455 long companyId, String name, String description, Integer[] types,
456 LinkedHashMap<String, Object> params, boolean andOperator,
457 int start, int end, OrderByComparator obc)
458 throws SystemException {
459
460 String[] names = CustomSQLUtil.keywords(name);
461 String[] descriptions = CustomSQLUtil.keywords(description);
462
463 return findByC_N_D_T(
464 companyId, names, descriptions, types, params, andOperator, start,
465 end, obc);
466 }
467
468 public List<Role> findByC_N_D_T(
469 long companyId, String[] names, String[] descriptions,
470 Integer[] types, LinkedHashMap<String, Object> params,
471 boolean andOperator, int start, int end, OrderByComparator obc)
472 throws SystemException {
473
474 names = CustomSQLUtil.keywords(names, true);
475 descriptions = CustomSQLUtil.keywords(descriptions, true);
476
477 if (types == null) {
478 types = new Integer[0];
479 }
480
481 Session session = null;
482
483 try {
484 session = openSession();
485
486 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
487
488 sql = CustomSQLUtil.replaceKeywords(
489 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
490 sql = CustomSQLUtil.replaceKeywords(
491 sql, "lower(Role_.description)", StringPool.LIKE, true,
492 descriptions);
493 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
494 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
495 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
496 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
497 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
498
499 SQLQuery q = session.createSQLQuery(sql);
500
501 q.addEntity("Role_", RoleImpl.class);
502
503 QueryPos qPos = QueryPos.getInstance(q);
504
505 setJoin(qPos, params);
506
507 qPos.add(companyId);
508 qPos.add(names, 2);
509 qPos.add(descriptions, 2);
510 qPos.add(types);
511
512 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
513 }
514 catch (Exception e) {
515 throw new SystemException(e);
516 }
517 finally {
518 closeSession(session);
519 }
520 }
521
522 public Map<String, List<String>> findByC_N_S_P(
523 long companyId, String name, int scope, String primKey)
524 throws SystemException {
525
526 Session session = null;
527
528 try {
529 session = openSession();
530
531 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
532
533 SQLQuery q = session.createSQLQuery(sql);
534
535 q.addScalar("roleName", Type.STRING);
536 q.addScalar("actionId", Type.STRING);
537
538 QueryPos qPos = QueryPos.getInstance(q);
539
540 qPos.add(companyId);
541 qPos.add(name);
542 qPos.add(scope);
543 qPos.add(primKey);
544
545 Map<String, List<String>> roleMap =
546 new HashMap<String, List<String>>();
547
548 Iterator<Object[]> itr = q.iterate();
549
550 while (itr.hasNext()) {
551 Object[] array = itr.next();
552
553 String roleName = (String)array[0];
554 String actionId = (String)array[1];
555
556 List<String> roleList = roleMap.get(roleName);
557
558 if (roleList == null) {
559 roleList = new ArrayList<String>();
560 }
561
562 roleList.add(actionId);
563
564 roleMap.put(roleName, roleList);
565 }
566
567 return roleMap;
568 }
569 catch (Exception e) {
570 throw new SystemException(e);
571 }
572 finally {
573 closeSession(session);
574 }
575 }
576
577 public List<Role> findByC_N_S_P_A(
578 long companyId, String name, int scope, String primKey,
579 String actionId)
580 throws SystemException {
581
582 Session session = null;
583
584 try {
585 session = openSession();
586
587 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
588
589 SQLQuery q = session.createSQLQuery(sql);
590
591 q.addEntity("Role_", RoleImpl.class);
592
593 QueryPos qPos = QueryPos.getInstance(q);
594
595 qPos.add(companyId);
596 qPos.add(name);
597 qPos.add(scope);
598 qPos.add(primKey);
599 qPos.add(actionId);
600
601 return q.list(true);
602 }
603 catch (Exception e) {
604 throw new SystemException(e);
605 }
606 finally {
607 closeSession(session);
608 }
609 }
610
611 public List<Role> findByKeywords(
612 long companyId, String keywords, Integer[] types, int start,
613 int end, OrderByComparator obc)
614 throws SystemException {
615
616 return findByKeywords(
617 companyId, keywords, types, new LinkedHashMap<String, Object>(),
618 start, end, obc);
619 }
620
621 public List<Role> findByKeywords(
622 long companyId, String keywords, Integer[] types,
623 LinkedHashMap<String, Object> params, int start, int end,
624 OrderByComparator obc)
625 throws SystemException {
626
627 String[] names = null;
628 String[] descriptions = null;
629 boolean andOperator = false;
630
631 if (Validator.isNotNull(keywords)) {
632 names = CustomSQLUtil.keywords(keywords);
633 descriptions = CustomSQLUtil.keywords(keywords);
634 }
635 else {
636 andOperator = true;
637 }
638
639 return findByC_N_D_T(
640 companyId, names, descriptions, types, params, andOperator, start,
641 end, obc);
642 }
643
644 protected String getCountByR_U_SQL() {
645 if (_countByR_U == null) {
646 StringBundler sb = new StringBundler(13);
647
648 sb.append("(");
649 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
650 sb.append(") UNION (");
651 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
652 sb.append(") UNION (");
653 sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
654 sb.append(") UNION (");
655 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
656 sb.append(") UNION (");
657 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
658 sb.append(") UNION (");
659 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
660 sb.append(")");
661
662 _countByR_U = sb.toString();
663 }
664
665 return _countByR_U;
666 }
667
668 protected String getGroupIds(long[] groupIds, String table) {
669 if (groupIds.length == 0) {
670 return StringPool.BLANK;
671 }
672
673 StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
674
675 for (int i = 0; i < groupIds.length; i++) {
676 sb.append(table);
677 sb.append(".groupId = ?");
678
679 if ((i + 1) < groupIds.length) {
680 sb.append(" OR ");
681 }
682 }
683
684 return sb.toString();
685 }
686
687 protected String getJoin(LinkedHashMap<String, Object> params) {
688 if ((params == null) || params.isEmpty()) {
689 return StringPool.BLANK;
690 }
691
692 StringBundler sb = new StringBundler(params.size());
693
694 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
695
696 while (itr.hasNext()) {
697 Map.Entry<String, Object> entry = itr.next();
698
699 String key = entry.getKey();
700 Object value = entry.getValue();
701
702 if (Validator.isNotNull(value)) {
703 sb.append(getJoin(key));
704 }
705 }
706
707 return sb.toString();
708 }
709
710 protected String getJoin(String key) {
711 String join = StringPool.BLANK;
712
713 if (key.equals("permissionsResourceId")) {
714 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
715 }
716 else if (key.equals("usersRoles")) {
717 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
718 }
719
720 if (Validator.isNotNull(join)) {
721 int pos = join.indexOf("WHERE");
722
723 if (pos != -1) {
724 join = join.substring(0, pos);
725 }
726 }
727
728 return join;
729 }
730
731 protected String getTypes(Integer[] types) {
732 if (types.length == 0) {
733 return StringPool.BLANK;
734 }
735
736 StringBundler sb = new StringBundler(types.length * 2);
737
738 sb.append(" AND (");
739
740 for (int i = 0; i < types.length; i++) {
741 sb.append("Role_.type_ = ?");
742
743 if ((i + 1) < types.length) {
744 sb.append(" OR ");
745 }
746 }
747
748 sb.append(")");
749
750 return sb.toString();
751 }
752
753 protected String getWhere(LinkedHashMap<String, Object> params) {
754 if ((params == null) || params.isEmpty()) {
755 return StringPool.BLANK;
756 }
757
758 StringBundler sb = new StringBundler(params.size());
759
760 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
761
762 while (itr.hasNext()) {
763 Map.Entry<String, Object> entry = itr.next();
764
765 String key = entry.getKey();
766 Object value = entry.getValue();
767
768 if (Validator.isNotNull(value)) {
769 sb.append(getWhere(key));
770 }
771 }
772
773 return sb.toString();
774 }
775
776 protected String getWhere(String key) {
777 String join = StringPool.BLANK;
778
779 if (key.equals("permissionsResourceId")) {
780 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
781 }
782 else if (key.equals("usersRoles")) {
783 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
784 }
785
786 if (Validator.isNotNull(join)) {
787 int pos = join.indexOf("WHERE");
788
789 if (pos != -1) {
790 join = join.substring(pos + 5, join.length()).concat(" AND ");
791 }
792 else {
793 join = StringPool.BLANK;
794 }
795 }
796
797 return join;
798 }
799
800 protected void setJoin(
801 QueryPos qPos, LinkedHashMap<String, Object> params) {
802
803 if (params == null) {
804 return;
805 }
806
807 for (Map.Entry<String, Object> entry : params.entrySet()) {
808 Object value = entry.getValue();
809
810 if (value instanceof Long) {
811 Long valueLong = (Long)value;
812
813 if (Validator.isNotNull(valueLong)) {
814 qPos.add(valueLong);
815 }
816 }
817 else if (value instanceof String) {
818 String valueString = (String)value;
819
820 if (Validator.isNotNull(valueString)) {
821 qPos.add(valueString);
822 }
823 }
824 }
825 }
826
827 private String _countByR_U;
828
829 }