TaxonomyGenusCRUDServiceImpl.java

/*
 * Copyright 2020 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.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.QTaxonomyGenus;
import org.gringlobal.model.TaxonomyGenus;
import org.gringlobal.persistence.TaxonomyGenusRepository;
import org.gringlobal.service.TaxonomyGenusCRUDService;
import org.gringlobal.service.filter.TaxonomyGenusFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * The Class TaxonomyGenusCRUDService.
 */
@Service
@Slf4j
public class TaxonomyGenusCRUDServiceImpl extends FilteredCRUDServiceImpl<TaxonomyGenus, TaxonomyGenusFilter, TaxonomyGenusRepository> implements TaxonomyGenusCRUDService {

	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
	}

	@Override
	public NumberPath<Long> entityIdPredicate() {
		return QTaxonomyGenus.taxonomyGenus.id;
	}
	
	@Override
	protected JPAQuery<TaxonomyGenus> entityListQuery() {
		return jpaQueryFactory.selectFrom(QTaxonomyGenus.taxonomyGenus)
				// family
				.join(QTaxonomyGenus.taxonomyGenus.taxonomyFamily()).fetchJoin()
				// owner
				.join(QTaxonomyGenus.taxonomyGenus.ownedBy()).fetchJoin();
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Taxonomy', 'CREATE')")
	public TaxonomyGenus create(TaxonomyGenus source) {
		TaxonomyGenus record = new TaxonomyGenus();
		record.apply(source);
		record = repository.save(record);
		if (record.getCurrentTaxonomyGenus() == null) {
			record.setCurrentTaxonomyGenus(record);
			record = repository.save(record);
		}
		return _lazyLoad(record);
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Taxonomy', 'ADMINISTRATION')")
	public TaxonomyGenus update(TaxonomyGenus updated, TaxonomyGenus target) {
		if (target.getGrinId() != null) {
			throw new InvalidApiUsageException("TaxonomyGenus records sourced from GRIN Taxonomy are unmodifiable");
		}
		target.apply(updated);
		return _lazyLoad(repository.save(target));
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Taxonomy', 'DELETE')")
	public TaxonomyGenus remove(TaxonomyGenus entity) {
		return super.remove(entity);
	}

	@Override
	public Page<TaxonomyGenus> list(TaxonomyGenusFilter filter, Pageable page) throws SearchException {
		return super.list(TaxonomyGenus.class, filter, page);
	}
	
	@Override
	public TaxonomyGenus getGenus(String genusName, String genusAuthority) {
		List<TaxonomyGenus> matches = (List<TaxonomyGenus>) repository.findAll(QTaxonomyGenus.taxonomyGenus.genusName.eq(genusName)
			.and(genusAuthority == null ? QTaxonomyGenus.taxonomyGenus.genusAuthority.isNull() : QTaxonomyGenus.taxonomyGenus.genusAuthority.eq(genusAuthority))
		);

		if (matches.size() == 0) {
			matches = (List<TaxonomyGenus>) repository.findAll(QTaxonomyGenus.taxonomyGenus.genusName.eq(genusName));
		}

		if (matches.size() > 0) {
			TaxonomyGenus best = matches.get(0);
			matches.forEach((match) -> log.info("Match {} {} ?== {} {}", genusName, genusAuthority, match.getGenusName(), match.getGenusAuthority()));
			return best;
		}

		return null;
	}


}