Project

General

Profile

1
package eu.dnetlib.organizations.controller;
2

    
3
import java.util.Arrays;
4
import java.util.List;
5
import java.util.stream.Collectors;
6

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

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

    
34
@RestController
35
@RequestMapping("/api/organizations")
36
public class OrganizationController {
37

    
38
	@Autowired
39
	private OrganizationRepository organizationRepository;
40
	@Autowired
41
	private OrganizationViewRepository organizationViewRepository;
42
	@Autowired
43
	private OrganizationSimpleViewRepository organizationSimpleViewRepository;
44
	@Autowired
45
	private OpenaireSimRelRepository openaireSimRelRepository;
46
	@Autowired
47
	private WarningViewRepository warningViewRepository;
48
	@Autowired
49
	private DatabaseUtils databaseUtils;
50

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

    
67
	@RequestMapping(value = "/get", method = RequestMethod.GET)
68
	public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
69
		final OrganizationView org = organizationViewRepository.findById(id).get();
70

    
71
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
72
			return org;
73
		} else {
74
			throw new RuntimeException("User not authorized");
75
		}
76
	}
77

    
78
	@RequestMapping(value = "/relations", method = RequestMethod.GET)
79
	public List<RelationByOrg> findRelationsById(@RequestParam final String id, final Authentication authentication) {
80
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
81
			return organizationViewRepository.findRelations(id);
82
		} else {
83
			throw new RuntimeException("User not authorized");
84
		}
85
	}
86

    
87
	@RequestMapping(value = "/relations", method = RequestMethod.PUT)
88
	public List<RelationByOrg> addRelation(@RequestParam final String from,
89
			@RequestParam final String to,
90
			@RequestParam final RelationType type,
91
			final Authentication authentication) {
92
		if (from.equals(to)) {
93
			throw new IllegalArgumentException("Invalid relation !!!");
94
		} else if (UserInfo.isSuperUser(authentication)
95
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
96
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
97

    
98
			databaseUtils.addRelation(from, to, type);
99

    
100
			return organizationViewRepository.findRelations(from);
101
		} else {
102
			throw new RuntimeException("User not authorized");
103
		}
104

    
105
	}
106

    
107
	@RequestMapping(value = "/relations", method = RequestMethod.DELETE)
108
	public List<RelationByOrg> deleteRelation(@RequestParam final String from,
109
			@RequestParam final String to,
110
			@RequestParam final RelationType type,
111
			final Authentication authentication) {
112
		if (from.equals(to)) {
113
			throw new IllegalArgumentException("Invalid relation !!!");
114
		} else if (UserInfo.isSuperUser(authentication)
115
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
116
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
117

    
118
			databaseUtils.deleteRelation(from, to, type);
119

    
120
			return organizationViewRepository.findRelations(from);
121
		} else {
122
			throw new RuntimeException("User not authorized");
123
		}
124

    
125
	}
126

    
127
	@RequestMapping(value = "/similarities", method = RequestMethod.GET)
128
	public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id, final Authentication authentication) {
129
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
130
			return openaireSimRelRepository.findByLocalId(id).stream().filter(s -> !s.getOaOriginalId().startsWith(OpenOrgsConstants.OPENORGS_PREFIX)).collect(Collectors.toList());
131
		} else {
132
			throw new RuntimeException("User not authorized");
133
		}
134
	}
135

    
136
	@RequestMapping(value = "/warnings", method = RequestMethod.GET)
137
	public Iterable<WarningView> findWarningsById(final Authentication authentication) {
138
		if (UserInfo.isSuperUser(authentication)) {
139
			return warningViewRepository.findAll();
140
		} else {
141
			throw new RuntimeException("User not authorized");
142
		}
143
	}
144

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

    
148
		final boolean b = UserInfo.isSuperUser(authentication)
149
				|| simrels.stream().map(OpenaireSimRel::getLocalId).distinct().allMatch(id -> organizationRepository.verifyAuthorizationForId(id, authentication.getName()));
150

    
151
		if (b) {
152
			return openaireSimRelRepository.saveAll(simrels);
153
		} else {
154
			throw new RuntimeException("User not authorized");
155
		}
156
	}
157

    
158
	@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
159
	public Page<OrganizationSimpleView> search(@PathVariable final int page,
160
			@PathVariable final int size,
161
			@RequestParam final String q,
162
			final Authentication authentication) {
163
		return UserInfo.isSuperUser(authentication)
164
				? organizationSimpleViewRepository.findByNameContainingIgnoreCase(q, PageRequest.of(page, size))
165
				: organizationSimpleViewRepository.findByNameForUser(q, authentication.getName(), PageRequest.of(page, size));
166
	}
167

    
168
	@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
169
	public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
170
			@PathVariable final int page,
171
			@PathVariable final int size,
172
			final Authentication authentication) {
173
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
174
			return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
175
		} else {
176
			throw new RuntimeException("User not authorized");
177
		}
178
	}
179

    
180
	@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
181
	public Page<OrganizationSimpleView> findByType(@PathVariable final String type,
182
			@PathVariable final int page,
183
			@PathVariable final int size,
184
			final Authentication authentication) {
185
		return UserInfo.isSuperUser(authentication)
186
				? organizationSimpleViewRepository.findByType(type, PageRequest.of(page, size))
187
				: organizationSimpleViewRepository.findByTypeForUser(type, authentication.getName(), PageRequest.of(page, size));
188
	}
189

    
190
	@RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
191
	public List<BrowseEntry> browseCountries(final Authentication authentication) {
192
		return UserInfo.isSuperUser(authentication)
193
				? organizationSimpleViewRepository.browseCountries()
194
				: organizationSimpleViewRepository.browseCountriesForUser(authentication.getName());
195
	}
196

    
197
	@RequestMapping(value = "/browse/types", method = RequestMethod.GET)
198
	public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
199
		return UserInfo.isSuperUser(authentication)
200
				? organizationSimpleViewRepository.browseTypes()
201
				: organizationSimpleViewRepository.browseTypesForUser(authentication.getName());
202
	}
203

    
204
}
(2-2/5)