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