1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchUserGroupGroupRoleException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.ResourceConstants;
30  import com.liferay.portal.model.Role;
31  import com.liferay.portal.model.UserGroup;
32  import com.liferay.portal.model.UserGroupGroupRole;
33  import com.liferay.portal.security.permission.PermissionCacheUtil;
34  import com.liferay.portal.service.base.UserGroupGroupRoleLocalServiceBaseImpl;
35  import com.liferay.portal.service.persistence.UserGroupGroupRolePK;
36  
37  import java.util.List;
38  
39  /**
40   * <a href="UserGroupGroupRoleLocalServiceImpl.java.html"><b><i>View Source</i>
41   * </b></a>
42   *
43   * @author Brett Swaim
44   *
45   */
46  public class UserGroupGroupRoleLocalServiceImpl
47      extends UserGroupGroupRoleLocalServiceBaseImpl {
48  
49      public void addUserGroupGroupRoles(
50              long userGroupId, long groupId, long[] roleIds)
51          throws PortalException, SystemException {
52  
53          checkGroupResource(groupId);
54  
55          for (long roleId : roleIds) {
56              UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
57                  userGroupId, groupId, roleId);
58  
59              UserGroupGroupRole userGroupGroupRole =
60                  userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
61  
62              if (userGroupGroupRole == null) {
63                  userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
64  
65                  userGroupGroupRolePersistence.update(userGroupGroupRole, false);
66              }
67          }
68  
69          PermissionCacheUtil.clearCache();
70      }
71  
72      public void addUserGroupGroupRoles(
73              long[] userGroupIds, long groupId, long roleId)
74          throws PortalException, SystemException {
75  
76          checkGroupResource(groupId);
77  
78          for (long userGroupId : userGroupIds) {
79              UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
80                  userGroupId, groupId, roleId);
81  
82              UserGroupGroupRole userGroupGroupRole =
83                  userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
84  
85              if (userGroupGroupRole == null) {
86                  userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
87  
88                  userGroupGroupRolePersistence.update(userGroupGroupRole, false);
89              }
90          }
91  
92          PermissionCacheUtil.clearCache();
93      }
94  
95      public void deleteUserGroupGroupRole(UserGroupGroupRole userGroupGroupRole)
96          throws SystemException {
97  
98          userGroupGroupRolePersistence.remove(userGroupGroupRole);
99  
100         PermissionCacheUtil.clearCache();
101     }
102 
103     public void deleteUserGroupGroupRoles(
104             long userGroupId, long groupId, long[] roleIds)
105         throws SystemException {
106 
107         for (long roleId : roleIds) {
108             UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
109                 userGroupId, groupId, roleId);
110 
111             try {
112                 userGroupGroupRolePersistence.remove(pk);
113             }
114             catch (NoSuchUserGroupGroupRoleException nsuggre) {
115             }
116         }
117 
118         PermissionCacheUtil.clearCache();
119     }
120 
121     public void deleteUserGroupGroupRoles(long userGroupId, long[] groupIds)
122         throws SystemException {
123 
124         for (long groupId : groupIds) {
125             userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
126         }
127 
128         PermissionCacheUtil.clearCache();
129     }
130 
131     public void deleteUserGroupGroupRoles(long[] userGroupIds, long groupId)
132         throws SystemException {
133 
134         for (long userGroupId : userGroupIds) {
135             userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
136         }
137 
138         PermissionCacheUtil.clearCache();
139     }
140 
141     public void deleteUserGroupGroupRoles(
142             long[] userGroupIds, long groupId, long roleId)
143         throws SystemException {
144 
145         for (long userGroupId : userGroupIds) {
146             UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
147                 userGroupId, groupId, roleId);
148 
149             try {
150                 userGroupGroupRolePersistence.remove(pk);
151             }
152             catch (NoSuchUserGroupGroupRoleException nsuggre) {
153             }
154         }
155 
156         PermissionCacheUtil.clearCache();
157     }
158 
159     public void deleteUserGroupGroupRolesByGroupId(long groupId)
160         throws SystemException {
161 
162         userGroupGroupRolePersistence.removeByGroupId(groupId);
163 
164         PermissionCacheUtil.clearCache();
165     }
166 
167     public void deleteUserGroupGroupRolesByRoleId(long roleId)
168         throws SystemException {
169 
170         userGroupGroupRolePersistence.removeByRoleId(roleId);
171 
172         PermissionCacheUtil.clearCache();
173     }
174 
175     public void deleteUserGroupGroupRolesByuserGroupId(long userGroupId)
176         throws SystemException {
177 
178         userGroupGroupRolePersistence.removeByUserGroupId(userGroupId);
179 
180         PermissionCacheUtil.clearCache();
181     }
182 
183     public List<UserGroupGroupRole> getUserGroupGroupRoles(long userGroupId)
184         throws SystemException {
185 
186         return userGroupGroupRolePersistence.findByUserGroupId(userGroupId);
187     }
188 
189     public List<UserGroupGroupRole> getUserGroupGroupRoles(
190             long userGroupId, long groupId)
191         throws SystemException {
192 
193         return userGroupGroupRolePersistence.findByU_G(userGroupId, groupId);
194     }
195 
196     public List<UserGroupGroupRole> getUserGroupGroupRolesByGroupAndRole(
197             long groupId, long roleId)
198         throws SystemException {
199 
200         return userGroupGroupRolePersistence.findByG_R(groupId, roleId);
201     }
202 
203     public boolean hasUserGroupGroupRole(
204             long userGroupId, long groupId, long roleId)
205         throws SystemException {
206 
207         UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
208             userGroupId, groupId, roleId);
209 
210         UserGroupGroupRole userGroupGroupRole =
211             userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
212 
213         if (userGroupGroupRole != null) {
214             return true;
215         }
216         else {
217             return false;
218         }
219     }
220 
221     public boolean hasUserGroupGroupRole(
222             long userGroupId, long groupId, String roleName)
223         throws PortalException, SystemException {
224 
225         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
226             userGroupId);
227 
228         long companyId = userGroup.getCompanyId();
229 
230         Role role = rolePersistence.findByC_N(companyId, roleName);
231 
232         long roleId = role.getRoleId();
233 
234         return hasUserGroupGroupRole(userGroupId, groupId, roleId);
235     }
236 
237     protected void checkGroupResource(long groupId)
238         throws PortalException, SystemException {
239 
240         // Make sure that the individual resource for the group exists
241 
242         Group group = groupPersistence.findByPrimaryKey(groupId);
243 
244         resourceLocalService.addResource(
245             group.getCompanyId(), Group.class.getName(),
246             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
247     }
248 
249 }