PDCI.java

/**
 * Copyright 2015 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

package org.genesys.server.model.genesys;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.VersionedModel;
import org.genesys.custom.elasticsearch.IgnoreField;

@Entity
@Table(name = "pdci", indexes = { @Index(columnList = "score") })
@Getter
@Setter
public class PDCI extends VersionedModel implements AccessionRelated {
	private static final long serialVersionUID = -1312366054528702261L;

	@IgnoreField
	public static final String[] independentItems = { "genus", "species", "spAuthor", "subTaxa", "subtAuthor", "cropName", "acqDate", "sampStat", "donorCode", "donorNumb",
			"otherNumb", "duplSite", "storage", "donorName", "duplInstName", "acceUrl", "mlsStat" };
	@IgnoreField
	public static final String[] dependentItems = { "origCty", "collSite", "latitude", "longitude", "elevation", "collDate", "bredCode", "ancest", "collSrc", "acceName",
			"collNumb", "collCode", "collName" };

	@OneToOne(mappedBy = "pdci", cascade = {}, fetch = FetchType.LAZY, optional = false)
	private AccessionId accession;

	private Double score = null;
	private Double scoreHist = null;

	private int acqDate = 0;
	private int sampStat = 0;
	private int donorCode = 0;
	private int genus = 0;
	private int species = 0;
	private int spAuthor = 0;
	private int subTaxa = 0;
	private int subtAuthor = 0;
	private int donorNumb = 0;
	private int otherNumb = 0;
	private int duplSite = 0;
	private int storage = 0;
	private int donorName = 0;
	private int duplInstName = 0;
	private int acceUrl = 0;
	private int mlsStat = 0;
	private int origCty = 0;
	private int latitude = 0;
	private int longitude = 0;
	private int collSite = 0;
	private int elevation = 0;
	private int collDate = 0;
	private int collSrc = 0;
	private int collNumb = 0;
	private int collCode = 0;
	private int collName = 0;
	private int ancest = 0;
	private int acceName = 0;

	private int bredCode = 0;
	private int cropName = 0;

	@PrePersist
	@PreUpdate
	protected void prePersist() {
		this.score = calculateScore();
		this.scoreHist = (double) (Math.ceil(this.score * 2) / 2);
	}

	public Double getScore() {
		if (score == null) {
			score = calculateScore();
		}
		return score;
	}

	public Double getScoreHist() {
		if (scoreHist == null) {
			this.scoreHist = (double) (Math.ceil(getScore() * 2) / 2);
		}
		return scoreHist;
	}

	private Double calculateScore() {
		final float total = getTotal();
		return total / 100d;
	}

	@Transient
	public float getTotal() {
		float score = 0;

		score += acqDate;
		score += sampStat;
		score += donorCode;
		score += genus;
		score += species;
		score += spAuthor;
		score += subTaxa;
		score += subtAuthor;
		score += donorNumb;
		score += otherNumb;
		score += duplSite;
		score += storage;
		score += donorName;
		score += duplInstName;
		score += acceUrl;
		score += mlsStat;
		score += origCty;
		score += latitude;
		score += longitude;
		score += collSite;
		score += elevation;
		score += collDate;
		score += collSrc;
		score += collNumb;
		score += collCode;
		score += collName;
		score += ancest;
		score += acceName;

		score += bredCode;
		score += cropName;
		return score;
	}

	@IgnoreField
	public String[] getIndependentItems() {
		return independentItems;
	}

	@IgnoreField
	public String[] getDependentItems() {
		return dependentItems;
	}

	public static Set<String> getPdciItems() {
		final Set<String> descriptors = new HashSet<>();
		for (final java.lang.reflect.Method method : PDCI.class.getMethods()) {
			if (method.getName().startsWith("get") && method.getReturnType() == int.class) {
				final String str = method.getName().substring(3);
				descriptors.add(str.substring(0, 1).toLowerCase() + str.substring(1));
			}
		}
		return descriptors;
	}

	/**
	 * Reset all scores to 0.
	 */
	public void reset() {
		score = null;
		scoreHist = null;

		acqDate = 0;
		sampStat = 0;
		donorCode = 0;
		genus = 0;
		species = 0;
		spAuthor = 0;
		subTaxa = 0;
		subtAuthor = 0;
		donorNumb = 0;
		otherNumb = 0;
		duplSite = 0;
		storage = 0;
		donorName = 0;
		duplInstName = 0;
		acceUrl = 0;
		mlsStat = 0;
		origCty = 0;
		latitude = 0;
		longitude = 0;
		collSite = 0;
		elevation = 0;
		collDate = 0;
		collSrc = 0;
		collNumb = 0;
		collCode = 0;
		collName = 0;
		ancest = 0;
		acceName = 0;

		bredCode = 0;
		cropName = 0;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof PDCI;
	}
}