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

import java.sql.Timestamp;
import java.util.HashMap;

import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.view.bean.UserInfo;

public class VKCLicense {
    private Integer licenseId;

    private HashMap licenses = new HashMap();

    private Timestamp validFrom;

    private Timestamp validUntil;

    private Timestamp created;

    private Integer creatorId;
    
    private boolean active = false;
    
    private Timestamp accepted;

    public VKCLicense() {
    }

    public Timestamp getCreated() {
        return created;
    }

    public Integer getCreatorId() {
        return creatorId;
    }

    public Integer getLicenseId() {
        return licenseId;
    }

    public Timestamp getValidFrom() {
        return validFrom;
    }

    public Timestamp getValidUntil() {
        return validUntil;
    }

    public void setCreated(Timestamp timestamp) {
        created = timestamp;
    }

    public void setCreatorId(Integer integer) {
        creatorId = integer;
    }

    public void setLicenseId(Integer integer) {
        licenseId = integer;
    }

    public void setValidFrom(Timestamp timestamp) {
        validFrom = timestamp;
    }

    public void setValidUntil(Timestamp timestamp) {
        validUntil = timestamp;
    }
    
    public final void setLicense(Integer languageId, String text) {
        if (languageId != null) {
            licenses.put(languageId, text);
        }
    }
    
    public final String getLicense(Integer languageId) {
        return (String)licenses.get(languageId);
    }
    
    public final String getLicense(int languageId) {
        return (String)licenses.get(new Integer(languageId));
    }
    
    public final boolean isEmpty() {
        return licenses.isEmpty();
    }
    
    public final int getSize() {
        return licenses.keySet().size();
    }

    public final boolean isActive() {
        return active;
    }
    public final void setActive(boolean active) {
        this.active = active;
    }

    public final Timestamp getAccepted() {
        return accepted;
    }
    public final void setAccepted(Timestamp accepted) {
        this.accepted = accepted;
    }

    public UserInfo getCreator() {
        try {
            return VKCUserSerializer.retrieveUserInfo(getCreatorId());
        } catch (DBException e) {
            return null;
        }
    }

    public boolean equals(Object obj) {
        if (obj == null || obj instanceof VKCLicense == false) {
            return false;
        }

        VKCLicense license = (VKCLicense)obj;
        
        if (getLicenseId() != null && license.getLicenseId() != null) {
            return getLicenseId().equals(license.getLicenseId());
        }
        
        return super.equals(obj);
    }
    
    public boolean isValid() {
        Timestamp today = new Timestamp(System.currentTimeMillis());

        if (isActive()) {
            if (getValidFrom() != null && getValidUntil() == null) {
                if (getValidFrom().before(today)) {
                    return isActive();
                }

                return false;
            } else if (getValidFrom() == null && getValidUntil() != null) {
                if (getValidUntil().after(today)) {
                    return isActive();
                }

                return false;
            } else if (getValidFrom() != null && getValidUntil() != null) {
                if (getValidFrom().before(today) && getValidUntil().after(today)) {
                    return isActive();
                }

                return false;
            } else {
                return isActive();
            }
        }

        return isActive();
    }
}