/*
 * $Id: ChangeDocumentPublisherForm.java,v 1.3 2005/09/15 23:16:43 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ChangeDocumentPublisherForm.java,v $
 * Revision 1.3  2005/09/15 23:16:43  marbobo
 * + Sprachabhängiges Sortieren implementiert
 *
 * 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
 *
 * Revision 1.3  2005/07/06 21:44:27  jonyp
 * *** empty log message ***
 *
 * Revision 1.2  2005/07/06 13:57:20  jonyp
 * changes due to new serializer function "changePublisherId"
 *
 * Revision 1.1  2005/07/06 13:33:00  jonyp
 * first strike: does not work !
 *
 */

package vkc.administration.form;

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

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

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.serializer.VKCUserSerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.util.DocumentUtils;
import vkc.util.VKCStringUtils;
import vkc.view.PageId;
import vkc.view.bean.UserInfo;
import vkc.view.form.BasicActionForm;


public final class ChangeDocumentPublisherForm extends BasicActionForm {

	private Integer selectedNewPublisherId = null;
	private Integer documentId = null;
	private VKCDocument document = null;	
	private VKCCategory documentCategory = null;
	
    private Integer categoryId 		= null;
    private Integer selectedIndex 	= new Integer( -1 );
	private Integer sourcePageId 	= PageId.CATEGORY_VIEW;
	private Integer subTargetId 	= null;
	private Integer targetId 		= null;


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

		selectedNewPublisherId = null;
		documentId = 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(getDocument().getPublisherId()); 
		Collections.sort(users, c);
	    
		return users;
	}
	
    public Integer getDocumentId() {
        return documentId;
    }

    public void setDocumentId(Integer documentId) {
        this.documentId = documentId;
    }
    
    public Integer getSelectedNewPublisherId() {
        return selectedNewPublisherId;
    }

    public void setSelectedNewPublisherId(Integer selectedPublisherId) {
        this.selectedNewPublisherId = selectedPublisherId;
    }
    
    public VKCDocument getDocument() throws DBException {
        if (document == null) {
            document = VKCDocumentSerializer.retrieveDocument(getDocumentId());
        }
            
        return document;
    }
    
    public void setDocument(VKCDocument document) {
        this.document = document;
    }
    
    public Integer getOldPublisherId() throws DBException {
        return getDocument().getPublisherId();
    }
    
    public String getOldPublisherLastnameLabel() throws DBException {
        return VKCUserSerializer.retrieveUserInfo(getDocument().getPublisherId()).getLastnameLabel();
    }
    
	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(Integer selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public Integer getSourcePageId() {
		return sourcePageId;
	}

	public void setSourcePageId(Integer sourcePageId) {
		this.sourcePageId = sourcePageId;
	}

	public Integer getSubTargetId() {
		return subTargetId;
	}

	public void setSubTargetId(Integer subTargetId) {
		this.subTargetId = subTargetId;
	}

	public Integer getTargetId() {
		return targetId;
	}

	public void setTargetId(Integer targetId) {
		this.targetId = targetId;
	}
	
	public VKCCategory getDocumentCategory() throws DBException {
        if ( documentCategory == null && getDocument() != null ) {
            List list = DocumentUtils.getDocumentVersions( getDocument().getDocumentId() );
            
            for ( Iterator iter = list.iterator() ; iter.hasNext() ; ) {
                VKCDocument doc = (VKCDocument)iter.next();
                documentCategory = doc.getCategory();
                
                if ( documentCategory != null ) {
                    break;
                }
            }
        }
        
        return documentCategory;
    }
}
