/*
 * $Id: LicenseListAction.java,v 1.2 2007/08/09 14:38:53 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: LicenseListAction.java,v $
 * Revision 1.2  2007/08/09 14:38:53  marbobo
 * - 2 falsch zugeordnete LDAP- Parameter entfernt
 * + Löschen und Aktivieren von Lizenzen hinzugefügt
 *
 * Revision 1.1  2007/08/08 12:14:45  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 */
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.LicenseListForm;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCLicenseSerializer;
import vkc.exception.DBException;
import vkc.view.WebShare;

public class LicenseListAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/licenseList.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 {
        LicenseListForm form = (LicenseListForm) 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.license") != null) {
            return handleEditLicenseAction(form, request);
        } else if (request.getParameter("action.delete.licenses") != null) {
            return handleDeleteLicensesAction(form, request);
        } else if (request.getParameter("action.activate.license") != null) {
            return handleActivateLicenseAction(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.license.language") != null) {
            return handleShowLicenseLanguageAction(form, request);
        } else if (request.getParameter("action.hide.license.language") != null) {
            return handleHideLicenseLanguageAction(form, request);
        }
        
        return handleInitializeAction(form, request);
    }

    private ActionForward handleDeleteLicensesAction(LicenseListForm form, HttpServletRequest request) throws DBException {
        Integer[] selectedIds = form.getSelectedIds();
        
        for (int i = 0; i < selectedIds.length; i++) {
            VKCLicenseSerializer.deleteLicense(selectedIds[i]);
        }
        
        form.setSelectedIds(new Integer[0]);

        return DEFAULT_FORWARD;
    }

    private ActionForward handleActivateLicenseAction(LicenseListForm form, HttpServletRequest request) throws DBException {
        Integer[] selectedIds = form.getSelectedIds();
        
        for (int i = 0; i < selectedIds.length; i++) {
            VKCLicenseSerializer.setLicenseActive(selectedIds[i]);
        }
        
        form.setSelectedIds(new Integer[0]);
        
        return DEFAULT_FORWARD;
    }

    private ActionForward handleEditLicenseAction(LicenseListForm form, HttpServletRequest request) {
        return DEFAULT_FORWARD;
    }

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

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

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

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

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

    private ActionForward handleShowLicenseLanguageAction(LicenseListForm form, HttpServletRequest request) {
        String key = form.getSelectedId() + "_" + form.getSelectedLanguageId();
        form.setDynaValue(key, "true");
        
        return new ActionForward("/templates/${template.name}/administration/licenseList.jsp?anchor=" + key, false);
    }

    private ActionForward handleHideLicenseLanguageAction(LicenseListForm form, HttpServletRequest request) {
        String key = form.getSelectedId() + "_" + form.getSelectedLanguageId();
        form.removeDynaValue(key);

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