1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.model.ResourcePermission;
25  import com.liferay.portal.model.impl.ResourcePermissionImpl;
26  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.util.dao.orm.CustomSQLUtil;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="ResourcePermissionFinderImpl.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class ResourcePermissionFinderImpl
39      extends BasePersistenceImpl<ResourcePermission>
40      implements ResourcePermissionFinder {
41  
42      public static String COUNT_BY_R_S =
43          ResourcePermissionFinder.class.getName() + ".countByR_S";
44  
45      public static String FIND_BY_R_S =
46          ResourcePermissionFinder.class.getName() + ".findByR_S";
47  
48      public int countByR_S(long roleId, int[] scopes) throws SystemException {
49          Session session = null;
50  
51          try {
52              session = openSession();
53  
54              String sql = CustomSQLUtil.get(COUNT_BY_R_S);
55  
56              sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
57  
58              SQLQuery q = session.createSQLQuery(sql);
59  
60              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
61  
62              QueryPos qPos = QueryPos.getInstance(q);
63  
64              qPos.add(roleId);
65              qPos.add(scopes);
66  
67              Iterator<Long> itr = q.list().iterator();
68  
69              if (itr.hasNext()) {
70                  Long count = itr.next();
71  
72                  if (count != null) {
73                      return count.intValue();
74                  }
75              }
76  
77              return 0;
78          }
79          catch (Exception e) {
80              throw new SystemException(e);
81          }
82          finally {
83              closeSession(session);
84          }
85      }
86  
87      public List<ResourcePermission> findByR_S(
88              long roleId, int[] scopes, int start, int end)
89          throws SystemException {
90  
91          Session session = null;
92  
93          try {
94              session = openSession();
95  
96              String sql = CustomSQLUtil.get(FIND_BY_R_S);
97  
98              sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
99  
100             SQLQuery q = session.createSQLQuery(sql);
101 
102             q.addEntity("ResourcePermission", ResourcePermissionImpl.class);
103 
104             QueryPos qPos = QueryPos.getInstance(q);
105 
106             qPos.add(roleId);
107             qPos.add(scopes);
108 
109             return (List<ResourcePermission>)QueryUtil.list(
110                 q, getDialect(), start, end);
111         }
112         catch (Exception e) {
113             throw new SystemException(e);
114         }
115         finally {
116             closeSession(session);
117         }
118     }
119 
120     protected String getScopes(int[] scopes) {
121         StringBuilder sb = new StringBuilder();
122 
123         for (int i = 0; i < scopes.length; i++) {
124             sb.append("scope = ? ");
125 
126             if ((i + 1) != scopes.length) {
127                 sb.append("OR ");
128             }
129         }
130 
131         return sb.toString();
132     }
133 
134 }