001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
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.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.model.ResourcePermission;
028 import com.liferay.portal.model.impl.ResourcePermissionImpl;
029 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
030 import com.liferay.util.dao.orm.CustomSQLUtil;
031
032 import java.util.Iterator;
033 import java.util.List;
034
035
038 public class ResourcePermissionFinderImpl
039 extends BasePersistenceImpl<ResourcePermission>
040 implements ResourcePermissionFinder {
041
042 public static final String COUNT_BY_R_S =
043 ResourcePermissionFinder.class.getName() + ".countByR_S";
044
045 public static final String COUNT_BY_C_N_S_P_R_A =
046 ResourcePermissionFinder.class.getName() + ".countByC_N_S_P_R_A";
047
048 public static final String FIND_BY_RESOURCE =
049 ResourcePermissionFinder.class.getName() + ".findByResource";
050
051 public static final String FIND_BY_R_S =
052 ResourcePermissionFinder.class.getName() + ".findByR_S";
053
054 public static final String FIND_BY_C_N_S =
055 ResourcePermissionFinder.class.getName() + ".findByC_N_S";
056
057 public int countByR_S(long roleId, int[] scopes) throws SystemException {
058 Session session = null;
059
060 try {
061 session = openSession();
062
063 String sql = CustomSQLUtil.get(COUNT_BY_R_S);
064
065 sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
066
067 SQLQuery q = session.createSQLQuery(sql);
068
069 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
070
071 QueryPos qPos = QueryPos.getInstance(q);
072
073 qPos.add(roleId);
074 qPos.add(scopes);
075
076 Iterator<Long> itr = q.iterate();
077
078 if (itr.hasNext()) {
079 Long count = itr.next();
080
081 if (count != null) {
082 return count.intValue();
083 }
084 }
085
086 return 0;
087 }
088 catch (Exception e) {
089 throw new SystemException(e);
090 }
091 finally {
092 closeSession(session);
093 }
094 }
095
096 public int countByC_N_S_P_R_A(
097 long companyId, String name, int scope, String primKey,
098 long[] roleIds, long actionId)
099 throws SystemException {
100
101 Object[] finderArgs = new Object[] {
102 companyId, name, scope, primKey, roleIds, actionId
103 };
104
105 Long count = (Long)FinderCacheUtil.getResult(
106 ResourcePermissionPersistenceImpl.FINDER_PATH_COUNT_BY_C_N_S_P_R_A,
107 finderArgs, this);
108
109 if (count != null) {
110 return count.intValue();
111 }
112
113 Session session = null;
114
115 try {
116 session = openSession();
117
118 String sql = CustomSQLUtil.get(COUNT_BY_C_N_S_P_R_A);
119
120 if (roleIds.length > 1) {
121 StringBundler sb = new StringBundler(roleIds.length * 2 - 1);
122
123 for (int i = 0; i < roleIds.length; i++) {
124 if (i > 0) {
125 sb.append(" OR ");
126 }
127
128 sb.append("ResourcePermission.roleId = ?");
129 }
130
131 sql = StringUtil.replace(
132 sql, "ResourcePermission.roleId = ?", sb.toString());
133 }
134
135 SQLQuery q = session.createSQLQuery(sql);
136
137 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
138
139 QueryPos qPos = QueryPos.getInstance(q);
140
141 qPos.add(companyId);
142 qPos.add(name);
143 qPos.add(scope);
144 qPos.add(primKey);
145 qPos.add(roleIds);
146 qPos.add(actionId);
147 qPos.add(actionId);
148
149 count = (Long)q.uniqueResult();
150 }
151 catch (Exception e) {
152 throw new SystemException(e);
153 }
154 finally {
155 if (count == null) {
156 count = Long.valueOf(0);
157 }
158
159 FinderCacheUtil.putResult(
160 ResourcePermissionPersistenceImpl.
161 FINDER_PATH_COUNT_BY_C_N_S_P_R_A,
162 finderArgs, count);
163
164 closeSession(session);
165 }
166
167 return count.intValue();
168 }
169
170 public List<ResourcePermission> findByResource(
171 long companyId, long groupId, String name, String primKey)
172 throws SystemException {
173
174 Session session = null;
175
176 try {
177 session = openSession();
178
179 String sql = CustomSQLUtil.get(FIND_BY_RESOURCE);
180
181 SQLQuery q = session.createSQLQuery(sql);
182
183 q.addEntity("ResourcePermission", ResourcePermissionImpl.class);
184
185 QueryPos qPos = QueryPos.getInstance(q);
186
187 qPos.add(companyId);
188 qPos.add(name);
189 qPos.add(primKey);
190 qPos.add(String.valueOf(groupId));
191
192 return (List<ResourcePermission>)QueryUtil.list(
193 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
194 }
195 catch (Exception e) {
196 throw new SystemException(e);
197 }
198 finally {
199 closeSession(session);
200 }
201 }
202
203 public List<ResourcePermission> findByR_S(
204 long roleId, int[] scopes, int start, int end)
205 throws SystemException {
206
207 Session session = null;
208
209 try {
210 session = openSession();
211
212 String sql = CustomSQLUtil.get(FIND_BY_R_S);
213
214 sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
215
216 SQLQuery q = session.createSQLQuery(sql);
217
218 q.addEntity("ResourcePermission", ResourcePermissionImpl.class);
219
220 QueryPos qPos = QueryPos.getInstance(q);
221
222 qPos.add(roleId);
223 qPos.add(scopes);
224
225 return (List<ResourcePermission>)QueryUtil.list(
226 q, getDialect(), start, end);
227 }
228 catch (Exception e) {
229 throw new SystemException(e);
230 }
231 finally {
232 closeSession(session);
233 }
234 }
235
236 public List<String> findByC_N_S(long companyId, String name, int scope)
237 throws SystemException {
238
239 Session session = null;
240
241 try {
242 session = openSession();
243
244 String sql = CustomSQLUtil.get(FIND_BY_C_N_S);
245
246 SQLQuery q = session.createSQLQuery(sql);
247
248 q.addScalar("primKey", Type.STRING);
249
250 QueryPos qPos = QueryPos.getInstance(q);
251
252 qPos.add(companyId);
253 qPos.add(name);
254 qPos.add(scope);
255
256 return q.list(true);
257 }
258 catch (Exception e) {
259 throw new SystemException(e);
260 }
261 finally {
262 closeSession(session);
263 }
264 }
265
266
269 protected String getScopes(int[] scopes) {
270 if (scopes.length == 0) {
271 return StringPool.BLANK;
272 }
273
274 StringBundler sb = new StringBundler(scopes.length * 2 + 1);
275
276 sb.append("(");
277
278 for (int i = 0; i < scopes.length; i++) {
279 sb.append("ResourcePermission.scope = ? ");
280
281 if ((i + 1) != scopes.length) {
282 sb.append("OR ");
283 }
284 }
285
286 sb.append(")");
287
288 return sb.toString();
289 }
290
291 }