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