irish-monitor-service/src/main/java/eu/dnetlib/irishmonitorservice/controllers/StakeholderPublicationsCont...

49 lines
2.2 KiB
Java

package eu.dnetlib.irishmonitorservice.controllers;
import eu.dnetlib.irishmonitorservice.entities.StakeholderPublications;
import eu.dnetlib.irishmonitorservice.services.CacheService;
import eu.dnetlib.irishmonitorservice.services.StatsToolService;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("publications")
public class StakeholderPublicationsController {
private final StakeholderService service;
private final CacheService cacheService;
@Autowired
public StakeholderPublicationsController(StakeholderService service, CacheService cacheService) {
this.service = service;
this.cacheService = cacheService;
}
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException, InterruptedException {
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false);
List<StakeholderPublications> stakeholderPublications = new ArrayList<>();
List<List<String>> data = this.cacheService.getResponse(type);
List<Stakeholder> remain = new ArrayList<>(stakeholders);
if (data != null) {
data.forEach(entity -> {
String id = entity.get(1);
stakeholders.stream().filter(stakeholder -> stakeholder.getIndex_id().equals(id)).findFirst().ifPresent(stakeholder -> {
remain.remove(stakeholder);
stakeholderPublications.add(new StakeholderPublications(stakeholder, Integer.parseInt(entity.get(0))));
});
});
}
remain.forEach(stakeholder -> stakeholderPublications.add(new StakeholderPublications(stakeholder, 0)));
return stakeholderPublications;
}
}