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