Revision 57097
Added by Michele Artini over 4 years ago
OrganizationController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.organizations.controller; |
2 | 2 |
|
3 |
import java.security.Principal; |
|
3 | 4 |
import java.util.Arrays; |
4 | 5 |
import java.util.List; |
5 |
import java.util.UUID; |
|
6 | 6 |
|
7 |
import org.apache.commons.codec.digest.DigestUtils; |
|
8 | 7 |
import org.apache.commons.lang3.StringUtils; |
9 | 8 |
import org.springframework.beans.factory.annotation.Autowired; |
10 | 9 |
import org.springframework.data.domain.Page; |
... | ... | |
16 | 15 |
import org.springframework.web.bind.annotation.RequestParam; |
17 | 16 |
import org.springframework.web.bind.annotation.RestController; |
18 | 17 |
|
19 |
import eu.dnetlib.organizations.model.Acronym; |
|
20 | 18 |
import eu.dnetlib.organizations.model.OpenaireSimRel; |
21 |
import eu.dnetlib.organizations.model.Organization; |
|
22 |
import eu.dnetlib.organizations.model.OtherIdentifier; |
|
23 |
import eu.dnetlib.organizations.model.OtherName; |
|
24 |
import eu.dnetlib.organizations.model.Type; |
|
25 |
import eu.dnetlib.organizations.model.Url; |
|
26 | 19 |
import eu.dnetlib.organizations.model.utils.BrowseEntry; |
27 | 20 |
import eu.dnetlib.organizations.model.utils.RelationByOrg; |
28 | 21 |
import eu.dnetlib.organizations.model.view.OrganizationSimpleView; |
29 | 22 |
import eu.dnetlib.organizations.model.view.OrganizationView; |
30 |
import eu.dnetlib.organizations.repository.AcronymRepository; |
|
31 | 23 |
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository; |
32 |
import eu.dnetlib.organizations.repository.OrganizationRepository; |
|
33 |
import eu.dnetlib.organizations.repository.OtherIdentifierRepository; |
|
34 |
import eu.dnetlib.organizations.repository.OtherNameRepository; |
|
35 |
import eu.dnetlib.organizations.repository.TypeRepository; |
|
36 |
import eu.dnetlib.organizations.repository.UrlRepository; |
|
37 | 24 |
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository; |
38 | 25 |
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository; |
26 |
import eu.dnetlib.organizations.utils.DatabaseUtils; |
|
39 | 27 |
|
40 | 28 |
@RestController |
41 | 29 |
@RequestMapping("/api/organizations") |
... | ... | |
47 | 35 |
private OrganizationSimpleViewRepository organizationSimpleViewRepository; |
48 | 36 |
@Autowired |
49 | 37 |
private OpenaireSimRelRepository openaireSimRelRepository; |
50 |
|
|
51 | 38 |
@Autowired |
52 |
private AcronymRepository acronymRepository; |
|
53 |
@Autowired |
|
54 |
private OrganizationRepository organizationRepository; |
|
55 |
@Autowired |
|
56 |
private OtherIdentifierRepository otherIdentifierRepository; |
|
57 |
@Autowired |
|
58 |
private OtherNameRepository otherNameRepository; |
|
59 |
@Autowired |
|
60 |
private TypeRepository typeRepository; |
|
61 |
@Autowired |
|
62 |
private UrlRepository urlRepository; |
|
39 |
private DatabaseUtils databaseUtils; |
|
63 | 40 |
|
64 | 41 |
@RequestMapping(value = "/save", method = RequestMethod.POST) |
65 |
public List<String> save(@RequestBody final OrganizationView org) { |
|
66 |
final String orgId = insertOrUpdateOrganization(org, StringUtils.isNotBlank(org.getId())); |
|
42 |
public List<String> save(@RequestBody final OrganizationView org, final Principal principal) { |
|
43 |
final String user = principal != null ? principal.getName() : "anonymous"; |
|
44 |
final String orgId = databaseUtils.insertOrUpdateOrganization(org, user, StringUtils.isNotBlank(org.getId())); |
|
67 | 45 |
return Arrays.asList(orgId); |
68 | 46 |
} |
69 | 47 |
|
70 |
private String insertOrUpdateOrganization(final OrganizationView org, final boolean update) { |
|
71 |
|
|
72 |
final String orgId = update ? org.getId() : "openorgs____::" + DigestUtils.md5Hex(UUID.randomUUID().toString()); |
|
73 |
|
|
74 |
if (update) { |
|
75 |
acronymRepository.deleteByOrgId(orgId); |
|
76 |
otherNameRepository.deleteByOrgId(orgId); |
|
77 |
otherIdentifierRepository.deleteByOrgId(orgId); |
|
78 |
typeRepository.deleteByOrgId(orgId); |
|
79 |
urlRepository.deleteByOrgId(orgId); |
|
80 |
} |
|
81 |
|
|
82 |
organizationRepository.save(new Organization(orgId, org.getName(), org.getLat(), org.getLng(), org.getCity(), org.getCountry())); |
|
83 |
org.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s))); |
|
84 |
org.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang()))); |
|
85 |
org.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType()))); |
|
86 |
org.getTypes().forEach(t -> typeRepository.save(new Type(orgId, t))); |
|
87 |
org.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u))); |
|
88 |
|
|
89 |
if (update) { |
|
90 |
// SET LAST MODIFICATION DATE AND USER |
|
91 |
} else { |
|
92 |
// SET LAST CREATION AND MODIFICATION 5DATE AND USER |
|
93 |
} |
|
94 |
|
|
95 |
return orgId; |
|
96 |
} |
|
97 |
|
|
98 | 48 |
@RequestMapping(value = "/get", method = RequestMethod.GET) |
99 | 49 |
public OrganizationView findById(@RequestParam final String id) { |
100 | 50 |
return organizationViewRepository.findById(id).get(); |
Also available in: Unified diff