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.FinderCacheUtil;
28 import com.liferay.portal.kernel.dao.orm.QueryPos;
29 import com.liferay.portal.kernel.dao.orm.QueryUtil;
30 import com.liferay.portal.kernel.dao.orm.SQLQuery;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.dao.orm.Type;
33 import com.liferay.portal.kernel.util.OrderByComparator;
34 import com.liferay.portal.kernel.util.StringPool;
35 import com.liferay.portal.kernel.util.StringUtil;
36 import com.liferay.portal.kernel.util.Validator;
37 import com.liferay.portal.model.UserGroup;
38 import com.liferay.portal.model.impl.UserGroupImpl;
39 import com.liferay.portal.model.impl.UserGroupModelImpl;
40 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41 import com.liferay.util.dao.orm.CustomSQLUtil;
42
43 import java.util.Iterator;
44 import java.util.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47
48
54 public class UserGroupFinderImpl
55 extends BasePersistenceImpl implements UserGroupFinder {
56
57 public static String COUNT_BY_C_N_D =
58 UserGroupFinder.class.getName() + ".countByC_N_D";
59
60 public static String FIND_BY_C_N =
61 UserGroupFinder.class.getName() + ".findByC_N";
62
63 public static String FIND_BY_C_N_D =
64 UserGroupFinder.class.getName() + ".findByC_N_D";
65
66 public static String JOIN_BY_GROUPS_PERMISSIONS =
67 UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
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 boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
132 String finderClassName = UserGroup.class.getName();
133 String finderMethodName = "customFindByC_N";
134 String finderParams[] = new String[] {
135 Long.class.getName(), String.class.getName()
136 };
137 Object finderArgs[] = new Object[] {new Long(companyId), name};
138
139 Object result = FinderCacheUtil.getResult(
140 finderClassName, finderMethodName, finderParams, finderArgs, this);
141
142 if (result == null) {
143 Session session = null;
144
145 try {
146 session = openSession();
147
148 String sql = CustomSQLUtil.get(FIND_BY_C_N);
149
150 SQLQuery q = session.createSQLQuery(sql);
151
152 q.addEntity("UserGroup", UserGroupImpl.class);
153
154 QueryPos qPos = QueryPos.getInstance(q);
155
156 qPos.add(companyId);
157 qPos.add(name);
158
159 Iterator<UserGroup> itr = q.list().iterator();
160
161 if (itr.hasNext()) {
162 UserGroup userGroup = itr.next();
163
164 FinderCacheUtil.putResult(
165 finderClassNameCacheEnabled, finderClassName,
166 finderMethodName, finderParams, finderArgs, userGroup);
167
168 return userGroup;
169 }
170 }
171 catch (Exception e) {
172 throw new SystemException(e);
173 }
174 finally {
175 closeSession(session);
176 }
177
178 throw new NoSuchUserGroupException(
179 "No UserGroup exists with the key {companyId=" + companyId +
180 ", name=" + name + "}");
181 }
182 else {
183 return (UserGroup)result;
184 }
185 }
186
187 public List<UserGroup> findByC_N_D(
188 long companyId, String name, String description,
189 LinkedHashMap<String, Object> params, int start, int end,
190 OrderByComparator obc)
191 throws SystemException {
192
193 name = StringUtil.lowerCase(name);
194 description = StringUtil.lowerCase(description);
195
196 Session session = null;
197
198 try {
199 session = openSession();
200
201 String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
202
203 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
204 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
205 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
206
207 SQLQuery q = session.createSQLQuery(sql);
208
209 q.addEntity("UserGroup", UserGroupImpl.class);
210
211 QueryPos qPos = QueryPos.getInstance(q);
212
213 setJoin(qPos, params);
214 qPos.add(companyId);
215 qPos.add(name);
216 qPos.add(name);
217 qPos.add(description);
218 qPos.add(description);
219
220 return (List<UserGroup>)QueryUtil.list(
221 q, getDialect(), start, end);
222 }
223 catch (Exception e) {
224 throw new SystemException(e);
225 }
226 finally {
227 closeSession(session);
228 }
229 }
230
231 protected String getJoin(LinkedHashMap<String, Object> params) {
232 if (params == null) {
233 return StringPool.BLANK;
234 }
235
236 StringBuilder sb = new StringBuilder();
237
238 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
239
240 while (itr.hasNext()) {
241 Map.Entry<String, Object> entry = itr.next();
242
243 String key = entry.getKey();
244 Object value = entry.getValue();
245
246 if (Validator.isNotNull(value)) {
247 sb.append(getJoin(key));
248 }
249 }
250
251 return sb.toString();
252 }
253
254 protected String getJoin(String key) {
255 String join = StringPool.BLANK;
256
257 if (key.equals("permissionsResourceId")) {
258 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
259 }
260 else if (key.equals("userGroupsGroups")) {
261 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
262 }
263 else if (key.equals("userGroupsRoles")) {
264 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
265 }
266
267 if (Validator.isNotNull(join)) {
268 int pos = join.indexOf("WHERE");
269
270 if (pos != -1) {
271 join = join.substring(0, pos);
272 }
273 }
274
275 return join;
276 }
277
278 protected String getWhere(LinkedHashMap<String, Object> params) {
279 if (params == null) {
280 return StringPool.BLANK;
281 }
282
283 StringBuilder sb = new StringBuilder();
284
285 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
286
287 while (itr.hasNext()) {
288 Map.Entry<String, Object> entry = itr.next();
289
290 String key = entry.getKey();
291 Object value = entry.getValue();
292
293 if (Validator.isNotNull(value)) {
294 sb.append(getWhere(key));
295 }
296 }
297
298 return sb.toString();
299 }
300
301 protected String getWhere(String key) {
302 String join = StringPool.BLANK;
303
304 if (key.equals("permissionsResourceId")) {
305 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
306 }
307 else if (key.equals("userGroupsGroups")) {
308 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
309 }
310 else if (key.equals("userGroupsRoles")) {
311 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
312 }
313
314 if (Validator.isNotNull(join)) {
315 int pos = join.indexOf("WHERE");
316
317 if (pos != -1) {
318 StringBuilder sb = new StringBuilder();
319
320 sb.append(join.substring(pos + 5, join.length()));
321 sb.append(" AND ");
322
323 join = sb.toString();
324 }
325 else {
326 join = StringPool.BLANK;
327 }
328 }
329
330 return join;
331 }
332
333 protected void setJoin(
334 QueryPos qPos, LinkedHashMap<String, Object> params) {
335
336 if (params != null) {
337 Iterator<Map.Entry<String, Object>> itr =
338 params.entrySet().iterator();
339
340 while (itr.hasNext()) {
341 Map.Entry<String, Object> entry = itr.next();
342
343 Object value = entry.getValue();
344
345 if (value instanceof Long) {
346 Long valueLong = (Long)value;
347
348 if (Validator.isNotNull(valueLong)) {
349 qPos.add(valueLong);
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 }