1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
47   * <a href="UserGroupFinderImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Charles May
50   *
51   */
52  public class UserGroupFinderImpl
53      extends BasePersistenceImpl implements UserGroupFinder {
54  
55      public static String COUNT_BY_C_N_D =
56          UserGroupFinder.class.getName() + ".countByC_N_D";
57  
58      public static String FIND_BY_C_N =
59          UserGroupFinder.class.getName() + ".findByC_N";
60  
61      public static String FIND_BY_C_N_D =
62          UserGroupFinder.class.getName() + ".findByC_N_D";
63  
64      public static String JOIN_BY_GROUPS_PERMISSIONS =
65          UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
66  
67      public static String JOIN_BY_USER_GROUPS_GROUPS =
68          UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
69  
70      public static String JOIN_BY_USER_GROUPS_ROLES =
71          UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
72  
73      public int countByC_N_D(
74              long companyId, String name, String description,
75              LinkedHashMap<String, Object> params)
76          throws SystemException {
77  
78          name = StringUtil.lowerCase(name);
79          description = StringUtil.lowerCase(description);
80  
81          Session session = null;
82  
83          try {
84              session = openSession();
85  
86              String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
87  
88              sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
89              sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
90  
91              SQLQuery q = session.createSQLQuery(sql);
92  
93              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
94  
95              QueryPos qPos = QueryPos.getInstance(q);
96  
97              setJoin(qPos, params);
98              qPos.add(companyId);
99              qPos.add(name);
100             qPos.add(name);
101             qPos.add(description);
102             qPos.add(description);
103 
104             Iterator<Long> itr = q.list().iterator();
105 
106             if (itr.hasNext()) {
107                 Long count = itr.next();
108 
109                 if (count != null) {
110                     return count.intValue();
111                 }
112             }
113 
114             return 0;
115         }
116         catch (Exception e) {
117             throw new SystemException(e);
118         }
119         finally {
120             closeSession(session);
121         }
122     }
123 
124     public UserGroup findByC_N(long companyId, String name)
125         throws NoSuchUserGroupException, SystemException {
126 
127         name = StringUtil.lowerCase(name);
128 
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             String sql = CustomSQLUtil.get(FIND_BY_C_N);
135 
136             SQLQuery q = session.createSQLQuery(sql);
137 
138             q.addEntity("UserGroup", UserGroupImpl.class);
139 
140             QueryPos qPos = QueryPos.getInstance(q);
141 
142             qPos.add(companyId);
143             qPos.add(name);
144 
145             List<UserGroup> list = q.list();
146 
147             if (!list.isEmpty()) {
148                 return list.get(0);
149             }
150         }
151         catch (Exception e) {
152             throw new SystemException(e);
153         }
154         finally {
155             closeSession(session);
156         }
157 
158         StringBuilder sb = new StringBuilder();
159 
160         sb.append("No UserGroup exists with the key {companyId=");
161         sb.append(companyId);
162         sb.append(", name=");
163         sb.append(name);
164         sb.append("}");
165 
166         throw new NoSuchUserGroupException(sb.toString());
167     }
168 
169     public List<UserGroup> findByC_N_D(
170             long companyId, String name, String description,
171             LinkedHashMap<String, Object> params, int start, int end,
172             OrderByComparator obc)
173         throws SystemException {
174 
175         name = StringUtil.lowerCase(name);
176         description = StringUtil.lowerCase(description);
177 
178         Session session = null;
179 
180         try {
181             session = openSession();
182 
183             String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
184 
185             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
186             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
187             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
188 
189             SQLQuery q = session.createSQLQuery(sql);
190 
191             q.addEntity("UserGroup", UserGroupImpl.class);
192 
193             QueryPos qPos = QueryPos.getInstance(q);
194 
195             setJoin(qPos, params);
196             qPos.add(companyId);
197             qPos.add(name);
198             qPos.add(name);
199             qPos.add(description);
200             qPos.add(description);
201 
202             return (List<UserGroup>)QueryUtil.list(
203                 q, getDialect(), start, end);
204         }
205         catch (Exception e) {
206             throw new SystemException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     protected String getJoin(LinkedHashMap<String, Object> params) {
214         if (params == null) {
215             return StringPool.BLANK;
216         }
217 
218         StringBuilder sb = new StringBuilder();
219 
220         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
221 
222         while (itr.hasNext()) {
223             Map.Entry<String, Object> entry = itr.next();
224 
225             String key = entry.getKey();
226             Object value = entry.getValue();
227 
228             if (Validator.isNotNull(value)) {
229                 sb.append(getJoin(key));
230             }
231         }
232 
233         return sb.toString();
234     }
235 
236     protected String getJoin(String key) {
237         String join = StringPool.BLANK;
238 
239         if (key.equals("permissionsResourceId")) {
240             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
241         }
242         else if (key.equals("userGroupsGroups")) {
243             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
244         }
245         else if (key.equals("userGroupsRoles")) {
246             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
247         }
248 
249         if (Validator.isNotNull(join)) {
250             int pos = join.indexOf("WHERE");
251 
252             if (pos != -1) {
253                 join = join.substring(0, pos);
254             }
255         }
256 
257         return join;
258     }
259 
260     protected String getWhere(LinkedHashMap<String, Object> params) {
261         if (params == null) {
262             return StringPool.BLANK;
263         }
264 
265         StringBuilder sb = new StringBuilder();
266 
267         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
268 
269         while (itr.hasNext()) {
270             Map.Entry<String, Object> entry = itr.next();
271 
272             String key = entry.getKey();
273             Object value = entry.getValue();
274 
275             if (Validator.isNotNull(value)) {
276                 sb.append(getWhere(key));
277             }
278         }
279 
280         return sb.toString();
281     }
282 
283     protected String getWhere(String key) {
284         String join = StringPool.BLANK;
285 
286         if (key.equals("permissionsResourceId")) {
287             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
288         }
289         else if (key.equals("userGroupsGroups")) {
290             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
291         }
292         else if (key.equals("userGroupsRoles")) {
293             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
294         }
295 
296         if (Validator.isNotNull(join)) {
297             int pos = join.indexOf("WHERE");
298 
299             if (pos != -1) {
300                 StringBuilder sb = new StringBuilder();
301 
302                 sb.append(join.substring(pos + 5, join.length()));
303                 sb.append(" AND ");
304 
305                 join = sb.toString();
306             }
307             else {
308                 join = StringPool.BLANK;
309             }
310         }
311 
312         return join;
313     }
314 
315     protected void setJoin(
316         QueryPos qPos, LinkedHashMap<String, Object> params) {
317 
318         if (params != null) {
319             Iterator<Map.Entry<String, Object>> itr =
320                 params.entrySet().iterator();
321 
322             while (itr.hasNext()) {
323                 Map.Entry<String, Object> entry = itr.next();
324 
325                 Object value = entry.getValue();
326 
327                 if (value instanceof Long) {
328                     Long valueLong = (Long)value;
329 
330                     if (Validator.isNotNull(valueLong)) {
331                         qPos.add(valueLong);
332                     }
333                 }
334                 else if (value instanceof String) {
335                     String valueString = (String)value;
336 
337                     if (Validator.isNotNull(valueString)) {
338                         qPos.add(valueString);
339                     }
340                 }
341             }
342         }
343     }
344 
345 }