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