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 final String COUNT_BY_ORGANIZATION =
051 RoleFinder.class.getName() + ".countByOrganization";
052
053 public static final String COUNT_BY_ORGANIZATION_SITE =
054 RoleFinder.class.getName() + ".countByOrganizationSite";
055
056 public static final String COUNT_BY_SITE =
057 RoleFinder.class.getName() + ".countBySite";
058
059 public static final String COUNT_BY_USER =
060 RoleFinder.class.getName() + ".countByUser";
061
062 public static final String COUNT_BY_USER_GROUP =
063 RoleFinder.class.getName() + ".countByUserGroup";
064
065 public static final String COUNT_BY_USER_GROUP_SITE =
066 RoleFinder.class.getName() + ".countByUserGroupSite";
067
068 public static final String COUNT_BY_U_G_R =
069 RoleFinder.class.getName() + ".countByU_G_R";
070
071 public static final String COUNT_BY_C_N_D_T =
072 RoleFinder.class.getName() + ".countByC_N_D_T";
073
074 public static final String FIND_BY_SYSTEM =
075 RoleFinder.class.getName() + ".findBySystem";
076
077 public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
078 RoleFinder.class.getName() + ".findByUserGroupGroupRole";
079
080 public static final String FIND_BY_USER_GROUP_ROLE =
081 RoleFinder.class.getName() + ".findByUserGroupRole";
082
083 public static final String FIND_BY_C_N =
084 RoleFinder.class.getName() + ".findByC_N";
085
086 public static final String FIND_BY_U_G =
087 RoleFinder.class.getName() + ".findByU_G";
088
089 public static final String FIND_BY_R_N_A =
090 RoleFinder.class.getName() + ".findByR_N_A";
091
092 public static final String FIND_BY_C_N_D_T =
093 RoleFinder.class.getName() + ".findByC_N_D_T";
094
095 public static final String FIND_BY_C_N_S_P =
096 RoleFinder.class.getName() + ".findByC_N_S_P";
097
098 public static final String FIND_BY_C_N_S_P_A =
099 RoleFinder.class.getName() + ".findByC_N_S_P_A";
100
101 public static final 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(long companyId, String keywords, Integer[] types)
245 throws SystemException {
246
247 return countByKeywords(
248 companyId, keywords, types, new LinkedHashMap<String, Object>());
249 }
250
251 public int countByKeywords(
252 long companyId, String keywords, Integer[] types,
253 LinkedHashMap<String, Object> params)
254 throws SystemException {
255
256 String[] names = null;
257 String[] descriptions = null;
258 boolean andOperator = false;
259
260 if (Validator.isNotNull(keywords)) {
261 names = CustomSQLUtil.keywords(keywords);
262 descriptions = CustomSQLUtil.keywords(keywords);
263 }
264 else {
265 andOperator = true;
266 }
267
268 return countByC_N_D_T(
269 companyId, names, descriptions, types, params, andOperator);
270 }
271
272 public List<Role> findBySystem(long companyId) throws SystemException {
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
279
280 SQLQuery q = session.createSQLQuery(sql);
281
282 q.addEntity("Role_", RoleImpl.class);
283
284 QueryPos qPos = QueryPos.getInstance(q);
285
286 qPos.add(companyId);
287
288 return q.list(true);
289 }
290 catch (Exception e) {
291 throw new SystemException(e);
292 }
293 finally {
294 closeSession(session);
295 }
296 }
297
298 public List<Role> findByUserGroupGroupRole(long userId, long groupId)
299 throws SystemException {
300
301 Session session = null;
302
303 try {
304 session = openSession();
305
306 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
307
308 SQLQuery q = session.createSQLQuery(sql);
309
310 q.addEntity("Role_", RoleImpl.class);
311
312 QueryPos qPos = QueryPos.getInstance(q);
313
314 qPos.add(userId);
315 qPos.add(groupId);
316
317 return q.list(true);
318 }
319 catch (Exception e) {
320 throw new SystemException(e);
321 }
322 finally {
323 closeSession(session);
324 }
325 }
326
327 public List<Role> findByUserGroupRole(long userId, long groupId)
328 throws SystemException {
329
330 Session session = null;
331
332 try {
333 session = openSession();
334
335 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
336
337 SQLQuery q = session.createSQLQuery(sql);
338
339 q.addEntity("Role_", RoleImpl.class);
340
341 QueryPos qPos = QueryPos.getInstance(q);
342
343 qPos.add(userId);
344 qPos.add(groupId);
345
346 return q.list(true);
347 }
348 catch (Exception e) {
349 throw new SystemException(e);
350 }
351 finally {
352 closeSession(session);
353 }
354 }
355
356 public Role findByC_N(long companyId, String name)
357 throws NoSuchRoleException, SystemException {
358
359 name = StringUtil.lowerCase(name);
360
361 Session session = null;
362
363 try {
364 session = openSession();
365
366 String sql = CustomSQLUtil.get(FIND_BY_C_N);
367
368 SQLQuery q = session.createSQLQuery(sql);
369
370 q.addEntity("Role_", RoleImpl.class);
371
372 QueryPos qPos = QueryPos.getInstance(q);
373
374 qPos.add(companyId);
375 qPos.add(name);
376
377 List<Role> roles = q.list();
378
379 if (!roles.isEmpty()) {
380 return roles.get(0);
381 }
382 }
383 catch (Exception e) {
384 throw new SystemException(e);
385 }
386 finally {
387 closeSession(session);
388 }
389
390 StringBundler sb = new StringBundler(5);
391
392 sb.append("No Role exists with the key {companyId=");
393 sb.append(companyId);
394 sb.append(", name=");
395 sb.append(name);
396 sb.append("}");
397
398 throw new NoSuchRoleException(sb.toString());
399 }
400
401 public List<Role> findByU_G(long userId, List<Group> groups)
402 throws SystemException {
403
404 long[] groupIds = new long[groups.size()];
405
406 for (int i = 0; i < groups.size(); i++) {
407 Group group = groups.get(i);
408
409 groupIds[i] = group.getGroupId();
410 }
411
412 return findByU_G(userId, groupIds);
413 }
414
415 public List<Role> findByU_G(long userId, long groupId)
416 throws SystemException {
417
418 return findByU_G(userId, new long[] {groupId});
419 }
420
421 public List<Role> findByU_G(long userId, long[] groupIds)
422 throws SystemException {
423
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.createSQLQuery(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 public List<Role> findByR_N_A(
454 long resourceBlockId, String className, String actionId)
455 throws SystemException {
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.createSQLQuery(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 qPos.add(actionId);
473
474 return q.list(true);
475 }
476 catch (Exception e) {
477 throw new SystemException(e);
478 }
479 finally {
480 closeSession(session);
481 }
482 }
483
484 public List<Role> findByC_N_D_T(
485 long companyId, String name, String description, Integer[] types,
486 LinkedHashMap<String, Object> params, boolean andOperator,
487 int start, int end, OrderByComparator obc)
488 throws SystemException {
489
490 String[] names = CustomSQLUtil.keywords(name);
491 String[] descriptions = CustomSQLUtil.keywords(description);
492
493 return findByC_N_D_T(
494 companyId, names, descriptions, types, params, andOperator, start,
495 end, obc);
496 }
497
498 public List<Role> findByC_N_D_T(
499 long companyId, String[] names, String[] descriptions,
500 Integer[] types, LinkedHashMap<String, Object> params,
501 boolean andOperator, int start, int end, OrderByComparator obc)
502 throws SystemException {
503
504 names = CustomSQLUtil.keywords(names, true);
505 descriptions = CustomSQLUtil.keywords(descriptions, true);
506
507 if (types == null) {
508 types = new Integer[0];
509 }
510
511 Session session = null;
512
513 try {
514 session = openSession();
515
516 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
517
518 sql = CustomSQLUtil.replaceKeywords(
519 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
520 sql = CustomSQLUtil.replaceKeywords(
521 sql, "lower(Role_.description)", StringPool.LIKE, true,
522 descriptions);
523 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
524 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
525 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
526 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
527 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
528
529 SQLQuery q = session.createSQLQuery(sql);
530
531 q.addEntity("Role_", RoleImpl.class);
532
533 QueryPos qPos = QueryPos.getInstance(q);
534
535 setJoin(qPos, params);
536
537 qPos.add(companyId);
538 qPos.add(names, 2);
539 qPos.add(descriptions, 2);
540 qPos.add(types);
541
542 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
543 }
544 catch (Exception e) {
545 throw new SystemException(e);
546 }
547 finally {
548 closeSession(session);
549 }
550 }
551
552 public Map<String, List<String>> findByC_N_S_P(
553 long companyId, String name, int scope, String primKey)
554 throws SystemException {
555
556 Session session = null;
557
558 try {
559 session = openSession();
560
561 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
562
563 SQLQuery q = session.createSQLQuery(sql);
564
565 q.addScalar("roleName", Type.STRING);
566 q.addScalar("actionId", Type.STRING);
567
568 QueryPos qPos = QueryPos.getInstance(q);
569
570 qPos.add(companyId);
571 qPos.add(name);
572 qPos.add(scope);
573 qPos.add(primKey);
574
575 Map<String, List<String>> roleMap =
576 new HashMap<String, List<String>>();
577
578 Iterator<Object[]> itr = q.iterate();
579
580 while (itr.hasNext()) {
581 Object[] array = itr.next();
582
583 String roleName = (String)array[0];
584 String actionId = (String)array[1];
585
586 List<String> roleList = roleMap.get(roleName);
587
588 if (roleList == null) {
589 roleList = new ArrayList<String>();
590 }
591
592 roleList.add(actionId);
593
594 roleMap.put(roleName, roleList);
595 }
596
597 return roleMap;
598 }
599 catch (Exception e) {
600 throw new SystemException(e);
601 }
602 finally {
603 closeSession(session);
604 }
605 }
606
607 public List<Role> findByC_N_S_P_A(
608 long companyId, String name, int scope, String primKey,
609 String actionId)
610 throws SystemException {
611
612 Session session = null;
613
614 try {
615 session = openSession();
616
617 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
618
619 SQLQuery q = session.createSQLQuery(sql);
620
621 q.addEntity("Role_", RoleImpl.class);
622
623 QueryPos qPos = QueryPos.getInstance(q);
624
625 qPos.add(companyId);
626 qPos.add(name);
627 qPos.add(scope);
628 qPos.add(primKey);
629 qPos.add(actionId);
630
631 return q.list(true);
632 }
633 catch (Exception e) {
634 throw new SystemException(e);
635 }
636 finally {
637 closeSession(session);
638 }
639 }
640
641 public List<Role> findByKeywords(
642 long companyId, String keywords, Integer[] types, int start,
643 int end, OrderByComparator obc)
644 throws SystemException {
645
646 return findByKeywords(
647 companyId, keywords, types, new LinkedHashMap<String, Object>(),
648 start, end, obc);
649 }
650
651 public List<Role> findByKeywords(
652 long companyId, String keywords, Integer[] types,
653 LinkedHashMap<String, Object> params, int start, int end,
654 OrderByComparator obc)
655 throws SystemException {
656
657 String[] names = null;
658 String[] descriptions = null;
659 boolean andOperator = false;
660
661 if (Validator.isNotNull(keywords)) {
662 names = CustomSQLUtil.keywords(keywords);
663 descriptions = CustomSQLUtil.keywords(keywords);
664 }
665 else {
666 andOperator = true;
667 }
668
669 return findByC_N_D_T(
670 companyId, names, descriptions, types, params, andOperator, start,
671 end, obc);
672 }
673
674 protected String getCountByR_U_SQL() {
675 if (_countByR_U == null) {
676 StringBundler sb = new StringBundler(13);
677
678 sb.append("(");
679 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
680 sb.append(") UNION (");
681 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
682 sb.append(") UNION (");
683 sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
684 sb.append(") UNION (");
685 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
686 sb.append(") UNION (");
687 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
688 sb.append(") UNION (");
689 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
690 sb.append(")");
691
692 _countByR_U = sb.toString();
693 }
694
695 return _countByR_U;
696 }
697
698 protected String getGroupIds(long[] groupIds, String table) {
699 if (groupIds.length == 0) {
700 return StringPool.BLANK;
701 }
702
703 StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
704
705 for (int i = 0; i < groupIds.length; i++) {
706 sb.append(table);
707 sb.append(".groupId = ?");
708
709 if ((i + 1) < groupIds.length) {
710 sb.append(" OR ");
711 }
712 }
713
714 return sb.toString();
715 }
716
717 protected String getJoin(LinkedHashMap<String, Object> params) {
718 if ((params == null) || params.isEmpty()) {
719 return StringPool.BLANK;
720 }
721
722 StringBundler sb = new StringBundler(params.size());
723
724 for (Map.Entry<String, Object> entry : params.entrySet()) {
725 String key = entry.getKey();
726 Object value = entry.getValue();
727
728 if (Validator.isNotNull(value)) {
729 sb.append(getJoin(key));
730 }
731 }
732
733 return sb.toString();
734 }
735
736 protected String getJoin(String key) {
737 String join = StringPool.BLANK;
738
739 if (key.equals("usersRoles")) {
740 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
741 }
742
743 if (Validator.isNotNull(join)) {
744 int pos = join.indexOf("WHERE");
745
746 if (pos != -1) {
747 join = join.substring(0, pos);
748 }
749 }
750
751 return join;
752 }
753
754 protected String getTypes(Integer[] types) {
755 if (types.length == 0) {
756 return StringPool.BLANK;
757 }
758
759 StringBundler sb = new StringBundler(types.length * 2);
760
761 sb.append(" AND (");
762
763 for (int i = 0; i < types.length; i++) {
764 sb.append("Role_.type_ = ?");
765
766 if ((i + 1) < types.length) {
767 sb.append(" OR ");
768 }
769 }
770
771 sb.append(")");
772
773 return sb.toString();
774 }
775
776 protected String getWhere(LinkedHashMap<String, Object> params) {
777 if ((params == null) || params.isEmpty()) {
778 return StringPool.BLANK;
779 }
780
781 StringBundler sb = new StringBundler(params.size());
782
783 for (Map.Entry<String, Object> entry : params.entrySet()) {
784 String key = entry.getKey();
785 Object value = entry.getValue();
786
787 if (Validator.isNotNull(value)) {
788 sb.append(getWhere(key));
789 }
790 }
791
792 return sb.toString();
793 }
794
795 protected String getWhere(String key) {
796 String join = StringPool.BLANK;
797
798 if (key.equals("usersRoles")) {
799 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
800 }
801
802 if (Validator.isNotNull(join)) {
803 int pos = join.indexOf("WHERE");
804
805 if (pos != -1) {
806 join = join.substring(pos + 5, join.length()).concat(" AND ");
807 }
808 else {
809 join = StringPool.BLANK;
810 }
811 }
812
813 return join;
814 }
815
816 protected void setJoin(
817 QueryPos qPos, LinkedHashMap<String, Object> params) {
818
819 if (params == null) {
820 return;
821 }
822
823 for (Map.Entry<String, Object> entry : params.entrySet()) {
824 Object value = entry.getValue();
825
826 if (value instanceof Long) {
827 Long valueLong = (Long)value;
828
829 if (Validator.isNotNull(valueLong)) {
830 qPos.add(valueLong);
831 }
832 }
833 else if (value instanceof String) {
834 String valueString = (String)value;
835
836 if (Validator.isNotNull(valueString)) {
837 qPos.add(valueString);
838 }
839 }
840 }
841 }
842
843 private String _countByR_U;
844
845 }