InventoryViabilityRuleServiceImpl.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.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.querydsl.jpa.impl.JPAQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.InventoryViabilityRule;
import org.gringlobal.model.InventoryViabilityRuleMap;
import org.gringlobal.model.QInventoryViabilityRuleMap;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.persistence.InventoryViabilityRuleMapRepository;
import org.gringlobal.persistence.InventoryViabilityRuleRepository;
import org.gringlobal.service.InventoryViabilityRuleService;
import org.gringlobal.service.TaxonomySpeciesCRUDService;
import org.gringlobal.service.filter.InventoryViabilityRuleFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@Slf4j
public class InventoryViabilityRuleServiceImpl extends FilteredCRUDServiceImpl<InventoryViabilityRule, InventoryViabilityRuleFilter, InventoryViabilityRuleRepository>
		implements InventoryViabilityRuleService {

	@Autowired
	private InventoryViabilityRuleMapRepository viabilityRuleMapRepository;

	@Autowired
	private TaxonomySpeciesCRUDService taxonomySpeciesService;

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

	@Override
	public Page<InventoryViabilityRuleMap> listSpecies(InventoryViabilityRule inventoryViabilityRule, Pageable page) {
		JPAQuery<InventoryViabilityRuleMap> query = jpaQueryFactory.selectFrom(QInventoryViabilityRuleMap.inventoryViabilityRuleMap)
				// joins
				.innerJoin(QInventoryViabilityRuleMap.inventoryViabilityRuleMap.taxonomySpecies()).fetchJoin()
				// predicate
				.where(QInventoryViabilityRuleMap.inventoryViabilityRuleMap.inventoryViabilityRule().eq(inventoryViabilityRule));

		return viabilityRuleMapRepository.findAll(query, page);
	}

	@Override
	@Transactional
	public InventoryViabilityRuleMap addInventoryViabilityRuleMap(InventoryViabilityRuleMap source) {
		log.debug("Create InventoryViabilityRuleMap. Input data {}", source);
		source.setInventoryViabilityRule(get(source.getInventoryViabilityRule().getId()));
		source.setTaxonomySpecies(taxonomySpeciesService.get(source.getTaxonomySpecies().getId()));

		var saved = viabilityRuleMapRepository.save(source);
		saved.lazyLoad();
		return saved;
	}

	@Override
	@Transactional
	public InventoryViabilityRuleMap removeInventoryViabilityRuleMap(long id) {
		var viabilityRuleMap = viabilityRuleMapRepository.getReferenceById(id);
		viabilityRuleMapRepository.delete(viabilityRuleMap);
		return viabilityRuleMap;
	}

	@Override
	@Transactional
	public List<InventoryViabilityRuleMap> addSpecies(InventoryViabilityRule rule, List<TaxonomySpecies> species, String note) {
		if (CollectionUtils.isEmpty(species)) {
			return Collections.emptyList();
		}

		final var loadedRule = get(rule);
		List<InventoryViabilityRuleMap> taxCropMaps = species.stream().map((taxonomySpecies) -> {
			InventoryViabilityRuleMap ruleMap = new InventoryViabilityRuleMap();
			ruleMap.setInventoryViabilityRule(loadedRule);
			ruleMap.setTaxonomySpecies(taxonomySpecies);
			ruleMap.setNote(note);
			return ruleMap;
		}).collect(Collectors.toList());
		var savedMaps = viabilityRuleMapRepository.saveAll(taxCropMaps);
		log.debug("Added {} species to viability rule {}", savedMaps.size(), loadedRule.getName());

		return savedMaps;
	}

	@Override
	@Transactional
	public InventoryViabilityRule create(InventoryViabilityRule source) {
		log.debug("Create InventoryViabilityRule. Input data {}", source);
		var rule = new InventoryViabilityRule();
		rule.apply(source);

		InventoryViabilityRule saved = repository.save(rule);
		saved.lazyLoad();

		return saved;
	}

	@Override
	@Transactional
	public InventoryViabilityRule update(InventoryViabilityRule updated, InventoryViabilityRule target) {
		log.debug("Update InventoryViabilityRule. Input data {}", updated);
		target.apply(updated);

		InventoryViabilityRule saved = repository.save(target);
		saved.lazyLoad();
		return saved;
	}
}