package eu.dnetlib.domain.functionality;

import eu.dnetlib.domain.SearchCriteria;
import eu.dnetlib.domain.SearchCriteriaImpl;


public class CollectionSearchCriteria 
		extends SearchCriteriaImpl
		implements SearchCriteria {
	
	private String parentId = null;
	private Boolean root = null;
	private Boolean isPrivate=true;
	private String ownerId = null;
	private String name = null;
	
	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public boolean equals(Object o) {
		return this.equals((CollectionSearchCriteria) o);
	}
	
	public boolean equals(CollectionSearchCriteria c) {
		
		if (!super.equals(c))
			return false;

		if (this.getParentId() != null && c.getParentId() == null)
			return false;
		else if (this.getParentId() == null && c.getParentId() != null)
			return false;
		else if (this.getParentId() != null && c.getParentId() != null)
			return this.getParentId().equals(c.getParentId());
		
		if (this.getRoot() != null && c.getRoot() == null)
			return false;
		else if (this.getRoot() == null && c.getRoot() != null)
			return false;
		else if (this.getRoot() != null && c.getRoot() != null)
			return this.getRoot().equals(c.getRoot());
				
		if (getIsPrivate() != c.getIsPrivate())
		{
			return false;
		}
		//check if ownerId fields are both either null or non-null
		if ((getOwnerId()==null && c.getOwnerId() != null) ||
				(getOwnerId()!=null && c.getOwnerId() == null))
		{
			return false;
		}
		else if (getOwnerId()!= null && c.getOwnerId() !=null &&
				!getOwnerId().equals(c.getOwnerId()))
		{
			return false;
		}	
		
		if ( (getName() == null && c.getName() != null)||
				(getName() != null && c.getName() == null))
		{
			return false;
		}
		else if (getName()!=null && c.getName() != null && !getName().equals(c.getName()))
		{
			return false;
		}
		
		return true;
	}
	
	public int hashCode() {
		int code = super.hashCode();
		
		if (parentId != null)
			code |= parentId.hashCode();
		
		if (root != null)
			code |= root.hashCode();
		
		return code;
	}

	public Boolean getRoot() {
		return root;
	}

	public void setRoot(Boolean root) {
		this.root = root;
	}

	public boolean matches(Object o) {
		Collection collection = (Collection) o;
		
		if (this.getContains() != null) {
			if (collection.getName() == null || 
					!collection.getName().toLowerCase().contains(this.getContains().toLowerCase()))
				return false;
		}
		
		if (this.getStartsWith() != null) {
			if (collection.getName() == null || 
					!collection.getName().toLowerCase().startsWith(this.getStartsWith().toLowerCase()))
				return false;
		}
		
		if (this.getEndsWith() != null) {
			if (collection.getName() == null || 
					!collection.getName().toLowerCase().endsWith(this.getEndsWith().toLowerCase()))
				return false;
		}
		
		if (this.getParentId() != null) {
			if (collection.getFather() == null || 
					!collection.getFather().equals(this.getParentId()))
				return false;
		}
		
		if (this.getRoot() != null) {
			if ( (collection.getFather() == null) != this.getRoot())
				return false;
		}		
		
		return true;
	}

	public Boolean getIsPrivate()
	{
		return isPrivate;
	}

	public void setIsPrivate(Boolean isPrivate)
	{
		this.isPrivate = isPrivate;
	}

	public String getOwnerId()
	{
		return ownerId;
	}

	public void setOwnerId(String ownerId)
	{
		this.ownerId = ownerId;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
}