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

package vkc.administration.form;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.util.VKCStringUtils;
import vkc.view.bean.UserInfo;
import vkc.view.form.BasicActionForm;

public final class CopyAdminRightsForm extends BasicActionForm {
    private Integer selectedUserId = null;

    private Integer userId = null;

    private List validUsers = null;

    private String redirect = "";

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping, request);

        selectedUserId = null;
        userId = null;
        validUsers = null;
        redirect = "";
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();

        return errors;
    }

    public List getValidUsers() throws DBException {
        if (validUsers == null) {
            final Collator collator = Collator.getInstance(getUser().getLanguage().getLocale());

            Comparator c = new Comparator() {
                public int compare(Object o1, Object o2) {
                    UserInfo u1 = (UserInfo) o1;
                    UserInfo u2 = (UserInfo) o2;

                    CollationKey key1 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u1.getLastnameLabel()));
                    CollationKey key2 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u2.getLastnameLabel()));

                    return key1.compareTo(key2);
                }
            };

            validUsers = VKCUserSerializer.retrieveValidUserInfos();
            validUsers.remove(new UserInfo(getUserId()));
            Collections.sort(validUsers, c);
        }

        return validUsers;
    }

    public Integer getSelectedUserId() {
        return selectedUserId;
    }

    public void setSelectedUserId(Integer selectedUserId) {
        this.selectedUserId = selectedUserId;
    }

    public void setUserId(Integer i) {
        userId = i;
    }

    public Integer getUserId() {
        return userId;
    }

    public UserInfo getUserInfo() throws DBException {
        return VKCUserSerializer.retrieveUserInfo(getUserId());
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

    public String getRedirectPath() {
        return "/editUser.do?initialize=true&userId=" + getUserId() + "&redirect=" + getRedirect();
    }
}