/*
 * $Id: VKCAclEntry.java,v 1.12 2005/05/30 06:12:47 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCAclEntry.java,v $
 * Revision 1.12  2005/05/30 06:12:47  jonyp
 * *** empty log message ***
 *
 * Revision 1.11  2005/02/15 09:26:47  bigmac
 * Refactorings
 *
 * Revision 1.10  2005/01/18 12:19:47  jonyp
 * added creator/created to toString
 *
 * Revision 1.9  2004/11/18 10:20:45  jonyp
 * set/getCreated: changed from date to Timestamp
 *
 * Revision 1.8  2004/11/10 15:02:52  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.7  2004/07/20 08:15:52  jonyp
 * added copyright info + cvs log
 *
 *
 */
 
 package vkc.db.entities;

import java.io.Serializable;
import java.sql.Timestamp;

import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.ObjectCloner;


/**
 * Klasse representiert eine CategoryAcl VKC-System
 */
public class VKCAclEntry implements Serializable, Cloneable {
    private Integer aclEntryId;
    private Integer aclId;
    private VKCAclType aclType;
    private Integer accessorId;
    private VKCPermission permission;
    private Timestamp created;
    private Integer creatorId;
    private VKCUser creator = null;
    private String accessor = null;

    public VKCAclEntry() {
    }

    public void setAclEntryId(Integer aclEntryId) {
        this.aclEntryId = aclEntryId;
    }

    public Integer getAclEntryId() {
        return this.aclEntryId;
    }

    public void setAclId(Integer aclId) {
        this.aclId = aclId;
    }

    public Integer getAclId() {
        return this.aclId;
    }

    public void setAclType(VKCAclType aclType) {
        this.aclType = aclType;
    }

    public VKCAclType getAclType() {
        return this.aclType;
    }

    public void setAccessorId(Integer accessorId) {
        this.accessorId = accessorId;
    }

    public Integer getAccessorId() {
        return this.accessorId;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    public Timestamp getCreated() {
        return this.created;
    }

    public void setCreatorId(Integer creatorId) {
        this.creatorId = creatorId;
    }

    public Integer getCreatorId() {
        return this.creatorId;
    }

    public VKCPermission getPermission() {
        return permission;
    }

    public void setPermission(VKCPermission permission) {
        this.permission = permission;
    }

    public VKCUser getCreator() {
        if (creator != null) {
            return creator;
        }

        try {
            creator = VKCUserSerializer.retrieveUser(creatorId);
        } catch (DBException ex) {
            VKCLogManager.errorLog(this, "", ex.getMessage(),ex);
        }

        return creator;
    }

    public String getAccessor() {
        if (accessor != null) {
            return accessor;
        }

        if (this.getAclType().getAclTypeId().intValue() == VKCAclType.USER_TYPE) {
            // user acl type
            try {
                accessor = VKCUserSerializer.retrieveUserInfo(accessorId).getLastnameLabel();
            } catch (DBException ex) {
                VKCLogManager.errorLog(this, "", ex.getMessage(),ex);
            }
        } else if (this.getAclType().getAclTypeId().intValue() == VKCAclType.GROUP_TYPE) {
            // group acl type
            try {
                accessor = VKCGroupSerializer.retrieveGroup( accessorId ).getName();
            } catch (DBException ex) {
                VKCLogManager.errorLog(this, "", ex.getMessage(),ex);
            }
        } else {
            accessor = "";
        }

        return accessor;
    }

    public boolean equals(Object o) {
        if (!(o instanceof VKCAclEntry)) {
            return false;
        }

        VKCAclEntry a = (VKCAclEntry) o;
        String unique = "" + a.getAclId() + a.getAclType() +
                        a.getAccessorId() + a.getPermission();

        return unique.equals("" + this.getAclId() + this.getAclType() +
                             this.getAccessorId() + this.getPermission());
    }

    public String toString() {
        return "aclId = " + this.getAclId() + 
               ", aclType = " + this.getAclType() +
               ", accessorId = " + this.getAccessorId() + 
               ", permission = " + this.getPermission() +
               ", creatorId = " + this.getCreatorId() +
               ", created = " + this.getCreated();
    }

    public VKCAclEntry cloneEntry() throws Exception {
        return (VKCAclEntry) ObjectCloner.deepCopy( this );
    }
}
