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 public int countByKeywords(
070 long companyId, String keywords,
071 LinkedHashMap<String, Object> params)
072 throws SystemException {
073
074 String[] names = null;
075 String[] descriptions = null;
076 boolean andOperator = false;
077
078 if (Validator.isNotNull(keywords)) {
079 names = CustomSQLUtil.keywords(keywords);
080 descriptions = CustomSQLUtil.keywords(keywords);
081 }
082 else {
083 andOperator = true;
084 }
085
086 return countByC_N_D(
087 companyId, names, descriptions, params, andOperator);
088 }
089
090 public int countByC_N_D(
091 long companyId, String name, String description,
092 LinkedHashMap<String, Object> params, boolean andOperator)
093 throws SystemException {
094
095 String[] names = CustomSQLUtil.keywords(name);
096 String[] descriptions = CustomSQLUtil.keywords(description);
097
098 return countByC_N_D(
099 companyId, names, descriptions, params, andOperator);
100 }
101
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 public List<UserGroup> findByKeywords(
159 long companyId, String keywords,
160 LinkedHashMap<String, Object> params, int start, int end,
161 OrderByComparator obc)
162 throws SystemException {
163
164 String[] names = null;
165 String[] descriptions = null;
166 boolean andOperator = false;
167
168 if (Validator.isNotNull(keywords)) {
169 names = CustomSQLUtil.keywords(keywords);
170 descriptions = CustomSQLUtil.keywords(keywords);
171 }
172 else {
173 andOperator = true;
174 }
175
176 return findByC_N_D(
177 companyId, names, descriptions, params, andOperator, start, end,
178 obc);
179 }
180
181 public UserGroup findByC_N(long companyId, String name)
182 throws NoSuchUserGroupException, SystemException {
183
184 name = StringUtil.lowerCase(name);
185
186 Session session = null;
187
188 try {
189 session = openSession();
190
191 String sql = CustomSQLUtil.get(FIND_BY_C_N);
192
193 SQLQuery q = session.createSQLQuery(sql);
194
195 q.addEntity("UserGroup", UserGroupImpl.class);
196
197 QueryPos qPos = QueryPos.getInstance(q);
198
199 qPos.add(companyId);
200 qPos.add(name);
201
202 List<UserGroup> userGroups = q.list();
203
204 if (!userGroups.isEmpty()) {
205 return userGroups.get(0);
206 }
207 }
208 catch (Exception e) {
209 throw new SystemException(e);
210 }
211 finally {
212 closeSession(session);
213 }
214
215 StringBundler sb = new StringBundler(5);
216
217 sb.append("No UserGroup exists with the key {companyId=");
218 sb.append(companyId);
219 sb.append(", name=");
220 sb.append(name);
221 sb.append("}");
222
223 throw new NoSuchUserGroupException(sb.toString());
224 }
225
226 public List<UserGroup> findByC_N_D(
227 long companyId, String name, String description,
228 LinkedHashMap<String, Object> params, boolean andOperator,
229 int start, int end, OrderByComparator obc)
230 throws SystemException {
231
232 String[] names = CustomSQLUtil.keywords(name);
233 String[] descriptions = CustomSQLUtil.keywords(description);
234
235 return findByC_N_D(
236 companyId, names, descriptions, params, andOperator, start, end,
237 obc);
238 }
239
240 public List<UserGroup> findByC_N_D(
241 long companyId, String[] names, String[] descriptions,
242 LinkedHashMap<String, Object> params, boolean andOperator,
243 int start, int end, OrderByComparator obc)
244 throws SystemException {
245
246 names = CustomSQLUtil.keywords(names);
247 descriptions = CustomSQLUtil.keywords(descriptions);
248
249 Session session = null;
250
251 try {
252 session = openSession();
253
254 String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
255
256 sql = CustomSQLUtil.replaceKeywords(
257 sql, "lower(UserGroup.name)", StringPool.LIKE, false, names);
258 sql = CustomSQLUtil.replaceKeywords(
259 sql, "lower(UserGroup.description)", StringPool.LIKE, true,
260 descriptions);
261
262 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
263 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
264 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
265 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
266
267 SQLQuery q = session.createSQLQuery(sql);
268
269 q.addEntity("UserGroup", UserGroupImpl.class);
270
271 QueryPos qPos = QueryPos.getInstance(q);
272
273 setJoin(qPos, params);
274
275 qPos.add(companyId);
276 qPos.add(names, 2);
277 qPos.add(descriptions, 2);
278
279 return (List<UserGroup>)QueryUtil.list(q, getDialect(), start, end);
280 }
281 catch (Exception e) {
282 throw new SystemException(e);
283 }
284 finally {
285 closeSession(session);
286 }
287 }
288
289 protected String getJoin(LinkedHashMap<String, Object> params) {
290 if ((params == null) || params.isEmpty()) {
291 return StringPool.BLANK;
292 }
293
294 StringBundler sb = new StringBundler(params.size());
295
296 for (Map.Entry<String, Object> entry : params.entrySet()) {
297 String key = entry.getKey();
298 Object value = entry.getValue();
299
300 if (Validator.isNotNull(value)) {
301 sb.append(getJoin(key));
302 }
303 }
304
305 return sb.toString();
306 }
307
308 protected String getJoin(String key) {
309 String join = StringPool.BLANK;
310
311 if (key.equals("userGroupGroupRole")) {
312 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
313 }
314 else if (key.equals("userGroupsGroups")) {
315 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
316 }
317 else if (key.equals("userGroupsRoles")) {
318 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
319 }
320 else if (key.equals("userGroupsTeams")) {
321 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
322 }
323 else if (key.equals("userGroupsUsers")) {
324 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
325 }
326
327 if (Validator.isNotNull(join)) {
328 int pos = join.indexOf("WHERE");
329
330 if (pos != -1) {
331 join = join.substring(0, pos);
332 }
333 }
334
335 return join;
336 }
337
338 protected String getWhere(LinkedHashMap<String, Object> params) {
339 if ((params == null) || params.isEmpty()) {
340 return StringPool.BLANK;
341 }
342
343 StringBundler sb = new StringBundler(params.size());
344
345 for (Map.Entry<String, Object> entry : params.entrySet()) {
346 String key = entry.getKey();
347 Object value = entry.getValue();
348
349 if (Validator.isNotNull(value)) {
350 sb.append(getWhere(key));
351 }
352 }
353
354 return sb.toString();
355 }
356
357 protected String getWhere(String key) {
358 String join = StringPool.BLANK;
359
360 if (key.equals("userGroupGroupRole")) {
361 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
362 }
363 else if (key.equals("userGroupsGroups")) {
364 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
365 }
366 else if (key.equals("userGroupsRoles")) {
367 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
368 }
369 else if (key.equals("userGroupsTeams")) {
370 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_TEAMS);
371 }
372 else if (key.equals("userGroupsUsers")) {
373 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_USERS);
374 }
375
376 if (Validator.isNotNull(join)) {
377 int pos = join.indexOf("WHERE");
378
379 if (pos != -1) {
380 join = join.substring(pos + 5, join.length()).concat(" AND ");
381 }
382 else {
383 join = StringPool.BLANK;
384 }
385 }
386
387 return join;
388 }
389
390 protected void setJoin(
391 QueryPos qPos, LinkedHashMap<String, Object> params) {
392
393 if (params == null) {
394 return;
395 }
396
397 for (Map.Entry<String, Object> entry : params.entrySet()) {
398 Object value = entry.getValue();
399
400 if (value instanceof Long) {
401 Long valueLong = (Long)value;
402
403 if (Validator.isNotNull(valueLong)) {
404 qPos.add(valueLong);
405 }
406 }
407 else if (value instanceof Long[]) {
408 Long[] valueArray = (Long[])value;
409
410 for (int i = 0; i < valueArray.length; i++) {
411 if (Validator.isNotNull(valueArray[i])) {
412 qPos.add(valueArray[i]);
413 }
414 }
415 }
416 else if (value instanceof String) {
417 String valueString = (String)value;
418
419 if (Validator.isNotNull(valueString)) {
420 qPos.add(valueString);
421 }
422 }
423 }
424 }
425
426 }