dnet-openaire-users/src/main/java/eu/dnetlib/openaire/usermanagement/utils/JsonUtils.java

138 lines
5.6 KiB
Java

package eu.dnetlib.openaire.usermanagement.utils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
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 {
@Value("${registry.version}")
private String version;
@Value("${registry.coid}")
private String coid;
public JsonObject coPersonRoles(Integer coPersonId, Integer couId, String status) {
JsonObject role = new JsonObject();
JsonArray coPersonRoles = new JsonArray();
JsonObject coPersonRole = new JsonObject();
JsonObject person = new JsonObject();
person.addProperty("Type", "CO");
person.addProperty("Id", coPersonId.toString());
coPersonRole.addProperty("Version", version);
coPersonRole.add("Person", person);
coPersonRole.addProperty("CouId", couId.toString());
coPersonRole.addProperty("Affiliation", "member");
coPersonRole.addProperty("Title", "");
coPersonRole.addProperty("O", "Openaire");
coPersonRole.addProperty("Status", status);
coPersonRole.addProperty("ValidFrom", "");
coPersonRole.addProperty("ValidThrough", "");
coPersonRoles.add(coPersonRole);
role.addProperty("RequestType", "CoPersonRoles");
role.addProperty("Version", version);
role.add("CoPersonRoles", coPersonRoles);
return role;
}
public JsonObject coGroupMembers(Integer coGroupId, Integer coPersonId, boolean member) {
JsonObject coGroup = new JsonObject();
JsonArray coGroupMembers = new JsonArray();
JsonObject coGroupMember = new JsonObject();
JsonObject person = new JsonObject();
person.addProperty("Type", "CO");
person.addProperty("Id", coPersonId.toString());
coGroupMember.addProperty("Version", version);
coGroupMember.add("Person", person);
coGroupMember.addProperty("CoGroupId", coGroupId.toString());
coGroupMember.addProperty("Member", member);
coGroupMember.addProperty("Owner", false);
coGroupMember.addProperty("ValidFrom", "");
coGroupMember.addProperty("ValidThrough", "");
coGroupMembers.add(coGroupMember);
coGroup.addProperty("RequestType", "CoGroupMembers");
coGroup.addProperty("Version", version);
coGroup.add("CoGroupMembers", coGroupMembers);
return coGroup;
}
public JsonObject createNewCou(Role role) {
JsonObject cou = new JsonObject();
JsonArray cous = new JsonArray();
JsonObject newCou = new JsonObject();
newCou.addProperty("Version", version);
newCou.addProperty("CoId", coid);
newCou.addProperty("Name", role.getName());
newCou.addProperty("Description", role.getDescription());
cous.add(newCou);
cou.addProperty("RequestType", "Cous");
cou.addProperty("Version", version);
cou.add("Cous", cous);
return cou;
}
public JsonObject createVerification(RoleVerification roleVerification) {
JsonObject verification = new JsonObject();
verification.addProperty("id", roleVerification.getId());
verification.addProperty("email", roleVerification.getEmail());
verification.addProperty("type", roleVerification.getType());
verification.addProperty("entity", roleVerification.getEntity());
verification.addProperty("verificationType", roleVerification.getVerificationType());
verification.addProperty("date", roleVerification.getDate().getTime());
return verification;
}
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;
}
public JsonObject createResponse(JsonElement response) {
JsonObject json = new JsonObject();
json.add("response", response);
return json;
}
public JsonObject createResponse(String response) {
JsonObject json = new JsonObject();
json.addProperty("response", response);
return json;
}
public JsonObject createResponse(Number response) {
JsonObject json = new JsonObject();
json.addProperty("response", response);
return json;
}
public JsonObject createResponse(Boolean response) {
JsonObject json = new JsonObject();
json.addProperty("response", response);
return json;
}
public JsonObject createResponse(Character response) {
JsonObject json = new JsonObject();
json.addProperty("response", response);
return json;
}
}