CropApiServiceImpl.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.genesys.server.api.v2.facade.impl;

import org.apache.commons.lang3.time.StopWatch;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.server.api.v2.facade.CropApiService;
import org.genesys.server.api.v2.mapper.MapstructMapper;
import org.genesys.server.api.v2.model.impl.CropDTO;
import org.genesys.server.api.v2.model.impl.CropDetailsDTO;
import org.genesys.server.api.v2.model.impl.CropInfo;
import org.genesys.server.api.v2.model.impl.CropLangDTO;
import org.genesys.server.api.v2.model.impl.TranslatedCropDTO;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.model.impl.CropLang;
import org.genesys.server.service.CropService;
import org.genesys.server.service.CropTranslationService;
import org.genesys.server.service.filter.CropFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Locale;

@Service("cropV2APIService")
@Validated
@Transactional(readOnly = true)
@Slf4j
public class CropApiServiceImpl extends APIFilteredTranslatedServiceFacadeImpl<CropService, CropDTO,
	TranslatedCropDTO, CropLangDTO, Crop, CropLang,
	CropTranslationService.TranslatedCrop, CropFilter> implements CropApiService {
	
	private static final String CACHE_CROPS = "hibernate.org.genesys.server.model.impl.Crop.cache";

	@Autowired
	private MapstructMapper mapper;

	@Autowired
	private CropService cropService;

	@Override
	protected TranslatedCropDTO convertTranslation(CropTranslationService.TranslatedCrop source) {
		return mapper.map(source);
	}

	@Override
	protected CropLang convertLang(CropLangDTO source) {
		return mapper.map(source);
	}

	@Override
	protected CropLangDTO convertLang(CropLang source) {
		return mapper.map(source);
	}

	@Override
	protected Crop convert(CropDTO source) {
		return mapper.map(source);
	}

	@Override
	protected CropDTO convert(Crop source) {
		return mapper.map(source);
	}

	@Override
	public CropDTO getCrop(String shortName) {
		return mapper.map(cropService.getCrop(shortName));
	}

	@Override
	public CropDetailsDTO getDetails(String shortName, Locale locale) throws InvalidRepositoryPathException {
		return mapper.map(cropService.getDetails(shortName, locale));
	}

	@Override
	@Transactional
	public void unlinkAccessionsForCrop(CropDTO crop) {
		cropService.unlinkAccessionsForCrop(mapper.map(crop));
	}

	@Override
	@Transactional
	public void linkAccessionsWithCrop(CropDTO crop) {
		cropService.linkAccessionsWithCrop(mapper.map(crop));
	}

	@Override
	@Cacheable(value = CACHE_CROPS, key = "'listCropsInfo-'.concat(#locale)")
	public List<CropInfo> listCropsInfo(Locale locale) throws Exception {
		StopWatch stopWatch = StopWatch.createStarted();
		var list = cropService.listDetails(locale);
		log.debug("Loaded all crops in {}ms", stopWatch.getTime());
		var m = mapper.map(list, mapper::mapInfo);
		log.debug("Mapped all crop info in {}ms", stopWatch.getTime());
		return m;
	}
}