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 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 itr = q.list().iterator();
108
109 if (itr.hasNext()) {
110 Long count = (Long)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 itr = q.list().iterator();
161
162 if (itr.hasNext()) {
163 UserGroup 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 findByC_N_D(
189 long companyId, String name, String description,
190 LinkedHashMap params, int begin, int end, 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 = HibernateUtil.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 QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
221 }
222 catch (Exception e) {
223 throw new SystemException(e);
224 }
225 finally {
226 HibernateUtil.closeSession(session);
227 }
228 }
229
230 protected String getJoin(LinkedHashMap params) {
231 if (params == null) {
232 return StringPool.BLANK;
233 }
234
235 StringMaker sm = new StringMaker();
236
237 Iterator itr = params.entrySet().iterator();
238
239 while (itr.hasNext()) {
240 Map.Entry entry = (Map.Entry)itr.next();
241
242 String key = (String)entry.getKey();
243 Object value = entry.getValue();
244
245 if (Validator.isNotNull(value)) {
246 sm.append(getJoin(key));
247 }
248 }
249
250 return sm.toString();
251 }
252
253 protected String getJoin(String key) {
254 String join = StringPool.BLANK;
255
256 if (key.equals("permissionsResourceId")) {
257 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
258 }
259 else if (key.equals("userGroupsGroups")) {
260 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
261 }
262 else if (key.equals("userGroupsRoles")) {
263 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
264 }
265
266 if (Validator.isNotNull(join)) {
267 int pos = join.indexOf("WHERE");
268
269 if (pos != -1) {
270 join = join.substring(0, pos);
271 }
272 }
273
274 return join;
275 }
276
277 protected String getWhere(LinkedHashMap params) {
278 if (params == null) {
279 return StringPool.BLANK;
280 }
281
282 StringMaker sm = new StringMaker();
283
284 Iterator itr = params.entrySet().iterator();
285
286 while (itr.hasNext()) {
287 Map.Entry entry = (Map.Entry)itr.next();
288
289 String key = (String)entry.getKey();
290 Object value = entry.getValue();
291
292 if (Validator.isNotNull(value)) {
293 sm.append(getWhere(key));
294 }
295 }
296
297 return sm.toString();
298 }
299
300 protected String getWhere(String key) {
301 String join = StringPool.BLANK;
302
303 if (key.equals("permissionsResourceId")) {
304 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
305 }
306 else if (key.equals("userGroupsGroups")) {
307 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
308 }
309 else if (key.equals("userGroupsRoles")) {
310 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
311 }
312
313 if (Validator.isNotNull(join)) {
314 int pos = join.indexOf("WHERE");
315
316 if (pos != -1) {
317 join = join.substring(pos + 5, join.length()) + " AND ";
318 }
319 }
320
321 return join;
322 }
323
324 protected void setJoin(QueryPos qPos, LinkedHashMap params) {
325 if (params != null) {
326 Iterator itr = params.entrySet().iterator();
327
328 while (itr.hasNext()) {
329 Map.Entry entry = (Map.Entry)itr.next();
330
331 Object value = entry.getValue();
332
333 if (value instanceof Long) {
334 Long valueLong = (Long)value;
335
336 if (Validator.isNotNull(valueLong)) {
337 qPos.add(valueLong);
338 }
339 }
340 else if (value instanceof String) {
341 String valueString = (String)value;
342
343 if (Validator.isNotNull(valueString)) {
344 qPos.add(valueString);
345 }
346 }
347 }
348 }
349 }
350
351 }