/*
 * $Id: ClusterResultSaveForm.java,v 1.2 2006/10/04 03:15:17 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ClusterResultSaveForm.java,v $
 * Revision 1.2  2006/10/04 03:15:17  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.1  2005/06/23 00:58:30  aram
 * + Initiale Version
 *
 */

package vkc.view.form;

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.VKCEntity;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.exception.DBException;
import vkc.procoba.project.ProjectViewForm;
import vkc.view.PageId;

public class ClusterResultSaveForm extends DynaForm {
    private boolean redirect = false;

    private Integer categoryId 				= VKCEntity.ID_UNDEFINED;
	private Integer sourcePageId 			= PageId.CATEGORY_VIEW;
	private Integer subTargetId 			= null;
	private Integer targetId 				= null;
	
	private VKCCategory category = null;
	
    public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

	    redirect = false;

	    categoryId 				= VKCEntity.ID_UNDEFINED;
		sourcePageId 			= PageId.CATEGORY_VIEW;
		subTargetId 			= null;
		targetId 				= null;

		category = null;
    }

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

		return errors;
    }

	public VKCCategory getCategory() throws DBException {
		if (category == null) {
			category = VKCCategorySerializer.retrieveCategory(getCategoryId());
		}

		return category;
	}
	
    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer i) {
        categoryId = i;
    }

	public Integer getSourcePageId() {
		return sourcePageId;
	}

	public void setSourcePageId(Integer i) {
		sourcePageId = i;
	}

	public Integer getSubTargetId() {
		return subTargetId;
	}

	public Integer getTargetId() {
		return targetId;
	}

	public void setSubTargetId(Integer i) {
		subTargetId = i;
	}

	public void setTargetId(Integer i) {
		targetId = i;
	}
    
	public boolean isRedirect() {
        return redirect;
    }
    
	public void setRedirect(boolean redirect) {
        this.redirect = redirect;
    }
	
	public String getRedirectPath() {
		String forwardURL = "";

		if (getSourcePageId().equals(PageId.PROJECT_VIEW)) {
			forwardURL = "/projectView.do?viewMode=" + ProjectViewForm.VIEW_MODE_FOLDER + "&projectId=" + getTargetId() + "&subfolderId=" + getSubTargetId();
			
			return forwardURL;
		} else if (getSourcePageId().equals(PageId.FUNCTIONAL_VIEW)) {
			forwardURL = "/functionalView.do?functionalViewId=" + getTargetId() + "&subfolderId=" + getSubTargetId();

			return forwardURL;
		} else if (getSourcePageId().equals(PageId.CATEGORY_VIEW)) {
			if (getTargetId() != null) {
				forwardURL = "/showCategory.do?categoryId=" + getTargetId();
			} else {
				forwardURL = "/showCategory.do?categoryId=" + getCategoryId();
			}

			return forwardURL;
		} else {
			return "/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID;
		}
	}
}