1
19
20 package com.liferay.portal.service.persistence;
21
22 import com.liferay.portal.NoSuchRoleException;
23 import com.liferay.portal.SystemException;
24 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
25 import com.liferay.portal.kernel.dao.orm.QueryPos;
26 import com.liferay.portal.kernel.dao.orm.QueryUtil;
27 import com.liferay.portal.kernel.dao.orm.SQLQuery;
28 import com.liferay.portal.kernel.dao.orm.Session;
29 import com.liferay.portal.kernel.dao.orm.Type;
30 import com.liferay.portal.kernel.util.OrderByComparator;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.model.Group;
35 import com.liferay.portal.model.Role;
36 import com.liferay.portal.model.impl.RoleImpl;
37 import com.liferay.portal.model.impl.RoleModelImpl;
38 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
39 import com.liferay.util.dao.orm.CustomSQLUtil;
40
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47
48
54 public class RoleFinderImpl extends BasePersistenceImpl implements RoleFinder {
55
56 public static String COUNT_BY_C_N_D_T =
57 RoleFinder.class.getName() + ".countByC_N_D_T";
58
59 public static String COUNT_BY_COMMUNITY =
60 RoleFinder.class.getName() + ".countByCommunity";
61
62 public static String COUNT_BY_ORGANIZATION =
63 RoleFinder.class.getName() + ".countByOrganization";
64
65 public static String COUNT_BY_USER =
66 RoleFinder.class.getName() + ".countByUser";
67
68 public static String COUNT_BY_USER_GROUP =
69 RoleFinder.class.getName() + ".countByUserGroup";
70
71 public static String FIND_BY_USER_GROUP_ROLE =
72 RoleFinder.class.getName() + ".findByUserGroupRole";
73
74 public static String FIND_BY_C_N =
75 RoleFinder.class.getName() + ".findByC_N";
76
77 public static String FIND_BY_U_G =
78 RoleFinder.class.getName() + ".findByU_G";
79
80 public static String FIND_BY_C_N_D_T =
81 RoleFinder.class.getName() + ".findByC_N_D_T";
82
83 public static String FIND_BY_C_N_S_P =
84 RoleFinder.class.getName() + ".findByC_N_S_P";
85
86 public static String JOIN_BY_ROLES_PERMISSIONS =
87 RoleFinder.class.getName() + ".joinByRolesPermissions";
88
89 public static String JOIN_BY_USERS_ROLES =
90 RoleFinder.class.getName() + ".joinByUsersRoles";
91
92 public int countByR_U(long roleId, long userId) throws SystemException {
93 Session session = null;
94
95 try {
96 session = openSession();
97
98 StringBuilder sb = new StringBuilder();
99
100 sb.append("(");
101 sb.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
102 sb.append(") UNION (");
103 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
104 sb.append(") UNION (");
105 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
106 sb.append(") UNION (");
107 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
108 sb.append(")");
109
110 SQLQuery q = session.createSQLQuery(sb.toString());
111
112 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
113
114 QueryPos qPos = QueryPos.getInstance(q);
115
116 for (int i = 0; i < 4; i++) {
117 qPos.add(roleId);
118 qPos.add(userId);
119 }
120
121 int count = 0;
122
123 Iterator<Long> itr = q.list().iterator();
124
125 while (itr.hasNext()) {
126 Long l = itr.next();
127
128 if (l != null) {
129 count += l.intValue();
130 }
131 }
132
133 return count;
134 }
135 catch (Exception e) {
136 throw new SystemException(e);
137 }
138 finally {
139 closeSession(session);
140 }
141 }
142
143 public int countByC_N_D_T(
144 long companyId, String name, String description, Integer type,
145 LinkedHashMap<String, Object> params)
146 throws SystemException {
147
148 name = StringUtil.lowerCase(name);
149 description = StringUtil.lowerCase(description);
150
151 Session session = null;
152
153 try {
154 session = openSession();
155
156 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
157
158 if (type == null) {
159 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
160 }
161
162 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
163 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
164
165 SQLQuery q = session.createSQLQuery(sql);
166
167 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
168
169 QueryPos qPos = QueryPos.getInstance(q);
170
171 setJoin(qPos, params);
172 qPos.add(companyId);
173 qPos.add(name);
174 qPos.add(name);
175 qPos.add(description);
176 qPos.add(description);
177
178 if (type != null) {
179 qPos.add(type);
180 }
181
182 Iterator<Long> itr = q.list().iterator();
183
184 if (itr.hasNext()) {
185 Long count = itr.next();
186
187 if (count != null) {
188 return count.intValue();
189 }
190 }
191
192 return 0;
193 }
194 catch (Exception e) {
195 throw new SystemException(e);
196 }
197 finally {
198 closeSession(session);
199 }
200 }
201
202 public List<Role> findByUserGroupRole(long userId, long groupId)
203 throws SystemException {
204
205 Session session = null;
206
207 try {
208 session = openSession();
209
210 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
211
212 SQLQuery q = session.createSQLQuery(sql);
213
214 q.addEntity("Role_", RoleImpl.class);
215
216 QueryPos qPos = QueryPos.getInstance(q);
217
218 qPos.add(userId);
219 qPos.add(groupId);
220
221 return q.list();
222 }
223 catch (Exception e) {
224 throw new SystemException(e);
225 }
226 finally {
227 closeSession(session);
228 }
229 }
230
231 public Role findByC_N(long companyId, String name)
232 throws NoSuchRoleException, SystemException {
233
234 name = StringUtil.lowerCase(name);
235
236 boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
237 String finderClassName = Role.class.getName();
238 String finderMethodName = "customFindByC_N";
239 String finderParams[] = new String[] {
240 Long.class.getName(), String.class.getName()
241 };
242 Object finderArgs[] = new Object[] {new Long(companyId), name};
243
244 Object result = FinderCacheUtil.getResult(
245 finderClassName, finderMethodName, finderParams, finderArgs, this);
246
247 if (result == null) {
248 Session session = null;
249
250 try {
251 session = openSession();
252
253 String sql = CustomSQLUtil.get(FIND_BY_C_N);
254
255 SQLQuery q = session.createSQLQuery(sql);
256
257 q.addEntity("Role_", RoleImpl.class);
258
259 QueryPos qPos = QueryPos.getInstance(q);
260
261 qPos.add(companyId);
262 qPos.add(name);
263
264 Iterator<Role> itr = q.list().iterator();
265
266 if (itr.hasNext()) {
267 Role role = itr.next();
268
269 FinderCacheUtil.putResult(
270 finderClassNameCacheEnabled, finderClassName,
271 finderMethodName, finderParams, finderArgs, role);
272
273 return role;
274 }
275 }
276 catch (Exception e) {
277 throw new SystemException(e);
278 }
279 finally {
280 closeSession(session);
281 }
282
283 throw new NoSuchRoleException(
284 "No Role exists with the key {companyId=" + companyId +
285 ", name=" + name + "}");
286 }
287 else {
288 return (Role)result;
289 }
290 }
291
292 public List<Role> findByU_G(long userId, long groupId)
293 throws SystemException {
294
295 return findByU_G(userId, new long[] {groupId});
296 }
297
298 public List<Role> findByU_G(long userId, long[] groupIds)
299 throws SystemException {
300
301 Session session = null;
302
303 try {
304 session = openSession();
305
306 String sql = CustomSQLUtil.get(FIND_BY_U_G);
307
308 sql = StringUtil.replace(
309 sql, "[$GROUP_IDS$]", getGroupIds(groupIds, "Groups_Roles"));
310
311 SQLQuery q = session.createSQLQuery(sql);
312
313 q.addEntity("Role_", RoleImpl.class);
314
315 QueryPos qPos = QueryPos.getInstance(q);
316
317 qPos.add(userId);
318 setGroupIds(qPos, groupIds);
319
320 return q.list();
321 }
322 catch (Exception e) {
323 throw new SystemException(e);
324 }
325 finally {
326 closeSession(session);
327 }
328 }
329
330 public List<Role> findByU_G(long userId, List<Group> groups)
331 throws SystemException {
332
333 long[] groupIds = new long[groups.size()];
334
335 for (int i = 0; i < groups.size(); i++) {
336 Group group = groups.get(i);
337
338 groupIds[i] = group.getGroupId();
339 }
340
341 return findByU_G(userId, groupIds);
342 }
343
344 public List<Role> findByC_N_D_T(
345 long companyId, String name, String description, Integer type,
346 LinkedHashMap<String, Object> params, int start, int end,
347 OrderByComparator obc)
348 throws SystemException {
349
350 name = StringUtil.lowerCase(name);
351 description = StringUtil.lowerCase(description);
352
353 Session session = null;
354
355 try {
356 session = openSession();
357
358 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
359
360 if (type == null) {
361 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
362 }
363
364 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
365 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
366 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
367
368 SQLQuery q = session.createSQLQuery(sql);
369
370 q.addEntity("Role_", RoleImpl.class);
371
372 QueryPos qPos = QueryPos.getInstance(q);
373
374 setJoin(qPos, params);
375 qPos.add(companyId);
376 qPos.add(name);
377 qPos.add(name);
378 qPos.add(description);
379 qPos.add(description);
380
381 if (type != null) {
382 qPos.add(type);
383 }
384
385 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
386 }
387 catch (Exception e) {
388 throw new SystemException(e);
389 }
390 finally {
391 closeSession(session);
392 }
393 }
394
395 public Map<String, List<String>> findByC_N_S_P(
396 long companyId, String name, int scope, String primKey)
397 throws SystemException {
398
399 Session session = null;
400
401 try {
402 session = openSession();
403
404 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
405
406 SQLQuery q = session.createSQLQuery(sql);
407
408 q.addScalar("roleName", Type.STRING);
409 q.addScalar("actionId", Type.STRING);
410
411 QueryPos qPos = QueryPos.getInstance(q);
412
413 qPos.add(companyId);
414 qPos.add(name);
415 qPos.add(scope);
416 qPos.add(primKey);
417
418 Map<String, List<String>> roleMap =
419 new HashMap<String, List<String>>();
420
421 Iterator<Object[]> itr = q.list().iterator();
422
423 while (itr.hasNext()) {
424 Object[] array = itr.next();
425
426 String roleName = (String)array[0];
427 String actionId = (String)array[1];
428
429 List<String> roleList = roleMap.get(roleName);
430
431 if (roleList == null) {
432 roleList = new ArrayList<String>();
433 }
434
435 roleList.add(actionId);
436
437 roleMap.put(roleName, roleList);
438 }
439
440 return roleMap;
441 }
442 catch (Exception e) {
443 throw new SystemException(e);
444 }
445 finally {
446 closeSession(session);
447 }
448 }
449
450 protected String getGroupIds(long[] groupIds, String table) {
451 StringBuilder sb = new StringBuilder();
452
453 for (int i = 0; i < groupIds.length; i++) {
454 sb.append(table);
455 sb.append(".groupId = ?");
456
457 if ((i + 1) < groupIds.length) {
458 sb.append(" OR ");
459 }
460 }
461
462 return sb.toString();
463 }
464
465 protected void setGroupIds(QueryPos qPos, long[] groupIds) {
466 for (int i = 0; i < groupIds.length; i++) {
467 qPos.add(groupIds[i]);
468 }
469 }
470
471 protected String getJoin(LinkedHashMap<String, Object> params) {
472 if (params == null) {
473 return StringPool.BLANK;
474 }
475
476 StringBuilder sb = new StringBuilder();
477
478 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
479
480 while (itr.hasNext()) {
481 Map.Entry<String, Object> entry = itr.next();
482
483 String key = entry.getKey();
484 Object value = entry.getValue();
485
486 if (Validator.isNotNull(value)) {
487 sb.append(getJoin(key));
488 }
489 }
490
491 return sb.toString();
492 }
493
494 protected String getJoin(String key) {
495 String join = StringPool.BLANK;
496
497 if (key.equals("permissionsResourceId")) {
498 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
499 }
500 else if (key.equals("usersRoles")) {
501 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
502 }
503
504 if (Validator.isNotNull(join)) {
505 int pos = join.indexOf("WHERE");
506
507 if (pos != -1) {
508 join = join.substring(0, pos);
509 }
510 }
511
512 return join;
513 }
514
515 protected String getWhere(LinkedHashMap<String, Object> params) {
516 if (params == null) {
517 return StringPool.BLANK;
518 }
519
520 StringBuilder sb = new StringBuilder();
521
522 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
523
524 while (itr.hasNext()) {
525 Map.Entry<String, Object> entry = itr.next();
526
527 String key = entry.getKey();
528 Object value = entry.getValue();
529
530 if (Validator.isNotNull(value)) {
531 sb.append(getWhere(key));
532 }
533 }
534
535 return sb.toString();
536 }
537
538 protected String getWhere(String key) {
539 String join = StringPool.BLANK;
540
541 if (key.equals("permissionsResourceId")) {
542 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
543 }
544 else if (key.equals("usersRoles")) {
545 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
546 }
547
548 if (Validator.isNotNull(join)) {
549 int pos = join.indexOf("WHERE");
550
551 if (pos != -1) {
552 StringBuilder sb = new StringBuilder();
553
554 sb.append(join.substring(pos + 5, join.length()));
555 sb.append(" AND ");
556
557 join = sb.toString();
558 }
559 else {
560 join = StringPool.BLANK;
561 }
562 }
563
564 return join;
565 }
566
567 protected void setJoin(
568 QueryPos qPos, LinkedHashMap<String, Object> params) {
569
570 if (params != null) {
571 Iterator<Map.Entry<String, Object>> itr =
572 params.entrySet().iterator();
573
574 while (itr.hasNext()) {
575 Map.Entry<String, Object> entry = itr.next();
576
577 Object value = entry.getValue();
578
579 if (value instanceof Long) {
580 Long valueLong = (Long)value;
581
582 if (Validator.isNotNull(valueLong)) {
583 qPos.add(valueLong);
584 }
585 }
586 else if (value instanceof String) {
587 String valueString = (String)value;
588
589 if (Validator.isNotNull(valueString)) {
590 qPos.add(valueString);
591 }
592 }
593 }
594 }
595 }
596
597 }