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