[Users | Trunk]: Fix merge info method

This commit is contained in:
Konstantinos Triantafyllou 2021-09-09 11:31:21 +00:00
parent 51b3acf5dc
commit 9c9d93dbfc
4 changed files with 98 additions and 33 deletions

View File

@ -1,12 +1,11 @@
package eu.dnetlib.openaire.usermanagement.api;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import eu.dnetlib.openaire.user.login.utils.AuthoritiesUpdater;
import eu.dnetlib.openaire.user.pojos.RoleVerification;
import eu.dnetlib.openaire.user.utils.EmailSender;
import eu.dnetlib.openaire.usermanagement.dto.Role;
import eu.dnetlib.openaire.usermanagement.dto.User;
import eu.dnetlib.openaire.usermanagement.utils.AuthorizationService;
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
@ -51,6 +50,8 @@ public class RegistryService {
@Autowired
private AuthorizationService authorizationService;
private final Gson gson = new Gson();
/**
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
*/
@ -517,8 +518,12 @@ public class RegistryService {
JsonArray members = calls.getUserIdByCouId(couId, false);
JsonArray emails = calls.getUserEmailByCouId(couId, false);
JsonArray names = calls.getUserNamesByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(JsonUtils.mergeUserInfo(members, emails, names)).toString()).type(MediaType.APPLICATION_JSON).build();
JsonArray managers = calls.getUserIdByCouId(couId, true);
members.getAsJsonArray().forEach(element -> {
element.getAsJsonObject().addProperty("isManager", managers.contains(element));
});
JsonUtils.mergeUserInfo(members, emails, names, gson);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
@ -537,7 +542,7 @@ public class RegistryService {
if(authorizationService.isManager(type, id) || authorizationService.isPortalAdmin() || authorizationService.isCurator(type)) {
JsonArray emails = calls.getUserEmailByCouId(couId, true);
JsonArray names = calls.getUserNamesByCouId(couId, true);
JsonUtils.mergeUserInfo(managers, emails, names);
JsonUtils.mergeUserInfo(managers, emails, names, gson);
}
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
} else {

View File

@ -0,0 +1,57 @@
package eu.dnetlib.openaire.usermanagement.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class User {
@JsonIgnore
private String coPersonId;
private String id;
private String email;
private String name;
private String memberSince;
public User() {
}
@JsonIgnore
public String getCoPersonId() {
return coPersonId;
}
public void setCoPersonId(String coPersonId) {
this.coPersonId = coPersonId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMemberSince() {
return memberSince;
}
public void setMemberSince(String memberSince) {
this.memberSince = memberSince;
}
}

View File

@ -6,9 +6,13 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import eu.dnetlib.openaire.user.pojos.RoleVerification;
import eu.dnetlib.openaire.usermanagement.dto.Role;
import eu.dnetlib.openaire.usermanagement.dto.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Optional;
@Component
public class JsonUtils {
@ -88,10 +92,15 @@ public class JsonUtils {
return verification;
}
public static JsonArray mergeUserInfo(JsonArray users, JsonArray emails, JsonArray names) {
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());
public static JsonArray mergeUserInfo(JsonArray users, JsonArray emails, JsonArray names, Gson gson) {
User[] emailsMapped = gson.fromJson(emails, User[].class);
User[] namesMapped = gson.fromJson(names, User[].class);
for(JsonElement user: users) {
Optional<User> emailUser = Arrays.stream(emailsMapped).filter(email -> user.getAsJsonObject().get("coPersonId").getAsString().equals(email.getCoPersonId())).findFirst();
Optional<User> nameUser = Arrays.stream(namesMapped).filter(name -> user.getAsJsonObject().get("coPersonId").getAsString().equals(name.getCoPersonId())).findFirst();
emailUser.ifPresent(value -> user.getAsJsonObject().addProperty("email", value.getEmail()));
nameUser.ifPresent(value -> user.getAsJsonObject().addProperty("name", value.getName()));
user.getAsJsonObject().remove("coPersonId");
}
return users;
}

View File

@ -33,9 +33,9 @@ public class RegistryCalls {
public String mapType(String type, boolean communityMap) {
if(type.equals("organization")) {
if (type.equals("organization")) {
type = "institution";
} else if(type.equals("ri") && communityMap) {
} else if (type.equals("ri") && communityMap) {
type = "community";
}
return type;
@ -64,9 +64,9 @@ public class RegistryCalls {
params.put("coid", coid);
params.put("mail", email);
JsonElement response = httpUtils.get("co_people.json", params);
if(response != null) {
if (response != null) {
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
if(coPeople.size() > 0) {
if (coPeople.size() > 0) {
return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
}
}
@ -79,9 +79,9 @@ public class RegistryCalls {
params.put("coid", coid);
params.put("mail", email);
JsonElement response = httpUtils.get("co_people.json", params);
if(response != null) {
if (response != null) {
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
for(int i = 0; i < coPeople.size(); i++) {
for (int i = 0; i < coPeople.size(); i++) {
coPersonIds.add(coPeople.get(i).getAsJsonObject().get("Id").getAsInt());
}
}
@ -120,7 +120,7 @@ public class RegistryCalls {
public JsonArray getCous(String name) {
Map<String, String> params = new HashMap<>();
params.put("coid", coid);
if(name != null) {
if (name != null) {
params.put("name", name.toLowerCase());
}
JsonElement response = httpUtils.get("cous.json", params);
@ -274,18 +274,10 @@ public class RegistryCalls {
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;
}
@ -304,6 +296,7 @@ public class RegistryCalls {
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);
@ -322,14 +315,15 @@ public class RegistryCalls {
}
JsonElement response = httpUtils.get("identifiers.json", params);
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
JsonArray emails = new JsonArray();
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());
emails.add(user);
ids.add(user);
});
return emails;
return ids;
}
/**
@ -347,7 +341,7 @@ public class RegistryCalls {
* 16. Remove a member role from a User
*/
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
if(id != null) {
if (id != null) {
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
}
}