Project

General

Profile

1
package eu.dnetlib.repo.manager.service.aai.registry;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import eu.dnetlib.repo.manager.domain.dto.Role;
7
import eu.dnetlib.repo.manager.domain.dto.User;
8
import eu.dnetlib.repo.manager.service.aai.registry.utils.RegistryUtils;
9
import eu.dnetlib.repo.manager.utils.HttpUtils;
10
import org.apache.log4j.Logger;
11
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.security.core.context.SecurityContextHolder;
15
import org.springframework.stereotype.Service;
16

    
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21

    
22
@Service
23
public class RegistryCalls implements AaiRegistryService {
24

    
25
    private static final Logger logger = Logger.getLogger(RegistryCalls.class);
26

    
27
    private final String coid;
28
    public final HttpUtils httpUtils;
29
    public final RegistryUtils jsonUtils;
30

    
31
    @Autowired
32
    RegistryCalls(@Value("${registry.coid:2}") String coid,
33
                  HttpUtils httpUtils, RegistryUtils registryUtils) {
34
        this.coid = coid;
35
        this.httpUtils = httpUtils;
36
        this.jsonUtils = registryUtils;
37
    }
38

    
39
    private String mapType(String type, boolean communityMap) {
40
        if (type.equals("organization")) {
41
            type = "institution";
42
        } else if (type.equals("ri") && communityMap) {
43
            type = "community";
44
        }
45
        return type;
46
    }
47

    
48
    @Override
49
    public Integer getCoPersonIdByEmail() {
50
        try {
51
            OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
52
            String email = authentication.getUserInfo().getEmail();
53
            Map<String, String> params = new HashMap<>();
54
            params.put("coid", coid);
55
            params.put("mail", email);
56
            JsonElement response = httpUtils.get("co_people.json", params);
57
            return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
58
        } catch (Exception e) {
59
            logger.error("Get User info: An error occurred ", e);
60
            return null;
61
        }
62
    }
63

    
64
    @Override
65
    public Integer getCoPersonIdByEmail(String email) {
66
        Map<String, String> params = new HashMap<>();
67
        params.put("coid", coid);
68
        params.put("mail", email);
69
        JsonElement response = httpUtils.get("co_people.json", params);
70
        if (response != null) {
71
            JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
72
            if (coPeople.size() > 0) {
73
                return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
74
            }
75
        }
76
        return null;
77
    }
78

    
79
    @Override
80
    public List<Integer> getCoPersonIdsByEmail(String email) {
81
        List<Integer> coPersonIds = new ArrayList<>();
82
        Map<String, String> params = new HashMap<>();
83
        params.put("coid", coid);
84
        params.put("mail", email);
85
        JsonElement response = httpUtils.get("co_people.json", params);
86
        if (response != null) {
87
            JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
88
            for (int i = 0; i < coPeople.size(); i++) {
89
                coPersonIds.add(coPeople.get(i).getAsJsonObject().get("Id").getAsInt());
90
            }
91
        }
92
        return coPersonIds;
93
    }
94

    
95
    @Override
96
    public Integer getCoPersonIdByIdentifier() {
97
        try {
98
            OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
99
            String sub = authentication.getUserInfo().getSub();
100
            Map<String, String> params = new HashMap<>();
101
            params.put("coid", coid);
102
            params.put("search.identifier", sub);
103
            JsonElement response = httpUtils.get("co_people.json", params);
104
            return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
105
        } catch (Exception e) {
106
            logger.error("Get User info: An error occurred ", e);
107
            return null;
108
        }
109
    }
110

    
111
    public Integer getCoPersonIdByIdentifier(String sub) {
112
        Map<String, String> params = new HashMap<>();
113
        params.put("coid", coid);
114
        params.put("search.identifier", sub);
115
        JsonElement response = httpUtils.get("co_people.json", params);
116
        return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
117
    }
118

    
119
    @Override
120
    public JsonArray getCous(String name) {
121
        Map<String, String> params = new HashMap<>();
122
        params.put("coid", coid);
123
        if (name != null) {
124
            params.put("name", name.toLowerCase());
125
        }
126
        JsonElement response = httpUtils.get("cous.json", params);
127
        return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
128
    }
129

    
130
    @Override
131
    public JsonArray getCous() {
132
        return getCous(null);
133
    }
134

    
135
    @Override
136
    public Integer getCouId(String name) {
137
        JsonArray cous = getCous(name);
138
        for (JsonElement cou : cous) {
139
            if (cou.getAsJsonObject().get("Name").getAsString().equalsIgnoreCase(name)) {
140
                return cou.getAsJsonObject().get("Id").getAsInt();
141
            }
142
        }
143
        return null;
144
    }
145

    
146
    @Override
147
    public Integer getCouId(String type, String id, boolean communityMap) {
148
        return getCouId(mapType(type, communityMap) + "." + id);
149
    }
150

    
151
    @Override
152
    public Integer getCouId(String type, String id) {
153
        return getCouId(type, id, true);
154
    }
155

    
156
    @Override
157
    public JsonArray getRoles(Integer coPersonId) {
158
        Map<String, String> params = new HashMap<>();
159
        params.put("copersonid", coPersonId.toString());
160
        JsonElement response = httpUtils.get("co_person_roles.json", params);
161
        return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
162
    }
163

    
164
    @Override
165
    public Integer getRoleId(Integer coPersonId, Integer couId) {
166
        JsonArray roles = getRoles(coPersonId);
167
        for (JsonElement role : roles) {
168
            JsonObject object = role.getAsJsonObject();
169
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
170
                return object.get("Id").getAsInt();
171
            }
172
        }
173
        return null;
174
    }
