ExplorationServiceImpl.java

/*
 * Copyright 2024 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 lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Exploration;
import org.gringlobal.model.QAccession;
import org.gringlobal.model.QExploration;
import org.gringlobal.persistence.AccessionRepository;
import org.gringlobal.persistence.ExplorationRepository;
import org.gringlobal.service.ExplorationService;
import org.gringlobal.service.filter.ExplorationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.querydsl.core.types.dsl.BooleanExpression;

import java.util.List;

@Service
@Transactional(readOnly = true)
@Validated
@Slf4j
public class ExplorationServiceImpl extends FilteredCRUDService2Impl<Exploration, ExplorationFilter, ExplorationRepository> implements ExplorationService {
	
	@Autowired
	private AccessionRepository accessionRepository;
	
	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'ADMINISTRATION')")
	public Exploration updateFast(Exploration updated, Exploration target) {
		target.apply(updated);
		return repository.save(target);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'CREATE')")
	public Exploration create(Exploration source) {
		return createFast(source);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE')")
	public Exploration update(Exploration updated, Exploration target) {
		return updateFast(updated, target);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE')")
	public Exploration addAccessions(Exploration exploration, List<Accession> accessions) {
		var loadedExploration = get(exploration.getId());
		var loadedAccessions = accessionRepository.findAll(QAccession.accession.in(accessions));
		loadedAccessions.forEach(accession -> accession.setExploration(loadedExploration));
		accessionRepository.saveAllAndFlush(loadedAccessions);
		return get(loadedExploration.getId());
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'WRITE')")
	public Exploration removeAccessions(Exploration exploration, List<Accession> accessions) {
		var loadedExploration = get(exploration.getId());
		var loadedAccessions = accessionRepository.findAll(QAccession.accession.in(accessions));
		loadedAccessions.forEach(accession -> accession.setExploration(null));
		accessionRepository.saveAllAndFlush(loadedAccessions);
		return get(loadedExploration.getId());
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('PassportData', 'DELETE')")
	public Exploration remove(Exploration entity) {
		var loadedAccessions = accessionRepository.findAll(QAccession.accession.exploration().eq(entity));
		loadedAccessions.forEach(accession -> accession.setExploration(null));
		accessionRepository.saveAllAndFlush(loadedAccessions);
		return super.remove(entity);
	}

	@Override
	public List<Exploration> autocomplete(String text) {
		if (StringUtils.isBlank(text) || text.strip().length() < 1) {
			return repository.findAll(PageRequest.of(0, 15, Sort.by("title"))).getContent();
		}
		BooleanExpression expression = QExploration.exploration.title.containsIgnoreCase(text).or(QExploration.exploration.explorationNumber.containsIgnoreCase(text));
		return repository.findAll(expression, PageRequest.of(0, 15, Sort.by("title"))).getContent();
	}
}