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