/*
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: LicenseAction.java,v $
 * Revision 1.1  2006/11/06 10:56:26  korowa
 * Initiale Version
 *
 *
 *
 */
package vkc.administration.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.administration.form.LicenseForm;
import vkc.db.entities.VKCUser;
import vkc.view.WebShare;

public class LicenseAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward(
			"/templates/${template.name}/administration/License.jsp");

	private ActionForward INFO_FORWARD = new ActionForward(
			"/templates/${template.name}/common/InfoPage.jsp");

	public ActionForward execute(ActionMapping mapping, ActionForm f,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LicenseForm form = (LicenseForm) f;
		VKCUser user = form.getUser();

		if (!user.hasAdminPrivileges()) {
			request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");

			return INFO_FORWARD;
		}

		if (request.getParameter("action.edit.news") != null) {
			return handleEditNewsAction(form, request);
		} else if (request.getParameter("action.show.license") != null) {
			return handleShowLicenseAction(form, request);
		} else if (request.getParameter("action.hide.license") != null) {
			return handleHideLicenseAction(form, request);
		}else if (request.getParameter("action.show.language") != null) {
			return handleShowLanguageAction(form, request);
		} else if (request.getParameter("action.hide.language") != null) {
			return handleHideLanguageAction(form, request);
		}
		return handleInitializeAction(form, request);
	}

	private ActionForward handleEditNewsAction(LicenseForm form,
			HttpServletRequest request) {
		return DEFAULT_FORWARD;
	}

	private ActionForward handleInitializeAction(LicenseForm form,
			HttpServletRequest request) {
		return DEFAULT_FORWARD;
	}

	private ActionForward handleShowLicenseAction(LicenseForm form,
			HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getSelectedIds()));
		ids.add(form.getSelectedId());
		form.setSelectedIds((Integer[]) ids.toArray(new Integer[0]));

		return new ActionForward(
				"/templates/${template.name}/administration/License.jsp?anchor="
						+ form.getSelectedId(), false);
	}

	private ActionForward handleHideLicenseAction(LicenseForm form,
			HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getSelectedIds()));
		ids.remove(form.getSelectedId());
		form.setSelectedIds((Integer[]) ids.toArray(new Integer[0]));

		return new ActionForward(
				"/templates/${template.name}/administration/License.jsp?anchor="
						+ form.getSelectedId(), false);
	}
	
	private ActionForward handleShowLanguageAction(LicenseForm form,
			HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getSelectedLanguages()));
		ids.add(form.getSelectedLanguageId());
		form.setSelectedLanguages((Integer[]) ids.toArray(new Integer[0]));

		return new ActionForward(
				"/templates/${template.name}/administration/License.jsp?anchor="
						+ form.getSelectedLanguageId(), false);
	}

	private ActionForward handleHideLanguageAction(LicenseForm form,
			HttpServletRequest request) {
		List ids = new ArrayList(Arrays.asList(form.getSelectedLanguages()));
		ids.remove(form.getSelectedLanguageId());
		form.setSelectedLanguages((Integer[]) ids.toArray(new Integer[0]));

		return new ActionForward(
				"/templates/${template.name}/administration/License.jsp?anchor="
						+ form.getSelectedLanguageId(), false);
	}
}