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