Project

General

Profile

1
package eu.dnetlib.organizations.controller;
2

    
3
import java.util.Arrays;
4
import java.util.Collection;
5
import java.util.HashMap;
6
import java.util.LinkedHashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
import java.util.TreeMap;
11
import java.util.stream.Collectors;
12

    
13
import org.apache.commons.lang3.StringUtils;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.data.domain.Page;
16
import org.springframework.data.domain.PageRequest;
17
import org.springframework.security.core.Authentication;
18
import org.springframework.web.bind.annotation.PathVariable;
19
import org.springframework.web.bind.annotation.RequestBody;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestMethod;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24

    
25
import eu.dnetlib.organizations.model.OpenaireSimRel;
26
import eu.dnetlib.organizations.model.utils.BrowseEntry;
27
import eu.dnetlib.organizations.model.utils.RelationByOrg;
28
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
29
import eu.dnetlib.organizations.model.view.OrganizationView;
30
import eu.dnetlib.organizations.model.view.WarningView;
31
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
32
import eu.dnetlib.organizations.repository.OrganizationRepository;
33
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
34
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
35
import eu.dnetlib.organizations.repository.readonly.WarningViewRepository;
36
import eu.dnetlib.organizations.utils.DatabaseUtils;
37
import eu.dnetlib.organizations.utils.OpenOrgsConstants;
38
import eu.dnetlib.organizations.utils.RelationType;
39

    
40
@RestController
41
@RequestMapping("/api/organizations")
42
public class OrganizationController {
43

    
44
	@Autowired
45
	private OrganizationRepository organizationRepository;
46
	@Autowired
47
	private OrganizationViewRepository organizationViewRepository;
48
	@Autowired
49
	private OrganizationSimpleViewRepository organizationSimpleViewRepository;
50
	@Autowired
51
	private OpenaireSimRelRepository openaireSimRelRepository;
52
	@Autowired
53
	private WarningViewRepository warningViewRepository;
54
	@Autowired
55
	private DatabaseUtils databaseUtils;
56

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

    
73
	@RequestMapping(value = "/get", method = RequestMethod.GET)
74
	public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
75
		final OrganizationView org = organizationViewRepository.findById(id).get();
76

    
77
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
78
			return org;
79
		} else {
80
			throw new RuntimeException("User not authorized");
81
		}
82
	}
83

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

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

    
104
			databaseUtils.addRelation(from, to, type);
105

    
106
			return organizationViewRepository.findRelations(from);
107
		} else {
108
			throw new RuntimeException("User not authorized");
109
		}
110

    
111
	}
112

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

    
124
			databaseUtils.deleteRelation(from, to, type);
125

    
126
			return organizationViewRepository.findRelations(from);
127
		} else {
128
			throw new RuntimeException("User not authorized");
129
		}
130

    
131
	}
132

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

    
142
	@RequestMapping(value = "/warnings", method = RequestMethod.GET)
143
	public Collection<Set<OrganizationSimpleView>> findWarningsById(final Authentication authentication) {
144

    
145
		final Map<String, Set<OrganizationSimpleView>> res = new TreeMap<>();
146

    
147
		final Map<String, String> roots = new HashMap<>();
148

    
149
		if (UserInfo.isSuperUser(authentication)) {
150
			for (final WarningView w : warningViewRepository.findAll()) {
151
				final String root = findWarningRoot(w, roots);
152
				if (!res.containsKey(root)) {
153
					res.put(root, new LinkedHashSet<OrganizationSimpleView>());
154
					res.get(root).add(new OrganizationSimpleView(w.getId1(), w.getName1(), w.getType1(), w.getCity1(), w.getCountry1(), new String[] {}));
155
				}
156
				res.get(root).add(new OrganizationSimpleView(w.getId2(), w.getName2(), w.getType2(), w.getCity2(), w.getCountry2(), new String[] {}));
157
			}
158
			return res.values();
159
		} else {
160
			throw new RuntimeException("User not authorized");
161
		}
162
	}
163

    
164
	private String findWarningRoot(final WarningView w, final Map<String, String> roots) {
165
		if (roots.containsKey(w.getId1())) {
166
			return roots.get(w.getId1());
167
		} else if (roots.containsKey(w.getId2())) {
168
			return roots.get(w.getId2());
169
		} else {
170
			// id1 is the new root
171
			roots.put(w.getId1(), w.getId1());
172
			roots.put(w.getId2(), w.getId1());
173
			return w.getId1();
174
		}
175

    
176
	}
177

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

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

    
184
		if (b) {
185
			return openaireSimRelRepository.saveAll(simrels);
186
		} else {
187
			throw new RuntimeException("User not authorized");
188
		}
189
	}
190

    
191
	@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
192
	public Page<OrganizationSimpleView> search(@PathVariable final int page,
193
			@PathVariable final int size,
194
			@RequestParam final String q,
195
			final Authentication authentication) {
196
		return UserInfo.isSuperUser(authentication)
197
				? organizationSimpleViewRepository.findByNameContainingIgnoreCase(q, PageRequest.of(page, size))
198
				: organizationSimpleViewRepository.findByNameForUser(q, authentication.getName(), PageRequest.of(page, size));
199
	}
200

    
201
	@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
202
	public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
203
			@PathVariable final int page,
204
			@PathVariable final int size,
205
			final Authentication authentication) {
206
		if (UserInfo.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
207
			return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
208
		} else {
209
			throw new RuntimeException("User not authorized");
210
		}
211
	}
212

    
213
	@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
214
	public Page<OrganizationSimpleView> findByType(@PathVariable final String type,
215
			@PathVariable final int page,
216
			@PathVariable final int size,
217
			final Authentication authentication) {
218
		return UserInfo.isSuperUser(authentication)
219
				? organizationSimpleViewRepository.findByType(type, PageRequest.of(page, size))
220
				: organizationSimpleViewRepository.findByTypeForUser(type, authentication.getName(), PageRequest.of(page, size));
221
	}
222

    
223
	@RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
224
	public List<BrowseEntry> browseCountries(final Authentication authentication) {
225
		return UserInfo.isSuperUser(authentication)
226
				? organizationSimpleViewRepository.browseCountries()
227
				: organizationSimpleViewRepository.browseCountriesForUser(authentication.getName());
228
	}
229

    
230
	@RequestMapping(value = "/browse/types", method = RequestMethod.GET)
231
	public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
232
		return UserInfo.isSuperUser(authentication)
233
				? organizationSimpleViewRepository.browseTypes()
234
				: organizationSimpleViewRepository.browseTypesForUser(authentication.getName());
235
	}
236

    
237
}
(2-2/5)