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.impl;
016    
017    import com.liferay.portal.NoSuchUserGroupGroupRoleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.model.UserGroup;
022    import com.liferay.portal.model.UserGroupGroupRole;
023    import com.liferay.portal.security.permission.PermissionCacheUtil;
024    import com.liferay.portal.service.base.UserGroupGroupRoleLocalServiceBaseImpl;
025    import com.liferay.portal.service.persistence.UserGroupGroupRolePK;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brett Swaim
031     */
032    public class UserGroupGroupRoleLocalServiceImpl
033            extends UserGroupGroupRoleLocalServiceBaseImpl {
034    
035            public void addUserGroupGroupRoles(
036                            long userGroupId, long groupId, long[] roleIds)
037                    throws SystemException {
038    
039                    for (long roleId : roleIds) {
040                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
041                                    userGroupId, groupId, roleId);
042    
043                            UserGroupGroupRole userGroupGroupRole =
044                                    userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
045    
046                            if (userGroupGroupRole == null) {
047                                    userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
048    
049                                    userGroupGroupRolePersistence.update(userGroupGroupRole);
050                            }
051                    }
052    
053                    PermissionCacheUtil.clearCache();
054            }
055    
056            public void addUserGroupGroupRoles(
057                            long[] userGroupIds, long groupId, long roleId)
058                    throws SystemException {
059    
060                    for (long userGroupId : userGroupIds) {
061                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
062                                    userGroupId, groupId, roleId);
063    
064                            UserGroupGroupRole userGroupGroupRole =
065                                    userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
066    
067                            if (userGroupGroupRole == null) {
068                                    userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
069    
070                                    userGroupGroupRolePersistence.update(userGroupGroupRole);
071                            }
072                    }
073    
074                    PermissionCacheUtil.clearCache();
075            }
076    
077            @Override
078            public UserGroupGroupRole deleteUserGroupGroupRole(
079                            UserGroupGroupRole userGroupGroupRole)
080                    throws SystemException {
081    
082                    userGroupGroupRolePersistence.remove(userGroupGroupRole);
083    
084                    PermissionCacheUtil.clearCache();
085    
086                    return userGroupGroupRole;
087            }
088    
089            public void deleteUserGroupGroupRoles(
090                            long userGroupId, long groupId, long[] roleIds)
091                    throws SystemException {
092    
093                    for (long roleId : roleIds) {
094                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
095                                    userGroupId, groupId, roleId);
096    
097                            try {
098                                    userGroupGroupRolePersistence.remove(pk);
099                            }
100                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
101                            }
102                    }
103    
104                    PermissionCacheUtil.clearCache();
105            }
106    
107            public void deleteUserGroupGroupRoles(long userGroupId, long[] groupIds)
108                    throws SystemException {
109    
110                    for (long groupId : groupIds) {
111                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
112                    }
113    
114                    PermissionCacheUtil.clearCache();
115            }
116    
117            public void deleteUserGroupGroupRoles(long[] userGroupIds, long groupId)
118                    throws SystemException {
119    
120                    for (long userGroupId : userGroupIds) {
121                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
122                    }
123    
124                    PermissionCacheUtil.clearCache();
125            }
126    
127            public void deleteUserGroupGroupRoles(
128                            long[] userGroupIds, long groupId, long roleId)
129                    throws SystemException {
130    
131                    for (long userGroupId : userGroupIds) {
132                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
133                                    userGroupId, groupId, roleId);
134    
135                            try {
136                                    userGroupGroupRolePersistence.remove(pk);
137                            }
138                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
139                            }
140                    }
141    
142                    PermissionCacheUtil.clearCache();
143            }
144    
145            public void deleteUserGroupGroupRolesByGroupId(long groupId)
146                    throws SystemException {
147    
148                    userGroupGroupRolePersistence.removeByGroupId(groupId);
149    
150                    PermissionCacheUtil.clearCache();
151            }
152    
153            public void deleteUserGroupGroupRolesByRoleId(long roleId)
154                    throws SystemException {
155    
156                    userGroupGroupRolePersistence.removeByRoleId(roleId);
157    
158                    PermissionCacheUtil.clearCache();
159            }
160    
161            public void deleteUserGroupGroupRolesByUserGroupId(long userGroupId)
162                    throws SystemException {
163    
164                    userGroupGroupRolePersistence.removeByUserGroupId(userGroupId);
165    
166                    PermissionCacheUtil.clearCache();
167            }
168    
169            public List<UserGroupGroupRole> getUserGroupGroupRoles(long userGroupId)
170                    throws SystemException {
171    
172                    return userGroupGroupRolePersistence.findByUserGroupId(userGroupId);
173            }
174    
175            public List<UserGroupGroupRole> getUserGroupGroupRoles(
176                            long userGroupId, long groupId)
177                    throws SystemException {
178    
179                    return userGroupGroupRolePersistence.findByU_G(userGroupId, groupId);
180            }
181    
182            public List<UserGroupGroupRole> getUserGroupGroupRolesByGroupAndRole(
183                            long groupId, long roleId)
184                    throws SystemException {
185    
186                    return userGroupGroupRolePersistence.findByG_R(groupId, roleId);
187            }
188    
189            public boolean hasUserGroupGroupRole(
190                            long userGroupId, long groupId, long roleId)
191                    throws SystemException {
192    
193                    UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
194                            userGroupId, groupId, roleId);
195    
196                    UserGroupGroupRole userGroupGroupRole =
197                            userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
198    
199                    if (userGroupGroupRole != null) {
200                            return true;
201                    }
202                    else {
203                            return false;
204                    }
205            }
206    
207            public boolean hasUserGroupGroupRole(
208                            long userGroupId, long groupId, String roleName)
209                    throws PortalException, SystemException {
210    
211                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
212                            userGroupId);
213    
214                    long companyId = userGroup.getCompanyId();
215    
216                    Role role = rolePersistence.findByC_N(companyId, roleName);
217    
218                    long roleId = role.getRoleId();
219    
220                    return hasUserGroupGroupRole(userGroupId, groupId, roleId);
221            }
222    
223    }