Refactor build stakeholder in order to support multiple generation.

This commit is contained in:
Konstantinos Triantafyllou 2024-11-14 11:32:08 +02:00
parent 2a19bbae7f
commit 66f1e6dad4
4 changed files with 105 additions and 25 deletions

View File

@ -2,11 +2,9 @@ package eu.dnetlib.uoamonitorservice.controllers;
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
import eu.dnetlib.uoaadmintoolslibrary.services.PortalService;
import eu.dnetlib.uoamonitorservice.dto.BuildStakeholder;
import eu.dnetlib.uoamonitorservice.dto.ManageStakeholders;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.dto.UpdateUmbrella;
import eu.dnetlib.uoamonitorservice.dto.*;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.service.CommonService;
@ -51,23 +49,25 @@ public class StakeholderController {
"@AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#buildStakeholder.stakeholder.getType()))")
@RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
public ResponseEntity<Stakeholder> buildStakeholder(@RequestBody BuildStakeholder buildStakeholder) {
Stakeholder stakeholder = buildStakeholder.getStakeholder();
log.debug("build stakeholder");
log.debug("Alias: " + stakeholder.getAlias());
Portal portal = portalService.getPortal(stakeholder.getAlias());
if (portal == null) {
portal = new Portal();
portal.setPid(stakeholder.getAlias());
portal.setName(stakeholder.getName());
portal.setType(stakeholder.getType());
portalService.insertPortal(portal);
public ResponseEntity<?> buildStakeholder(@RequestBody BuildStakeholder buildStakeholder) {
log.debug("build stakeholders");
List<Stakeholder> stakeholders = this.stakeholderService.buildStakeholders(buildStakeholder);
log.debug("Alias: " + stakeholders.stream().map(Common::getAlias).toString());
stakeholders.forEach(stakeholder -> {
Portal portal = portalService.getPortal(stakeholder.getAlias());
if (portal == null) {
portal = new Portal();
portal.setPid(stakeholder.getAlias());
portal.setName(stakeholder.getName());
portal.setType(stakeholder.getType());
portalService.insertPortal(portal);
}
});
if(stakeholders.size() == 1) {
return ResponseEntity.ok(stakeholders.get(0));
} else {
return ResponseEntity.ok(stakeholders);
}
stakeholder.setStandalone(buildStakeholder.isStandalone());
if (buildStakeholder.isUmbrella()) {
stakeholder.setUmbrella(new Umbrella<>());
}
return ResponseEntity.ok(this.stakeholderService.buildStakeholder(stakeholder, buildStakeholder.getCopyId()));
}
@PreAuthorize("hasAnyAuthority(" + "@AuthorizationService.PORTAL_ADMIN)")

View File

@ -2,11 +2,15 @@ package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import java.util.List;
import java.util.Map;
public class BuildStakeholder {
private Stakeholder stakeholder;
private String copyId;
private boolean umbrella = false;
private boolean standalone = true;
private List<Map<String, String>> indexFields;
public BuildStakeholder() {
}
@ -42,4 +46,17 @@ public class BuildStakeholder {
public void setStandalone(boolean standalone) {
this.standalone = standalone;
}
public List<Map<String, String>> getIndexFields() {
return indexFields;
}
public void setIndexFields(List<Map<String, String>> indexFields) {
this.indexFields = indexFields;
}
public boolean isMultiBuild() {
return this.indexFields != null && !this.indexFields.isEmpty();
}
}

View File

@ -2,6 +2,7 @@ package eu.dnetlib.uoamonitorservice.entities;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.generics.StakeholderGeneric;
import eu.dnetlib.uoamonitorservice.handlers.BadRequestException;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import org.springframework.data.mongodb.core.mapping.Document;
@ -32,4 +33,13 @@ public class Stakeholder extends StakeholderGeneric<String, String> {
public void removeTopic(String id) {
this.topics.remove(id);
}
public Stakeholder validate() {
if(this.getIndex_id() == null || this.getIndex_id().isEmpty()) {
throw new BadRequestException("index_id cannot be null or empty");
} else if(this.getIndex_name() == null || this.getIndex_name().isEmpty()) {
throw new BadRequestException("index_name cannot be null or empty");
}
return this;
}
}

View File

@ -2,10 +2,7 @@ package eu.dnetlib.uoamonitorservice.service;
import eu.dnetlib.uoaauthorizationlibrary.authorization.exceptions.http.NotFoundException;
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
import eu.dnetlib.uoamonitorservice.dto.ManageStakeholders;
import eu.dnetlib.uoamonitorservice.dto.StakeholderFull;
import eu.dnetlib.uoamonitorservice.dto.TopicFull;
import eu.dnetlib.uoamonitorservice.dto.UpdateUmbrella;
import eu.dnetlib.uoamonitorservice.dto.*;
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.handlers.BadRequestException;
@ -13,6 +10,7 @@ import eu.dnetlib.uoamonitorservice.primitives.Action;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import eu.dnetlib.uoamonitorservice.primitives.Umbrella;
import eu.dnetlib.uoamonitorservice.primitives.Visibility;
import eu.dnetlib.uoamonitorservice.utils.GenericAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
@ -124,6 +122,60 @@ public class StakeholderService {
return stakeholderFull;
}
public Stakeholder generateAlias(Stakeholder stakeholder) {
return this.generateAlias(stakeholder, null);
}
public Stakeholder generateAlias(Stakeholder stakeholder, String field) {
String alias = stakeholder.getAlias();
if(field != null) {
try {
// Convert to lower case
alias = Objects.requireNonNull(GenericAccessor.getField(stakeholder, field)).toString().toLowerCase();
// Replace all invalid characters with a hyphen
alias = Objects.requireNonNull(alias).replaceAll("[^a-z0-9-]", "-");
// Replace multiple consecutive hyphens with a single hyphen
alias = Objects.requireNonNull(alias).replaceAll("-+", "-");
// Remove leading or trailing hyphens
alias = Objects.requireNonNull(alias).replaceAll("^-+|-+$", "-");
} catch (NullPointerException e) {
throw new BadRequestException("Cannot generate alias with field " + field + " for stakeholder: " + stakeholder.getName());
}
}
if(alias == null) {
throw new BadRequestException("Cannot generate alias for stakeholder: " + stakeholder.getName());
}
int duplicates = 1;
while(this.dao.findByAlias(alias).isPresent()) {
alias = alias + duplicates;
duplicates++;
}
stakeholder.setAlias(alias);
return stakeholder;
}
public List<Stakeholder> buildStakeholders(BuildStakeholder buildStakeholder) {
List<Stakeholder> stakeholders = new ArrayList<>();
if(buildStakeholder.isMultiBuild()) {
stakeholders = buildStakeholder.getIndexFields().stream().map( fields -> {
Stakeholder stakeholder = new Stakeholder(buildStakeholder.getStakeholder());
stakeholder.setIndex_id(fields.get(stakeholder.getIndex_id()));
stakeholder.setIndex_name(fields.get(stakeholder.getIndex_name()));
stakeholder.setIndex_shortName(fields.get(stakeholder.getIndex_shortName()));
stakeholder.setName(stakeholder.getIndex_name());
stakeholder = generateAlias(stakeholder, stakeholder.getAlias());
return stakeholder;
}).toList();
} else {
stakeholders.add(buildStakeholder.getStakeholder());
}
stakeholders.forEach(stakeholder -> stakeholder.setStandalone(buildStakeholder.isStandalone()));
if (buildStakeholder.isUmbrella()) {
stakeholders.forEach(stakeholder -> stakeholder.setUmbrella(new Umbrella<>()));
}
return stakeholders.stream().map(stakeholder -> this.buildStakeholder(stakeholder, buildStakeholder.getCopyId())).toList();
}
public Stakeholder buildStakeholder(Stakeholder stakeholder, String copyId) {
if (stakeholder.getDefaultId() == null) {
stakeholder.setCopy(false);
@ -134,6 +186,7 @@ public class StakeholderService {
stakeholder.setTopics(copyFrom.getTopics().stream().map(this.topicService::copy).collect(Collectors.toList()));
}
} else {
stakeholder = generateAlias(stakeholder);
stakeholder.setTopics(new ArrayList<>());
if (stakeholder.isCopy()) {
Stakeholder defaultStakeholder = this.findByPath(stakeholder.getDefaultId());
@ -170,7 +223,7 @@ public class StakeholderService {
stakeholder.getTopics().forEach(this.topicService::find);
}
}
return this.dao.save(stakeholder);
return this.dao.save(stakeholder.validate());
}
public StakeholderFull reorderTopics(Stakeholder stakeholder, List<String> topics) {