package eu.dnetlib.dlms.jdbc;

import java.sql.SQLException;

import eu.dnetlib.dlms.jdbc.server.Variable;
import eu.dnetlib.dlms.lowlevel.objects.LLDigitalObject;

/**
 * InformationObjects are the objects returned in a ResultSet instead of LLDigitalObject.
 * 
 * @author alessia
 * 
 */
public class InformationObjectImpl implements InformationObject {

	private long id;
	private String type;

	static protected long parseId(String descriptor) {
		return Long.parseLong(descriptor.split(",")[0]);
	}

	static protected String parseType(String descriptor) {
		if (descriptor.contains(","))
			return descriptor.split(",")[1];
		return "unknown";
	}

	public InformationObjectImpl() {
	}

	public InformationObjectImpl(String descriptor) {
		this.id = parseId(descriptor);
		this.type = parseType(descriptor);
	}

	
	public InformationObjectImpl(final long id) {
		this.id = id;
		this.type = "unknown";
	}

	public InformationObjectImpl(final LLDigitalObject obj) {
		this.id = obj.getId();
		this.type = obj.getObjectType().getName();
	}

	
	public InformationObjectImpl(final Variable<?> v) throws SQLException {
		switch (v.getVariableType()) {
		case atom:
		case object:
		case relation:
		case set:
		case structure:
			LLDigitalObject obj = (LLDigitalObject) v.getVariableValue();
			this.id = obj.getId();
			this.type = obj.getObjectType().getName();
			break;
		default:
			throw new SQLException("Cannot create " + this.getClass() + " from variable of type: " + v.getVariableType());
		}
	}

	@Override
	public String toString() {
		return this.getClass() + " id = " + this.getId();
	}

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

	public long getId() {
		return id;
	}

	public String getType() {
		return type;
	}

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

}
