/*
 * $Id: LicenseListForm.java,v 1.2 2007/08/09 14:38:52 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: LicenseListForm.java,v $
 * Revision 1.2  2007/08/09 14:38:52  marbobo
 * - 2 falsch zugeordnete LDAP- Parameter entfernt
 * + Löschen und Aktivieren von Lizenzen hinzugefügt
 *
 * Revision 1.1  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.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCLanguage;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.db.serializer.VKCLicenseSerializer;
import vkc.exception.DBException;
import vkc.util.StringUtils;
import vkc.view.form.DynaForm;

public class LicenseListForm extends DynaForm {
    private List licenses = null;

    private Integer selectedId = new Integer(-1);
    private Integer selectedLanguageId = new Integer(-1);
    private Integer[] selectedIds = new Integer[0];

    private Integer[] showLicenses = new Integer[0];

    private List languages = null;

    private String selectedCmdAction = "noaction";

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

    public List getLicenses() throws DBException {
        if (licenses == null) {
            licenses = VKCLicenseSerializer.retrieveLicenses();
        }

        return licenses;
    }

    public void setLicenses(List licenses) {
        this.licenses = licenses;
    }

    public Integer[] getShowLicenses() {
        return showLicenses;
    }

    public void setShowLicenses(Integer[] showLicense) {
        this.showLicenses = showLicense;
    }

    public Integer getSelectedId() {
        return selectedId;
    }

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

    public Integer getSelectedLanguageId() {
        return selectedLanguageId;
    }

    public void setSelectedLanguageId(Integer selectedLanguageId) {
        this.selectedLanguageId = selectedLanguageId;
    }

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

    public boolean getShowLicenseLanguage(String id) {
        return isContainsKey(id);
    }

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

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

    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;
    }

    public String getSelectedCmdAction() {
        return selectedCmdAction;
    }

    public void setSelectedCmdAction(String s) {
        selectedCmdAction = StringUtils.normalize(s);
    }
}