package uk.ac.ebi.biosamples.neo.service.modelconverter;

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

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Service;

import uk.ac.ebi.biosamples.model.Attribute;
import uk.ac.ebi.biosamples.model.ExternalReference;
import uk.ac.ebi.biosamples.model.Relationship;
import uk.ac.ebi.biosamples.model.Sample;
import uk.ac.ebi.biosamples.neo.model.NeoAttribute;
import uk.ac.ebi.biosamples.neo.model.NeoExternalReference;
import uk.ac.ebi.biosamples.neo.model.NeoExternalReferenceLink;
import uk.ac.ebi.biosamples.neo.model.NeoRelationship;
import uk.ac.ebi.biosamples.neo.model.NeoSample;

@Service
@ConfigurationPropertiesBinding
public class NeoSampleToSampleConverter
		implements Converter<NeoSample, Sample> {
	
	@Override
	public Sample convert(NeoSample neo) {		
		Set<Attribute> attributes = new HashSet<>();
		if (neo.getAttributes() != null) {
			for (NeoAttribute attribute : neo.getAttributes()) {
				attributes.add(Attribute.build(attribute.getType(), attribute.getValue(), attribute.getIri(), attribute.getUnit()));
			}
		}
		Set<ExternalReference> externalReferences = new HashSet<>();
		if (neo.getExternalReferenceLinks() != null) {
			for (NeoExternalReferenceLink externalReferenceApplication : neo.getExternalReferenceLinks()) {
				NeoExternalReference neoExternalReference = externalReferenceApplication.getExternalReference(); 
				if (neoExternalReference != null) {
					externalReferences.add(ExternalReference.build(neoExternalReference.getUrl()));
				}
			}
		}
		Set<Relationship> relationships = new HashSet<>();
		if (neo.getRelationships() != null) {
			for (NeoRelationship relationship : neo.getRelationships()) {
				relationships.add(Relationship.build(relationship.getOwner().getAccession(), relationship.getType(), relationship.getTarget().getAccession()));
			}				
		}
		return Sample.build(neo.getName(), neo.getAccession(), neo.getRelease(), neo.getUpdate(),
				attributes, relationships, externalReferences);
	}

}

@@ -764,135 +764,587 @@
 le;%0A
-%0A@Service%0A@ConfigurationPropertiesBinding%0Apublic class NeoSampleToSampleConverter%0A%09%09implements Converter%3CNeoSample, Sample%3E %7B%0A%09
+import uk.ac.ebi.biosamples.neo.repo.NeoExternalReferenceLinkRepository;%0Aimport uk.ac.ebi.biosamples.neo.repo.NeoSampleRepository;%0A%0A@Service%0Apublic class NeoSampleToSampleConverter%0A%09%09implements Converter%3CNeoSample, Sample%3E %7B%0A%09%0A%09private final NeoExternalReferenceLinkRepository neoExternalReferenceLinkRepository;%0A%09%0A%09public NeoSampleToSampleConverter(NeoExternalReferenceLinkRepository neoExternalReferenceLinkRepository) %7B%0A%09%09this.neoExternalReferenceLinkRepository = neoExternalReferenceLinkRepository;%0A%09%7D%0A%09%0A%09/**%0A%09 * This will load associated objects as necessary.%0A%09 * %0A%09 * %0A%09 */
 %0A%09@O
@@ -1393,16 +1393,22 @@
 eo) %7B%09%09%0A
+%09%09%0A%09%09%0A
 %09%09Set%3CAt
@@ -1831,27 +1831,20 @@
 eference
-Application
+Link
  : neo.g
@@ -1877,28 +1877,45 @@
 ) %7B%0A%09%09%09%09
-NeoE
+//recursively load e
 xternal
-R
+ r
 eference
@@ -1915,20 +1915,72 @@
 ference 
-neoE
+links to ensure that the external references exist%0A%09%09%09%09e
 xternalR
@@ -1991,12 +1991,19 @@
 ence
- = e
+Link = neoE
 xter
@@ -2018,19 +2018,148 @@
 ence
-Application
+LinkRepository.findOneByHash(externalReferenceLink.getHash(), 1);%0A%09%09%09%09%0A%09%09%09%09NeoExternalReference neoExternalReference = externalReferenceLink
 .get

Improve performance of neo loading