001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.model.ResourcePermission;
025    import com.liferay.portal.model.impl.ResourcePermissionImpl;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.util.dao.orm.CustomSQLUtil;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class ResourcePermissionFinderImpl
036            extends BasePersistenceImpl<ResourcePermission>
037            implements ResourcePermissionFinder {
038    
039            public static String COUNT_BY_R_S =
040                    ResourcePermissionFinder.class.getName() + ".countByR_S";
041    
042            public static String FIND_BY_R_S =
043                    ResourcePermissionFinder.class.getName() + ".findByR_S";
044    
045            public int countByR_S(long roleId, int[] scopes) throws SystemException {
046                    Session session = null;
047    
048                    try {
049                            session = openSession();
050    
051                            String sql = CustomSQLUtil.get(COUNT_BY_R_S);
052    
053                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
054    
055                            SQLQuery q = session.createSQLQuery(sql);
056    
057                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
058    
059                            QueryPos qPos = QueryPos.getInstance(q);
060    
061                            qPos.add(roleId);
062                            qPos.add(scopes);
063    
064                            Iterator<Long> itr = q.list().iterator();
065    
066                            if (itr.hasNext()) {
067                                    Long count = itr.next();
068    
069                                    if (count != null) {
070                                            return count.intValue();
071                                    }
072                            }
073    
074                            return 0;
075                    }
076                    catch (Exception e) {
077                            throw new SystemException(e);
078                    }
079                    finally {
080                            closeSession(session);
081                    }
082            }
083    
084            public List<ResourcePermission> findByR_S(
085                            long roleId, int[] scopes, int start, int end)
086                    throws SystemException {
087    
088                    Session session = null;
089    
090                    try {
091                            session = openSession();
092    
093                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
094    
095                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
096    
097                            SQLQuery q = session.createSQLQuery(sql);
098    
099                            q.addEntity("ResourcePermission", ResourcePermissionImpl.class);
100    
101                            QueryPos qPos = QueryPos.getInstance(q);
102    
103                            qPos.add(roleId);
104                            qPos.add(scopes);
105    
106                            return (List<ResourcePermission>)QueryUtil.list(
107                                    q, getDialect(), start, end);
108                    }
109                    catch (Exception e) {
110                            throw new SystemException(e);
111                    }
112                    finally {
113                            closeSession(session);
114                    }
115            }
116    
117            protected String getScopes(int[] scopes) {
118                    StringBuilder sb = new StringBuilder();
119    
120                    for (int i = 0; i < scopes.length; i++) {
121                            sb.append("scope = ? ");
122    
123                            if ((i + 1) != scopes.length) {
124                                    sb.append("OR ");
125                            }
126                    }
127    
128                    return sb.toString();
129            }
130    
131    }