RegionServiceImpl.java

/*
 * Copyright 2026 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.List;

import org.gringlobal.model.QGeographyRegionMap;
import org.gringlobal.model.QRegion;
import org.gringlobal.model.Region;
import org.gringlobal.persistence.GeographyRegionMapRepository;
import org.gringlobal.persistence.RegionRepository;
import org.gringlobal.service.RegionService;
import org.gringlobal.service.filter.RegionFilter;
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;

@Service
@Transactional(readOnly = true)
public class RegionServiceImpl extends FilteredCRUDService2Impl<Region, RegionFilter, RegionRepository>
	implements RegionService {

	@Autowired
	private GeographyRegionMapRepository geographyRegionMapRepository;

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

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'WRITE')")
	public Region updateFast(Region updated, Region target) {
		target.apply(updated);
		return repository.save(target);
	}

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

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'CREATE')")
	public Region createFast(Region source) {
		Region region = new Region();
		region.apply(source);
		return repository.save(region);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('GeographyData', 'DELETE')")
	public Region remove(Region entity) {
		var regionGeographies = geographyRegionMapRepository.findAll(QGeographyRegionMap.geographyRegionMap.region().eq(entity));
		geographyRegionMapRepository.deleteAll(regionGeographies);
		return super.remove(entity);
	}

	@Override
	public List<Region> autocomplete(String term) {
		if (term == null || term.length() == 0) return List.of();
		return jpaQueryFactory
			.selectFrom(QRegion.region)
			.where(QRegion.region.continent.startsWithIgnoreCase(term).or(QRegion.region.subcontinent.startsWithIgnoreCase(term)))
			.limit(20)
			.orderBy(QRegion.region.continent.asc(), QRegion.region.subcontinent.asc(), QRegion.region.id.asc())
			.fetch();
	}
}