TaxonomyFamilyCRUDServiceImpl.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 org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.QTaxonomyFamily;
import org.gringlobal.model.TaxonomyFamily;
import org.gringlobal.persistence.TaxonomyFamilyRepository;
import org.gringlobal.service.TaxonomyFamilyCRUDService;
import org.gringlobal.service.filter.TaxonomyFamilyFilter;
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 TaxonomyFamilyCRUDService.
 */
@Service
public class TaxonomyFamilyCRUDServiceImpl extends FilteredCRUDService2Impl<TaxonomyFamily, TaxonomyFamilyFilter, TaxonomyFamilyRepository> implements TaxonomyFamilyCRUDService {

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

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Taxonomy', 'CREATE')")
	public TaxonomyFamily createFast(TaxonomyFamily source) {
		TaxonomyFamily record = new TaxonomyFamily();
		record.apply(source);
		return repository.save(record);
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Taxonomy', 'ADMINISTRATION')")
	public TaxonomyFamily update(TaxonomyFamily updated, TaxonomyFamily target) {
		if (target.getGrinId() != null) {
			throw new InvalidApiUsageException("TaxonomyFamily 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', 'ADMINISTRATION')")
	public TaxonomyFamily updateFast(TaxonomyFamily updated, TaxonomyFamily target) {
		if (target.getGrinId() != null) {
			throw new InvalidApiUsageException("TaxonomyFamily records sourced from GRIN Taxonomy are unmodifiable");
		}
		target.apply(updated);
		return repository.save(target);
	}

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

	@Override
	public Page<TaxonomyFamily> list(TaxonomyFamilyFilter filter, Pageable page) throws SearchException {
		return super.list(TaxonomyFamily.class, filter, page);
	}

	@Override
	protected JPAQuery<TaxonomyFamily> entityListQuery() {
		return jpaQueryFactory.selectFrom(QTaxonomyFamily.taxonomyFamily)
				// type genus
				.leftJoin(QTaxonomyFamily.taxonomyFamily.typeTaxonomyGenus()).fetchJoin()
				// current
				.leftJoin(QTaxonomyFamily.taxonomyFamily.currentTaxonomyFamily()).fetchJoin();
	}

	@Override
	protected NumberPath<Long> entityIdPredicate() {
		return QTaxonomyFamily.taxonomyFamily.id;
	}
}