/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.ac.usecases;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.lenya.ac.Group;
import org.apache.lenya.ac.Groupable;
import org.apache.lenya.ac.User;
import org.apache.lenya.cms.usecase.UsecaseException;

/**
 * Usecase to change the members of a group.
 * 
 * @version $Id: GroupMembers.java 407305 2006-05-17 16:21:49Z andreas $
 */
public class GroupMembers extends AccessControlUsecase {

    protected static final String GROUP_USERS = "groupUsers";
    protected static final String OTHER_USERS = "otherUsers";
    protected static final String ADD = "add";
    protected static final String REMOVE = "remove";
    protected static final String GROUP_USER = "groupUser";
    protected static final String OTHER_USER = "otherUser";

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
     */
    protected void initParameters() {
        super.initParameters();
        
        Groupable[] members = getGroup().getMembers();
        List groupUsers = new ArrayList();
        for (int i = 0; i < members.length; i++) {
            if (members[i] instanceof User) {
                groupUsers.add(members[i]);
            }
        }
        setParameter(GROUP_USERS, groupUsers);

        User[] allUsers = getUserManager().getUsers();

        List otherUsers = new ArrayList();
        for (int i = 0; i < allUsers.length; i++) {
            if (!groupUsers.contains(allUsers[i])) {
                otherUsers.add(allUsers[i]);
            }
        }
        setParameter(OTHER_USERS, otherUsers);
        
        setExitParameter(GroupProfile.ID, getParameterAsString(GroupProfile.ID));
    }

    /**
     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
     */
    protected void doExecute() throws Exception {
        super.doExecute();

        final Group group = getGroup();
        group.removeAllMembers();

        List groupUsers = (List) getParameter(GROUP_USERS);
        for (Iterator i = groupUsers.iterator(); i.hasNext();) {
            User user = (User) i.next();
            group.add(user);
            user.save();
        }
    }

    /**
     * @see org.apache.lenya.cms.usecase.Usecase#advance()
     */
    public void advance() throws UsecaseException {
        super.advance();

        String add = getParameterAsString(ADD);
        String remove = getParameterAsString(REMOVE);
        if (add != null || remove != null) {

            List groupUsers = (List) getParameter(GROUP_USERS);
            List otherUsers = (List) getParameter(OTHER_USERS);

            if (add != null) {
                String userId = getParameterAsString(OTHER_USER);
                if (userId != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("add user [" + userId + "]");
                    }
                    User user = getUserManager().getUser(userId);
                    groupUsers.add(user);
                    otherUsers.remove(user);
                }
            }

            if (remove != null) {
                String userId = getParameterAsString(GROUP_USER);
                if (userId != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("remove user [" + userId + "]");
                    }
                    User user = getUserManager().getUser(userId);
                    otherUsers.add(user);
                    groupUsers.remove(user);
                }
            }

            deleteParameter(ADD);
            deleteParameter(REMOVE);
            deleteParameter(GROUP_USER);
            deleteParameter(OTHER_USER);
        }

    }
    
    protected Group getGroup() {
        String groupId = getParameterAsString(GroupProfile.ID);
        Group group = getGroupManager().getGroup(groupId);
        if (group == null) {
            throw new RuntimeException("Group [" + groupId + "] not found.");
        }
        return group;
    }

}
@@ -880,16 +880,42 @@
 ayList;%0A
+import java.util.HashSet;%0A
 import j
@@ -955,16 +955,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -2745,39 +2745,314 @@
     
-final Group group = getGroup();
+Set usersToSave = new HashSet();%0A        %0A        final Group group = getGroup();%0A        Groupable%5B%5D members = group.getMembers();%0A        for (int i = 0; i %3C members.length; i++) %7B%0A            if (members%5Bi%5D instanceof User) %7B%0A                usersToSave.add(members%5Bi%5D);%0A            %7D%0A        %7D%0A        
 %0A   
@@ -3270,32 +3270,32 @@
 roup.add(user);%0A
-
             user
@@ -3294,16 +3294,150 @@
     user
+sToSave.add(user);%0A        %7D%0A        %0A        for (Iterator i = usersToSave.iterator(); i.hasNext(); ) %7B%0A            ((User) i.next())
 .save();

Save users after they have been removed from a group. Fixes bug 46111.