dnet-applications/apps/dnet-orgs-database-application/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java

413 lines
17 KiB
Java
Raw Normal View History

2020-07-03 12:09:22 +02:00
package eu.dnetlib.organizations.controller;
import java.io.IOException;
2021-04-09 12:17:11 +02:00
import java.time.OffsetDateTime;
2020-07-03 12:09:22 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletResponse;
2020-07-03 12:09:22 +02:00
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
2020-07-03 12:09:22 +02:00
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
2020-07-03 12:09:22 +02:00
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
2021-01-27 12:17:38 +01:00
import eu.dnetlib.common.controller.AbstractDnetController;
2021-04-09 12:17:11 +02:00
import eu.dnetlib.organizations.model.JournalEntry;
import eu.dnetlib.organizations.model.Note;
2020-07-03 12:09:22 +02:00
import eu.dnetlib.organizations.model.OpenaireDuplicate;
import eu.dnetlib.organizations.model.utils.BrowseEntry;
import eu.dnetlib.organizations.model.utils.OrganizationConflict;
import eu.dnetlib.organizations.model.view.ConflictGroupView;
import eu.dnetlib.organizations.model.view.DuplicateGroupView;
import eu.dnetlib.organizations.model.view.OpenaireDuplicateView;
2020-07-03 12:09:22 +02:00
import eu.dnetlib.organizations.model.view.OrganizationInfoView;
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
import eu.dnetlib.organizations.model.view.OrganizationView;
2021-04-09 12:17:11 +02:00
import eu.dnetlib.organizations.repository.JournalEntryRepository;
import eu.dnetlib.organizations.repository.NoteRepository;
2020-07-03 12:09:22 +02:00
import eu.dnetlib.organizations.repository.UserCountryRepository;
import eu.dnetlib.organizations.repository.readonly.ConflictGroupViewRepository;
import eu.dnetlib.organizations.repository.readonly.DuplicateGroupViewRepository;
import eu.dnetlib.organizations.repository.readonly.OpenaireDuplicateViewRepository;
2020-07-03 12:09:22 +02:00
import eu.dnetlib.organizations.repository.readonly.OrganizationInfoViewRepository;
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
import eu.dnetlib.organizations.repository.readonly.SuggestionInfoViewByCountryRepository;
import eu.dnetlib.organizations.utils.CSVConverter;
2020-07-03 12:09:22 +02:00
import eu.dnetlib.organizations.utils.DatabaseUtils;
2020-10-29 15:31:47 +01:00
import eu.dnetlib.organizations.utils.OrganizationStatus;
2020-07-03 12:09:22 +02:00
@RestController
@RequestMapping("/api/organizations")
2021-01-27 12:17:38 +01:00
public class OrganizationController extends AbstractDnetController {
2020-07-03 12:09:22 +02:00
2021-05-05 15:12:53 +02:00
private static final String SPECIAL_STATUS_FOR_CANDIDATE_DUP = "candidate_dup";
2020-07-03 12:09:22 +02:00
@Autowired
private OrganizationViewRepository organizationViewRepository;
@Autowired
private OrganizationInfoViewRepository organizationInfoViewRepository;
@Autowired
private OrganizationSimpleViewRepository organizationSimpleViewRepository;
@Autowired
private OpenaireDuplicateViewRepository openaireDuplicateViewRepository;
2020-07-03 12:09:22 +02:00
@Autowired
private ConflictGroupViewRepository conflictGroupViewRepository;
@Autowired
private SuggestionInfoViewByCountryRepository suggestionInfoViewByCountryRepository;
@Autowired
private UserCountryRepository userCountryRepository;
@Autowired
private DuplicateGroupViewRepository duplicateGroupViewRepository;
@Autowired
2021-04-09 12:17:11 +02:00
private NoteRepository noteRepository;
@Autowired
private JournalEntryRepository journalEntryRepository;
@Autowired
2020-07-03 12:09:22 +02:00
private DatabaseUtils databaseUtils;
@PostMapping("/save")
2020-07-03 12:09:22 +02:00
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
2020-07-03 12:09:22 +02:00
if (StringUtils.isBlank(org.getName())) {
throw new RuntimeException("Missing field: name");
} else if (StringUtils.isBlank(org.getCountry())) {
throw new RuntimeException("Missing field: country");
} else if (StringUtils.isBlank(org.getType())) {
throw new RuntimeException("Missing field: type");
2020-11-04 14:18:41 +01:00
} else if (UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
2020-11-04 14:18:41 +01:00
final String orgId =
2020-11-23 12:02:33 +01:00
databaseUtils.insertOrUpdateOrganization(org, UserInfo.getEmail(authentication), UserInfo.isSimpleUser(authentication));
2020-11-04 14:18:41 +01:00
return Arrays.asList(orgId);
} else {
throw new RuntimeException("User not authorized");
}
2020-07-03 12:09:22 +02:00
}
@GetMapping("/info")
2020-07-03 12:09:22 +02:00
public OrganizationInfoView infoById(@RequestParam final String id, final Authentication authentication) {
2022-09-26 14:16:22 +02:00
return organizationInfoViewRepository.findById(id).get();
2020-07-03 12:09:22 +02:00
}
@GetMapping("/suggestionsInfo")
2020-07-03 12:09:22 +02:00
public SuggestionInfo suggestionsInfo(final Authentication authentication) {
final SuggestionInfo info = new SuggestionInfo();
if (UserInfo.isSuperAdmin(authentication)) {
suggestionInfoViewByCountryRepository.findAll().forEach(info::add);
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
2020-11-23 12:02:33 +01:00
userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream()
.map(suggestionInfoViewByCountryRepository::findById)
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(info::add);
2020-07-03 12:09:22 +02:00
}
return info;
}
@GetMapping("/get")
2020-07-03 12:09:22 +02:00
public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
final OrganizationView org = organizationViewRepository.findById(id).get();
2020-11-04 14:18:41 +01:00
if (UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
2020-07-03 12:09:22 +02:00
return org;
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/conflicts")
2020-07-03 12:09:22 +02:00
public List<OrganizationConflict> conflicts(@RequestParam final String id, final Authentication authentication) {
2020-11-23 12:02:33 +01:00
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
return databaseUtils.listConflictsForId(id);
2020-07-03 12:09:22 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/duplicates")
public List<OpenaireDuplicateView> duplicates(@RequestParam final String id, final Authentication authentication) {
2020-11-23 12:02:33 +01:00
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
2022-05-26 11:47:01 +02:00
return listDuplicates(id);
2020-07-03 12:09:22 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
2022-05-26 11:47:01 +02:00
private List<OpenaireDuplicateView> listDuplicates(final String id) {
2022-09-26 14:16:22 +02:00
return openaireDuplicateViewRepository.findByLocalId(id);
2022-05-26 11:47:01 +02:00
}
@GetMapping("/conflicts/byCountry/{country}")
public Collection<Set<OrganizationConflict>> findConflictsByCountry(@PathVariable final String country, final Authentication authentication) {
2020-07-03 12:09:22 +02:00
if (UserInfo.isSuperAdmin(authentication)) {
return groupConflicts(conflictGroupViewRepository.findByCountry1OrCountry2(country, country).stream());
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
2020-11-23 12:02:33 +01:00
final Stream<ConflictGroupView> list = userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream()
.filter(country::equalsIgnoreCase)
.map(c -> conflictGroupViewRepository.findByCountry1OrCountry2(c, c).stream())
.findFirst()
.orElse(Stream.empty());
2020-07-03 12:09:22 +02:00
return groupConflicts(list);
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/duplicates/byCountry/{country}")
2020-07-03 12:09:22 +02:00
public Iterable<DuplicateGroupView> findDuplicatesByCountry(@PathVariable final String country, final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)) {
return duplicateGroupViewRepository.findByCountry(country);
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
2020-11-23 12:02:33 +01:00
return userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream()
.filter(country::equalsIgnoreCase)
.map(duplicateGroupViewRepository::findByCountry)
.findFirst()
.orElse(new ArrayList<DuplicateGroupView>());
2020-07-03 12:09:22 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
2020-07-03 12:09:22 +02:00
@GetMapping(value = "/duplicates/byCountry/{country}/csv", produces = "text/csv")
public void findDuplicatesByCountryCSV(@PathVariable final String country, final HttpServletResponse res, final Authentication authentication)
throws IOException {
final Iterable<DuplicateGroupView> list = findDuplicatesByCountry(country, authentication);
CSVConverter.writeCSV(res.getOutputStream(), list, DuplicateGroupView.class, "id", "name", "city", "country", "numberOfDuplicates");
2020-07-03 12:09:22 +02:00
}
private Collection<Set<OrganizationConflict>> groupConflicts(final Stream<ConflictGroupView> stream) {
final Map<String, Set<OrganizationConflict>> map = new TreeMap<>();
2020-07-03 12:09:22 +02:00
stream.forEach(c -> {
if (!map.containsKey(c.getGroup())) {
map.put(c.getGroup(), new TreeSet<>());
}
map.get(c.getGroup()).add(new OrganizationConflict(c.getId1(), c.getName1(), c.getType1(), c.getCity1(), c.getCountry1()));
map.get(c.getGroup()).add(new OrganizationConflict(c.getId2(), c.getName2(), c.getType2(), c.getCity2(), c.getCountry2()));
2020-07-03 12:09:22 +02:00
});
return map.values();
}
@PostMapping("/duplicates")
public List<OpenaireDuplicateView> duplicates(@RequestBody final List<OpenaireDuplicate> simrels, final Authentication authentication) {
if (simrels.isEmpty()) { return new ArrayList<>(); }
2020-07-03 12:09:22 +02:00
final boolean b = UserInfo.isSuperAdmin(authentication)
|| simrels.stream()
.map(OpenaireDuplicate::getLocalId)
.distinct()
2020-11-23 12:02:33 +01:00
.allMatch(id -> userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication)));
2020-07-03 12:09:22 +02:00
if (b) {
2020-11-23 12:02:33 +01:00
databaseUtils.saveDuplicates(simrels, UserInfo.getEmail(authentication));
2022-05-26 11:47:01 +02:00
return listDuplicates(simrels.get(0).getLocalId());
2020-07-03 12:09:22 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/search/{page}/{size}")
2021-04-20 11:43:17 +02:00
public Page<OrganizationSimpleView> search(@PathVariable final int page,
@PathVariable final int size,
@RequestParam final String q,
2020-10-29 15:31:47 +01:00
@RequestParam(required = false, defaultValue = "") final String status,
final Authentication authentication) {
2021-05-05 15:12:53 +02:00
if (status.equals(SPECIAL_STATUS_FOR_CANDIDATE_DUP)) {
return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.searchCandidateDuplicates(q, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchCandidateDuplicatesForUser(q, UserInfo.getEmail(authentication), PageRequest.of(page, size));
2021-04-20 11:43:17 +02:00
} else {
2021-05-05 15:12:53 +02:00
final List<String> statuses;
if (StringUtils.isNotBlank(status)) {
statuses = Arrays.asList(status.split(","));
} else if (UserInfo.isSimpleUser(authentication)) {
statuses = Arrays.asList(OrganizationStatus.approved.toString());
} else {
statuses = Arrays.asList(OrganizationStatus.approved.toString(), OrganizationStatus.suggested.toString());
}
2021-04-20 11:43:17 +02:00
2021-05-05 15:12:53 +02:00
return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.search(q, statuses, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, PageRequest.of(page, size));
}
2020-07-03 12:09:22 +02:00
}
2020-10-13 14:48:04 +02:00
@GetMapping("/byCountry/{status}/{code}/{page}/{size}")
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String status,
@PathVariable final String code,
@PathVariable final int page,
@PathVariable final int size,
final Authentication authentication) {
2020-11-04 14:18:41 +01:00
if (UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
2020-10-13 14:48:04 +02:00
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByCountryOrderByName(code, PageRequest.of(page, size));
} else {
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status, PageRequest.of(page, size));
}
2020-07-03 12:09:22 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
2020-10-05 12:16:49 +02:00
@GetMapping("/byCountry/{status}/{code}")
2020-10-14 16:28:00 +02:00
public Iterable<OrganizationSimpleView> findOrgsByStatusAndCountry(@PathVariable final String status,
2020-10-05 12:16:49 +02:00
@PathVariable final String code,
final Authentication authentication) {
2020-11-04 14:18:41 +01:00
if (UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
if (status.equalsIgnoreCase("all")) {
2020-10-05 12:16:49 +02:00
return organizationSimpleViewRepository.findByCountryOrderByName(code);
} else {
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status);
2020-10-05 12:16:49 +02:00
}
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping(value = "/byCountry/{status}/{code}/csv", produces = "text/csv")
public void findOrgsByStatusAndCountryCSV(@PathVariable final String status,
@PathVariable final String code,
final HttpServletResponse res,
final Authentication authentication) throws IOException {
final Iterable<OrganizationSimpleView> list = findOrgsByStatusAndCountry(status, code, authentication);
CSVConverter.writeCSV(res
.getOutputStream(), list, OrganizationSimpleView.class, "id", "name", "type", "city", "country", "acronyms", "urls", "status", "nSimilarDups", "nSuggestedDups", "nDifferentDups");
}
2020-10-13 14:48:04 +02:00
@GetMapping("/byType/{status}/{type}/{page}/{size}")
public Page<OrganizationSimpleView> findByType(@PathVariable final String status,
@PathVariable final String type,
@PathVariable final int page,
@PathVariable final int size,
final Authentication authentication) {
2020-10-13 14:48:04 +02:00
if (UserInfo.isSuperAdmin(authentication)) {
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size));
} else {
return organizationSimpleViewRepository.findByTypeAndStatusOrderByName(type, status, PageRequest.of(page, size));
}
} else {
if (status.equalsIgnoreCase("all")) {
2020-11-23 12:02:33 +01:00
return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), PageRequest.of(page, size));
2020-10-13 14:48:04 +02:00
} else {
2020-11-04 14:18:41 +01:00
return organizationSimpleViewRepository
2020-11-23 12:02:33 +01:00
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), PageRequest.of(page, size));
2020-10-13 14:48:04 +02:00
}
}
2020-07-03 12:09:22 +02:00
}
@GetMapping("/browse/countries")
2020-07-03 12:09:22 +02:00
public List<BrowseEntry> browseCountries(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication)
? databaseUtils.browseCountries()
2020-11-23 12:02:33 +01:00
: databaseUtils.browseCountriesForUser(UserInfo.getEmail(authentication));
2020-07-03 12:09:22 +02:00
}
@GetMapping("/browse/types")
2020-07-03 12:09:22 +02:00
public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication)
? databaseUtils.browseTypes()
2020-11-23 12:02:33 +01:00
: databaseUtils.browseTypesForUser(UserInfo.getEmail(authentication));
2020-07-03 12:09:22 +02:00
}
2020-10-30 10:05:23 +01:00
@PostMapping("/conflicts/fix/similar")
public List<String> fixConflictSim(final Authentication authentication, @RequestBody final List<String> ids) {
2020-11-04 14:18:41 +01:00
if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
final String newOrgId = databaseUtils.fixConflictSimilars(ids, UserInfo.getEmail(authentication));
2020-10-19 15:12:08 +02:00
return Arrays.asList(newOrgId);
2020-07-03 12:09:22 +02:00
} else {
return new ArrayList<>();
}
2020-10-30 10:05:23 +01:00
}
2020-07-03 12:09:22 +02:00
2020-10-30 10:05:23 +01:00
@PostMapping("/conflicts/fix/different")
public List<String> fixConflictDiff(final Authentication authentication, @RequestBody final List<String> ids) {
2020-11-04 14:18:41 +01:00
if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication)
2020-11-23 12:02:33 +01:00
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
databaseUtils.fixConflictDifferents(ids, UserInfo.getEmail(authentication));
2020-10-30 10:05:23 +01:00
return ids;
} else {
return new ArrayList<>();
}
2020-07-03 12:09:22 +02:00
}
2020-10-30 10:05:23 +01:00
2021-04-09 12:17:11 +02:00
@GetMapping("/note")
public Note noteById(@RequestParam final String id, final Authentication authentication) {
final OrganizationView org = organizationViewRepository.findById(id).get();
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return noteRepository.findById(id).orElse(new Note(id, "", null, null));
} else {
throw new RuntimeException("User not authorized");
}
}
@PostMapping("/note")
public Note saveNote(@RequestBody final Note note, final Authentication authentication) {
2021-04-09 16:50:26 +02:00
final String orgId = note.getOrgId();
final OrganizationView org = organizationViewRepository.findById(orgId).get();
2021-04-09 12:17:11 +02:00
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
2021-04-09 16:50:26 +02:00
if (StringUtils.isNotBlank(note.getNote())) {
note.setModifiedBy(UserInfo.getEmail(authentication));
note.setModificationDate(OffsetDateTime.now());
return noteRepository.save(note);
} else {
noteRepository.deleteById(orgId);
return new Note(orgId, "", null, null);
}
2021-04-09 12:17:11 +02:00
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/journal")
public List<JournalEntry> journalEntriesById(@RequestParam final String id, final Authentication authentication) {
final OrganizationView org = organizationViewRepository.findById(id).get();
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return journalEntryRepository.findByOrgIdOrderByDateDesc(id);
} else {
throw new RuntimeException("User not authorized");
}
}
2020-07-03 12:09:22 +02:00
}