/*
 * $Id: AccessorPermPair.java,v 1.3 2005/08/09 22:43:27 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: AccessorPermPair.java,v $
 * Revision 1.3  2005/08/09 22:43:27  marbobo
 * - Unnötige Import-Anweisungen entfernt
 *
 * Revision 1.2  2005/05/30 16:11:43  jonyp
 * added getAccessorMethod
 *
 * Revision 1.1  2005/05/30 06:13:30  jonyp
 * *** empty log message ***
 *
 * Revision 1.6  2004/11/10 15:02:51  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.5  2004/07/20 08:38:46  jonyp
 * added copyright info + cvs log
 *
 * Revision 1.4  2003/12/24 10:12:11  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.3  2003/05/02 16:12:22  vchris
 * Diverse Änderungen für neuen ACL Dialog
 *
 * Revision 1.2  2002/12/14 04:34:44  koppy
 * <No Comment Entered>
 *
 * Revision 1.1  2002/12/12 02:36:51  koppy
 * Acl editing feature for categories added
 *
 */

package vkc.security;

import vkc.db.entities.VKCAclType;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;

public class AccessorPermPair {

    private Integer aclTypeId = null;
    private Integer accessorId = null;
    private String permTokenString = null;
    private char[] permTokenList = null;
    private String accessor = null;

    public AccessorPermPair(){}

    public AccessorPermPair( Integer aclTypeId, Integer accessorId ) {

    	this.aclTypeId = aclTypeId;
		this.accessorId = accessorId;
    }

	public Integer getAccessorId() {
		return accessorId;
	}

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

	public Integer getAclTypeId() {
		return aclTypeId;
	}

	public void setAclTypeId(Integer aclType) {
		this.aclTypeId = aclType;
	}

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

        if (this.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.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 String getPermTokenString() {
		return this.permTokenString;
	}
	
	public char[] getPermTokenList() {

		if (this.permTokenList == null ) {
			if (this.permTokenString != null) {
			    this.permTokenList = this.permTokenString.toCharArray();
			}
		}
		
		return this.permTokenList;
	}
	
	public void setPermTokenList(char[] permTokenList) {
		this.permTokenList = permTokenList;
	}

	public void setPermTokenString(String permTokenString) {
		this.permTokenString = permTokenString;
	}
	
	public boolean hasPermission(char token) {
	    
	    if ( this.permTokenString.indexOf(token) != -1 ) {
	        return true;
	    }
	    
	    return false;
	}
	
}
