InventoryViabilityEnvServiceImpl.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 lombok.extern.slf4j.Slf4j;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.api.exception.NotFoundElement;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.InventoryViabilityDataEnvironmentMap;
import org.gringlobal.model.InventoryViabilityEnvironment;
import org.gringlobal.persistence.InventoryViabilityDataEnvMapRepository;
import org.gringlobal.persistence.InventoryViabilityDataRepository;
import org.gringlobal.persistence.InventoryViabilityEnvRepository;
import org.gringlobal.service.InventoryViabilityEnvService;
import org.gringlobal.service.filter.InventoryViabilityEnvFilter;
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;
import org.springframework.validation.annotation.Validated;

@Service
@Validated
@Transactional(readOnly = true)
@Slf4j
public class InventoryViabilityEnvServiceImpl extends FilteredCRUDServiceImpl<InventoryViabilityEnvironment, InventoryViabilityEnvFilter, InventoryViabilityEnvRepository> implements InventoryViabilityEnvService {

	@Autowired
	private InventoryViabilityDataRepository iVDataRepository;

	@Autowired
	private InventoryViabilityDataEnvMapRepository mapRepository;

	@Override
	@Transactional
	public InventoryViabilityEnvironment create(final InventoryViabilityEnvironment source) {
		log.debug("Create InventoryViabilityEnvironment. Input data {}", source);
		InventoryViabilityEnvironment env = new InventoryViabilityEnvironment();
		env.apply(source);

		InventoryViabilityEnvironment saved = repository.save(env);
		saved.lazyLoad();

		return saved;
	}

	@Override
	public InventoryViabilityEnvironment update(InventoryViabilityEnvironment input, InventoryViabilityEnvironment target) {
		throw new InvalidApiUsageException("InventoryViabilityEnvironment cannot be modifiable.");
	}

	@Override
	@Transactional
	public InventoryViabilityDataEnvironmentMap registerEnvironment(final InventoryViabilityDataEnvironmentMap source) {
		var map = new InventoryViabilityDataEnvironmentMap();
		map.apply(source);

		if (source.getInventoryViabilityData().getId() == null) {
			throw new org.gringlobal.api.exception.InvalidApiUsageException("InventoryViabilityData must be provided.");
		} else {
			var loaded = iVDataRepository.findById(source.getInventoryViabilityData().getId());
			map.setInventoryViabilityData(loaded.orElseThrow(() -> { throw new NotFoundElement("InventoryViabilityData not found."); }));
		}

		if (source.getInventoryViabilityEnvironment().getId() != null) {
			var loaded = repository.findById(source.getInventoryViabilityEnvironment().getId());
			map.setInventoryViabilityEnvironment(loaded.orElseThrow(() -> { throw new NotFoundElement("InventoryViabilityEnvironment not found."); }));
		} else {
			map.setInventoryViabilityEnvironment(create(source.getInventoryViabilityEnvironment()));
		}

		InventoryViabilityDataEnvironmentMap saved = mapRepository.save(map);
		saved.lazyLoad();

		return saved;
	}

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