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