package ca.corefacility.bioinformatics.irida.service.impl;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException;
import ca.corefacility.bioinformatics.irida.model.MiseqRun;
import ca.corefacility.bioinformatics.irida.model.SequenceFile;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.MiseqRunSequenceFileJoin;
import ca.corefacility.bioinformatics.irida.repositories.MiseqRunRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.sequencefile.MiseqRunSequenceFileJoinRepository;
import ca.corefacility.bioinformatics.irida.service.MiseqRunService;

/**
 * 
 * @author Thomas Matthews <thomas.matthews@phac-aspc.gc.ca>
 */
@Service
public class MiseqRunServiceImpl extends CRUDServiceImpl<Long, MiseqRun> implements MiseqRunService {

	private MiseqRunSequenceFileJoinRepository mrsfRepository;

	protected MiseqRunServiceImpl() {
		super(null, null, MiseqRun.class);
	}

	@Autowired
	public MiseqRunServiceImpl(MiseqRunRepository repository, MiseqRunSequenceFileJoinRepository mrsfRepository,
			Validator validator) {
		super(repository, validator, MiseqRun.class);
		this.mrsfRepository = mrsfRepository;
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	@Transactional
	public Join<MiseqRun, SequenceFile> addSequenceFileToMiseqRun(MiseqRun run, SequenceFile file) {
		try {
			MiseqRunSequenceFileJoin join = new MiseqRunSequenceFileJoin(run, file);
			return mrsfRepository.save(join);
		} catch (DataIntegrityViolationException e) {
			throw new EntityExistsException("Sequence file [" + file.getId() + "] has already been added to MiseqRun ["
					+ run.getId() + "]");
		}
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	@Transactional(readOnly = true)
	public Join<MiseqRun, SequenceFile> getMiseqRunForSequenceFile(SequenceFile file) {
		return mrsfRepository.getMiseqRunForSequenceFile(file);
	}

}

package ca.corefacility.bioinformatics.irida.service.impl;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException;
import ca.corefacility.bioinformatics.irida.model.MiseqRun;
import ca.corefacility.bioinformatics.irida.model.SequenceFile;
import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.MiseqRunSequenceFileJoin;
import ca.corefacility.bioinformatics.irida.repositories.MiseqRunRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.sequencefile.MiseqRunSequenceFileJoinRepository;
import ca.corefacility.bioinformatics.irida.service.MiseqRunService;

/**
 * 
 * @author Thomas Matthews <thomas.matthews@phac-aspc.gc.ca>
 */
@Service
public class MiseqRunServiceImpl extends CRUDServiceImpl<Long, MiseqRun> implements MiseqRunService {

	private MiseqRunSequenceFileJoinRepository mrsfRepository;

	protected MiseqRunServiceImpl() {
		super(null, null, MiseqRun.class);
	}

	@Autowired
	public MiseqRunServiceImpl(MiseqRunRepository repository, MiseqRunSequenceFileJoinRepository mrsfRepository,
			Validator validator) {
		super(repository, validator, MiseqRun.class);
		this.mrsfRepository = mrsfRepository;
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	@Transactional
	public Join<MiseqRun, SequenceFile> addSequenceFileToMiseqRun(MiseqRun run, SequenceFile file) {
		try {
			MiseqRunSequenceFileJoin join = new MiseqRunSequenceFileJoin(run, file);
			return mrsfRepository.save(join);
		} catch (DataIntegrityViolationException e) {
			throw new EntityExistsException("Sequence file [" + file.getId() + "] has already been added to MiseqRun ["
					+ run.getId() + "]");
		}
	}

	/**
	 * {@inheritDoc }
	 */
	@Override
	@Transactional(readOnly = true)
	public Join<MiseqRun, SequenceFile> getMiseqRunForSequenceFile(SequenceFile file) {
		return mrsfRepository.getMiseqRunForSequenceFile(file);
	}

	@Override
	public MiseqRun create(MiseqRun o) {
		return super.create(o);
	}
}

Implement the method in the subclass to ensure that the security annotations on the superclass are applied.