Add method to create a general role. Fix merge info method

This commit is contained in:
Konstantinos Triantafyllou 2021-09-09 14:32:59 +03:00
parent d1088e9f83
commit be31b7c90b
6 changed files with 85 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import java.util.Collection;
import java.util.HashSet;
@ -40,13 +41,13 @@ public class CuratorController {
*/
@RequestMapping(value = "/{type}/create", method = RequestMethod.POST)
public ResponseEntity<Response> createRole(@PathVariable("type") String type, @RequestParam(value = "description", required = false) String description) {
if (registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase()) == null) {
if (registryService.createRole(AuthoritiesUtils.curatorRole(type), description != null ? description : "") != null) {
try {
if (registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase()) == null) {
return ResponseEntity.ok(new Response("Role has been created successfully"));
} else {
throw new UnprocessableException("Role creation failed. Try again later");
throw new ConflictException("This role already exists");
}
} else {
} catch (HttpClientErrorException e) {
throw new ConflictException("This role already exists");
}
}

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import java.util.Collection;
import java.util.HashSet;
@ -41,13 +42,13 @@ public class MemberController {
@RequestMapping(value = "/{type}/{id}/create", method = RequestMethod.POST)
public ResponseEntity<Response> createGroup(@PathVariable("type") String type, @PathVariable("id") String id,
@RequestParam(value = "description", required = false) String description) {
if (registryService.getCouId(AuthoritiesUtils.memberRole(type, id)) == null) {
try {
if (registryService.createRole(AuthoritiesUtils.memberRole(type, id), description != null ? description : "") != null) {
return ResponseEntity.ok(new Response("Role has been created successfully"));
} else {
throw new UnprocessableException("Role creation failed. Try again later");
throw new ConflictException("This role already exists");
}
} else {
} catch (HttpClientErrorException e) {
throw new ConflictException("This role already exists");
}
}

View File

@ -0,0 +1,41 @@
package eu.dnetlib.dnetrolemanagement.controllers;
import eu.dnetlib.dnetrolemanagement.entities.Response;
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
@RestController
@RequestMapping("/super")
public class SuperAdminController {
private final RegistryService registryService;
@Autowired
public SuperAdminController(RegistryService registryService) {
this.registryService = registryService;
}
/**
* Create a new role (only for admins)
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity<Response> createRole(@RequestParam("name") String name, @RequestParam(value = "description", required = false) String description) {
try {
if (registryService.createRole(name, description != null ? description : "") != null) {
return ResponseEntity.ok(new Response("Role has been created successfully"));
} else {
throw new ConflictException("This role already exists");
}
} catch (HttpClientErrorException e) {
throw new ConflictException("This role already exists");
}
}
}

View File

@ -1,7 +1,11 @@
package eu.dnetlib.dnetrolemanagement.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
@JsonIgnore
private String coPersonId;
private String id;
private String email;
private String name;
@ -10,6 +14,14 @@ public class User {
public User() {
}
public String getCoPersonId() {
return coPersonId;
}
public void setCoPersonId(String coPersonId) {
this.coPersonId = coPersonId;
}
public String getId() {
return id;
}

View File

@ -36,7 +36,7 @@ public class RegistryService {
* 1.1 Get CoPersonId by Email
*/
public List<Integer> getCoPersonIdsByEmail(String email) {
if(email != null) {
if (email != null) {
List<Integer> coPersonIds = new ArrayList<>();
Map<String, String> params = new HashMap<>();
params.put("coid", coid);
@ -51,7 +51,7 @@ public class RegistryService {
return coPersonIds;
} else {
Integer coPersonId = getCoPersonIdByIdentifier();
return (coPersonId != null)? Collections.singletonList(coPersonId):new ArrayList<>();
return (coPersonId != null) ? Collections.singletonList(coPersonId) : new ArrayList<>();
}
}
@ -85,8 +85,8 @@ public class RegistryService {
Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("identifiers.json", params);
JsonArray ids = (response != null)?response.getAsJsonObject().get("Identifiers").getAsJsonArray():new JsonArray();
if(ids.size() > 0) {
JsonArray ids = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
if (ids.size() > 0) {
return ids.get(0).getAsJsonObject().get("Identifier").getAsString();
}
return null;
@ -230,18 +230,10 @@ public class RegistryService {
JsonArray emails = new JsonArray();
infos.forEach(info -> {
JsonObject user = new JsonObject();
boolean add = true;
String email = info.getAsJsonObject().get("Mail").getAsString();
for (JsonElement element : emails) {
if (element.getAsJsonObject().get("email").getAsString().equals(email)) {
add = false;
}
}
if (add) {
user.addProperty("email", email);
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
emails.add(user);
}
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
emails.add(user);
});
return emails;
}
@ -260,6 +252,7 @@ public class RegistryService {
JsonArray names = new JsonArray();
infos.forEach(info -> {
JsonObject user = new JsonObject();
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
names.add(user);
@ -281,6 +274,7 @@ public class RegistryService {
JsonArray ids = new JsonArray();
infos.forEach(info -> {
JsonObject user = new JsonObject();
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
ids.add(user);

View File

@ -9,6 +9,10 @@ import eu.dnetlib.dnetrolemanagement.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.text.html.Option;
import java.util.Arrays;
import java.util.Optional;
@Component
public class JsonUtils {
@ -22,11 +26,16 @@ public class JsonUtils {
}
public static User[] mergeUserInfo(JsonArray users, JsonArray emails, JsonArray names, Gson gson) {
for (int i = 0; i < users.size(); i++) {
users.get(i).getAsJsonObject().addProperty("email", emails.get(i).getAsJsonObject().get("email").getAsString());
users.get(i).getAsJsonObject().addProperty("name", names.get(i).getAsJsonObject().get("name").getAsString());
User[] usersMapped = gson.fromJson(users, User[].class);
User[] emailsMapped = gson.fromJson(emails, User[].class);
User[] namesMapped = gson.fromJson(names, User[].class);
for(User user: usersMapped) {
Optional<User> emailUser = Arrays.stream(emailsMapped).filter(email -> user.getCoPersonId().equals(email.getCoPersonId())).findFirst();
Optional<User> nameUser = Arrays.stream(namesMapped).filter(name -> user.getCoPersonId().equals(name.getCoPersonId())).findFirst();
emailUser.ifPresent(value -> user.setEmail(value.getEmail()));
nameUser.ifPresent(value -> user.setName(value.getName()));
}
return gson.fromJson(users, User[].class);
return usersMapped;
}
public JsonObject coPersonRoles(Integer coPersonId, Integer couId, String status) {