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