001    /**
002     * Copyright (c) 2000-present 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.impl;
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.exception.SystemException;
022    import com.liferay.portal.model.ResourceTypePermission;
023    import com.liferay.portal.model.impl.ResourceTypePermissionImpl;
024    import com.liferay.portal.service.persistence.ResourceTypePermissionFinder;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Connor McKay
031     */
032    public class ResourceTypePermissionFinderImpl
033            extends ResourceTypePermissionFinderBaseImpl
034            implements ResourceTypePermissionFinder {
035    
036            public static final String FIND_BY_EITHER_SCOPE_C_G_N =
037                    ResourceTypePermissionFinder.class.getName() +
038                            ".findByEitherScopeC_G_N";
039    
040            public static final String FIND_BY_GROUP_SCOPE_C_N_R =
041                    ResourceTypePermissionFinder.class.getName() + ".findByGroupScopeC_N_R";
042    
043            /**
044             * Returns all the resource type permissions with either scope that apply to
045             * resources of the type within the group. This method is used to find all
046             * the resource type permissions that apply to a newly created resource.
047             *
048             * @param  companyId the primary key of the company
049             * @param  groupId the primary key of the group
050             * @param  name the fully qualified class name of the resource type
051             * @return all the resource type permissions that apply to resources of the
052             *         type within the group
053             */
054            @Override
055            public List<ResourceTypePermission> findByEitherScopeC_G_N(
056                    long companyId, long groupId, String name) {
057    
058                    Session session = null;
059    
060                    try {
061                            session = openSession();
062    
063                            String sql = CustomSQLUtil.get(FIND_BY_EITHER_SCOPE_C_G_N);
064    
065                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
066    
067                            q.addEntity(
068                                    "ResourceTypePermission", ResourceTypePermissionImpl.class);
069    
070                            QueryPos qPos = QueryPos.getInstance(q);
071    
072                            qPos.add(companyId);
073                            qPos.add(name);
074                            qPos.add(groupId);
075    
076                            return (List<ResourceTypePermission>)QueryUtil.list(
077                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
078                    }
079                    catch (Exception e) {
080                            throw new SystemException(e);
081                    }
082                    finally {
083                            closeSession(session);
084                    }
085            }
086    
087            /**
088             * Returns all of the role's group scope resource type permissions that
089             * apply to resources of the type. This method is used during the process of
090             * adding a company scope resource type permissions to remove all the group
091             * scope permissions that would be overridden by it.
092             *
093             * @param  companyId the primary key of the company
094             * @param  name the fully qualified class name of the resource type
095             * @param  roleId the primary key of the role
096             * @return all of the role's group scope resource type permissions that
097             *         apply to resources of the type
098             */
099            @Override
100            public List<ResourceTypePermission> findByGroupScopeC_N_R(
101                    long companyId, String name, long roleId) {
102    
103                    Session session = null;
104    
105                    try {
106                            session = openSession();
107    
108                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_SCOPE_C_N_R);
109    
110                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
111    
112                            q.addEntity(
113                                    "ResourceTypePermission", ResourceTypePermissionImpl.class);
114    
115                            QueryPos qPos = QueryPos.getInstance(q);
116    
117                            qPos.add(companyId);
118                            qPos.add(name);
119                            qPos.add(roleId);
120    
121                            return (List<ResourceTypePermission>)QueryUtil.list(
122                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132    }