001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.DuplicateUserGroupException;
024: import com.liferay.portal.NoSuchUserGroupException;
025: import com.liferay.portal.PortalException;
026: import com.liferay.portal.RequiredUserGroupException;
027: import com.liferay.portal.SystemException;
028: import com.liferay.portal.UserGroupNameException;
029: import com.liferay.portal.kernel.util.OrderByComparator;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.Group;
033: import com.liferay.portal.model.UserGroup;
034: import com.liferay.portal.model.impl.ResourceImpl;
035: import com.liferay.portal.model.impl.UserGroupImpl;
036: import com.liferay.portal.security.permission.PermissionCacheUtil;
037: import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
038:
039: import java.util.LinkedHashMap;
040: import java.util.List;
041:
042: /**
043: * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Charles May
046: *
047: */
048: public class UserGroupLocalServiceImpl extends
049: UserGroupLocalServiceBaseImpl {
050:
051: public void addGroupUserGroups(long groupId, long[] userGroupIds)
052: throws PortalException, SystemException {
053:
054: groupPersistence.addUserGroups(groupId, userGroupIds);
055:
056: PermissionCacheUtil.clearCache();
057: }
058:
059: public UserGroup addUserGroup(long userId, long companyId,
060: String name, String description) throws PortalException,
061: SystemException {
062:
063: // User Group
064:
065: validate(0, companyId, name);
066:
067: long userGroupId = counterLocalService.increment();
068:
069: UserGroup userGroup = userGroupPersistence.create(userGroupId);
070:
071: userGroup.setCompanyId(companyId);
072: userGroup
073: .setParentUserGroupId(UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
074: userGroup.setName(name);
075: userGroup.setDescription(description);
076:
077: userGroupPersistence.update(userGroup);
078:
079: // Group
080:
081: groupLocalService.addGroup(userId, UserGroup.class.getName(),
082: userGroup.getUserGroupId(), null, null, 0, null, true);
083:
084: // Resources
085:
086: resourceLocalService.addResources(companyId, 0, userId,
087: UserGroup.class.getName(), userGroup.getUserGroupId(),
088: false, false, false);
089:
090: return userGroup;
091: }
092:
093: public void clearUserUserGroups(long userId)
094: throws PortalException, SystemException {
095:
096: userPersistence.clearUserGroups(userId);
097:
098: PermissionCacheUtil.clearCache();
099: }
100:
101: public void deleteUserGroup(long userGroupId)
102: throws PortalException, SystemException {
103:
104: UserGroup userGroup = userGroupPersistence
105: .findByPrimaryKey(userGroupId);
106:
107: if (userGroupPersistence.containsUsers(userGroup
108: .getUserGroupId())) {
109: throw new RequiredUserGroupException();
110: }
111:
112: // Group
113:
114: Group group = userGroup.getGroup();
115:
116: groupLocalService.deleteGroup(group.getGroupId());
117:
118: // Resources
119:
120: resourceLocalService.deleteResource(userGroup.getCompanyId(),
121: UserGroup.class.getName(),
122: ResourceImpl.SCOPE_INDIVIDUAL, userGroup
123: .getUserGroupId());
124:
125: // User Group
126:
127: userGroupPersistence.remove(userGroupId);
128:
129: // Permission cache
130:
131: PermissionCacheUtil.clearCache();
132: }
133:
134: public UserGroup getUserGroup(long userGroupId)
135: throws PortalException, SystemException {
136:
137: return userGroupPersistence.findByPrimaryKey(userGroupId);
138: }
139:
140: public UserGroup getUserGroup(long companyId, String name)
141: throws PortalException, SystemException {
142:
143: return userGroupFinder.findByC_N(companyId, name);
144: }
145:
146: public List getUserGroups(long companyId) throws PortalException,
147: SystemException {
148:
149: return userGroupPersistence.findByCompanyId(companyId);
150: }
151:
152: public List getUserUserGroups(long userId) throws PortalException,
153: SystemException {
154:
155: return userPersistence.getUserGroups(userId);
156: }
157:
158: public boolean hasGroupUserGroup(long groupId, long userGroupId)
159: throws PortalException, SystemException {
160:
161: return groupPersistence.containsUserGroup(groupId, userGroupId);
162: }
163:
164: public List search(long companyId, String name, String description,
165: LinkedHashMap params, int begin, int end,
166: OrderByComparator obc) throws SystemException {
167:
168: return userGroupFinder.findByC_N_D(companyId, name,
169: description, params, begin, end, obc);
170: }
171:
172: public int searchCount(long companyId, String name,
173: String description, LinkedHashMap params)
174: throws SystemException {
175:
176: return userGroupFinder.countByC_N_D(companyId, name,
177: description, params);
178: }
179:
180: public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
181: throws PortalException, SystemException {
182:
183: groupPersistence.removeUserGroups(groupId, userGroupIds);
184:
185: PermissionCacheUtil.clearCache();
186: }
187:
188: public UserGroup updateUserGroup(long companyId, long userGroupId,
189: String name, String description) throws PortalException,
190: SystemException {
191:
192: validate(userGroupId, companyId, name);
193:
194: UserGroup userGroup = userGroupPersistence
195: .findByPrimaryKey(userGroupId);
196:
197: userGroup.setName(name);
198: userGroup.setDescription(description);
199:
200: userGroupPersistence.update(userGroup);
201:
202: return userGroup;
203: }
204:
205: protected void validate(long userGroupId, long companyId,
206: String name) throws PortalException, SystemException {
207:
208: if ((Validator.isNull(name)) || (Validator.isNumber(name))
209: || (name.indexOf(StringPool.COMMA) != -1)
210: || (name.indexOf(StringPool.STAR) != -1)) {
211:
212: throw new UserGroupNameException();
213: }
214:
215: try {
216: UserGroup userGroup = userGroupFinder.findByC_N(companyId,
217: name);
218:
219: if (userGroup.getUserGroupId() != userGroupId) {
220: throw new DuplicateUserGroupException();
221: }
222: } catch (NoSuchUserGroupException nsuge) {
223: }
224: }
225:
226: }
|