AccessionSourceMapServiceImpl.java

/*
 * Copyright 2021 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.gringlobal.service.impl;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionSource;
import org.gringlobal.model.AccessionSourceMap;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.QAccessionSource;
import org.gringlobal.model.QAccessionSourceMap;
import org.gringlobal.persistence.AccessionSourceMapRepository;
import org.gringlobal.service.AccessionSourceMapService;
import org.gringlobal.service.AccessionSourceService;
import org.gringlobal.service.CooperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@Slf4j
public class AccessionSourceMapServiceImpl extends CRUDService2Impl<AccessionSourceMap, AccessionSourceMapRepository> implements AccessionSourceMapService {

	@Autowired
	private AccessionSourceService accessionSourceService;

	@Autowired
	private CooperatorService cooperatorService;

	@Override
//	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE', #source.accession.site)")
	public AccessionSourceMap create(AccessionSourceMap source) {
		log.debug("Create AccessionSourceMap. Input data {}", source);

		AccessionSource accessionSource = accessionSourceService.get(source.getAccessionSource().getId());
		Cooperator cooperator = cooperatorService.get(source.getCooperator().getId());

		var saved = repository.save(new AccessionSourceMap(accessionSource, cooperator));
		return _lazyLoad(saved);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE', #target.accessionSource.accession.site)")
	public AccessionSourceMap update(AccessionSourceMap input, AccessionSourceMap target) {
		log.debug("Update AccessionSourceMap. Input data {}", input);

		target.setAccessionSource(accessionSourceService.get(input.getAccessionSource().getId()));
		target.setCooperator(cooperatorService.get(input.getCooperator().getId()));

		var saved = repository.save(target);
		return _lazyLoad(saved);
	}

	@Override
	public List<AccessionSourceMap> listAccessionSourceMaps(Accession accession) {
		QAccessionSource accessionSource = new QAccessionSource("source");
		return jpaQueryFactory.selectFrom(QAccessionSourceMap.accessionSourceMap)
				.join(QAccessionSourceMap.accessionSourceMap.accessionSource(), accessionSource).fetchJoin()
				.join(QAccessionSourceMap.accessionSourceMap.cooperator()).fetchJoin()
				.where(accessionSource.accession().eq(accession))
				.fetch();
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE', #target.accessionSource.accession.site)")
	public AccessionSourceMap updateFast(AccessionSourceMap updated, AccessionSourceMap target) {
		target.setAccessionSource(updated.getAccessionSource());
		target.setCooperator(updated.getCooperator());

		return repository.save(target);
	}

}