001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.lenya.ac.Group;
025: import org.apache.lenya.ac.Groupable;
026: import org.apache.lenya.ac.User;
027: import org.apache.lenya.cms.usecase.UsecaseException;
028:
029: /**
030: * Usecase to change the members of a group.
031: *
032: * @version $Id: GroupMembers.java 407305 2006-05-17 16:21:49Z andreas $
033: */
034: public class GroupMembers extends AccessControlUsecase {
035:
036: protected static final String GROUP_USERS = "groupUsers";
037: protected static final String OTHER_USERS = "otherUsers";
038: protected static final String ADD = "add";
039: protected static final String REMOVE = "remove";
040: protected static final String GROUP_USER = "groupUser";
041: protected static final String OTHER_USER = "otherUser";
042:
043: /**
044: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
045: */
046: protected void initParameters() {
047: super .initParameters();
048:
049: Groupable[] members = getGroup().getMembers();
050: List groupUsers = new ArrayList();
051: for (int i = 0; i < members.length; i++) {
052: if (members[i] instanceof User) {
053: groupUsers.add(members[i]);
054: }
055: }
056: setParameter(GROUP_USERS, groupUsers);
057:
058: User[] allUsers = getUserManager().getUsers();
059:
060: List otherUsers = new ArrayList();
061: for (int i = 0; i < allUsers.length; i++) {
062: if (!groupUsers.contains(allUsers[i])) {
063: otherUsers.add(allUsers[i]);
064: }
065: }
066: setParameter(OTHER_USERS, otherUsers);
067:
068: setExitParameter(GroupProfile.ID,
069: getParameterAsString(GroupProfile.ID));
070: }
071:
072: /**
073: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
074: */
075: protected void doExecute() throws Exception {
076: super .doExecute();
077:
078: final Group group = getGroup();
079: group.removeAllMembers();
080:
081: List groupUsers = (List) getParameter(GROUP_USERS);
082: for (Iterator i = groupUsers.iterator(); i.hasNext();) {
083: User user = (User) i.next();
084: group.add(user);
085: user.save();
086: }
087: }
088:
089: /**
090: * @see org.apache.lenya.cms.usecase.Usecase#advance()
091: */
092: public void advance() throws UsecaseException {
093: super .advance();
094:
095: String add = getParameterAsString(ADD);
096: String remove = getParameterAsString(REMOVE);
097: if (add != null || remove != null) {
098:
099: List groupUsers = (List) getParameter(GROUP_USERS);
100: List otherUsers = (List) getParameter(OTHER_USERS);
101:
102: if (add != null) {
103: String userId = getParameterAsString(OTHER_USER);
104: if (userId != null) {
105: if (getLogger().isDebugEnabled()) {
106: getLogger().debug("add user [" + userId + "]");
107: }
108: User user = getUserManager().getUser(userId);
109: groupUsers.add(user);
110: otherUsers.remove(user);
111: }
112: }
113:
114: if (remove != null) {
115: String userId = getParameterAsString(GROUP_USER);
116: if (userId != null) {
117: if (getLogger().isDebugEnabled()) {
118: getLogger().debug(
119: "remove user [" + userId + "]");
120: }
121: User user = getUserManager().getUser(userId);
122: otherUsers.add(user);
123: groupUsers.remove(user);
124: }
125: }
126:
127: deleteParameter(ADD);
128: deleteParameter(REMOVE);
129: deleteParameter(GROUP_USER);
130: deleteParameter(OTHER_USER);
131: }
132:
133: }
134:
135: protected Group getGroup() {
136: String groupId = getParameterAsString(GroupProfile.ID);
137: Group group = getGroupManager().getGroup(groupId);
138: if (group == null) {
139: throw new RuntimeException("Group [" + groupId
140: + "] not found.");
141: }
142: return group;
143: }
144:
145: }
|