/*
 * Copyright (c) 2004 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Created: Apr 1, 2004
 */
package com.n2bb.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.n2bb.action.AbstractAction;
import com.n2bb.util.ActionConstants;
import com.n2bb.util.N2bbException;
import com.n2bb.util.N2bbSettings;
import com.tandbergtv.workflow.resourcemanager.ResourceManagement;
import com.tandbergtv.workflow.resourcemanager.ResourceManager;
import com.tandbergtv.workflow.resourcemanager.entities.Resource;
import com.tandbergtv.workflow.resourcemanager.entities.ResourceState;
/**
 * Modify users as defined in security framework.
 *
 * @author kmatsuoka
 * @version $Id: ModifyUsersAction.java,v 1.7 2007/06/22 21:44:50 vjakobac Exp $
 */ 
public class ModifyUsersAction extends AbstractAction {

    protected ActionForward executeAction(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	List<String> unChangedUsers = new ArrayList<String>();
        ActionErrors errors = new ActionErrors();
        log.debug("enter");
        String method = request.getParameter("methodName");
        if (method == null) {
            method = "";
        }
        String[] ids = request.getParameterValues("ids");
        if (ids != null) {
            if (method.equals(ActionConstants.delete)) {
                delete(ids, errors,unChangedUsers);
                log.debug("Undeleted users size-->"+unChangedUsers.size());
                if (unChangedUsers.size() > 0){
                	request.setAttribute(ActionConstants.UNDELETED_RESOURCES,unChangedUsers);
                }
            }
            else if (method.equals(ActionConstants.changeState)) {
                changeState(ids, errors, unChangedUsers);
                log.debug("Unchanged users size-->"+unChangedUsers.size());
                if (unChangedUsers.size() > 0){
                	request.setAttribute(ActionConstants.UNCHANGED_RESOURCES,unChangedUsers);
                }
            }
        }
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
        }
        return mapping.findForward("success");
    }

    private void delete(String[] ids, ActionErrors errors, List<String> unDeletedUsers) {
        for (int i = 0; i < ids.length; i++) {
            String userName = ids[i];
            log.debug("name... " + userName);
            if (userName.equalsIgnoreCase(N2bbSettings.ADMIN_USER)) {
                continue;
            }
            try {
            	ResourceManagement resourceManagement = ResourceManager.getInstance();
            	if (resourceManagement.getResourceByUser(userName) == null) {
            		UserManager.getInstance().deleteUser(userName);
            		
            	} else {
            		unDeletedUsers.add(userName);
            	}
            	
            }
            catch (N2bbException e) {
                errors.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError("error.listUsersAction.delete", userName));
                //errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getErrorCode()));
            }
        }
    }

    private void changeState(String[] ids, ActionErrors errors, List<String> unChangedUsers) {
        for (int i = 0; i < ids.length; i++) {
            String userName = ids[i];
            log.debug("name... " + userName);
            if (userName.equalsIgnoreCase(N2bbSettings.ADMIN_USER)) {
                continue;
            }
            try {
            	ResourceManagement resourceManagement = ResourceManager.getInstance();
            	Resource resource = resourceManagement.getResourceByUser(userName);
            	if (resource != null && 
            			(resource.getAdministrationState() != ResourceState.OFFLINE ||
            					(resource.getOperationalState() != ResourceState.INVALID &&
            					resource.getOperationalState() != ResourceState.INACTIVE &&
            					resource.getOperationalState() != ResourceState.OFFLINE))){
            		unChangedUsers.add(userName);
            	} else {
            		UserManager.getInstance().toggleUserStatus(userName);
     	      	}
            }
            catch (N2bbException e) {
                errors.add(ActionErrors.GLOBAL_ERROR,
                        new ActionError("error.listUsersAction.changeStatus", userName));
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getErrorCode()));
            }
        }
    }

}



