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 JsonArray getRolesWithStatus(Integer coPersonId, RoleStatus status) {
166
        JsonArray roles = getRoles(coPersonId);
167
        if (status == null) {
168
            return roles;
169
        }
170
        JsonArray activeRoles = new JsonArray();
171
        for (JsonElement role : roles) {
172
            if (role.getAsJsonObject().get("Status").getAsString().equalsIgnoreCase(status.toString())) {
173
                activeRoles.add(role);
174
            }
175
        }
176
        return activeRoles;
177
    }
178

    
179
    @Override
180
    public Integer getRoleId(Integer coPersonId, Integer couId) {
181
        JsonArray roles = getRoles(coPersonId);
182
        for (JsonElement role : roles) {
183
            JsonObject object = role.getAsJsonObject();
184
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
185
                return object.get("Id").getAsInt();
186
            }
187
        }
188
        return null;
189
    }
190

    
191
    @Override
192
    public JsonArray getUserGroups(Integer coPersonId) {
193
        Map<String, String> params = new HashMap<>();
194
        params.put("copersonid", coPersonId.toString());
195
        JsonElement response = httpUtils.get("co_groups.json", params);
196
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
197
    }
198

    
199
    @Override
200
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
201
        Map<String, String> params = new HashMap<>();
202
        params.put("copersonid", coPersonId.toString());
203
        JsonElement response = httpUtils.get("co_groups.json", params);
204
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
205
        for (JsonElement role : roles) {
206
            JsonObject object = role.getAsJsonObject();
207
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
208
                if (object.get("Name").getAsString().contains("admins")) {
209
                    return object;
210
                }
211
            }
212
        }
213
        return null;
214
    }
215

    
216
    @Override
217
    public JsonArray getCouGroups(Integer couId) {
218
        Map<String, String> params = new HashMap<>();
219
        params.put("coid", coid);
220
        params.put("couid", couId.toString());
221
        JsonElement response = httpUtils.get("co_groups.json", params);
222
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
223
    }
224

    
225
    @Override
226
    public JsonObject getCouAdminGroup(Integer couId) {
227
        JsonArray groups = getCouGroups(couId);
228
        for (JsonElement group : groups) {
229
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
230
                return group.getAsJsonObject();
231
            }
232
        }
233
        return null;
234
    }
235

    
236
    @Override
237
    public JsonArray getGroupMembers(Integer coGroupId) {
238
        Map<String, String> params = new HashMap<>();
239
        params.put("cogroupid", coGroupId.toString());
240
        JsonElement response = httpUtils.get("co_group_members.json", params);
241
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
242
    }
243

    
244

    
245
    @Override
246
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
247
        Map<String, String> params = new HashMap<>();
248
        params.put("couid", couId.toString());
249
        if (admin) {
250
            params.put("admin", "true");
251
        }
252
        JsonElement response = httpUtils.get("email_addresses.json", params);
253
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
254
        JsonArray emails = new JsonArray();
255
        infos.forEach(info -> {
256
            JsonObject user = new JsonObject();
257
            boolean add = true;
258
            String email = info.getAsJsonObject().get("Mail").getAsString();
259
            for (JsonElement element : emails) {
260
                if (element.getAsJsonObject().get("email").getAsString().equals(email)) {
261
                    add = false;
262
                }
263
            }
264
            if (add) {
265
                user.addProperty("email", email);
266
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
267
                emails.add(user);
268
            }
269
        });
270
        return emails;
271
    }
272

    
273
    @Override
274
    public JsonArray getUsersByCouId(Integer couId) {
275
        Map<String, String> params = new HashMap<>();
276
        params.put("couid", couId.toString());
277
        JsonElement response = httpUtils.get("co_person_roles.json", params);
278
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
279
//        JsonArray users = new JsonArray();
280
//        infos.forEach(info -> {
281
//            JsonObject user = new JsonObject();
282
//            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
283
//            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
284
//            emails.add(user);
285
//        });
286
        return infos;
287
    }
288

    
289
    @Override
