/*
 * $Id: ChangeCreatorPublishershipForm.java,v 1.4 2005/09/15 23:16:43 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ChangeCreatorPublishershipForm.java,v $
 * Revision 1.4  2005/09/15 23:16:43  marbobo
 * + Sprachabhängiges Sortieren implementiert
 *
 * Revision 1.3  2005/08/10 12:14:55  marbobo
 * + RedirectPath korrigiert
 *
 * Revision 1.2  2005/08/09 22:43:27  marbobo
 * - Unnötige Import-Anweisungen entfernt
 *
 * Revision 1.1  2005/08/09 19:00:26  jonyp
 * support for change of publisher
 *
 */

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.helper.UserHelper;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;
import vkc.view.bean.UserInfo;
import vkc.view.bean.UserStatistics;
import vkc.view.bean.VKCQuotaInfo;
import vkc.view.form.BasicActionForm;

public final class ChangeCreatorPublishershipForm extends BasicActionForm {
	private Integer selectedNewPublisherId = null;
    private Integer userId = null;

    private String redirect = "";
    
    private UserStatistics userStatistics = null;

    private VKCQuotaInfo userQuota = null;
    

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

		selectedNewPublisherId = null;
	    userId = null;

	    redirect = "";
	    
	    userStatistics = null;

	    userQuota = null;
	}

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

	public List getNewPublisherList() throws DBException {
		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);
			}
		};

		List users = VKCUserSerializer.retrieveValidNewPublisherInfos(getUserId()); 
		Collections.sort(users, c);
	    
		return users;
	}
    
    public Integer getSelectedNewPublisherId() {
        return selectedNewPublisherId;
    }

    public void setSelectedNewPublisherId(Integer selectedPublisherId) {
        this.selectedNewPublisherId = selectedPublisherId;
    }
    
    public Integer getOldPublisherId() {
        return getUserId();
    }
    
    public String getOldPublisherLastnameLabel() throws DBException {
        return VKCUserSerializer.retrieveUserInfo(getUserId()).getLastnameLabel();
    }
	
	public VKCQuotaInfo getUserQuota() throws DBException {
        if (userQuota == null) {
            userQuota = WebShare.database.getQuotaInfo(getUserId(), true);
        }

        return userQuota;
    }

    public UserStatistics getUserStatistics() throws DBException {
        if (userStatistics == null) {
            userStatistics = UserHelper.getUserStatistics(getUserId());
        }

        return userStatistics;
    }
	
	public Integer getUserId() {
		return userId;
	}

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