/*
 * $Id: CopyAdminRightsAction.java,v 1.3 2007/10/15 06:29:46 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 */

package vkc.administration.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.administration.form.CopyAdminRightsForm;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.util.StrutsUtils;
import vkc.view.WebShare;

public final class CopyAdminRightsAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/copyAdminRights.jsp");
    private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm f, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) 
    throws Exception {
        CopyAdminRightsForm form = (CopyAdminRightsForm) f;
        VKCUser user = form.getUser();

        if (!user.hasAdminPrivileges()) {
            request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");
            
            return INFO_FORWARD;
        }

        if (request.getParameter("action.copy.adminrights") != null) {
            return handleCopyAdminRightsAction(form, request);
        } else if (request.getParameter("action.back") != null) {
            return handleBackAction(form, request);
        } else {
            return handleinitializeAction(form, request);
        }

    }

    private ActionForward handleinitializeAction(CopyAdminRightsForm form, HttpServletRequest request) {
        return DEFAULT_FORWARD;
    }

    private ActionForward handleCopyAdminRightsAction(CopyAdminRightsForm form, HttpServletRequest request) throws Exception {
        VKCUser newOwner = VKCUserSerializer.retrieveUser(form.getSelectedUserId());
        VKCUser oldOwner = VKCUserSerializer.retrieveUser(form.getUserId());

        newOwner.setManageOwnUsers(newOwner.getManageOwnUsers()||oldOwner.getManageOwnUsers());
        newOwner.setManageOwnGroups(newOwner.getManageOwnGroups()||oldOwner.getManageOwnGroups());
        newOwner.setManageOwnProxies(newOwner.getManageOwnProxies()||oldOwner.getManageOwnProxies());
        newOwner.setManageOwnUsersSystemViews(newOwner.isManageOwnUsersSystemViews()||oldOwner.isManageOwnUsersSystemViews());
        newOwner.setManageVirtualDocumentQueries(newOwner.isManageVirtualDocumentQueries()||oldOwner.isManageVirtualDocumentQueries());
        VKCUserSerializer.saveUser(newOwner);

        VKCUserSerializer.changeUsersOwner(oldOwner.getUserId(), newOwner.getUserId(), form.getUser().getUserId());
        VKCGroupSerializer.changeGroupsOwner(oldOwner.getUserId(), newOwner.getUserId(), form.getUser().getUserId());

        StrutsUtils.addInfoMessage(request, "vkc.administration.copyAdminRights.succeeded", new String[] {newOwner.getLastnameLabel()});

        return DEFAULT_FORWARD;
    }

    private ActionForward handleBackAction(CopyAdminRightsForm form, HttpServletRequest request) {
        return new ActionForward(form.getRedirectPath(), true);
    }
}