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