1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchUserGroupException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.dao.orm.QueryPos;
28 import com.liferay.portal.kernel.dao.orm.QueryUtil;
29 import com.liferay.portal.kernel.dao.orm.SQLQuery;
30 import com.liferay.portal.kernel.dao.orm.Session;
31 import com.liferay.portal.kernel.dao.orm.Type;
32 import com.liferay.portal.kernel.util.OrderByComparator;
33 import com.liferay.portal.kernel.util.StringPool;
34 import com.liferay.portal.kernel.util.StringUtil;
35 import com.liferay.portal.kernel.util.Validator;
36 import com.liferay.portal.model.UserGroup;
37 import com.liferay.portal.model.impl.UserGroupImpl;
38 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
39 import com.liferay.util.dao.orm.CustomSQLUtil;
40
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45
46
52 public class UserGroupFinderImpl
53 extends BasePersistenceImpl implements UserGroupFinder {
54
55 public static String COUNT_BY_C_N_D =
56 UserGroupFinder.class.getName() + ".countByC_N_D";
57
58 public static String FIND_BY_C_N =
59 UserGroupFinder.class.getName() + ".findByC_N";
60
61 public static String FIND_BY_C_N_D =
62 UserGroupFinder.class.getName() + ".findByC_N_D";
63
64 public static String JOIN_BY_GROUPS_PERMISSIONS =
65 UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
66
67 public static String JOIN_BY_USER_GROUP_GROUP_ROLE =
68 UserGroupFinder.class.getName() + ".joinByUserGroupGroupRole";
69
70 public static String JOIN_BY_USER_GROUPS_GROUPS =
71 UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
72
73 public static String JOIN_BY_USER_GROUPS_ROLES =
74 UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
75
76 public int countByC_N_D(
77 long companyId, String name, String description,
78 LinkedHashMap<String, Object> params)
79 throws SystemException {
80
81 name = StringUtil.lowerCase(name);
82 description = StringUtil.lowerCase(description);
83
84 Session session = null;
85
86 try {
87 session = openSession();
88
89 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
90
91 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
92 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
93
94 SQLQuery q = session.createSQLQuery(sql);
95
96 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
97
98 QueryPos qPos = QueryPos.getInstance(q);
99
100 setJoin(qPos, params);
101 qPos.add(companyId);
102 qPos.add(name);
103 qPos.add(name);
104 qPos.add(description);
105 qPos.add(description);
106
107 Iterator<Long> itr = q.list().iterator();
108
109 if (itr.hasNext()) {
110 Long count = itr.next();
111
112 if (count != null) {
113 return count.intValue();
114 }
115 }
116
117 return 0;
118 }
119 catch (Exception e) {
120 throw new SystemException(e);
121 }
122 finally {
123 closeSession(session);
124 }
125 }
126
127 public UserGroup findByC_N(long companyId, String name)
128 throws NoSuchUserGroupException, SystemException {
129
130 name = StringUtil.lowerCase(name);
131
132 Session session = null;
133
134 try {
135 session = openSession();
136
137 String sql = CustomSQLUtil.get(FIND_BY_C_N);
138
139 SQLQuery q = session.createSQLQuery(sql);
140
141 q.addEntity("UserGroup", UserGroupImpl.class);
142
143 QueryPos qPos = QueryPos.getInstance(q);
144
145 qPos.add(companyId);
146 qPos.add(name);
147
148 List<UserGroup> list = q.list();
149
150 if (!list.isEmpty()) {
151 return list.get(0);
152 }
153 }
154 catch (Exception e) {
155 throw new SystemException(e);
156 }
157 finally {
158 closeSession(session);
159 }
160
161 StringBuilder sb = new StringBuilder();
162
163 sb.append("No UserGroup exists with the key {companyId=");
164 sb.append(companyId);
165 sb.append(", name=");
166 sb.append(name);
167 sb.append("}");
168
169 throw new NoSuchUserGroupException(sb.toString());
170 }
171
172 public List<UserGroup> findByC_N_D(
173 long companyId, String name, String description,
174 LinkedHashMap<String, Object> params, int start, int end,
175 OrderByComparator obc)
176 throws SystemException {
177
178 name = StringUtil.lowerCase(name);
179 description = StringUtil.lowerCase(description);
180
181 Session session = null;
182
183 try {
184 session = openSession();
185
186 String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
187
188 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
189 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
190 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
191
192 SQLQuery q = session.createSQLQuery(sql);
193
194 q.addEntity("UserGroup", UserGroupImpl.class);
195
196 QueryPos qPos = QueryPos.getInstance(q);
197
198 setJoin(qPos, params);
199 qPos.add(companyId);
200 qPos.add(name);
201 qPos.add(name);
202 qPos.add(description);
203 qPos.add(description);
204
205 return (List<UserGroup>)QueryUtil.list(
206 q, getDialect(), start, end);
207 }
208 catch (Exception e) {
209 throw new SystemException(e);
210 }
211 finally {
212 closeSession(session);
213 }
214 }
215
216 protected String getJoin(LinkedHashMap<String, Object> params) {
217 if (params == null) {
218 return StringPool.BLANK;
219 }
220
221 StringBuilder sb = new StringBuilder();
222
223 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
224
225 while (itr.hasNext()) {
226 Map.Entry<String, Object> entry = itr.next();
227
228 String key = entry.getKey();
229 Object value = entry.getValue();
230
231 if (Validator.isNotNull(value)) {
232 sb.append(getJoin(key));
233 }
234 }
235
236 return sb.toString();
237 }
238
239 protected String getJoin(String key) {
240 String join = StringPool.BLANK;
241
242 if (key.equals("permissionsResourceId")) {
243 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
244 }
245 else if (key.equals("userGroupGroupRole")) {
246 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
247 }
248 else if (key.equals("userGroupsGroups")) {
249 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
250 }
251 else if (key.equals("userGroupsRoles")) {
252 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
253 }
254
255 if (Validator.isNotNull(join)) {
256 int pos = join.indexOf("WHERE");
257
258 if (pos != -1) {
259 join = join.substring(0, pos);
260 }
261 }
262
263 return join;
264 }
265
266 protected String getWhere(LinkedHashMap<String, Object> params) {
267 if (params == null) {
268 return StringPool.BLANK;
269 }
270
271 StringBuilder sb = new StringBuilder();
272
273 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
274
275 while (itr.hasNext()) {
276 Map.Entry<String, Object> entry = itr.next();
277
278 String key = entry.getKey();
279 Object value = entry.getValue();
280
281 if (Validator.isNotNull(value)) {
282 sb.append(getWhere(key));
283 }
284 }
285
286 return sb.toString();
287 }
288
289 protected String getWhere(String key) {
290 String join = StringPool.BLANK;
291
292 if (key.equals("permissionsResourceId")) {
293 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
294 }
295 else if (key.equals("userGroupGroupRole")) {
296 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
297 }
298 else if (key.equals("userGroupsGroups")) {
299 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
300 }
301 else if (key.equals("userGroupsRoles")) {
302 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
303 }
304
305 if (Validator.isNotNull(join)) {
306 int pos = join.indexOf("WHERE");
307
308 if (pos != -1) {
309 StringBuilder sb = new StringBuilder();
310
311 sb.append(join.substring(pos + 5, join.length()));
312 sb.append(" AND ");
313
314 join = sb.toString();
315 }
316 else {
317 join = StringPool.BLANK;
318 }
319 }
320
321 return join;
322 }
323
324 protected void setJoin(
325 QueryPos qPos, LinkedHashMap<String, Object> params) {
326
327 if (params != null) {
328 Iterator<Map.Entry<String, Object>> itr =
329 params.entrySet().iterator();
330
331 while (itr.hasNext()) {
332 Map.Entry<String, Object> entry = itr.next();
333
334 Object value = entry.getValue();
335
336 if (value instanceof Long) {
337 Long valueLong = (Long)value;
338
339 if (Validator.isNotNull(valueLong)) {
340 qPos.add(valueLong);
341 }
342 }
343 else if (value instanceof Long[]) {
344 Long[] valueArray = (Long[])value;
345
346 for (int i = 0; i < valueArray.length; i++) {
347 if (Validator.isNotNull(valueArray[i])) {
348 qPos.add(valueArray[i]);
349 }
350 }
351 }
352 else if (value instanceof String) {
353 String valueString = (String)value;
354
355 if (Validator.isNotNull(valueString)) {
356 qPos.add(valueString);
357 }
358 }
359 }
360 }
361 }
362
363 }