290
    public List<User> getUsers(Integer couId) {
291
        List<User> users = new ArrayList<>();
292
        JsonArray infos = getUserEmailByCouId(couId, false);
293

    
294
        infos.forEach(info -> {
295
            User user = new User();
296
            user.setEmail(info.getAsJsonObject().get("email").getAsString());
297

    
298
            users.add(user);
299
        });
300
        return users;
301
    }
302

    
303
    @Override
304
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
305
        Map<String, String> params = new HashMap<>();
306
        params.put("couid", couId.toString());
307
        if (admin) {
308
            params.put("admin", "true");
309
        }
310
        JsonElement response = httpUtils.get("names.json", params);
311
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
312
        JsonArray names = new JsonArray();
313
        infos.forEach(info -> {
314
            JsonObject user = new JsonObject();
315
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
316
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
317
            names.add(user);
318
        });
319
        return names;
320
    }
321

    
322
    @Override
323
    public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
324
        Map<String, String> params = new HashMap<>();
325
        params.put("couid", couId.toString());
326
        if (admin) {
327
            params.put("admin", "true");
328
        }
329
        JsonElement response = httpUtils.get("identifiers.json", params);
330
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
331
        JsonArray emails = new JsonArray();
332
        infos.forEach(info -> {
333
            JsonObject user = new JsonObject();
334
            user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
335
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
336
            emails.add(user);
337
        });
338
        return emails;
339
    }
340

    
341
    @Override
342
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
343
        if (id != null) {
344
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
345
        } else {
346
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
347
        }
348
    }
349

    
350
    @Override
351
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
352
        if (id != null) {
353
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
354
        }
355
    }
356

    
357
    @Override
358
    public Integer createRole(Role role) {
359
        JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
360
        return element.getAsJsonObject().get("Id").getAsInt();
361
    }
362

    
363
    @Override
364
    public String getUserEmail(Integer coPersonId) {
365
        Map<String, String> params = new HashMap<>();
366
        params.put("copersonid", coPersonId.toString());
367
        JsonElement response = httpUtils.get("email_addresses.json", params);
368
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
369
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
370
    }
371

    
372
    @Override
373
    public String getUserNames(Integer coPersonId) {
374
        Map<String, String> params = new HashMap<>();
375
        params.put("copersonid", coPersonId.toString());
376
        JsonElement response = httpUtils.get("names.json", params);
377
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
378
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
379
    }
380

    
381
    @Override
382
    public String getUserId(Integer coPersonId) {
383
        Map<String, String> params = new HashMap<>();
384
        params.put("copersonid", coPersonId.toString());
385
        JsonElement response = httpUtils.get("identifiers.json", params);
386
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
387
        return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
388
    }
389

    
390
    @Override
391
    public void assignAdminRole(Integer coPersonId, Integer couId) {
392
        JsonObject group = getCouAdminGroup(couId);
393
        if (group != null) {
394
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
395
        }
396
    }
397

    
398
    @Override
399
    public void removeAdminRole(Integer coPersonId, Integer couId) {
400
        JsonObject adminGroup = this.getCouAdminGroup(couId);
401
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
402
        Integer id = null;
403
        for (JsonElement admin : admins) {
404
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
405
                id = admin.getAsJsonObject().get("Id").getAsInt();
406
            }
407
        }
408
        if (id != null) {
409
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
410
        }
411
    }
412

    
413
    @Override
414
    public Map<Integer, String> getCouNames(List<Integer> couIds) {
415
        Map<Integer, String> idNameMap = new HashMap<>();
416
        for (Integer id : couIds) {
417
            idNameMap.put(id, null);
418
        }
419

    
420
        JsonArray cous = getCous();
421
        int count = 0;
422
        int total = couIds.size();
423
        for (JsonElement cou : cous) {
424
            if (count < total) {
425
                if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) {
426
                    idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString());
427
                    count++;
428
                }
429
            } else {
430
                break;
431
            }
432
        }
433
        return idNameMap;
434
    }
435
}
(2-2/2)