package org.emonocot.job.dwc.taxon;

import org.emonocot.api.TaxonService;
import org.emonocot.model.Taxon;
import org.gbif.ecat.voc.Rank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;


public class CalculateDerivedPropertiesProcessor implements
		ItemProcessor<Long, Taxon> {
	
	private static Logger logger = LoggerFactory.getLogger(CalculateDerivedPropertiesProcessor.class);
	
	private TaxonService taxonService;
	
	public void setTaxonService(TaxonService taxonService) {
		this.taxonService = taxonService;
	}

	@Override
	public Taxon process(Long id) throws Exception {
		Taxon taxon = taxonService.load(id, "taxon-page");
		
		if(taxon.getTaxonRank() == null) {
			return null;
		}
		
		if(taxon.getTaxonRank().ordinal() <= Rank.ORDER.ordinal()) {
			
		} else if(taxon.getParentNameUsage() != null) {
			if(taxon.getParentNameUsage().getTaxonRank().equals(Rank.ORDER)) {
				taxon.setOrder(taxon.getParentNameUsage().getScientificName());
			} else {
				taxon.setOrder(taxon.getParentNameUsage().getOrder());
			}	
		}
		
        if(taxon.getTaxonRank().ordinal() <= Rank.FAMILY.ordinal()) {
			
		} else if(taxon.getParentNameUsage() != null) {
			if(taxon.getParentNameUsage().getTaxonRank().equals(Rank.FAMILY)) {
				taxon.setFamily(taxon.getParentNameUsage().getScientificName());
			} else {
				taxon.setFamily(taxon.getParentNameUsage().getFamily());
			}	
		}
        
        if(taxon.getTaxonRank().ordinal() <= Rank.Subfamily.ordinal()) {
			
		} else if(taxon.getParentNameUsage() != null) {
			if(taxon.getParentNameUsage().getTaxonRank().equals(Rank.Subfamily)) {
				taxon.setSubfamily(taxon.getParentNameUsage().getScientificName());
			} else {
				taxon.setSubfamily(taxon.getParentNameUsage().getSubfamily());
			}	
		}
        
        if(taxon.getTaxonRank().ordinal() <= Rank.Tribe.ordinal()) {
			
		} else if(taxon.getParentNameUsage() != null) {
			if(taxon.getParentNameUsage().getTaxonRank().equals(Rank.Tribe)) {
				taxon.setTribe(taxon.getParentNameUsage().getScientificName());
			} else {
				taxon.setTribe(taxon.getParentNameUsage().getTribe());
			}	
		}
        
        if(taxon.getTaxonRank().ordinal() <= Rank.Subtribe.ordinal()) {
			
		} else if(taxon.getParentNameUsage() != null) {
			if(taxon.getParentNameUsage().getTaxonRank().equals(Rank.Subtribe)) {
				taxon.setSubtribe(taxon.getParentNameUsage().getScientificName());
			} else {
				taxon.setSubtribe(taxon.getParentNameUsage().getSubtribe());
			}	
		}
		return taxon;
	}

}

@@ -852,32 +852,85 @@
 ntNameUsage() !=
+ null && taxon.getParentNameUsage().getTaxonRank() !=
  null) %7B%0A%09%09%09if(t
@@ -914,32 +914,32 @@
 nk() != null) %7B%0A
-
 %09%09%09if(taxon.getP
@@ -1251,32 +1251,85 @@
 ntNameUsage() !=
+ null && taxon.getParentNameUsage().getTaxonRank() !=
  null) %7B%0A%09%09%09if(t
@@ -1663,32 +1663,85 @@
 ntNameUsage() !=
+ null && taxon.getParentNameUsage().getTaxonRank() !=
  null) %7B%0A%09%09%09if(t
@@ -2083,32 +2083,85 @@
 ntNameUsage() !=
+ null && taxon.getParentNameUsage().getTaxonRank() !=
  null) %7B%0A%09%09%09if(t
@@ -2449,32 +2449,32 @@
 rdinal()) %7B%0A%09%09%09%0A
-
 %09%09%7D else if(taxo
@@ -2494,24 +2494,77 @@
 meUsage() !=
+ null && taxon.getParentNameUsage().getTaxonRank() !=
  null) %7B%0A%09%09%09

Some taxa have null taxonRanks