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  public class UserGroupGroupRoleLocalServiceImpl
46      extends UserGroupGroupRoleLocalServiceBaseImpl {
47  
48      public void addUserGroupGroupRoles(
49              long userGroupId, long groupId, long[] roleIds)
50          throws PortalException, SystemException {
51  
52          checkGroupResource(groupId);
53  
54          for (long roleId : roleIds) {
55              UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
56                  userGroupId, groupId, roleId);
57  
58              UserGroupGroupRole userGroupGroupRole =
59                  userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
60  
61              if (userGroupGroupRole == null) {
62                  userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
63  
64                  userGroupGroupRolePersistence.update(userGroupGroupRole, false);
65              }
66          }
67  
68          PermissionCacheUtil.clearCache();
69      }
70  
71      public void addUserGroupGroupRoles(
72              long[] userGroupIds, long groupId, long roleId)
73          throws PortalException, SystemException {
74  
75          checkGroupResource(groupId);
76  
77          for (long userGroupId : userGroupIds) {
78              UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
79                  userGroupId, groupId, roleId);
80  
81              UserGroupGroupRole userGroupGroupRole =
82                  userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
83  
84              if (userGroupGroupRole == null) {
85                  userGroupGroupRole = userGroupGroupRolePersistence.create(pk);
86  
87                  userGroupGroupRolePersistence.update(userGroupGroupRole, false);
88              }
89          }
90  
91          PermissionCacheUtil.clearCache();
92      }
93  
94      public void deleteUserGroupGroupRole(UserGroupGroupRole userGroupGroupRole)
95          throws SystemException {
96  
97          userGroupGroupRolePersistence.remove(userGroupGroupRole);
98  
99          PermissionCacheUtil.clearCache();
100     }
101 
102     public void deleteUserGroupGroupRoles(
103             long userGroupId, long groupId, long[] roleIds)
104         throws SystemException {
105 
106         for (long roleId : roleIds) {
107             UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
108                 userGroupId, groupId, roleId);
109 
110             try {
111                 userGroupGroupRolePersistence.remove(pk);
112             }
113             catch (NoSuchUserGroupGroupRoleException nsuggre) {
114             }
115         }
116 
117         PermissionCacheUtil.clearCache();
118     }
119 
120     public void deleteUserGroupGroupRoles(long userGroupId, long[] groupIds)
121         throws SystemException {
122 
123         for (long groupId : groupIds) {
124             userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
125         }
126 
127         PermissionCacheUtil.clearCache();
128     }
129 
130     public void deleteUserGroupGroupRoles(long[] userGroupIds, long groupId)
131         throws SystemException {
132 
133         for (long userGroupId : userGroupIds) {
134             userGroupGroupRolePersistence.removeByU_G(userGroupId, groupId);
135         }
136 
137         PermissionCacheUtil.clearCache();
138     }
139 
140     public void deleteUserGroupGroupRoles(
141             long[] userGroupIds, long groupId, long roleId)
142         throws SystemException {
143 
144         for (long userGroupId : userGroupIds) {
145             UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
146                 userGroupId, groupId, roleId);
147 
148             try {
149                 userGroupGroupRolePersistence.remove(pk);
150             }
151             catch (NoSuchUserGroupGroupRoleException nsuggre) {
152             }
153         }
154 
155         PermissionCacheUtil.clearCache();
156     }
157 
158     public void deleteUserGroupGroupRolesByGroupId(long groupId)
159         throws SystemException {
160 
161         userGroupGroupRolePersistence.removeByGroupId(groupId);
162 
163         PermissionCacheUtil.clearCache();
164     }
165 
166     public void deleteUserGroupGroupRolesByRoleId(long roleId)
167         throws SystemException {
168 
169         userGroupGroupRolePersistence.removeByRoleId(roleId);
170 
171         PermissionCacheUtil.clearCache();
172     }
173 
174     public void deleteUserGroupGroupRolesByUserGroupId(long userGroupId)
175         throws SystemException {
176 
177         userGroupGroupRolePersistence.removeByUserGroupId(userGroupId);
178 
179         PermissionCacheUtil.clearCache();
180     }
181 
182     public List<UserGroupGroupRole> getUserGroupGroupRoles(long userGroupId)
183         throws SystemException {
184 
185         return userGroupGroupRolePersistence.findByUserGroupId(userGroupId);
186     }
187 
188     public List<UserGroupGroupRole> getUserGroupGroupRoles(
189             long userGroupId, long groupId)
190         throws SystemException {
191 
192         return userGroupGroupRolePersistence.findByU_G(userGroupId, groupId);
193     }
194 
195     public List<UserGroupGroupRole> getUserGroupGroupRolesByGroupAndRole(
196             long groupId, long roleId)
197         throws SystemException {
198 
199         return userGroupGroupRolePersistence.findByG_R(groupId, roleId);
200     }
201 
202     public boolean hasUserGroupGroupRole(
203             long userGroupId, long groupId, long roleId)
204         throws SystemException {
205 
206         UserGroupGroupRolePK pk = new UserGroupGroupRolePK(
207             userGroupId, groupId, roleId);
208 
209         UserGroupGroupRole userGroupGroupRole =
210             userGroupGroupRolePersistence.fetchByPrimaryKey(pk);
211 
212         if (userGroupGroupRole != null) {
213             return true;
214         }
215         else {
216             return false;
217         }
218     }
219 
220     public boolean hasUserGroupGroupRole(
221             long userGroupId, long groupId, String roleName)
222         throws PortalException, SystemException {
223 
224         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
225             userGroupId);
226 
227         long companyId = userGroup.getCompanyId();
228 
229         Role role = rolePersistence.findByC_N(companyId, roleName);
230 
231         long roleId = role.getRoleId();
232 
233         return hasUserGroupGroupRole(userGroupId, groupId, roleId);
234     }
235 
236     protected void checkGroupResource(long groupId)
237         throws PortalException, SystemException {
238 
239         // Make sure that the individual resource for the group exists
240 
241         Group group = groupPersistence.findByPrimaryKey(groupId);
242 
243         resourceLocalService.addResource(
244             group.getCompanyId(), Group.class.getName(),
245             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
246     }
247 
248 }