package org.openlca.core.model.descriptors;

import java.io.Serializable;

import org.openlca.core.model.ModelType;

import com.google.common.primitives.Longs;

/**
 * Descriptors are lightweight models containing only descriptive information of
 * the corresponding model.The intention of descriptors is to get these
 * information fast from the database without loading the complete model.
 * Therefore, the respective DAO classes should provide these.
 */
public class BaseDescriptor implements Comparable<BaseDescriptor>, Serializable {

	private static final long serialVersionUID = -8609519818770549160L;

	private String refId;
	private long id;
	private String name;
	private String description;
	private ModelType type = ModelType.UNKNOWN;

	@Override
	public int compareTo(BaseDescriptor o) {
		if (name == null)
			return 0;
		return name.toLowerCase().compareTo(
				o.name != null ? o.name.toLowerCase() : null);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getRefId() {
		return refId;
	}

	public void setRefId(String refId) {
		this.refId = refId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setType(ModelType type) {
		this.type = type;
	}

	public ModelType getModelType() {
		return type;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj == this)
			return true;
		if (!(this.getClass().isInstance(obj)))
			return false;
		BaseDescriptor other = (BaseDescriptor) obj;
		return this.id == other.id;
	}

	@Override
	public int hashCode() {
		return Longs.hashCode(id);
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + " [id=" + id + ", name=" + name
				+ ", type=" + type + "]";
	}

}

@@ -66,16 +66,42 @@
 lizable;
+%0Aimport java.util.Objects;
 %0A%0Aimport
@@ -1753,34 +1753,284 @@
 ;%0A%09%09
-return this.id == other.id
+if (this.type != other.type)%0A%09%09%09return false;%0A%09%09if (this.id != 0 %7C%7C other.id != 0)%0A%09%09%09return this.id == other.id;%0A%09%09else%0A%09%09%09return Objects.equals(this.refId, other.refId)%0A%09%09%09%09%09&& Objects.equals(this.name, other.name)%0A%09%09%09%09%09&& Objects.equals(this.description, other.description)
 ;%0A%09%7D

compare descriptors by attributes when ID is 0