package eu.dnetlib.services.async;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AsyncResponse {

	private static final Log log = LogFactory.getLog(AsyncResponse.class);
	private String responseJson;
	private AsyncMethodStatus status;

	public AsyncResponse() {}

	public AsyncResponse(final String responseJson, final AsyncMethodStatus status) {
		this.responseJson = responseJson;
		this.status = status;
	}

	public String getResponseJson() {
		return responseJson;
	}

	public void setResponseJson(final String responseJson) {
		this.responseJson = responseJson;
	}

	public AsyncMethodStatus getStatus() {
		return status;
	}

	public void setStatus(final AsyncMethodStatus status) {
		this.status = status;
	}

	public <K> K parseResponse(final Class<K> clazz) throws Exception {
		return (new ObjectMapper()).readValue(responseJson, clazz);
	}

	public void prepareResponse(final Object o) {
		try {
			responseJson = (new ObjectMapper()).writeValueAsString(o);
		} catch (final JsonProcessingException e) {
			log.warn("Problem serializing object: " + o, e);
		}

	}

}
