001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
036     * @author Brian Wing Shun Chan
037     */
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            public static final String FIND_BY_C_N_S =
055                    ResourcePermissionFinder.class.getName() + ".findByC_N_S";
056    
057            public int countByR_S(long roleId, int[] scopes) throws SystemException {
058                    Session session = null;
059    
060                    try {
061                            session = openSession();
062    
063                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
064    
065                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
066    
067                            SQLQuery q = session.createSQLQuery(sql);
068    
069                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
070    
071                            QueryPos qPos = QueryPos.getInstance(q);
072    
073                            qPos.add(roleId);
074                            qPos.add(scopes);
075    
076                            Iterator<Long> itr = q.iterate();
077    
078                            if (itr.hasNext()) {
079                                    Long count = itr.next();
080    
081                                    if (count != null) {
082                                            return count.intValue();
083                                    }
084                            }
085    
086                            return 0;
087                    }
088                    catch (Exception e) {
089                            throw new SystemException(e);
090                    }
091                    finally {
092                            closeSession(session);
093                    }
094            }
095    
096            public int countByC_N_S_P_R_A(
097                            long companyId, String name, int scope, String primKey,
098                            long[] roleIds, long actionId)
099                    throws SystemException {
100    
101                    Object[] finderArgs = new Object[] {
102                            companyId, name, scope, primKey, roleIds, actionId
103                    };
104    
105                    Long count = (Long)FinderCacheUtil.getResult(
106                            ResourcePermissionPersistenceImpl.FINDER_PATH_COUNT_BY_C_N_S_P_R_A,
107                            finderArgs, this);
108    
109                    if (count != null) {
110                            return count.intValue();
111                    }
112    
113                    Session session = null;
114    
115                    try {
116                            session = openSession();
117    
118                            String sql = CustomSQLUtil.get(COUNT_BY_C_N_S_P_R_A);
119    
120                            if (roleIds.length > 1) {
121                                    StringBundler sb = new StringBundler(roleIds.length * 2 - 1);
122    
123                                    for (int i = 0; i < roleIds.length; i++) {
124                                            if (i > 0) {
125                                                    sb.append(" OR ");
126                                            }
127    
128                                            sb.append("ResourcePermission.roleId = ?");
129                                    }
130    
131                                    sql = StringUtil.replace(
132                                            sql, "ResourcePermission.roleId = ?", sb.toString());
133                            }
134    
135                            SQLQuery q = session.createSQLQuery(sql);
136    
137                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
138    
139                            QueryPos qPos = QueryPos.getInstance(q);
140    
141                            qPos.add(companyId);
142                            qPos.add(name);
143                            qPos.add(scope);
144                            qPos.add(primKey);
145                            qPos.add(roleIds);
146                            qPos.add(actionId);
147                            qPos.add(actionId);
148    
149                            count = (Long)q.uniqueResult();
150                    }
151                    catch (Exception e) {
152                            throw new SystemException(e);
153                    }
154                    finally {
155                            if (count == null) {
156                                    count = Long.valueOf(0);
157                            }
158    
159                            FinderCacheUtil.putResult(
160                                    ResourcePermissionPersistenceImpl.
161                                            FINDER_PATH_COUNT_BY_C_N_S_P_R_A,
162                                    finderArgs, count);
163    
164                            closeSession(session);
165                    }
166    
167                    return count.intValue();
168            }
169    
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            /**
204             * @deprecated
205             */
206            public List<ResourcePermission> findByC_P(long companyId, String primKey)
207                    throws SystemException {
208    
209                    return ResourcePermissionUtil.findByC_P(companyId, primKey);
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            public List<String> findByC_N_S(long companyId, String name, int scope)
246                    throws SystemException {
247    
248                    Session session = null;
249    
250                    try {
251                            session = openSession();
252    
253                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S);
254    
255                            SQLQuery q = session.createSQLQuery(sql);
256    
257                            q.addScalar("primKey", Type.STRING);
258    
259                            QueryPos qPos = QueryPos.getInstance(q);
260    
261                            qPos.add(companyId);
262                            qPos.add(name);
263                            qPos.add(scope);
264    
265                            return q.list(true);
266                    }
267                    catch (Exception e) {
268                            throw new SystemException(e);
269                    }
270                    finally {
271                            closeSession(session);
272                    }
273            }
274    
275            /**
276             * @see {@link PermissionFinderImpl#getScopes(int[])}
277             */
278            protected String getScopes(int[] scopes) {
279                    if (scopes.length == 0) {
280                            return StringPool.BLANK;
281                    }
282    
283                    StringBundler sb = new StringBundler(scopes.length * 2 + 1);
284    
285                    sb.append("(");
286    
287                    for (int i = 0; i < scopes.length; i++) {
288                            sb.append("ResourcePermission.scope = ? ");
289    
290                            if ((i + 1) != scopes.length) {
291                                    sb.append("OR ");
292                            }
293                    }
294    
295                    sb.append(")");
296    
297                    return sb.toString();
298            }
299    
300    }