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