/*
 * $Id: UserInfo.java,v 1.35 2006/04/29 18:39:49 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: UserInfo.java,v $
 * Revision 1.35  2006/04/29 18:39:49  marbobo
 * + Refactoriengs und eine Ausgaben vereinheitlicht
 *
 * Revision 1.34  2005/11/24 01:26:04  marbobo
 * + Begrenzung von Fehlversuchen bei Passworteingabe und Sperrung von Benutzer eingebaut (beide sind Konfigurierbar)
 *
 * Revision 1.33  2005/02/16 16:00:29  jonyp
 * format things (displayUsername ...)
 *
 * Revision 1.32  2005/02/16 15:16:05  jonyp
 * changed order of display of getUsernameLabel method
 *
 * Revision 1.31  2005/02/15 09:26:50  bigmac
 * Refactorings
 *
 * Revision 1.30  2005/02/08 16:17:13  bigmac
 * Quotabehandlung überarbeitet
 *
 * Revision 1.29  2004/11/19 16:04:57  jonyp
 * bugfix for userrating
 *
 * Revision 1.28  2004/11/19 16:00:45  jonyp
 * added userrating
 *
 * Revision 1.27  2004/11/10 15:30:14  jonyp
 * added info field for externalServerId
 *
 * Revision 1.26  2004/11/05 01:59:14  marbobo
 * + isUserValid() optimiert
 * + Bei isAdminPrivilegesByGroup() nur gültige Gruppen berücksichtigen
 *
 * Revision 1.25  2004/10/22 15:29:07  jonyp
 * externalServerId
 *
 * Revision 1.24  2004/08/19 22:35:54  jonyp
 * added copyright section
 *
 */

package vkc.view.bean;

import java.util.HashSet;
import java.util.Iterator;
import java.sql.Timestamp;

import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.db.serializer.VKCUserRatingSerializer;
import vkc.db.entities.VKCUserRating;
import vkc.db.helper.UserHelper;
import vkc.exception.DBException;
import vkc.util.DateUtils;
import vkc.view.bean.VKCQuotaInfo;
import vkc.view.WebShare;

public final class UserInfo {
    private boolean accountValid = true;
    private boolean adminPrivileges = false;
    private boolean manageOwnGroups = false;
    private boolean manageOwnUsers = false;
    private boolean manageOwnProxies = false;
    private boolean	messagingEnabled = false;
    private boolean pwdChanged = false;

    private HashSet groupInfos = null;

    private Integer	creatorId;
    private Integer externalServerId;
    private Integer	messagingInterval;
    private Integer	quota;
    private Integer	userId;
    private Integer pwdMismatchCount;

    private String email = "";
    private String firstname = "";
    private String lastname = "";
    private String username = "";

    private Timestamp created;
    private Timestamp mostRecentLogin;
    private Timestamp mostRecentNews;
    private Timestamp validFrom;
    private Timestamp validUntil;

    private VKCUserRating rating = null;
    private UserStatistics userStatistics = null;

    public UserInfo() {
    }

    public UserInfo(Integer i) {
        userId = i;
    }

    public final String getFirstname() {
        return firstname;
    }

    public final String getLastname() {
        return lastname;
    }

    public final Integer getQuota() {
        return quota;
    }

    public final Integer getUserId() {
        return userId;
    }

    public final String getUsername() {
        return username;
    }

    public final String getUsernameLabel() {
        return getUsername() + " (" + getFirstname() + " " + getLastname() + ")";
    }

    public final String getFirstnameLabel() {
        return getFirstname() + " " + getLastname() + " (" + getUsername() + ")";
    }

    public final String getLastnameLabel() {
        return getLastname() + ", " + getFirstname() + " (" + getUsername() + ")";
    }

    public final String getFullName() {
        return getLastname() + ", " + getFirstname() + " (" + getUsername() + ")";
    }

    public final boolean isAccountValid() {
        return accountValid;
    }

    public final boolean isPwdChanged() {
        return pwdChanged;
    }

    public final String getEmail() {
        return email;
    }

    public final VKCQuotaInfo getQuotaInfo() throws DBException {
        return WebShare.database.getQuotaInfo(getUserId());
    }

    public final UserStatistics getUserStatistics() throws DBException {
        if (userStatistics == null) {
            userStatistics = UserHelper.getUserStatistics(getUserId());
        }
        return userStatistics;
    }

    public final Integer getMessagingInterval() {
        return messagingInterval;
    }

    public Integer getCreatorId() {
        return creatorId;
    }

    public Timestamp getCreated() {
        return created;
    }

    public Timestamp getMostRecentLogin() {
        return mostRecentLogin;
    }

    public Timestamp getMostRecentNews() {
        return mostRecentNews;
    }

    public boolean isMessagingEnabled() {
        return messagingEnabled;
    }

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

    public void setAccountValid(boolean b) {
        accountValid = b;
    }

    public void setEmail(String s) {
        email = s;
    }

    public void setLastname(String s) {
        lastname = s;
    }

    public void setMessagingEnabled(boolean b) {
        messagingEnabled = b;
    }

    public void setMessagingInterval(Integer i) {
        messagingInterval = i;
    }

    public void setPwdChanged(boolean b) {
        pwdChanged = b;
    }

    public void setQuota(Integer i) {
        quota = i;
    }

