PartnerApiServiceImpl.java

/*
 * Copyright 2025 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.v1.facade.impl;

import org.genesys.server.api.v1.facade.PartnerApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.FaoInstitute;
import org.genesys.server.api.v1.model.Partner;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.filters.PartnerFilter;
import org.genesys.server.service.PartnerService;
import org.genesys.server.service.filter.InstituteFilter;
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;

import java.net.MalformedURLException;
import java.util.Set;
import java.util.UUID;

@Service("partnerV1APIService")
@Validated
@Transactional(readOnly = true)
public class PartnerApiServiceImpl implements PartnerApiService {

	@Autowired
	private PartnerService service;

	@Autowired
	private APIv1Mapper mapper;

	@Override
	public Partner load(UUID uuid) {
		return mapper.map(service.load(uuid));
	}

	@Override
	public PartnerDetails loadPartnerDetails(UUID uuid) {
		return mapper.map(service.loadPartnerDetails(uuid));
	}

	@Override
	@Transactional
	public Partner deletePartner(UUID uuid, int version) {
		return mapper.map(service.remove(service.loadPartner(uuid, version)));
	}

	@Override
	@Transactional
	public Partner create(Partner source) {
		return mapper.map(service.create(mapper.map(source)));
	}

	@Override
	@Transactional
	public Partner update(Partner source) {
		return mapper.map(service.update(mapper.map(source)));
	}

	@Override
	@Transactional
	public Partner addInstitutes(UUID uuid, Set<String> instituteCodes) {
		var partner = service.get(uuid);
		return mapper.map(service.addInstitutes(partner, instituteCodes));
	}

	@Override
	@Transactional
	public Partner removeInstitutes(UUID uuid, Set<String> instituteCodes) {
		var partner = service.get(uuid);
		return mapper.map(service.removeInstitutes(partner, instituteCodes));
	}

	@Override
	public Page<Partner> list(PartnerFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.list(filter, page), mapper::map);
	}

	@Override
	public Page<FaoInstitute> loadPartnerInstitutes(UUID uuid, InstituteFilter filter, Pageable page) {
		return mapper.map(service.loadPartnerInstitutes(uuid, filter, page), (inst) -> {
			try {
				return mapper.map(inst);
			} catch (MalformedURLException e) {
				throw new RuntimeException(e);
			}
		});
	}

	@Override
	public Page<Partner> listPartnersForCurrentUser(PartnerFilter partnerFilter, Pageable page) {
		return mapper.map(service.listPartnersForCurrentUser(partnerFilter, page), mapper::map);
	}

}