/*
 * $Id: ClusterEditCategoryViewForm.java,v 1.1 2005/06/23 00:58:30 aram Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterEditCategoryViewForm.java,v $
 * Revision 1.1  2005/06/23 00:58:30  aram
 * + Initiale Version
 *
 */

package vkc.view.form;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

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

import vkc.clusterer.ClusteringItem;
import vkc.clusterer.Clustern;

public class ClusterEditCategoryViewForm extends DynaForm {
    private boolean planeHierarchy = false;
    private boolean refreshTreeView = false;
    
    private ClusteringItem category = null;
    
    private Integer categoryId = null;
    
    private String name = "";
    private String description = "";
    
    private HashMap categories = new HashMap();

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

	    planeHierarchy = false;
	    refreshTreeView = false;

	    category = null;

	    categoryId = null;
	    
	    name = "";
	    description = "";
	    
	    categories = (HashMap)request.getSession().getAttribute(Clustern.CLUSTER_MAP_NAME);
	    
	    if (categories == null) {
		    categories = new HashMap();
	    }
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
        setPlaneHierarchy(request.getParameter("planeHierarchy") != null);

		if (request.getParameter("action.apply") != null) {
			if (isEmpty(getName())) {
				errors.add("name", new ActionError("error.category.name"));
			}
			if (getCategory()==null) {
                errors.add("name", new ActionError("error.category.exist"));
            }
		}
			
		return errors;
    }

    public ClusteringItem getCategory() {
        if (category == null) {
            if (getCategoryId() != null) {
                category = (ClusteringItem)categories.get(getCategoryId());
            }
        }
        
        return category;
    }
    
    public Integer getCategoryId() {
        return categoryId;
    }

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

    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public boolean isPlaneHierarchy() {
        return planeHierarchy;
    }
    
    public void setPlaneHierarchy(boolean planeHierarchy) {
        this.planeHierarchy = planeHierarchy;
    }

    public boolean isRefreshTreeView() {
        return refreshTreeView;
    }
    
    public void setRefreshTreeView(boolean refreshTreeView) {
        this.refreshTreeView = refreshTreeView;
    }
}