/*
 * $Id: LoginLicenseFramesetAction.java,v 1.1 2007/08/08 12:14:51 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: LoginLicenseFramesetAction.java,v $
 * Revision 1.1  2007/08/08 12:14:51  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 */
package vkc.view.action;

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.db.entities.VKCLanguage;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.view.WebShare;
import vkc.view.form.LoginLicenseFramesetForm;

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

/**
 * Klasse zum Darstellen der aktuellen License
 *
 * @author marbobo
 */
public final class LoginLicenseFramesetAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/common/loginLicenseFrameset.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 {
        if (Integer.parseInt(WebShare.getSysParameterValue("LICENSE", "EnableLicensemanagement", "0")) == 0) {
            request.setAttribute( WebShare.INFO_KEY, "vkc.info.action_not_allowed" );
            
            return INFO_FORWARD;
        }

		LoginLicenseFramesetForm form = (LoginLicenseFramesetForm) f;

        VKCUser user = form.getUser();
        
        if ( user == null ) {
    		return new ActionForward( WebShare.getLoginPage(), true );
        }

        // Sprache wechseln
        if (user.isGuestUser()) {
            if (request.getLocale() == null) {
                VKCLanguage lang = VKCLanguageSerializer.getDefaultLanguage();
                user.setLanguage(lang);
                setLocale(request, lang.getLocale());
            } else {
                VKCLanguage lang = VKCLanguageSerializer.getLanguageByShortName(request.getLocale().getLanguage());
                
                if (lang != null) {
                    user.setLanguage(lang);
                    setLocale(request, lang.getLocale());
                } else {
                    lang = VKCLanguageSerializer.getDefaultLanguage();
                    user.setLanguage(lang);
                    setLocale(request, lang.getLocale());
                }
            }
        } else {
            setLocale( request, user.getLanguage().getLocale() );
        }

        return DEFAULT_FORWARD;
        
	}
}