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