CooperatorApiServiceImpl.java
/*
* Copyright 2024 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.CooperatorDTO;
import org.gringlobal.api.v2.facade.CooperatorApiService;
import org.gringlobal.model.Cooperator;
import org.gringlobal.service.CooperatorService;
import org.gringlobal.service.filter.CooperatorFilter;
import org.gringlobal.worker.dupe.CooperatorDuplicateFinder;
import org.gringlobal.worker.dupe.DuplicateFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CooperatorApiServiceImpl extends APIFilteredServiceFacadeImpl<CooperatorService, CooperatorDTO, Cooperator, CooperatorFilter> implements CooperatorApiService {
@Autowired(required = false)
private CooperatorDuplicateFinder duplicateFinder;
@Override
protected Cooperator convert(CooperatorDTO source) {
return mapper.map(source);
}
@Override
protected CooperatorDTO convert(Cooperator source) {
return mapper.map(source);
}
@Override
@Transactional
public CooperatorDTO saveAsNew(CooperatorDTO updated) {
return mapper.map(service.saveAsNew(mapper.map(updated)));
}
@Override
@Transactional(readOnly = true)
public List<DuplicateFinder.Hit<CooperatorDTO>> getSimilarCooperatorForID(long id) {
return getSimilarCooperatorForSource(service.get(id));
}
@Override
@Transactional(readOnly = true)
public List<DuplicateFinder.Hit<CooperatorDTO>> getSimilarCooperatorForUnsaved(CooperatorDTO source) {
return getSimilarCooperatorForSource(mapper.map(source));
}
private List<DuplicateFinder.Hit<CooperatorDTO>> getSimilarCooperatorForSource(Cooperator source) {
var result = duplicateFinder.findSimilar(source);
return result.stream().map(hit -> {
var dtoHit = new DuplicateFinder.Hit<>(mapper.map(hit.result), hit.score);
dtoHit.hitRating = hit.hitRating;
dtoHit.matches = hit.matches;
return dtoHit;
}).collect(Collectors.toList());
}
}