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 @Override
055 public int countByR_S(long roleId, int[] scopes) throws SystemException {
056 Session session = null;
057
058 try {
059 session = openSession();
060
061 String sql = CustomSQLUtil.get(COUNT_BY_R_S);
062
063 sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
064
065 SQLQuery q = session.createSQLQuery(sql);
066
067 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
068
069 QueryPos qPos = QueryPos.getInstance(q);
070
071 qPos.add(roleId);
072 qPos.add(scopes);
073
074 Iterator<Long> itr = q.iterate();
075
076 if (itr.hasNext()) {
077 Long count = itr.next();
078
079 if (count != null) {
080 return count.intValue();
081 }
082 }
083
084 return 0;
085 }
086 catch (Exception e) {
087 throw new SystemException(e);
088 }
089 finally {
090 closeSession(session);
091 }
092 }
093
094 @Override
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 @Override
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
206 @Override
207 public List<ResourcePermission> findByC_P(long companyId, String primKey)
208 throws SystemException {
209
210 return ResourcePermissionUtil.findByC_P(companyId, primKey);
211 }
212
213 @Override
214 public List<ResourcePermission> findByR_S(
215 long roleId, int[] scopes, int start, int end)
216 throws SystemException {
217
218 Session session = null;
219
220 try {
221 session = openSession();
222
223 String sql = CustomSQLUtil.get(FIND_BY_R_S);
224
225 sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
226
227 SQLQuery q = session.createSQLQuery(sql);
228
229 q.addEntity("ResourcePermission", ResourcePermissionImpl.class);
230
231 QueryPos qPos = QueryPos.getInstance(q);
232
233 qPos.add(roleId);
234 qPos.add(scopes);
235
236 return (List<ResourcePermission>)QueryUtil.list(
237 q, getDialect(), start, end);
238 }
239 catch (Exception e) {
240 throw new SystemException(e);
241 }
242 finally {
243 closeSession(session);
244 }
245 }
246
247
250 protected String getScopes(int[] scopes) {
251 if (scopes.length == 0) {
252 return StringPool.BLANK;
253 }
254
255 StringBundler sb = new StringBundler(scopes.length * 2 + 1);
256
257 sb.append("(");
258
259 for (int i = 0; i < scopes.length; i++) {
260 sb.append("ResourcePermission.scope = ? ");
261
262 if ((i + 1) != scopes.length) {
263 sb.append("OR ");
264 }
265 }
266
267 sb.append(")");
268
269 return sb.toString();
270 }
271
272 }