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