175

    
176
    @Override
177
    public JsonArray getUserGroups(Integer coPersonId) {
178
        Map<String, String> params = new HashMap<>();
179
        params.put("copersonid", coPersonId.toString());
180
        JsonElement response = httpUtils.get("co_groups.json", params);
181
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
182
    }
183

    
184
    @Override
185
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
186
        Map<String, String> params = new HashMap<>();
187
        params.put("copersonid", coPersonId.toString());
188
        JsonElement response = httpUtils.get("co_groups.json", params);
189
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
190
        for (JsonElement role : roles) {
191
            JsonObject object = role.getAsJsonObject();
192
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
193
                if (object.get("Name").getAsString().contains("admins")) {
194
                    return object;
195
                }
196
            }
197
        }
198
        return null;
199
    }
200

    
201
    @Override
202
    public JsonArray getCouGroups(Integer couId) {
203
        Map<String, String> params = new HashMap<>();
204
        params.put("coid", coid);
205
        params.put("couid", couId.toString());
206
        JsonElement response = httpUtils.get("co_groups.json", params);
207
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
208
    }
209

    
210
    @Override
211
    public JsonObject getCouAdminGroup(Integer couId) {
212
        JsonArray groups = getCouGroups(couId);
213
        for (JsonElement group : groups) {
214
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
215
                return group.getAsJsonObject();
216
            }
217
        }
218
        return null;
219
    }
220

    
221
    @Override
222
    public JsonArray getGroupMembers(Integer coGroupId) {
223
        Map<String, String> params = new HashMap<>();
224
        params.put("cogroupid", coGroupId.toString());
225
        JsonElement response = httpUtils.get("co_group_members.json", params);
226
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
227
    }
228

    
229

    
230
    @Override
231
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
232
        Map<String, String> params = new HashMap<>();
233
        params.put("couid", couId.toString());
234
        if (admin) {
235
            params.put("admin", "true");
236
        }
237
        JsonElement response = httpUtils.get("email_addresses.json", params);
238
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
239
        JsonArray emails = new JsonArray();
240
        infos.forEach(info -> {
241
            JsonObject user = new JsonObject();
242
            boolean add = true;
243
            String email = info.getAsJsonObject().get("Mail").getAsString();
244
            for (JsonElement element : emails) {
245
                if (element.getAsJsonObject().get("email").getAsString().equals(email)) {
246
                    add = false;
247
                }
248
            }
249
            if (add) {
250
                user.addProperty("email", email);
251
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
252
                emails.add(user);
253
            }
254
        });
255
        return emails;
256
    }
257

    
258
    @Override
259
    public JsonArray getUsersByCouId(Integer couId) {
260
        Map<String, String> params = new HashMap<>();
261
        params.put("couid", couId.toString());
262
        JsonElement response = httpUtils.get("co_person_roles.json", params);
263
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
264
//        JsonArray users = new JsonArray();
265
//        infos.forEach(info -> {
266
//            JsonObject user = new JsonObject();
267
//            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
268
//            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
269
//            emails.add(user);
270
//        });
271
        return infos;
272
    }
