001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchUserGroupException;
018 import com.liferay.portal.kernel.dao.orm.QueryPos;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.util.OrderByComparator;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.UserGroup;
030 import com.liferay.portal.model.impl.UserGroupImpl;
031 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032 import com.liferay.util.dao.orm.CustomSQLUtil;
033
034 import java.util.Iterator;
035 import java.util.LinkedHashMap;
036 import java.util.List;
037 import java.util.Map;
038
039
042 public class UserGroupFinderImpl
043 extends BasePersistenceImpl<UserGroup> implements UserGroupFinder {
044
045 public static String COUNT_BY_C_N_D =
046 UserGroupFinder.class.getName() + ".countByC_N_D";
047
048 public static String FIND_BY_C_N =
049 UserGroupFinder.class.getName() + ".findByC_N";
050
051 public static String FIND_BY_C_N_D =
052 UserGroupFinder.class.getName() + ".findByC_N_D";
053
054 public static String JOIN_BY_GROUPS_PERMISSIONS =
055 UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
056
057 public static String JOIN_BY_USER_GROUP_GROUP_ROLE =
058 UserGroupFinder.class.getName() + ".joinByUserGroupGroupRole";
059
060 public static String JOIN_BY_USER_GROUPS_GROUPS =
061 UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
062
063 public static String JOIN_BY_USER_GROUPS_ROLES =
064 UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
065
066 public int countByC_N_D(
067 long companyId, String name, String description,
068 LinkedHashMap<String, Object> params)
069 throws SystemException {
070
071 name = StringUtil.lowerCase(name);
072 description = StringUtil.lowerCase(description);
073
074 Session session = null;
075
076 try {
077 session = openSession();
078
079 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
080
081 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
082 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
083
084 SQLQuery q = session.createSQLQuery(sql);
085
086 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
087
088 QueryPos qPos = QueryPos.getInstance(q);
089
090 setJoin(qPos, params);
091 qPos.add(companyId);
092 qPos.add(name);
093 qPos.add(name);
094 qPos.add(description);
095 qPos.add(description);
096
097 Iterator<Long> itr = q.list().iterator();
098
099 if (itr.hasNext()) {
100 Long count = itr.next();
101
102 if (count != null) {
103 return count.intValue();
104 }
105 }
106
107 return 0;
108 }
109 catch (Exception e) {
110 throw new SystemException(e);
111 }
112 finally {
113 closeSession(session);
114 }
115 }
116
117 public UserGroup findByC_N(long companyId, String name)
118 throws NoSuchUserGroupException, SystemException {
119
120 name = StringUtil.lowerCase(name);
121
122 Session session = null;
123
124 try {
125 session = openSession();
126
127 String sql = CustomSQLUtil.get(FIND_BY_C_N);
128
129 SQLQuery q = session.createSQLQuery(sql);
130
131 q.addEntity("UserGroup", UserGroupImpl.class);
132
133 QueryPos qPos = QueryPos.getInstance(q);
134
135 qPos.add(companyId);
136 qPos.add(name);
137
138 List<UserGroup> list = q.list();
139
140 if (!list.isEmpty()) {
141 return list.get(0);
142 }
143 }
144 catch (Exception e) {
145 throw new SystemException(e);
146 }
147 finally {
148 closeSession(session);
149 }
150
151 StringBundler sb = new StringBundler(5);
152
153 sb.append("No UserGroup exists with the key {companyId=");
154 sb.append(companyId);
155 sb.append(", name=");
156 sb.append(name);
157 sb.append("}");
158
159 throw new NoSuchUserGroupException(sb.toString());
160 }
161
162 public List<UserGroup> findByC_N_D(
163 long companyId, String name, String description,
164 LinkedHashMap<String, Object> params, int start, int end,
165 OrderByComparator obc)
166 throws SystemException {
167
168 name = StringUtil.lowerCase(name);
169 description = StringUtil.lowerCase(description);
170
171 Session session = null;
172
173 try {
174 session = openSession();
175
176 String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
177
178 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
179 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
180 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
181
182 SQLQuery q = session.createSQLQuery(sql);
183
184 q.addEntity("UserGroup", UserGroupImpl.class);
185
186 QueryPos qPos = QueryPos.getInstance(q);
187
188 setJoin(qPos, params);
189 qPos.add(companyId);
190 qPos.add(name);
191 qPos.add(name);
192 qPos.add(description);
193 qPos.add(description);
194
195 return (List<UserGroup>)QueryUtil.list(
196 q, getDialect(), start, end);
197 }
198 catch (Exception e) {
199 throw new SystemException(e);
200 }
201 finally {
202 closeSession(session);
203 }
204 }
205
206 protected String getJoin(LinkedHashMap<String, Object> params) {
207 if ((params == null) || params.isEmpty()) {
208 return StringPool.BLANK;
209 }
210
211 StringBundler sb = new StringBundler(params.size());
212
213 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
214
215 while (itr.hasNext()) {
216 Map.Entry<String, Object> entry = itr.next();
217
218 String key = entry.getKey();
219 Object value = entry.getValue();
220
221 if (Validator.isNotNull(value)) {
222 sb.append(getJoin(key));
223 }
224 }
225
226 return sb.toString();
227 }
228
229 protected String getJoin(String key) {
230 String join = StringPool.BLANK;
231
232 if (key.equals("permissionsResourceId")) {
233 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
234 }
235 else if (key.equals("userGroupGroupRole")) {
236 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
237 }
238 else if (key.equals("userGroupsGroups")) {
239 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
240 }
241 else if (key.equals("userGroupsRoles")) {
242 join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
243 }
244
245 if (Validator.isNotNull(join)) {
246 int pos = join.indexOf("WHERE");
247
248 if (pos != -1) {
249 join = join.substring(0, pos);
250 }
251 }
252
253 return join;
254 }
255
256 protected String getWhere(LinkedHashMap<String, Object> params) {
257 if ((params == null) || params.isEmpty()) {
258 return StringPool.BLANK;
259 }
260
261 StringBundler sb = new StringBundler(params.size());
262
263 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
264
265 while (itr.hasNext()) {
266 Map.Entry<String, Object> entry = itr.next();
267
268 String key = entry.getKey();
269 Object value = entry.getValue();
270
271 if (Validator.isNotNull(value)) {
272 sb.append(getWhere(key));
273 }
274 }
275
276 return sb.toString();
277 }
278
279 protected String getWhere(String key) {
280 String join = StringPool.BLANK;
281
282 if (key.equals("permissionsResourceId")) {
283 join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
284 }
285 else if (key.equals("userGroupGroupRole")) {
286 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_GROUP_ROLE);
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 join = join.substring(pos + 5, join.length()).concat(" AND ");
300 }
301 else {
302 join = StringPool.BLANK;
303 }
304 }
305
306 return join;
307 }
308
309 protected void setJoin(
310 QueryPos qPos, LinkedHashMap<String, Object> params) {
311
312 if (params != null) {
313 Iterator<Map.Entry<String, Object>> itr =
314 params.entrySet().iterator();
315
316 while (itr.hasNext()) {
317 Map.Entry<String, Object> entry = itr.next();
318
319 Object value = entry.getValue();
320
321 if (value instanceof Long) {
322 Long valueLong = (Long)value;
323
324 if (Validator.isNotNull(valueLong)) {
325 qPos.add(valueLong);
326 }
327 }
328 else if (value instanceof Long[]) {
329 Long[] valueArray = (Long[])value;
330
331 for (int i = 0; i < valueArray.length; i++) {
332 if (Validator.isNotNull(valueArray[i])) {
333 qPos.add(valueArray[i]);
334 }
335 }
336 }
337 else if (value instanceof String) {
338 String valueString = (String)value;
339
340 if (Validator.isNotNull(valueString)) {
341 qPos.add(valueString);
342 }
343 }
344 }
345 }
346 }
347
348 }