Project

General

Profile

1
package eu.dnetlib.organizations.controller;
2

    
3
import java.util.Arrays;
4
import java.util.List;
5

    
6
import org.apache.commons.lang3.StringUtils;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.data.domain.Page;
9
import org.springframework.data.domain.PageRequest;
10
import org.springframework.security.core.Authentication;
11
import org.springframework.web.bind.annotation.PathVariable;
12
import org.springframework.web.bind.annotation.RequestBody;
13
import org.springframework.web.bind.annotation.RequestMapping;
14
import org.springframework.web.bind.annotation.RequestMethod;
15
import org.springframework.web.bind.annotation.RequestParam;
16
import org.springframework.web.bind.annotation.RestController;
17

    
18
import eu.dnetlib.organizations.model.OpenaireSimRel;
19
import eu.dnetlib.organizations.model.utils.BrowseEntry;
20
import eu.dnetlib.organizations.model.utils.RelationByOrg;
21
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
22
import eu.dnetlib.organizations.model.view.OrganizationView;
23
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
24
import eu.dnetlib.organizations.repository.OrganizationRepository;
25
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
26
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
27
import eu.dnetlib.organizations.utils.DatabaseUtils;
28
import eu.dnetlib.organizations.utils.RelationType;
29

    
30
@RestController
31
@RequestMapping("/api/organizations")
32
public class OrganizationController {
33

    
34
	@Autowired
35
	private OrganizationRepository organizationRepository;
36
	@Autowired
37
	private OrganizationViewRepository organizationViewRepository;
38
	@Autowired
39
	private OrganizationSimpleViewRepository organizationSimpleViewRepository;
40
	@Autowired
41
	private OpenaireSimRelRepository openaireSimRelRepository;
42
	@Autowired
43
	private DatabaseUtils databaseUtils;
44

    
45
	@RequestMapping(value = "/save", method = RequestMethod.POST)
46
	public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
47
		if (StringUtils.isBlank(org.getName())) {
48
			throw new RuntimeException("Missing field: name");
49
		} else if (StringUtils.isBlank(org.getCountry())) {
50
			throw new RuntimeException("Missing field: country");
51
		} else if (StringUtils.isBlank(org.getType())) {
52
			throw new RuntimeException("Missing field: type");
53
		} else if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
54
			final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName(), StringUtils.isNotBlank(org.getId()));
55
			return Arrays.asList(orgId);
56
		} else {
57
			throw new RuntimeException("User not authorized");
58
		}
59
	}
60

    
61
	@RequestMapping(value = "/get", method = RequestMethod.GET)
62
	public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
63
		final OrganizationView org = organizationViewRepository.findById(id).get();
64

    
65
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
66
			return org;
67
		} else {
68
			throw new RuntimeException("User not authorized");
69
		}
70
	}
71

    
72
	@RequestMapping(value = "/relations", method = RequestMethod.GET)
73
	public List<RelationByOrg> findRelationsById(@RequestParam final String id, final Authentication authentication) {
74
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
75
			return organizationViewRepository.findRelations(id);
76
		} else {
77
			throw new RuntimeException("User not authorized");
78
		}
79
	}
80

    
81
	@RequestMapping(value = "/relations", method = RequestMethod.PUT)
82
	public List<RelationByOrg> addRelation(@RequestParam final String from,
83
			@RequestParam final String to,
84
			@RequestParam final RelationType type,
85
			final Authentication authentication) {
86
		if (from.equals(to)) {
87
			throw new IllegalArgumentException("Invalid relation !!!");
88
		} else if (User.isSuperUser(authentication)
89
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
90
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
91

    
92
			databaseUtils.addRelation(from, to, type);
93

    
94
			return organizationViewRepository.findRelations(from);
95
		} else {
96
			throw new RuntimeException("User not authorized");
97
		}
98

    
99
	}
100

    
101
	@RequestMapping(value = "/relations", method = RequestMethod.DELETE)
102
	public List<RelationByOrg> deleteRelation(@RequestParam final String from,
103
			@RequestParam final String to,
104
			@RequestParam final RelationType type,
105
			final Authentication authentication) {
106
		if (from.equals(to)) {
107
			throw new IllegalArgumentException("Invalid relation !!!");
108
		} else if (User.isSuperUser(authentication)
109
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
110
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
111

    
112
			databaseUtils.deleteRelation(from, to, type);
113

    
114
			return organizationViewRepository.findRelations(from);
115
		} else {
116
			throw new RuntimeException("User not authorized");
117
		}
118

    
119
	}
120

    
121
	@RequestMapping(value = "/similarities", method = RequestMethod.GET)
122
	public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id, final Authentication authentication) {
123
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
124
			return openaireSimRelRepository.findByLocalId(id);
125
		} else {
126
			throw new RuntimeException("User not authorized");
127
		}
128
	}
129

    
130
	@RequestMapping(value = "/similarities", method = RequestMethod.POST)
131
	public List<OpenaireSimRel> saveSimilarities(@RequestBody final List<OpenaireSimRel> simrels, final Authentication authentication) {
132

    
133
		final boolean b = User.isSuperUser(authentication) || simrels.stream()
134
				.map(OpenaireSimRel::getLocalId)
135
				.distinct()
136
				.allMatch(id -> organizationRepository.verifyAuthorizationForId(id, authentication.getName()));
137

    
138
		if (b) {
139
			return openaireSimRelRepository.saveAll(simrels);
140
		} else {
141
			throw new RuntimeException("User not authorized");
142
		}
143
	}
144

    
145
	@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
146
	public Page<OrganizationSimpleView> search(@PathVariable final int page,
147
			@PathVariable final int size,
148
			@RequestParam final String q,
149
			final Authentication authentication) {
150
		return User.isSuperUser(authentication)
151
				? organizationSimpleViewRepository.findByNameContainingIgnoreCase(q, PageRequest.of(page, size))
152
				: organizationSimpleViewRepository.findByNameForUser(q, authentication.getName(), PageRequest.of(page, size));
153
	}
154

    
155
	@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
156
	public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
157
			@PathVariable final int page,
158
			@PathVariable final int size,
159
			final Authentication authentication) {
160
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
161
			return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
162
		} else {
163
			throw new RuntimeException("User not authorized");
164
		}
165
	}
166

    
167
	@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
168
	public Page<OrganizationSimpleView> findByType(@PathVariable final String type,
169
			@PathVariable final int page,
170
			@PathVariable final int size,
171
			final Authentication authentication) {
172
		return User.isSuperUser(authentication)
173
				? organizationSimpleViewRepository.findByType(type, PageRequest.of(page, size))
174
				: organizationSimpleViewRepository.findByTypeForUser(type, authentication.getName(), PageRequest.of(page, size));
175
	}
176

    
177
	@RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
178
	public List<BrowseEntry> browseCountries(final Authentication authentication) {
179
		return User.isSuperUser(authentication)
180
				? organizationSimpleViewRepository.browseCountries()
181
				: organizationSimpleViewRepository.browseCountriesForUser(authentication.getName());
182
	}
183

    
184
	@RequestMapping(value = "/browse/types", method = RequestMethod.GET)
185
	public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
186
		return User.isSuperUser(authentication)
187
				? organizationSimpleViewRepository.browseTypes()
188
				: organizationSimpleViewRepository.browseTypesForUser(authentication.getName());
189
	}
190

    
191
}
(2-2/6)