001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
084            public UserGroupGroupRole deleteUserGroupGroupRole(
085                            UserGroupGroupRole userGroupGroupRole)
086                    throws SystemException {
087    
088                    userGroupGroupRolePersistence.remove(userGroupGroupRole);
089    
090                    PermissionCacheUtil.clearCache();
091    
092                    return userGroupGroupRole;
093            }
094    
095            public void deleteUserGroupGroupRoles(
096                            long userGroupId, long groupId, long[] roleIds)
097                    throws SystemException {
098    
099                    for (long roleId : roleIds) {
100                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
101                                    userGroupId, groupId, roleId);
102    
103                            try {
104                                    userGroupGroupRolePersistence.remove(pk);
105                            }
106                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
107                            }
108                    }
109    
110                    PermissionCacheUtil.clearCache();
111            }
112    
113            public void deleteUserGroupGroupRoles(long userGroupId, long[] groupIds)
114                    throws SystemException {
115    
116                    for (long groupId : groupIds) {
117                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
118                    }
119    
120                    PermissionCacheUtil.clearCache();
121            }
122    
123            public void deleteUserGroupGroupRoles(long[] userGroupIds, long groupId)
124                    throws SystemException {
125    
126                    for (long userGroupId : userGroupIds) {
127                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
128                    }
129    
130                    PermissionCacheUtil.clearCache();
131            }
132    
133            public void deleteUserGroupGroupRoles(
134                            long[] userGroupIds, long groupId, long roleId)
135                    throws SystemException {
136    
137                    for (long userGroupId : userGroupIds) {
138                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
139                                    userGroupId, groupId, roleId);
140    
141                            try {
142                                    userGroupGroupRolePersistence.remove(pk);
143                            }
144                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
145                            }
146                    }
147    
148                    PermissionCacheUtil.clearCache();
149            }
150    
151            public void deleteUserGroupGroupRolesByGroupId(long groupId)
152                    throws SystemException {
153    
154                    userGroupGroupRolePersistence.removeByGroupId(groupId);
155    
156                    PermissionCacheUtil.clearCache();
157            }
158    
159            public void deleteUserGroupGroupRolesByRoleId(long roleId)
160                    throws SystemException {
161    
162                    userGroupGroupRolePersistence.removeByRoleId(roleId);
163    
164                    PermissionCacheUtil.clearCache();
165            }
166    
167            public void deleteUserGroupGroupRolesByUserGroupId(long userGroupId)
168                    throws SystemException {
169    
170                    userGroupGroupRolePersistence.removeByUserGroupId(userGroupId);
171    
172                    PermissionCacheUtil.clearCache();
173            }
174    
175            public List<UserGroupGroupRole> getUserGroupGroupRoles(long userGroupId)
176                    throws SystemException {
177    
178                    return userGroupGroupRolePersistence.findByUserGroupId(userGroupId);
179            }
180    
181            public List<UserGroupGroupRole> getUserGroupGroupRoles(
182                            long userGroupId, long groupId)
183                    throws SystemException {
184    
185                    return userGroupGroupRolePersistence.findByU_G(userGroupId, groupId);
186            }
187    
188            public List<UserGroupGroupRole> getUserGroupGroupRolesByGroupAndRole(
189                            long groupId, long roleId)
190                    throws SystemException {
191    
192                    return userGroupGroupRolePersistence.findByG_R(groupId, roleId);
193            }
194    
195            public boolean hasUserGroupGroupRole(
196                            long userGroupId, long groupId, long roleId)
197                    throws SystemException {
198    
199                    UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
200                            userGroupId, groupId, roleId);
201    
202                    UserGroupGroupRole userGroupGroupRole =
203                            userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
204    
205                    if (userGroupGroupRole != null) {
206                            return true;
207                    }
208                    else {
209                            return false;
210                    }
211            }
212    
213            public boolean hasUserGroupGroupRole(
214                            long userGroupId, long groupId, String roleName)
215                    throws PortalException, SystemException {
216    
217                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
218                            userGroupId);
219    
220                    long companyId = userGroup.getCompanyId();
221    
222                    Role role = rolePersistence.findByC_N(companyId, roleName);
223    
224                    long roleId = role.getRoleId();
225    
226                    return hasUserGroupGroupRole(userGroupId, groupId, roleId);
227            }
228    
229            protected void checkGroupResource(long groupId)
230                    throws PortalException, SystemException {
231    
232                    // Make sure that the individual resource for the group exists
233    
234                    Group group = groupPersistence.findByPrimaryKey(groupId);
235    
236                    resourceLocalService.addResource(
237                            group.getCompanyId(), Group.class.getName(),
238                            ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
239            }
240    
241    }