001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.NoSuchUserGroupException;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.model.UserGroup;
025 import com.liferay.portal.kernel.security.permission.InlineSQLHelperUtil;
026 import com.liferay.portal.kernel.service.persistence.UserGroupFinder;
027 import com.liferay.portal.kernel.service.persistence.UserGroupUtil;
028 import com.liferay.portal.kernel.util.OrderByComparator;
029 import com.liferay.portal.kernel.util.StringBundler;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.StringUtil;
032 import com.liferay.portal.kernel.util.Validator;
033 import com.liferay.portal.model.impl.UserGroupImpl;
034 import com.liferay.util.dao.orm.CustomSQLUtil;
035
036 import java.util.Iterator;
037 import java.util.LinkedHashMap;
038 import java.util.List;
039 import java.util.Map;
040
041
044 public class UserGroupFinderImpl
045 extends UserGroupFinderBaseImpl implements UserGroupFinder {
046
047 public static final String COUNT_BY_C_N_D =
048 UserGroupFinder.class.getName() + ".countByC_N_D";
049
050 public static final String FIND_BY_C_N_D =
051 UserGroupFinder.class.getName() + ".findByC_N_D";
052
053 public static final String JOIN_BY_USER_GROUP_GROUP_ROLE =
054 UserGroupFinder.class.getName() + ".joinByUserGroupGroupRole";
055
056 public static final String JOIN_BY_USER_GROUPS_GROUPS =
057 UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
058
059 public static final String JOIN_BY_USER_GROUPS_ROLES =
060 UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
061
062 public static final String JOIN_BY_USER_GROUPS_TEAMS =
063 UserGroupFinder.class.getName() + ".joinByUserGroupsTeams";
064
065 public static final String JOIN_BY_USER_GROUPS_USERS =
066 UserGroupFinder.class.getName() + ".joinByUserGroupsUsers";
067
068 @Override
069 public int countByKeywords(
070 long companyId, String keywords, LinkedHashMap<String, Object> params) {
071
072 return doCountByKeywords(companyId, keywords, params, false);
073 }
074
075 @Override
076 public int countByC_N_D(
077 long companyId, String name, String description,
078 LinkedHashMap<String, Object> params, boolean andOperator) {
079
080 String[] names = CustomSQLUtil.keywords(name);
081 String[] descriptions = CustomSQLUtil.keywords(description);
082
083 return countByC_N_D(
084 companyId, names, descriptions, params, andOperator);
085 }
086
087 @Override
088 public int countByC_N_D(
089 long companyId, String[] names, String[] descriptions,
090 LinkedHashMap<String, Object> params, boolean andOperator) {
091
092 return doCountByC_N_D(
093 companyId, names, descriptions, params, andOperator, false);
094 }
095
096 @Override
097 public int filterCountByKeywords(
098 long companyId, String keywords, LinkedHashMap<String, Object> params) {
099
100 return doCountByKeywords(companyId, keywords, params, true);
101 }
102
103 @Override
104 public int filterCountByC_N_D(
105 long companyId, String name, String description,
106 LinkedHashMap<String, Object> params, boolean andOperator) {
107
108 String[] names = CustomSQLUtil.keywords(name);
109 String[] descriptions = CustomSQLUtil.keywords(description);
110
111 return filterCountByC_N_D(
112 companyId, names, descriptions, params, andOperator);
113 }
114
115 @Override
116 public int filterCountByC_N_D(
117 long companyId, String[] names, String[] descriptions,
118 LinkedHashMap<String, Object> params, boolean andOperator) {
119
120 return doCountByC_N_D(
121 companyId, names, descriptions, params, andOperator, true);
122 }
123
124 @Override
125 public List<UserGroup> filterFindByKeywords(
126 long companyId, String keywords, LinkedHashMap<String, Object> params,
127 int start, int end, OrderByComparator<UserGroup> obc) {
128
129 return doFindByKeywords(
130 companyId, keywords, params, start, end, obc, true);
131 }
132
133 @Override
134 public List<UserGroup> filterFindByC_N_D(
135 long companyId, String name, String description,
136 LinkedHashMap<String, Object> params, boolean andOperator, int start,
137 int end, OrderByComparator<UserGroup> obc) {
138
139 String[] names = CustomSQLUtil.keywords(name);
140 String[] descriptions = CustomSQLUtil.keywords(description);
141
142 return filterFindByC_N_D(
143 companyId, names, descriptions, params, andOperator, start, end,
144 obc);
145 }
146
147 @Override
148 public List<UserGroup> filterFindByC_N_D(
149 long companyId, String[] names, String[] descriptions,
150 LinkedHashMap<String, Object> params, boolean andOperator, int start,
151 int end, OrderByComparator<UserGroup> obc) {
152
153 return doFindByC_N_D(
154 companyId, names, descriptions, params, andOperator, start, end,
155 obc, true);
156 }
157
158 @Override
159 public List<UserGroup> findByKeywords(
160 long companyId, String keywords, LinkedHashMap<String, Object> params,
161 int start, int end, OrderByComparator<UserGroup> obc) {
162
163 return doFindByKeywords(
164 companyId, keywords, params, start, end, obc, false);
165 }
166
167
170 @Deprecated
171 @Override
172 public UserGroup findByC_N(long companyId, String name)
173 throws NoSuchUserGroupException {
174
175 return UserGroupUtil.findByC_N(companyId, name);
176 }
177
178 @Override
179 public List<UserGroup> findByC_N_D(
180 long companyId, String name, String description,
181 LinkedHashMap<String, Object> params, boolean andOperator, int start,
182 int end, OrderByComparator<UserGroup> obc) {
183
184 String[] names = CustomSQLUtil.keywords(name);
185 String[] descriptions = CustomSQLUtil.keywords(description);
186
187 return findByC_N_D(
188 companyId, names, descriptions, params, andOperator, start, end,
189 obc);
190 }
191
192 @Override
193 public List<UserGroup> findByC_N_D(
194 long companyId, String[] names, String[] descriptions,
195 LinkedHashMap<String, Object> params, boolean andOperator, int start,
196 int end, OrderByComparator<UserGroup> obc) {
197
198 return doFindByC_N_D(
199 companyId, names, descriptions, params, andOperator, start, end,
200 obc, false);
201 }
202
203 protected int doCountByKeywords(
204 long companyId, String keywords, LinkedHashMap<String, Object> params,
205 boolean inlineSQLHelper) {
206
207 String[] names = null;
208 String[] descriptions = null;
209 boolean andOperator = false;
210
211 if (Validator.isNotNull(keywords)) {
212 names = CustomSQLUtil.keywords(keywords);
213 descriptions = CustomSQLUtil.keywords(keywords);
214 }
215 else {
216 andOperator = true;
217 }
218
219 return doCountByC_N_D(
220 companyId, names, descriptions, params, andOperator,
221 inlineSQLHelper);
222 }
223
224 protected int doCountByC_N_D(
225 long companyId, String[] names, String[] descriptions,
226 LinkedHashMap<String, Object> params, boolean andOperator,
227 boolean inlineSQLHelper) {
228
229 names = CustomSQLUtil.keywords(names);
230 descriptions = CustomSQLUtil.keywords(descriptions);
231
232 Session session = null;
233
234 try {
235 session = openSession();
236
237 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
238
239 sql = CustomSQLUtil.replaceKeywords(
240 sql, "lower(UserGroup.name)", StringPool.LIKE, false, names);
241 sql = CustomSQLUtil.replaceKeywords(
242 sql, "lower(UserGroup.description)", StringPool.LIKE, true,
243 descriptions);
244 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
245 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
246 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
247
248 if (inlineSQLHelper) {
249 sql = InlineSQLHelperUtil.replacePermissionCheck(
250 sql, UserGroup.class.getName(), "UserGroup.userGroupId",
251 null, null, new long[] {0}, null);
252 }
253
254 SQLQuery q = session.createSynchronizedSQLQuery(sql);
255
256 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
257
258 QueryPos qPos = QueryPos.getInstance(q);
259
260 setJoin(qPos, params);
261
262 qPos.add(companyId);
263 qPos.add(names, 2);
264 qPos.add(descriptions, 2);
265
266 Iterator<Long> itr = q.iterate();
267
268 if (itr.hasNext()) {
269 Long count = itr.next();
270
271 if (count != null) {
272 return count.intValue();
273 }
274 }
275
276 return 0;
277 }
278 catch (Exception e) {
279 throw new SystemException(e);
280 }
281 finally {
282 closeSession(session);
283 }
284 }
285
286 protected List<UserGroup> doFindByKeywords(
287 long companyId, String keywords, LinkedHashMap<String, Object> params,
288 int start, int end, OrderByComparator<UserGroup> obc,
289 boolean inlineSQLHelper) {
290
291 String[] names = null;
292 String[] descriptions = null;
293 boolean andOperator = false;
294
295 if (Validator.isNotNull(keywords)) {
296 names = CustomSQLUtil.keywords(keywords);
297 descriptions = CustomSQLUtil.keywords(keywords);
298 }
299 else {
300 andOperator = true;
301 }
302
303 return doFindByC_N_D(
304 companyId, names, descriptions, params, andOperator, start, end,
305 obc, inlineSQLHelper);
306 }
307
308 protected List<UserGroup> doFindByC_N_D(
309 long companyId, String[] names, String[] descriptions,
310 LinkedHashMap<String, Object> params, boolean andOperator, int start,
311 int end, OrderByComparator<UserGroup> obc, boolean inlineSQLHelper) {
312
313 names = CustomSQLUtil.keywords(names);
314 descriptions = CustomSQLUtil.keywords(descriptions);
315
316 Session session = null;
317
318 try {
319 session = openSession();
320
321 String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
322
323 sql = CustomSQLUtil.replaceKeywords(
324 sql, "lower(UserGroup.name)", StringPool.LIKE, false, names);
325 sql = CustomSQLUtil.replaceKeywords(
326 sql, "lower(UserGroup.description)", StringPool.LIKE, true,
327 descriptions);
328
329 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
330 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
331 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
332 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
333
334 if (inlineSQLHelper) {
335 sql = InlineSQLHelperUtil.replacePermissionCheck(
336 sql, UserGroup.class.getName(), "UserGroup.userGroupId",
337 null, null, new long[] {0}, null);
338 }
339
340 SQLQuery q = session.createSynchronizedSQLQuery(sql);
341
342 q.addEntity("UserGroup", UserGroupImpl.class);
343
344 QueryPos qPos = QueryPos.getInstance(q);
345
346 setJoin(qPos, params);
347
348 qPos.add(companyId);
349 qPos.add(names, 2);
350 qPos.add(descriptions, 2);
351
352 return (List<UserGroup>)QueryUtil.list(q, getDialect(), start, end);
353 }
354 catch (Exception e) {
355 throw new SystemException(e);
356 }
357 finally {
358 closeSession(session);
359 }
360 }
361
362 protected String getJoin(LinkedHashMap<String, Object> params) {
363 if ((params == null) || params.isEmpty()) {
364 return StringPool.BLANK;
365 }
366
367 StringBundler sb = new StringBundler(params.size());
368
369 for (Map.Entry<String, Object> entry : params.entrySet()) {
370 String key = entry.getKey();
371 Object value = entry.getValue();
372
373 if (Validator.isNotNull(value)) {
374 sb.append(getJoin(key));
375 }
376 }
377
378 return sb.toString();
379 }
380
381 protected String getJoin(String key) {
382 String join = StringPool.BLANK;
383
384 if (key.equals("userGroupGroupRole")) {
385 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
386 }
387 else if (key.equals("userGroupsGroups")) {
388 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
389 }
390 else if (key.equals("userGroupsRoles")) {
391 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
392 }
393 else if (key.equals("userGroupsTeams")) {
394 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
395 }
396 else if (key.equals("userGroupsUsers")) {
397 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
398 }
399
400 if (Validator.isNotNull(join)) {
401 int pos = join.indexOf("WHERE");
402
403 if (pos != -1) {
404 join = join.substring(0, pos);
405 }
406 }
407
408 return join;
409 }
410
411 protected String getWhere(LinkedHashMap<String, Object> params) {
412 if ((params == null) || params.isEmpty()) {
413 return StringPool.BLANK;
414 }
415
416 StringBundler sb = new StringBundler(params.size());
417
418 for (Map.Entry<String, Object> entry : params.entrySet()) {
419 String key = entry.getKey();
420 Object value = entry.getValue();
421
422 if (Validator.isNotNull(value)) {
423 sb.append(getWhere(key, value));
424 }
425 }
426
427 return sb.toString();
428 }
429
430 protected String getWhere(String key, Object value) {
431 String join = StringPool.BLANK;
432
433 if (key.equals("userGroupGroupRole")) {
434 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
435 }
436 else if (key.equals("userGroupsGroups")) {
437 if (value instanceof Long) {
438 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
439 }
440 else if (value instanceof Long[]) {
441 Long[] userGroupIds = (Long[])value;
442
443 if (userGroupIds.length == 0) {
444 join = "WHERE (Groups_UserGroups.groupId = -1)";
445 }
446 else {
447 StringBundler sb = new StringBundler(
448 userGroupIds.length * 2 + 1);
449
450 sb.append("WHERE (");
451
452 for (int i = 0; i < userGroupIds.length; i++) {
453 sb.append("(Groups_UserGroups.groupId = ?) ");
454
455 if ((i + 1) < userGroupIds.length) {
456 sb.append("OR ");
457 }
458 }
459
460 sb.append(StringPool.CLOSE_PARENTHESIS);
461
462 join = sb.toString();
463 }
464 }
465 }
466 else if (key.equals("userGroupsRoles")) {
467 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
468 }
469 else if (key.equals("userGroupsTeams")) {
470 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
471 }
472 else if (key.equals("userGroupsUsers")) {
473 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
474 }
475
476 if (Validator.isNotNull(join)) {
477 int pos = join.indexOf("WHERE");
478
479 if (pos != -1) {
480 join = join.substring(pos + 5, join.length()).concat(" AND ");
481 }
482 else {
483 join = StringPool.BLANK;
484 }
485 }
486
487 return join;
488 }
489
490 protected void setJoin(
491 QueryPos qPos, LinkedHashMap<String, Object> params) {
492
493 if (params == null) {
494 return;
495 }
496
497 for (Map.Entry<String, Object> entry : params.entrySet()) {
498 Object value = entry.getValue();
499
500 if (value instanceof Long) {
501 Long valueLong = (Long)value;
502
503 if (Validator.isNotNull(valueLong)) {
504 qPos.add(valueLong);
505 }
506 }
507 else if (value instanceof Long[]) {
508 Long[] valueArray = (Long[])value;
509
510 for (int i = 0; i < valueArray.length; i++) {
511 if (Validator.isNotNull(valueArray[i])) {
512 qPos.add(valueArray[i]);
513 }
514 }
515 }
516 else if (value instanceof String) {
517 String valueString = (String)value;
518
519 if (Validator.isNotNull(valueString)) {
520 qPos.add(valueString);
521 }
522 }
523 }
524 }
525
526 }