[umbrella]: Add reorder methods in updateUmbrella.

This commit is contained in:
Konstantinos Triantafyllou 2024-06-12 01:42:49 +03:00
parent 1bc576ea69
commit 00b17fad1c
5 changed files with 85 additions and 14 deletions

View File

@ -155,7 +155,7 @@ public class StakeholderController {
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{stakeholderId}/umbrella", method = RequestMethod.POST)
public Umbrella<Stakeholder> changeStakeholderVisibility(@PathVariable("stakeholderId") String stakeholderId, @RequestBody UpdateUmbrella update) {
public Umbrella<Stakeholder> updateUmbrella(@PathVariable("stakeholderId") String stakeholderId, @RequestBody UpdateUmbrella update) {
log.debug("update stakeholder umbrella");
log.debug("Stakeholder: " + stakeholderId);
Stakeholder stakeholder = this.stakeholderService.findByPath(stakeholderId);

View File

@ -1,21 +1,25 @@
package eu.dnetlib.uoamonitorservice.dto;
import eu.dnetlib.uoamonitorservice.primitives.Action;
import eu.dnetlib.uoamonitorservice.primitives.StakeholderType;
import java.util.List;
public class UpdateUmbrella {
String type;
StakeholderType type;
Action action;
String child;
List<StakeholderType> types;
List<String> children;
public UpdateUmbrella() {
}
public String getType() {
public StakeholderType getType() {
return type;
}
public void setType(String type) {
public void setType(StakeholderType type) {
this.type = type;
}
@ -34,4 +38,20 @@ public class UpdateUmbrella {
public void setChild(String child) {
this.child = child;
}
public List<StakeholderType> getTypes() {
return types;
}
public void setTypes(List<StakeholderType> types) {
this.types = types;
}
public List<String> getChildren() {
return children;
}
public void setChildren(List<String> children) {
this.children = children;
}
}

View File

@ -1,5 +1,5 @@
package eu.dnetlib.uoamonitorservice.primitives;
public enum Action {
ADD, REMOVE
ADD, REMOVE, UPDATE
}

View File

@ -4,10 +4,7 @@ import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
import eu.dnetlib.uoamonitorservice.generics.Common;
import eu.dnetlib.uoamonitorservice.service.StakeholderService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public class Umbrella<T> {
@ -99,4 +96,30 @@ public class Umbrella<T> {
}
return false;
}
public boolean update(List<StakeholderType> types) {
if(types == null) {
return false;
} else if(types.size() != this.types.size()) {
return false;
} else {
if(new HashSet<>(this.types).containsAll(types)) {
this.types = types;
return true;
}
return false;
}
}
public boolean update(StakeholderType type, List<T> children) {
if(children == null) {
return false;
} else if(this.children.get(type) == null) {
return false;
} else if(children.size() != this.children.get(type).size()) {
return false;
} else {
return new HashSet<>(this.children.get(type)).containsAll(children);
}
}
}

View File

@ -246,15 +246,21 @@ public class StakeholderService {
if (this.commonService.hasEditAuthority(stakeholder.getType(), stakeholder.getAlias())) {
if(update.getAction().equals(Action.ADD)) {
if(update.getChild() != null) {
return this.addChild(stakeholder.getId(), StakeholderType.valueOf(update.getType()), update.getChild());
return this.addChild(stakeholder.getId(), update.getType(), update.getChild());
} else {
return this.addType(stakeholder.getId(), StakeholderType.valueOf(update.getType()));
return this.addType(stakeholder.getId(), update.getType());
}
} else if(update.getAction().equals(Action.REMOVE)) {
if(update.getChild() != null) {
return this.removeChild(stakeholder.getId(), StakeholderType.valueOf(update.getType()), update.getChild());
return this.removeChild(stakeholder.getId(), update.getType(), update.getChild());
} else {
return this.removeType(stakeholder.getId(), StakeholderType.valueOf(update.getType()));
return this.removeType(stakeholder.getId(), update.getType());
}
} else if(update.getAction().equals(Action.UPDATE)) {
if(update.getType() != null) {
return this.updateChildren(stakeholder.getId(), update.getType(), update.getChildren());
} else {
return this.updateTypes(stakeholder.getId(), update.getTypes());
}
}
} else {
@ -308,4 +314,26 @@ public class StakeholderService {
}
throw new BadRequestException("Cannot remove child: " + childId + " to stakeholder with id " + id);
}
public Umbrella<Stakeholder> updateTypes(String id, List<StakeholderType> types) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(types)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update types in umbrella of stakeholder with id " + id);
}
public Umbrella<Stakeholder> updateChildren(String id, StakeholderType type, List<String> children) {
Stakeholder stakeholder = this.findByPath(id);
Umbrella<String> umbrella = stakeholder.getUmbrellaOptional().orElseThrow(() -> new EntityNotFoundException("Umbrella not found in the stakeholder with id " + id));
if (stakeholder.getUmbrella().update(type, children)) {
stakeholder.setUmbrella(umbrella);
stakeholder.setUpdateDate(new Date());
return this.getFullStakeholder(this.dao.save(stakeholder)).getUmbrella();
}
throw new BadRequestException("Cannot update children of " + type + " in umbrella of stakeholder with id " + id);
}
}