AccessionOpResponse.java

package org.genesys.server.service.worker;

import java.io.Serializable;
import java.util.UUID;

import org.genesys.server.model.genesys.Accession;
import org.genesys.server.service.worker.AccessionOpResponse.UpsertResult.Type;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;

/**
 * Upsert response
 */
public class AccessionOpResponse implements Serializable {
	private static final long serialVersionUID = 3266359129838078166L;

	private String instCode;
	private String doi;
	private String acceNumb;
	private String genus;
	private String dataProviderId;
	private String error;
	private UpsertResult result;

	public static class UpsertResult implements Serializable {
		private static final long serialVersionUID = -7936259406612250074L;
		private Type action = Type.NOOP;
		private UUID uuid;

		public static enum Type {
			NOOP, INSERT, UPDATE, DELETE, ERROR
		}

		public UpsertResult() {
		}
		
		/**
		 * @param action UPSERT or INSERT or DELETE
		 */
		public UpsertResult(Type action) {
			this.action = action;
		}

		public Type getAction() {
			return action;
		}

		public void setAction(Type action) {
			this.action = action;
		}

		public UUID getUuid() {
			return uuid;
		}

		public UpsertResult setUUID(UUID uuid) {
			this.uuid = uuid;
			return this;
		}
	}
	
	public AccessionOpResponse() {
	}

	public AccessionOpResponse(Accession accession) {
		this.instCode = accession.getHoldingInstitute();
		this.doi = accession.getDoi();
		this.acceNumb = accession.getAccessionNumber();
		this.genus = accession.getTaxonomy() != null ? accession.getTaxonomy().getGenus() : null;
		this.dataProviderId = accession.getDataProviderId();
	}

	public AccessionOpResponse(JsonNode accn) {
		this.instCode = jsonT(accn, 0, "instituteCode");
		this.doi = jsonT(accn, 0, "doi");
		this.acceNumb = jsonT(accn, 0, "accessionNumber");
		this.genus = jsonT(accn, 0, "taxonomy", "genus");
		this.dataProviderId = jsonT(accn, 0, "dataProviderId");
	}

	private static String jsonT(JsonNode node, int pos, String... path) {
		if (node == null || pos > path.length - 1) {
			return null;
		}
		JsonNode ch = node.get(path[pos]);
		if (ch == null || ch instanceof NullNode) {
			return null;
		} else if (pos == path.length - 1) {
			return ch.asText();
		} else {
			return jsonT(ch, pos + 1, path);
		}
	}

	public String getInstCode() {
		return instCode;
	}

	public String getDoi() {
		return doi;
	}

	public String getAcceNumb() {
		return acceNumb;
	}

	public String getGenus() {
		return genus;
	}

	public String getDataProviderId() {
		return dataProviderId;
	}

	public String getError() {
		return error;
	}

	public UpsertResult getResult() {
		return result;
	}

	public AccessionOpResponse setResult(UpsertResult result) {
		this.result = result;
		return this;
	}

	public AccessionOpResponse setError(String error) {
		this.error = error;

		// update existing result
		if (error != null && this.result != null) {
			if (this.result.getAction() == Type.INSERT) {
				// clear UUID as it is not persisted
				this.result.setUUID(null);
			}
			// set result to error
			this.result.setAction(UpsertResult.Type.ERROR);
		}

		return this;
	}
}