1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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.FinderCacheUtil;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.SQLQuery;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.Type;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.UserGroup;
35  import com.liferay.portal.model.impl.UserGroupImpl;
36  import com.liferay.portal.model.impl.UserGroupModelImpl;
37  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * <a href="UserGroupFinderImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Charles May
49   *
50   */
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         boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
129         String finderClassName = UserGroup.class.getName();
130         String finderMethodName = "customFindByC_N";
131         String finderParams[] = new String[] {
132             Long.class.getName(), String.class.getName()
133         };
134         Object finderArgs[] = new Object[] {new Long(companyId), name};
135 
136         Object result = FinderCacheUtil.getResult(
137             finderClassName, finderMethodName, finderParams, finderArgs, this);
138 
139         if (result == null) {
140             Session session = null;
141 
142             try {
143                 session = openSession();
144 
145                 String sql = CustomSQLUtil.get(FIND_BY_C_N);
146 
147                 SQLQuery q = session.createSQLQuery(sql);
148 
149                 q.addEntity("UserGroup", UserGroupImpl.class);
150 
151                 QueryPos qPos = QueryPos.getInstance(q);
152 
153                 qPos.add(companyId);
154                 qPos.add(name);
155 
156                 Iterator<UserGroup> itr = q.list().iterator();
157 
158                 if (itr.hasNext()) {
159                     UserGroup userGroup = itr.next();
160 
161                     FinderCacheUtil.putResult(
162                         finderClassNameCacheEnabled, finderClassName,
163                         finderMethodName, finderParams, finderArgs, userGroup);
164 
165                     return userGroup;
166                 }
167             }
168             catch (Exception e) {
169                 throw new SystemException(e);
170             }
171             finally {
172                 closeSession(session);
173             }
174 
175             throw new NoSuchUserGroupException(
176                 "No UserGroup exists with the key {companyId=" + companyId +
177                     ", name=" + name + "}");
178         }
179         else {
180             return (UserGroup)result;
181         }
182     }
183 
184     public List<UserGroup> findByC_N_D(
185             long companyId, String name, String description,
186             LinkedHashMap<String, Object> params, int start, int end,
187             OrderByComparator obc)
188         throws SystemException {
189 
190         name = StringUtil.lowerCase(name);
191         description = StringUtil.lowerCase(description);
192 
193         Session session = null;
194 
195         try {
196             session = openSession();
197 
198             String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
199 
200             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
201             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
202             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
203 
204             SQLQuery q = session.createSQLQuery(sql);
205 
206             q.addEntity("UserGroup", UserGroupImpl.class);
207 
208             QueryPos qPos = QueryPos.getInstance(q);
209 
210             setJoin(qPos, params);
211             qPos.add(companyId);
212             qPos.add(name);
213             qPos.add(name);
214             qPos.add(description);
215             qPos.add(description);
216 
217             return (List<UserGroup>)QueryUtil.list(
218                 q, getDialect(), start, end);
219         }
220         catch (Exception e) {
221             throw new SystemException(e);
222         }
223         finally {
224             closeSession(session);
225         }
226     }
227 
228     protected String getJoin(LinkedHashMap<String, Object> params) {
229         if (params == null) {
230             return StringPool.BLANK;
231         }
232 
233         StringBuilder sb = new StringBuilder();
234 
235         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
236 
237         while (itr.hasNext()) {
238             Map.Entry<String, Object> entry = itr.next();
239 
240             String key = entry.getKey();
241             Object value = entry.getValue();
242 
243             if (Validator.isNotNull(value)) {
244                 sb.append(getJoin(key));
245             }
246         }
247 
248         return sb.toString();
249     }
250 
251     protected String getJoin(String key) {
252         String join = StringPool.BLANK;
253 
254         if (key.equals("permissionsResourceId")) {
255             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
256         }
257         else if (key.equals("userGroupsGroups")) {
258             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
259         }
260         else if (key.equals("userGroupsRoles")) {
261             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
262         }
263 
264         if (Validator.isNotNull(join)) {
265             int pos = join.indexOf("WHERE");
266 
267             if (pos != -1) {
268                 join = join.substring(0, pos);
269             }
270         }
271 
272         return join;
273     }
274 
275     protected String getWhere(LinkedHashMap<String, Object> params) {
276         if (params == null) {
277             return StringPool.BLANK;
278         }
279 
280         StringBuilder sb = new StringBuilder();
281 
282         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
283 
284         while (itr.hasNext()) {
285             Map.Entry<String, Object> entry = itr.next();
286 
287             String key = entry.getKey();
288             Object value = entry.getValue();
289 
290             if (Validator.isNotNull(value)) {
291                 sb.append(getWhere(key));
292             }
293         }
294 
295         return sb.toString();
296     }
297 
298     protected String getWhere(String key) {
299         String join = StringPool.BLANK;
300 
301         if (key.equals("permissionsResourceId")) {
302             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
303         }
304         else if (key.equals("userGroupsGroups")) {
305             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
306         }
307         else if (key.equals("userGroupsRoles")) {
308             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
309         }
310 
311         if (Validator.isNotNull(join)) {
312             int pos = join.indexOf("WHERE");
313 
314             if (pos != -1) {
315                 StringBuilder sb = new StringBuilder();
316 
317                 sb.append(join.substring(pos + 5, join.length()));
318                 sb.append(" AND ");
319 
320                 join = sb.toString();
321             }
322             else {
323                 join = StringPool.BLANK;
324             }
325         }
326 
327         return join;
328     }
329 
330     protected void setJoin(
331         QueryPos qPos, LinkedHashMap<String, Object> params) {
332 
333         if (params != null) {
334             Iterator<Map.Entry<String, Object>> itr =
335                 params.entrySet().iterator();
336 
337             while (itr.hasNext()) {
338                 Map.Entry<String, Object> entry = itr.next();
339 
340                 Object value = entry.getValue();
341 
342                 if (value instanceof Long) {
343                     Long valueLong = (Long)value;
344 
345                     if (Validator.isNotNull(valueLong)) {
346                         qPos.add(valueLong);
347                     }
348                 }
349                 else if (value instanceof String) {
350                     String valueString = (String)value;
351 
352                     if (Validator.isNotNull(valueString)) {
353                         qPos.add(valueString);
354                     }
355                 }
356             }
357         }
358     }
359 
360 }