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