    public void setUserId(Integer i) {
        userId = i;
    }

    public void setUsername(String s) {
        username = s;
    }

    public void setFirstname(String s) {
        firstname = s;
    }

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

    public void setMostRecentLogin(Timestamp t) {
        mostRecentLogin = t;
    }

    public void setMostRecentNews(Timestamp t) {
        mostRecentNews = t;
    }

    public UserInfo getCreator() throws DBException {
        if (getCreatorId() != null) {
            return VKCUserSerializer.retrieveUserInfo(getCreatorId());
        }

        return null;
    }

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

        UserInfo info = (UserInfo)object;

        return getUserId().equals(info.getUserId());
    }

    public UserInfo copy() {
        UserInfo u = new UserInfo();

        u.setUsername(new String(getUsername()));
        u.setUserId(new Integer(getUserId().intValue()));
        u.setCreatorId((getCreatorId() == null) ? null : new Integer(getCreatorId().intValue()));
        u.setFirstname(new String(getFirstname()));
        u.setLastname(new String(getLastname()));
        u.setEmail(new String(getEmail()));
        u.setQuota((getQuota() == null) ? null : new Integer(getQuota().intValue()));
        u.setPwdChanged(isPwdChanged());
        u.setAccountValid(isAccountValid());
        u.setMessagingInterval((getMessagingInterval() == null) ? null : new Integer(getMessagingInterval().intValue()));
        u.setMessagingEnabled(isMessagingEnabled());
        u.setCreated((getCreated() == null) ? null : getCreated());
        u.setValidFrom((getValidFrom() == null) ? null : getValidFrom());
        u.setValidUntil((getValidUntil() == null) ? null : getValidUntil());

        return u;
    }

    public String getCreatedStr() {
        if (getCreated() != null) {
            return DateUtils.getDD_MM_YYYY(getCreated());
        }

        return "";
    }

    public boolean isAdminPrivileges() {
        return adminPrivileges;
    }

    public boolean isManageOwnGroups() {
        return manageOwnGroups;
    }

    public boolean isManageOwnProxies() {
        return manageOwnProxies;
    }

    public boolean isManageOwnUsers() {
        return manageOwnUsers;
    }

    public boolean isAdminPrivilegesByGroup() {
        Iterator it = getGroupInfos().iterator();

        while (it.hasNext()) {
            GroupInfo g = (GroupInfo)it.next();

            if (g.isAdminPrivileges() && g.isGroupValid()) {
                return true;
            }
        }

        return false;
    }

    public boolean isHasAdminPrivileges() {
        return isAdminPrivileges() || isAdminPrivilegesByGroup();
    }

    public boolean isDedicatedGroupsAdmin() {
        return !isHasAdminPrivileges() && isManageOwnGroups();
    }

    public boolean isDedicatedUsersAdmin() {
        return !isHasAdminPrivileges() && isManageOwnUsers();
    }

    public boolean isDedicatedAdmin() {
        return !isHasAdminPrivileges() && (isManageOwnUsers() || isManageOwnGroups());
    }

    public boolean isAdmin() {
        return isAdminPrivileges();
    }

    public boolean isAdminByGroup() {
        return !isAdmin() && isAdminPrivilegesByGroup();
    }

    public boolean isExternalUser() {
        if (getExternalServerId() != null) {
            return true;
        }

        return false;
    }

    public boolean isUsualUser() {
        return !isAdmin() && !isAdminPrivilegesByGroup() && !isDedicatedUsersAdmin() && !isDedicatedGroupsAdmin() && !isExternalUser();
    }

    public void setAdminPrivileges(boolean b) {
        adminPrivileges = b;
    }

    public void setManageOwnGroups(boolean b) {
        manageOwnGroups = b;
    }

    public void setManageOwnProxies(boolean b) {
        manageOwnProxies = b;
    }

    public void setManageOwnUsers(boolean b) {
        manageOwnUsers = b;
    }

    public HashSet getGroupInfos() {
        if (groupInfos == null) {
            try {
                groupInfos = VKCGroupSerializer.retrieveUserGroupInfos(getUserId());
            } catch (DBException e) {
                groupInfos = new HashSet();
            }
        }
        
        return groupInfos;
    }

    public boolean isUserValid(){
        Timestamp today = new Timestamp(System.currentTimeMillis());

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

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

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

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

        return isAccountValid();
    }

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

    public Timestamp getValidFrom() {
        return validFrom;
    }

    public void setValidUntil(Timestamp t) {
        validUntil = t;
    }

    public Timestamp getValidUntil() {
        return validUntil;
    }

    public Integer getExternalServerId() {
        return externalServerId;
    }

    public void setExternalServerId(Integer i) {
        externalServerId = i;
    }

    public String getUserInfoIcon() {
        return "profile.gif";

    }

    public VKCUserRating getRating() throws DBException{
        if (rating == null) {
            rating = VKCUserRatingSerializer.retrieveRating(getUserId());
        }

        return rating;
    }

    public void setRating(VKCUserRating rating) {
        this.rating = rating;
    }

	public Integer getPwdMismatchCount() {
		return pwdMismatchCount;
	}

	public void setPwdMismatchCount(Integer pwdMismatchCount) {
		this.pwdMismatchCount = pwdMismatchCount;
	}
}