CountryServiceImpl.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.genesys.server.service.impl;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.model.impl.Country;
import org.genesys.server.model.impl.GeoRegion;
import org.genesys.server.persistence.CountryRepository;
import org.genesys.server.service.CountryService;
import org.genesys.server.service.GeoRegionService;
import org.genesys.server.service.filter.CountryFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;


@Service
@Transactional(readOnly = true)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class CountryServiceImpl extends FilteredCRUDServiceImpl<Country, CountryFilter, CountryRepository> implements CountryService {

	@Autowired
	private GeoRegionService geoRegionService;

	private final ObjectMapper objectMapper = new ObjectMapper();

	@Override
	public Country create(Country source) {
		throw new UnsupportedOperationException();
	}

	@Override
	@Transactional
	public Country update(Country updated, Country target) {

		GeoRegion region = null;
		if (updated.getRegion() != null && updated.getRegion().getId() != null) {
			region = geoRegionService.get(updated.getRegion().getId());
		}
		String nameL = target.getNameL();

		target.apply(updated);
		target.setNameL(nameL);
		target.setRegion(region);

		try {
			customizeTranslations(target, updated.getNameJCustom());
		} catch (IOException e) {
			throw new InvalidApiUsageException("Error while parsing 'nameJCustom': " + e.getLocalizedMessage(), e);
		}

		return _lazyLoad(repository.save(target));
	}

	private void customizeTranslations(final Country country, final String customizedJsonNames) throws IOException {

		ObjectNode jsonNames;
		if (StringUtils.isNotBlank(country.getNameL())) {
			jsonNames = (ObjectNode) objectMapper.readTree(country.getNameL());
		} else
			jsonNames = objectMapper.createObjectNode();

		Map<String, Object> customNamesMap = null;
		if (StringUtils.isNotBlank(country.getNameJCustom()))
			customNamesMap = objectMapper.readValue(country.getNameJCustom(), new TypeReference<>() {});

		// add or override jsonNames
		if (customNamesMap != null) {
			for (Map.Entry<String, Object> entry : customNamesMap.entrySet()) {
				jsonNames.put(entry.getKey(), String.valueOf(entry.getValue()));
			}
		}

		country.setNameJCustom(customizedJsonNames);
		country.setNameL(jsonNames.toString());
	}

	@Override
	public Country remove(Country entity) {
		throw new UnsupportedOperationException();
	}

	@Override
	protected Country _lazyLoad(Country entity) {
		if (entity == null)
			return null;

		if (entity.getRegion() != null)
			entity.getRegion().getId();

		return entity;
	}
}