/*
 * $Id: NewLicenseAction.java,v 1.5 2007/08/10 12:22:50 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: NewLicenseAction.java,v $
 * Revision 1.5  2007/08/10 12:22:50  marbobo
 * + Beim anlegen einer Licenz nicht mehr aktivieren
 *
 * Revision 1.4  2007/08/09 14:38:54  marbobo
 * - 2 falsch zugeordnete LDAP- Parameter entfernt
 * + Löschen und Aktivieren von Lizenzen hinzugefügt
 *
 * Revision 1.3  2007/08/08 12:14:46  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 */
package vkc.administration.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
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.NewLicenseForm;
import vkc.db.entities.VKCLanguage;
import vkc.db.entities.VKCLicense;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCLicenseSerializer;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.util.StringUtils;
import vkc.view.WebShare;

public class NewLicenseAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/newLicense.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 {
        NewLicenseForm form = (NewLicenseForm) f;

        VKCUser user = form.getUser();

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

            return INFO_FORWARD;
        }

        if (request.getParameter("action.save.license") != null) {
            return handleSaveLicenseAction(form, request);
        } else if (request.getParameter("action.back") != null) {
            return new ActionForward("/licenseList.do");
        } 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.toggle.tinymcetoolbar") != null) {
            return DEFAULT_FORWARD;
        }

        return handleInitializeAction(form, request);
    }

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

    private ActionForward handleSaveLicenseAction(NewLicenseForm form, HttpServletRequest request) throws DBException {
        VKCUser user = form.getUser();
        VKCLicense license = new VKCLicense();

        for (int i = 0; i < form.getLanguages().size(); i++) {
            VKCLanguage language = (VKCLanguage)form.getLanguages().get(i);
            String text = (String)form.getDynaValue("languageId_" + language.getLanguageId());
            
            if (!StringUtils.isEmpty(text)) {
                license.setLicense(language.getLanguageId(), text);
            }
            
        }
        
        Date f = DateUtils.parseDate(form.getValidFrom());
        
        if (f != null) {
            license.setValidFrom(new Timestamp(f.getTime()));
        } else {
            license.setValidFrom(null);
        }
        
        f = DateUtils.parseDate(form.getValidUntil());
        
        if (f != null) {
            license.setValidUntil(new Timestamp(f.getTime()));
        } else {
            license.setValidUntil(null);
        }

        license.setCreatorId(user.getUserId());
        license.setCreated(new Timestamp(System.currentTimeMillis()));
        license.setActive(false);

        VKCLicenseSerializer.saveLicense(license);

        return new ActionForward("/licenseList.do");
    }

    private ActionForward handleShowLicenseAction(NewLicenseForm 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/newLicense.jsp?anchor=" + form.getSelectedId(), false);
    }

    private ActionForward handleHideLicenseAction(NewLicenseForm 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/newLicense.jsp?anchor=" + form.getSelectedId(), false);
    }
}