GeographyServiceImpl.java

/*
 * Copyright 2026 Global Crop Diversity Trust
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
 */

package org.gringlobal.service.impl;

import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Geography;
import org.gringlobal.persistence.GeographyRepository;
import org.gringlobal.service.GeographyService;
import org.gringlobal.service.filter.GeographyFilter;

import lombok.extern.slf4j.Slf4j;
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;

/**
 * The Class GeographyServiceImpl.
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class GeographyServiceImpl extends FilteredCRUDService2Impl<Geography, GeographyFilter, GeographyRepository> implements GeographyService {

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'CREATE')")
	public Geography create(Geography source) {
		return _lazyLoad(createFast(source));
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'CREATE')")
	public Geography createFast(Geography source) {
		log.debug("Create Geography. Input data {}", source);
		Geography geography = new Geography();
		geography.apply(source);
		var created = repository.save(geography);
		if ("Y".equals(created.getIsValid()) && created.getCurrentGeography() == null) {
			created.setCurrentGeography(created);
		}
		return repository.save(created);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'WRITE')")
	public Geography update(Geography updated) {
		return super.update(updated);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'WRITE')")
	public Geography update(Geography updated, Geography target) {
		return _lazyLoad(updateFast(updated, target));
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'WRITE')")
	public Geography updateFast(Geography updated, Geography target) {
		log.debug("Update Geography. Input data {}", updated);
		target.apply(updated);
		if ("N".equals(target.getIsValid()) && target.getCurrentGeography() != null && target.getId().equals(target.getCurrentGeography().getId())) {
			throw new InvalidApiUsageException("Invalid geography cannot reference itself in the current geography.");
		}
		return repository.save(target);
	}

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

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'DELETE')")
	public Geography remove(Geography entity) {
		return super.remove(entity);
	}
}