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.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.impl.BasePersistenceImpl;
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 BasePersistenceImpl<ResourceTypePermission>
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             * @throws SystemException if a system exception occurred
054             */
055            @Override
056            public List<ResourceTypePermission> findByEitherScopeC_G_N(
057                            long companyId, long groupId, String name)
058                    throws SystemException {
059    
060                    Session session = null;
061    
062                    try {
063                            session = openSession();
064    
065                            String sql = CustomSQLUtil.get(FIND_BY_EITHER_SCOPE_C_G_N);
066    
067                            SQLQuery q = session.createSQLQuery(sql);
068    
069                            q.addEntity(
070                                    "ResourceTypePermission", ResourceTypePermissionImpl.class);
071    
072                            QueryPos qPos = QueryPos.getInstance(q);
073    
074                            qPos.add(companyId);
075                            qPos.add(name);
076                            qPos.add(groupId);
077    
078                            return (List<ResourceTypePermission>)QueryUtil.list(
079                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
080                    }
081                    catch (Exception e) {
082                            throw new SystemException(e);
083                    }
084                    finally {
085                            closeSession(session);
086                    }
087            }
088    
089            /**
090             * Returns all of the role's group scope resource type permissions that
091             * apply to resources of the type. This method is used during the process of
092             * adding a company scope resource type permissions to remove all the group
093             * scope permissions that would be overridden by it.
094             *
095             * @param  companyId the primary key of the company
096             * @param  name the fully qualified class name of the resource type
097             * @param  roleId the primary key of the role
098             * @return all of the role's group scope resource type permissions that
099             *         apply to resources of the type
100             * @throws SystemException if a system exception occurred
101             */
102            @Override
103            public List<ResourceTypePermission> findByGroupScopeC_N_R(
104                            long companyId, String name, long roleId)
105                    throws SystemException {
106    
107                    Session session = null;
108    
109                    try {
110                            session = openSession();
111    
112                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_SCOPE_C_N_R);
113    
114                            SQLQuery q = session.createSQLQuery(sql);
115    
116                            q.addEntity(
117                                    "ResourceTypePermission", ResourceTypePermissionImpl.class);
118    
119                            QueryPos qPos = QueryPos.getInstance(q);
120    
121                            qPos.add(companyId);
122                            qPos.add(name);
123                            qPos.add(roleId);
124    
125                            return (List<ResourceTypePermission>)QueryUtil.list(
126                                    q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
127                    }
128                    catch (Exception e) {
129                            throw new SystemException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136    }