Project

General

Profile

« Previous | Next » 

Revision 61649

Add integration with Role Management. Version 2.0.0

View differences:

JsonUtils.java
1 1
package eu.dnetlib.openaire.usermanagement.utils;
2 2

  
3
import com.google.gson.Gson;
4
import com.google.gson.JsonArray;
5 3
import com.google.gson.JsonElement;
6 4
import com.google.gson.JsonObject;
7 5
import eu.dnetlib.openaire.user.pojos.RoleVerification;
8
import eu.dnetlib.openaire.usermanagement.dto.Role;
9
import eu.dnetlib.openaire.usermanagement.dto.User;
10
import org.springframework.beans.factory.annotation.Value;
11 6
import org.springframework.stereotype.Component;
12 7

  
13
import java.util.Arrays;
14
import java.util.Optional;
15

  
16 8
@Component
17 9
public class JsonUtils {
18 10

  
19
    @Value("${registry.version}")
20
    private String version;
21

  
22
    @Value("${registry.coid}")
23
    private String coid;
24

  
25
    public JsonObject coPersonRoles(Integer coPersonId, Integer couId, String status) {
26
        JsonObject role = new JsonObject();
27
        JsonArray coPersonRoles = new JsonArray();
28
        JsonObject coPersonRole = new JsonObject();
29
        JsonObject person = new JsonObject();
30
        person.addProperty("Type", "CO");
31
        person.addProperty("Id", coPersonId.toString());
32
        coPersonRole.addProperty("Version", version);
33
        coPersonRole.add("Person", person);
34
        coPersonRole.addProperty("CouId", couId.toString());
35
        coPersonRole.addProperty("Affiliation", "member");
36
        coPersonRole.addProperty("Title", "");
37
        coPersonRole.addProperty("O", "Openaire");
38
        coPersonRole.addProperty("Status", status);
39
        coPersonRole.addProperty("ValidFrom", "");
40
        coPersonRole.addProperty("ValidThrough", "");
41
        coPersonRoles.add(coPersonRole);
42
        role.addProperty("RequestType", "CoPersonRoles");
43
        role.addProperty("Version", version);
44
        role.add("CoPersonRoles", coPersonRoles);
45
        return role;
46
    }
47

  
48
    public JsonObject coGroupMembers(Integer coGroupId, Integer coPersonId, boolean member) {
49
        JsonObject coGroup = new JsonObject();
50
        JsonArray coGroupMembers = new JsonArray();
51
        JsonObject coGroupMember = new JsonObject();
52
        JsonObject person = new JsonObject();
53
        person.addProperty("Type", "CO");
54
        person.addProperty("Id", coPersonId.toString());
55
        coGroupMember.addProperty("Version", version);
56
        coGroupMember.add("Person", person);
57
        coGroupMember.addProperty("CoGroupId", coGroupId.toString());
58
        coGroupMember.addProperty("Member", member);
59
        coGroupMember.addProperty("Owner", false);
60
        coGroupMember.addProperty("ValidFrom", "");
61
        coGroupMember.addProperty("ValidThrough", "");
62
        coGroupMembers.add(coGroupMember);
63
        coGroup.addProperty("RequestType", "CoGroupMembers");
64
        coGroup.addProperty("Version", version);
65
        coGroup.add("CoGroupMembers", coGroupMembers);
66
        return coGroup;
67
    }
68

  
69
    public JsonObject createNewCou(Role role) {
70
        JsonObject cou = new JsonObject();
71
        JsonArray cous = new JsonArray();
72
        JsonObject newCou = new JsonObject();
73
        newCou.addProperty("Version", version);
74
        newCou.addProperty("CoId", coid);
75
        newCou.addProperty("Name", role.getName());
76
        newCou.addProperty("Description", role.getDescription());
77
        cous.add(newCou);
78
        cou.addProperty("RequestType", "Cous");
79
        cou.addProperty("Version", version);
80
        cou.add("Cous", cous);
81
        return cou;
82
    }
83

  
84 11
    public JsonObject createVerification(RoleVerification roleVerification) {
85 12
        JsonObject verification = new JsonObject();
86 13
        verification.addProperty("id", roleVerification.getId());
......
92 19
        return verification;
93 20
    }
94 21

  
95
    public static JsonArray mergeUserInfo(JsonArray users, JsonArray emails, JsonArray names, Gson gson) {
96
        User[] emailsMapped =  gson.fromJson(emails, User[].class);
97
        User[] namesMapped =  gson.fromJson(names, User[].class);
98
        for(JsonElement user: users) {
99
            Optional<User> emailUser = Arrays.stream(emailsMapped).filter(email -> user.getAsJsonObject().get("coPersonId").getAsString().equals(email.getCoPersonId())).findFirst();
100
            Optional<User> nameUser = Arrays.stream(namesMapped).filter(name -> user.getAsJsonObject().get("coPersonId").getAsString().equals(name.getCoPersonId())).findFirst();
101
            emailUser.ifPresent(value -> user.getAsJsonObject().addProperty("email", value.getEmail()));
102
            nameUser.ifPresent(value -> user.getAsJsonObject().addProperty("name", value.getName()));
103
            user.getAsJsonObject().remove("coPersonId");
104
        }
105
        return users;
106
    }
107

  
108 22
    public JsonObject createResponse(JsonElement response) {
109 23
        JsonObject json = new JsonObject();
110 24
        json.add("response", response);

Also available in: Unified diff