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