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.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 void deleteUserGroupGroupRole(UserGroupGroupRole userGroupGroupRole)
085                    throws SystemException {
086    
087                    userGroupGroupRolePersistence.remove(userGroupGroupRole);
088    
089                    PermissionCacheUtil.clearCache();
090            }
091    
092            public void deleteUserGroupGroupRoles(
093                            long userGroupId, long groupId, long[] roleIds)
094                    throws SystemException {
095    
096                    for (long roleId : roleIds) {
097                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
098                                    userGroupId, groupId, roleId);
099    
100                            try {
101                                    userGroupGroupRolePersistence.remove(pk);
102                            }
103                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
104                            }
105                    }
106    
107                    PermissionCacheUtil.clearCache();
108            }
109    
110            public void deleteUserGroupGroupRoles(long userGroupId, long[] groupIds)
111                    throws SystemException {
112    
113                    for (long groupId : groupIds) {
114                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
115                    }
116    
117                    PermissionCacheUtil.clearCache();
118            }
119    
120            public void deleteUserGroupGroupRoles(long[] userGroupIds, long groupId)
121                    throws SystemException {
122    
123                    for (long userGroupId : userGroupIds) {
124                            userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
125                    }
126    
127                    PermissionCacheUtil.clearCache();
128            }
129    
130            public void deleteUserGroupGroupRoles(
131                            long[] userGroupIds, long groupId, long roleId)
132                    throws SystemException {
133    
134                    for (long userGroupId : userGroupIds) {
135                            UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
136                                    userGroupId, groupId, roleId);
137    
138                            try {
139                                    userGroupGroupRolePersistence.remove(pk);
140                            }
141                            catch (NoSuchUserGroupGroupRoleException nsuggre) {
142                            }
143                    }
144    
145                    PermissionCacheUtil.clearCache();
146            }
147    
148            public void deleteUserGroupGroupRolesByGroupId(long groupId)
149                    throws SystemException {
150    
151                    userGroupGroupRolePersistence.removeByGroupId(groupId);
152    
153                    PermissionCacheUtil.clearCache();
154            }
155    
156            public void deleteUserGroupGroupRolesByRoleId(long roleId)
157                    throws SystemException {
158    
159                    userGroupGroupRolePersistence.removeByRoleId(roleId);
160    
161                    PermissionCacheUtil.clearCache();
162            }
163    
164            public void deleteUserGroupGroupRolesByUserGroupId(long userGroupId)
165                    throws SystemException {
166    
167                    userGroupGroupRolePersistence.removeByUserGroupId(userGroupId);
168    
169                    PermissionCacheUtil.clearCache();
170            }
171    
172            public List<UserGroupGroupRole> getUserGroupGroupRoles(long userGroupId)
173                    throws SystemException {
174    
175                    return userGroupGroupRolePersistence.findByUserGroupId(userGroupId);
176            }
177    
178            public List<UserGroupGroupRole> getUserGroupGroupRoles(
179                            long userGroupId, long groupId)
180                    throws SystemException {
181    
182                    return userGroupGroupRolePersistence.findByU_G(userGroupId, groupId);
183            }
184    
185            public List<UserGroupGroupRole> getUserGroupGroupRolesByGroupAndRole(
186                            long groupId, long roleId)
187                    throws SystemException {
188    
189                    return userGroupGroupRolePersistence.findByG_R(groupId, roleId);
190            }
191    
192            public boolean hasUserGroupGroupRole(
193                            long userGroupId, long groupId, long roleId)
194                    throws SystemException {
195    
196                    UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
197                            userGroupId, groupId, roleId);
198    
199                    UserGroupGroupRole userGroupGroupRole =
200                            userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
201    
202                    if (userGroupGroupRole != null) {
203                            return true;
204                    }
205                    else {
206                            return false;
207                    }
208            }
209    
210            public boolean hasUserGroupGroupRole(
211                            long userGroupId, long groupId, String roleName)
212                    throws PortalException, SystemException {
213    
214                    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
215                            userGroupId);
216    
217                    long companyId = userGroup.getCompanyId();
218    
219                    Role role = rolePersistence.findByC_N(companyId, roleName);
220    
221                    long roleId = role.getRoleId();
222    
223                    return hasUserGroupGroupRole(userGroupId, groupId, roleId);
224            }
225    
226            protected void checkGroupResource(long groupId)
227                    throws PortalException, SystemException {
228    
229                    // Make sure that the individual resource for the group exists
230    
231                    Group group = groupPersistence.findByPrimaryKey(groupId);
232    
233                    resourceLocalService.addResource(
234                            group.getCompanyId(), Group.class.getName(),
235                            ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
236            }
237    
238    }