/*
 * $Id: NewLicenseForm.java,v 1.4 2007/08/09 14:38:52 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: NewLicenseForm.java,v $
 * Revision 1.4  2007/08/09 14:38:52  marbobo
 * - 2 falsch zugeordnete LDAP- Parameter entfernt
 * + Löschen und Aktivieren von Lizenzen hinzugefügt
 *
 * Revision 1.3  2007/08/08 12:14:58  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 */
package vkc.administration.form;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCLanguage;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.util.DateUtils;
import vkc.util.StringUtils;
import vkc.view.form.DynaForm;

public class NewLicenseForm extends DynaForm {
    private String validFrom = "";

    private String validUntil = "";

    Integer[] selectedIds = new Integer[0];

    Integer selectedId = new Integer(-1);

    List languages = null;

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping, request);
        validFrom = "";
        validUntil = "";
        selectedIds = new Integer[0];
        selectedId = new Integer(-1);
        languages = null;
    }

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

        if (request.getParameter("action.save.license") != null) {
            for (int i = 0; i < getLanguages().size(); i++) {
                VKCLanguage language = (VKCLanguage)getLanguages().get(i);
                String name = getResources().getMessage(getUser().getLanguage().getLocale(), language.getName_key());
                
                if (isEmpty((String)getDynaValue("languageId_" + language.getLanguageId()))) {
                    errors.add("languageText_" + language.getLanguageId(), new ActionError("vkc.administration.license.errors.licensetext", name));
                }
            }
            
            Date f = DateUtils.parseDate( getValidFrom() );
            Date u = DateUtils.parseDate( getValidUntil() );

            if (!isEmpty(getValidFrom())) {
                if (f == null) {
                    errors.add("validfrom", new ActionError("vkc.administration.license.errors.validfrom"));
                }
            }

            if (!isEmpty(getValidUntil())) {
                if (u == null) {
                    errors.add("validuntil", new ActionError("vkc.administration.license.errors.validuntil"));
                }
            }

            if (f != null && u != null && f.after(u)) {
                errors.add("daterange", new ActionError("error.date.range"));
            }
        }

        return errors;
    }

    public String getValidFrom() {
        return validFrom;
    }

    public String getValidUntil() {
        return validUntil;
    }

    public void setValidFrom(String s) {
        validFrom = StringUtils.normalize(s);
    }

    public void setValidUntil(String s) {
        validUntil = StringUtils.normalize(s);
    }

    public Integer getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(Integer selectedId) {
        this.selectedId = selectedId;
    }

    public Integer[] getSelectedIds() {
        return selectedIds;
    }

    public void setSelectedIds(Integer[] selectedIds) {
        this.selectedIds = selectedIds;
    }

    public boolean getShowLicenseForLanguage(int i) {
        return Arrays.asList(getSelectedIds()).contains(new Integer(i));
    }

    public List getLanguages() {
        if (languages == null) {
            languages = VKCLanguageSerializer.getLanguages();
        
            final Collator collator = Collator.getInstance(getUser().getLanguage().getLocale());
            
            final Comparator c = new Comparator() {
                public int compare(Object o1, Object o2) {
                    VKCLanguage l1 = (VKCLanguage)o1;
                    VKCLanguage l2 = (VKCLanguage)o2;

                    CollationKey key1 = collator.getCollationKey(getResources().getMessage(getUser().getLanguage().getLocale(), l1.getName_key()));
                    CollationKey key2 = collator.getCollationKey(getResources().getMessage(getUser().getLanguage().getLocale(), l2.getName_key()));
                    
                    return key1.compareTo(key2);
                }
            };
            
            Collections.sort(languages, c);
        }

        return languages;
    }
}