273

    
274
    @Override
275
    public List<User> getUsers(Integer couId) {
276
        List<User> users = new ArrayList<>();
277
        JsonArray infos = getUserEmailByCouId(couId, false);
278

    
279
        infos.forEach(info -> {
280
            User user = new User();
281
            user.setEmail(info.getAsJsonObject().get("email").getAsString());
282

    
283
            users.add(user);
284
        });
285
        return users;
286
    }
287

    
288
    @Override
289
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
290
        Map<String, String> params = new HashMap<>();
291
        params.put("couid", couId.toString());
292
        if (admin) {
293
            params.put("admin", "true");
294
        }
295
        JsonElement response = httpUtils.get("names.json", params);
296
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
297
        JsonArray names = new JsonArray();
298
        infos.forEach(info -> {
299
            JsonObject user = new JsonObject();
300
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
301
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
302
            names.add(user);
303
        });
304
        return names;
305
    }
306

    
307
    @Override
308
    public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
309
        Map<String, String> params = new HashMap<>();
310
        params.put("couid", couId.toString());
311
        if (admin) {
312
            params.put("admin", "true");
313
        }
314
        JsonElement response = httpUtils.get("identifiers.json", params);
315
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
316
        JsonArray emails = new JsonArray();
317
        infos.forEach(info -> {
318
            JsonObject user = new JsonObject();
319
            user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
320
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
321
            emails.add(user);
322
        });
323
        return emails;
324
    }
325

    
326
    @Override
327
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
328
        if (id != null) {
329
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
330
        } else {
331
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
332
        }
333
    }
334

    
335
    @Override
336
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
337
        if (id != null) {
338
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
339
        }
340
    }
341

    
342
    @Override
343
    public Integer createRole(Role role) {
344
        JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
345
        return element.getAsJsonObject().get("Id").getAsInt();
346
    }
347

    
348
    @Override
349
    public String getUserEmail(Integer coPersonId) {
350
        Map<String, String> params = new HashMap<>();
351
        params.put("copersonid", coPersonId.toString());
352
        JsonElement response = httpUtils.get("email_addresses.json", params);
353
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
354
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
355
    }
356

    
357
    @Override
358
    public String getUserNames(Integer coPersonId) {
359
        Map<String, String> params = new HashMap<>();
360
        params.put("copersonid", coPersonId.toString());
361
        JsonElement response = httpUtils.get("names.json", params);
362
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
363
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
364
    }
365

    
366
    @Override
367
    public String getUserId(Integer coPersonId) {
368
        Map<String, String> params = new HashMap<>();
369
        params.put("copersonid", coPersonId.toString());
370
        JsonElement response = httpUtils.get("identifiers.json", params);
371
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
372
        return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
373
    }
374

    
375
    @Override
376
    public void assignAdminRole(Integer coPersonId, Integer couId) {
377
        JsonObject group = getCouAdminGroup(couId);
378
        if (group != null) {
379
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
380
        }
381
    }
382

    
383
    @Override
384
    public void removeAdminRole(Integer coPersonId, Integer couId) {
385
        JsonObject adminGroup = this.getCouAdminGroup(couId);
386
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
387
        Integer id = null;
388
        for (JsonElement admin : admins) {
389
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
390
                id = admin.getAsJsonObject().get("Id").getAsInt();
391
            }
392
        }
393
        if (id != null) {
394
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
395
        }
396
    }
397

    
398
    @Override
399
    public Map<Integer, String> getCouNames(List<Integer> couIds) {
400
        Map<Integer, String> idNameMap = new HashMap<>();
401
        for (Integer id : couIds) {
402
            idNameMap.put(id, null);
403
        }
404

    
405
        JsonArray cous = getCous();
406
        int count = 0;
407
        int total = couIds.size();
408
        for (JsonElement cou : cous) {
409
            if (count < total) {
410
                if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) {
411
                    idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString());
412
                    count++;
413
                }
414
            } else {
415
                break;
416
            }
417
        }
418
        return idNameMap;
419
    }
420
}
(2-2/2)