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