AccessionInvGroupApiServiceImpl.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.gringlobal.api.v2.facade.impl;

import org.gringlobal.api.model.AccessionInvGroupDTO;
import org.gringlobal.api.model.AccessionInvGroupMapDTO;
import org.gringlobal.api.v2.facade.AccessionInvGroupApiService;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionInvGroup;
import org.gringlobal.model.Inventory;
import org.gringlobal.service.AccessionInvGroupService;
import org.gringlobal.service.filter.AccessionInvGroupFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class AccessionInvGroupApiServiceImpl extends APIFilteredServiceFacadeImpl<AccessionInvGroupService, AccessionInvGroupDTO, AccessionInvGroup, AccessionInvGroupFilter> 
implements AccessionInvGroupApiService {

	@Override
	protected AccessionInvGroup convert(AccessionInvGroupDTO source) {
		return mapper.map(source);
	}

	@Override
	protected AccessionInvGroupDTO convert(AccessionInvGroup source) {
		return mapper.map(source);
	}

	@Override
	public Page<AccessionInvGroupMapDTO> filterItems(Long groupId, Pageable page) {
		final AccessionInvGroup accessionInvGroup = service.get(groupId);
		return mapper.map(service.listMembers(accessionInvGroup, page), mapper::map);
	}

	@Override
	@Transactional
	public AccessionInvGroupDTO addInventories(Long groupId, List<Long> inventoryIds) {
		List<Inventory> inventories = inventoryIds.stream().map(Inventory::new).collect(Collectors.toList());
		return mapper.map(service.addMembers(service.get(groupId), inventories, null));
	}

	@Override
	@Transactional
	public AccessionInvGroupDTO addAccessions(Long groupId, List<Long> accessionIds) {
		List<Accession> accessions = accessionIds.stream().map(Accession::new).collect(Collectors.toList());
		return mapper.map(service.addMembers(service.get(groupId), accessions));
	}

	@Override
	@Transactional
	public AccessionInvGroupDTO removeMembers(Long groupId, List<AccessionInvGroupMapDTO> accessionInvGroupMap) {
		return mapper.map(service.removeMembers(service.get(groupId), mapper.map(accessionInvGroupMap, mapper::map)));
	}

}