package uk.ac.ebi.biosamples.neo;

import java.util.Collection;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import uk.ac.ebi.biosamples.MessageContent;
import uk.ac.ebi.biosamples.model.Sample;
import uk.ac.ebi.biosamples.neo.model.NeoCuration;
import uk.ac.ebi.biosamples.neo.model.NeoCurationLink;
import uk.ac.ebi.biosamples.neo.model.NeoSample;
import uk.ac.ebi.biosamples.neo.repo.NeoCurationLinkRepository;
import uk.ac.ebi.biosamples.neo.repo.NeoCurationRepository;
import uk.ac.ebi.biosamples.neo.repo.NeoSampleRepository;
import uk.ac.ebi.biosamples.neo.service.modelconverter.CurationToNeoCurationConverter;
import uk.ac.ebi.biosamples.neo.service.modelconverter.SampleToNeoSampleConverter;

@Component
public class NeoMessageBufferTransaction {


	private final NeoSampleRepository neoSampleRepository;
	private final SampleToNeoSampleConverter sampleToNeoSampleConverter;

	private final NeoCurationRepository neoCurationRepository;
	private final NeoCurationLinkRepository neoCurationLinkRepository;
	private final CurationToNeoCurationConverter curationToNeoCurationConverter;
	
	public NeoMessageBufferTransaction(NeoSampleRepository neoSampleRepository,
			SampleToNeoSampleConverter sampleToNeoSampleConverter,
			NeoCurationRepository neoCurationRepository,
			NeoCurationLinkRepository neoCurationLinkRepository,
			CurationToNeoCurationConverter curationToNeoCurationConverter) {
		this.neoSampleRepository = neoSampleRepository;
		this.sampleToNeoSampleConverter = sampleToNeoSampleConverter;
		this.neoCurationRepository = neoCurationRepository;
		this.neoCurationLinkRepository = neoCurationLinkRepository;
		this.curationToNeoCurationConverter = curationToNeoCurationConverter;
	}
	
	@Transactional
	public void save(Collection<MessageContent> messageContents) {		
		for (MessageContent messageContent : messageContents) {
			if (messageContent.hasSample()) {
				Sample sample = messageContent.getSample();
				NeoSample neoSample = sampleToNeoSampleConverter.convert(sample);
				neoSample = neoSampleRepository.save(neoSample);
			}
			if (messageContent.hasCurationLink()) {				
				NeoCuration neoCuration = curationToNeoCurationConverter.convert(messageContent.getCurationLink().getCuration());
				//make sure the neoCuration is saved
				neoCuration = neoCurationRepository.save(neoCuration);
				
				NeoSample neoSample = neoSampleRepository.findOneByAccession(messageContent.getCurationLink().getSample(),1);							
				NeoCurationLink neoCurationLink = NeoCurationLink.build(neoCuration, neoSample);
				neoCurationLink = neoCurationLinkRepository.save(neoCurationLink);
			}
		}
	}
}

@@ -56,16 +56,91 @@
 lection;
+%0Aimport java.util.HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -440,16 +440,71 @@
 onLink;%0A
+import uk.ac.ebi.biosamples.neo.model.NeoRelationship;%0A
 import u
@@ -2193,24 +2193,1178 @@
 rt(sample);%0A
+%09%09%09%09%0A%09%09%09%09//because relationships can refer to existing samples, make sure we use the existing NeoSample objects%09%09%09%09%0A%09%09%09%09Set%3CNeoRelationship%3E newRelationships = new HashSet%3C%3E();%0A%09%09%09%09for (NeoRelationship oldRelationship : neoSample.getRelationships()) %7B%0A%09%09%09%09%09NeoSample owner = oldRelationship.getOwner();%0A%09%09%09%09%09NeoSample target = oldRelationship.getTarget();%0A%09%09%09%09%09if (!owner.getAccession().equals(neoSample.getAccession())) %7B%0A%09%09%09%09%09%09owner = neoSampleRepository.findOneByAccession(owner.getAccession(), 0);%0A%09%09%09%09%09%09//if we couldn't find an existing one, use this dummy%0A%09%09%09%09%09%09if (owner == null) %7B%0A%09%09%09%09%09%09%09owner = oldRelationship.getOwner();%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09if (!target.getAccession().equals(neoSample.getAccession())) %7B%0A%09%09%09%09%09%09target = neoSampleRepository.findOneByAccession(target.getAccession(), 0);%0A%09%09%09%09%09%09//if we couldn't find an existing one, use this dummy%09%09%0A%09%09%09%09%09%09if (target == null) %7B%0A%09%09%09%09%09%09%09target = oldRelationship.getTarget();%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09newRelationships.add(NeoRelationship.build(owner, oldRelationship.getType(), target));%0A%09%09%09%09%7D%0A%09%09%09%09neoSample.getRelationships().clear();%0A%09%09%09%09neoSample.getRelationships().addAll(newRelationships);%09%09%09%09%0A%09%09%09%09%0A
 %09%09%09%09neoSampl
@@ -3397,24 +3397,27 @@
 ve(neoSample
+, 1
 );%0A%09%09%09%7D%0A%09%09%09i

fix to stop replacing of existing nodes in relationships