71 lines
3.4 KiB
Java
71 lines
3.4 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.enums.ReferenceType;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.UserEntity;
|
|
import eu.eudat.data.query.items.item.funder.FunderCriteriaRequest;
|
|
import eu.eudat.logic.builders.model.models.FunderBuilder;
|
|
import eu.eudat.service.reference.external.criteria.ExternalReferenceCriteria;
|
|
import eu.eudat.commons.exceptions.HugeResultSetException;
|
|
import eu.eudat.service.reference.external.RemoteFetcher;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.logic.utilities.helpers.ListHelper;
|
|
import eu.eudat.models.data.external.ExternalSourcesItemModel;
|
|
import eu.eudat.models.data.external.FundersExternalSourcesModel;
|
|
import eu.eudat.models.data.funder.Funder;
|
|
import eu.eudat.queryable.QueryableList;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class FunderManager {
|
|
|
|
private ApiContext apiContext;
|
|
private RemoteFetcher remoteFetcher;
|
|
private ListHelper listHelper;
|
|
private final UserScope userScope;
|
|
|
|
public FunderManager(ApiContext apiContext, RemoteFetcher remoteFetcher, ListHelper listHelper, UserScope userScope) {
|
|
this.apiContext = apiContext;
|
|
this.remoteFetcher = remoteFetcher;
|
|
this.listHelper = listHelper;
|
|
this.userScope = userScope;
|
|
}
|
|
|
|
public List<Funder> getCriteriaWithExternal(FunderCriteriaRequest funderCriteria) throws HugeResultSetException, MyNotFoundException, InvalidApplicationException {
|
|
UserEntity userInfo = new UserEntity();
|
|
userInfo.setId(this.userScope.getUserId());
|
|
funderCriteria.getCriteria().setReference("dmp:");
|
|
QueryableList<eu.eudat.data.old.Funder> items = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().getWithCritetia(funderCriteria.getCriteria());
|
|
QueryableList<eu.eudat.data.old.Funder> authItems = apiContext.getOperationsContext().getDatabaseRepository().getFunderDao().getAuthenticated(items, userInfo);
|
|
List<Funder> funders = authItems.select(item -> new eu.eudat.models.data.funder.Funder().fromDataModel(item));
|
|
ExternalReferenceCriteria externalReferenceCriteria = new ExternalReferenceCriteria(funderCriteria.getCriteria().getLike());
|
|
List<Map<String, String>> remoteRepos = this.apiContext.getOperationsContext().getRemoteFetcher().get(ReferenceType.Funder, externalReferenceCriteria, null);
|
|
FundersExternalSourcesModel fundersExternalSourcesModel = new FundersExternalSourcesModel().fromExternalItem(remoteRepos);
|
|
for (ExternalSourcesItemModel externalListingItem : fundersExternalSourcesModel) {
|
|
eu.eudat.models.data.funder.Funder funder = apiContext.getOperationsContext().getBuilderFactory().getBuilder(FunderBuilder.class)
|
|
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
|
|
.status(eu.eudat.data.old.Funder.Status.fromInteger(0))
|
|
.key(externalListingItem.getKey())
|
|
.source(externalListingItem.getTag())
|
|
.build();
|
|
if (externalListingItem.getSource() != null) {
|
|
funder.setSource(externalListingItem.getSource());
|
|
} else {
|
|
funder.setSource(externalListingItem.getTag());
|
|
}
|
|
|
|
funders.add(funder);
|
|
}
|
|
funders.sort(Comparator.comparing(Funder::getLabel));
|
|
funders = funders.stream().filter(listHelper.distinctByKey(Funder::getLabel)).collect(Collectors.toList());
|
|
return funders;
|
|
}
|
|
}
|