Revision 61545
Added by Konstantinos Triantafyllou over 2 years ago
JsonUtils.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.openaire.usermanagement.utils; |
2 | 2 |
|
3 |
import com.google.gson.Gson; |
|
3 | 4 |
import com.google.gson.JsonArray; |
4 | 5 |
import com.google.gson.JsonElement; |
5 | 6 |
import com.google.gson.JsonObject; |
6 | 7 |
import eu.dnetlib.openaire.user.pojos.RoleVerification; |
7 | 8 |
import eu.dnetlib.openaire.usermanagement.dto.Role; |
9 |
import eu.dnetlib.openaire.usermanagement.dto.User; |
|
8 | 10 |
import org.springframework.beans.factory.annotation.Value; |
9 | 11 |
import org.springframework.stereotype.Component; |
10 | 12 |
|
13 |
import java.util.Arrays; |
|
14 |
import java.util.Optional; |
|
15 |
|
|
11 | 16 |
@Component |
12 | 17 |
public class JsonUtils { |
13 | 18 |
|
... | ... | |
87 | 92 |
return verification; |
88 | 93 |
} |
89 | 94 |
|
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 |
|
|
90 | 108 |
public JsonObject createResponse(JsonElement response) { |
91 | 109 |
JsonObject json = new JsonObject(); |
92 | 110 |
json.add("response", response); |
Also available in: Unified diff
[Users | Beta]: Create methods for backwards compatibility