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