blob: 27a9c972cfccd36ad45770fc5e7853b44bfa717f [file] [log] [blame]
margarethaf093afb2017-11-12 21:38:23 +01001package de.ids_mannheim.korap.service;
2
margaretha351777b2017-12-13 19:55:04 +01003import java.util.ArrayList;
margaretha44573832018-03-21 16:59:59 +01004import java.util.Collections;
margaretha541b8cc2018-01-10 13:02:46 +01005import java.util.Iterator;
margaretha351777b2017-12-13 19:55:04 +01006import java.util.List;
margaretha2e1781f2018-08-21 11:45:26 +02007import java.util.regex.Pattern;
margaretha351777b2017-12-13 19:55:04 +01008
margaretha49cb6882018-07-04 04:19:54 +02009import org.apache.logging.log4j.LogManager;
10import org.apache.logging.log4j.Logger;
margarethaf093afb2017-11-12 21:38:23 +010011import org.springframework.beans.factory.annotation.Autowired;
12import org.springframework.stereotype.Service;
13
14import com.fasterxml.jackson.core.JsonProcessingException;
15import com.fasterxml.jackson.databind.JsonNode;
16
margaretha56e8e552017-12-05 16:31:21 +010017import de.ids_mannheim.korap.config.FullConfiguration;
margarethab874ef52018-01-23 20:26:31 +010018import de.ids_mannheim.korap.constant.GroupMemberStatus;
margaretha541b8cc2018-01-10 13:02:46 +010019import de.ids_mannheim.korap.constant.VirtualCorpusAccessStatus;
margarethaf093afb2017-11-12 21:38:23 +010020import de.ids_mannheim.korap.constant.VirtualCorpusType;
margaretha4edc70e2018-03-14 22:34:29 +010021import de.ids_mannheim.korap.dao.AdminDao;
margaretha541b8cc2018-01-10 13:02:46 +010022import de.ids_mannheim.korap.dao.VirtualCorpusAccessDao;
margarethaf093afb2017-11-12 21:38:23 +010023import de.ids_mannheim.korap.dao.VirtualCorpusDao;
margarethafc7d7772018-01-16 17:48:17 +010024import de.ids_mannheim.korap.dto.VirtualCorpusAccessDto;
margaretha351777b2017-12-13 19:55:04 +010025import de.ids_mannheim.korap.dto.VirtualCorpusDto;
margarethafc7d7772018-01-16 17:48:17 +010026import de.ids_mannheim.korap.dto.converter.VirtualCorpusAccessConverter;
margaretha351777b2017-12-13 19:55:04 +010027import de.ids_mannheim.korap.dto.converter.VirtualCorpusConverter;
margaretha45dde682018-01-04 21:33:46 +010028import de.ids_mannheim.korap.entity.UserGroup;
margarethacfea1ae2018-01-15 20:27:26 +010029import de.ids_mannheim.korap.entity.UserGroupMember;
margaretha351777b2017-12-13 19:55:04 +010030import de.ids_mannheim.korap.entity.VirtualCorpus;
margaretha541b8cc2018-01-10 13:02:46 +010031import de.ids_mannheim.korap.entity.VirtualCorpusAccess;
margarethaf093afb2017-11-12 21:38:23 +010032import de.ids_mannheim.korap.exceptions.KustvaktException;
33import de.ids_mannheim.korap.exceptions.StatusCodes;
34import de.ids_mannheim.korap.query.serialize.QuerySerializer;
margarethaf093afb2017-11-12 21:38:23 +010035import de.ids_mannheim.korap.user.User.CorpusAccess;
36import de.ids_mannheim.korap.utils.JsonUtils;
37import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
margarethad3bc71f2018-01-03 20:35:06 +010038import de.ids_mannheim.korap.utils.ParameterChecker;
margarethaf093afb2017-11-12 21:38:23 +010039import de.ids_mannheim.korap.web.SearchKrill;
margaretha0b63de42017-12-20 18:48:09 +010040import de.ids_mannheim.korap.web.controller.VirtualCorpusController;
margaretha9d3eb042017-12-22 11:02:30 +010041import de.ids_mannheim.korap.web.input.VirtualCorpusJson;
margarethaf093afb2017-11-12 21:38:23 +010042
margaretha2e1781f2018-08-21 11:45:26 +020043/**
44 * VirtualCorpusService handles the logic behind
45 * {@link VirtualCorpusController}.
46 * It communicates with {@link VirtualCorpusDao} and returns
47 * {@link VirtualCorpusDto} to {@link VirtualCorpusController}.
margaretha0b63de42017-12-20 18:48:09 +010048 *
49 * @author margaretha
50 *
51 */
margarethaf093afb2017-11-12 21:38:23 +010052@Service
53public class VirtualCorpusService {
54
55 private static Logger jlog =
margaretha49cb6882018-07-04 04:19:54 +020056 LogManager.getLogger(VirtualCorpusService.class);
margarethaf093afb2017-11-12 21:38:23 +010057
margaretha2e1781f2018-08-21 11:45:26 +020058 public static Pattern wordPattern = Pattern.compile("[\\w ]+");
59
margarethaf093afb2017-11-12 21:38:23 +010060 @Autowired
margaretha541b8cc2018-01-10 13:02:46 +010061 private VirtualCorpusDao vcDao;
62 @Autowired
63 private VirtualCorpusAccessDao accessDao;
margarethaf093afb2017-11-12 21:38:23 +010064 @Autowired
margaretha4edc70e2018-03-14 22:34:29 +010065 private AdminDao adminDao;
66 @Autowired
margaretha45dde682018-01-04 21:33:46 +010067 private UserGroupService userGroupService;
68 @Autowired
margarethaf093afb2017-11-12 21:38:23 +010069 private SearchKrill krill;
margarethaf093afb2017-11-12 21:38:23 +010070 @Autowired
margaretha56e8e552017-12-05 16:31:21 +010071 private FullConfiguration config;
margaretha351777b2017-12-13 19:55:04 +010072 @Autowired
margaretha351777b2017-12-13 19:55:04 +010073 private VirtualCorpusConverter converter;
margarethafc7d7772018-01-16 17:48:17 +010074 @Autowired
75 private VirtualCorpusAccessConverter accessConverter;
margarethaf093afb2017-11-12 21:38:23 +010076
margarethacfea1ae2018-01-15 20:27:26 +010077 public List<VirtualCorpusDto> listOwnerVC (String username)
78 throws KustvaktException {
79 List<VirtualCorpus> vcList = vcDao.retrieveOwnerVC(username);
80 return createVCDtos(vcList);
81 }
82
margaretha44573832018-03-21 16:59:59 +010083 public List<VirtualCorpusDto> listVCByUser (String contextUsername,
84 String createdBy) throws KustvaktException {
85
86 boolean isAdmin = adminDao.isAdmin(contextUsername);
87
88 if (createdBy != null) {
89 if (!createdBy.equals(contextUsername) && !isAdmin) {
90 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
91 "Unauthorized operation for user: " + contextUsername,
92 contextUsername);
93 }
94 }
95 else {
96 createdBy = contextUsername;
97 }
98 List<VirtualCorpus> vcList = vcDao.retrieveVCByUser(createdBy);
margaretha98ec15b2018-01-22 17:14:02 +010099 return createVCDtos(vcList);
margarethacfea1ae2018-01-15 20:27:26 +0100100 }
margarethab874ef52018-01-23 20:26:31 +0100101
margaretha44573832018-03-21 16:59:59 +0100102 public List<VirtualCorpusDto> listVCByType (String username,
103 String createdBy, VirtualCorpusType type) throws KustvaktException {
104
105 boolean isAdmin = adminDao.isAdmin(username);
106
107 if (isAdmin) {
108 List<VirtualCorpus> virtualCorpora =
109 vcDao.retrieveVCByType(type, createdBy);
110 Collections.sort(virtualCorpora);
111 return createVCDtos(virtualCorpora);
112 }
113 else {
114 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
115 "Unauthorized operation for user: " + username, username);
116 }
117 }
118
margarethacfea1ae2018-01-15 20:27:26 +0100119 private ArrayList<VirtualCorpusDto> createVCDtos (
margaretha98ec15b2018-01-22 17:14:02 +0100120 List<VirtualCorpus> vcList) throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100121 ArrayList<VirtualCorpusDto> dtos = new ArrayList<>(vcList.size());
122 VirtualCorpus vc;
123 Iterator<VirtualCorpus> i = vcList.iterator();
124 while (i.hasNext()) {
125 vc = i.next();
126 String json = vc.getCorpusQuery();
127 String statistics = krill.getStatistics(json);
128 VirtualCorpusDto vcDto =
129 converter.createVirtualCorpusDto(vc, statistics);
130 dtos.add(vcDto);
131 }
132 return dtos;
133 }
134
margaretha2e1781f2018-08-21 11:45:26 +0200135 /**
136 * Only admin and the owner of the virtual corpus are allowed to
137 * delete a virtual corpus.
138 *
139 * @param username
140 * username
141 * @param vcId
142 * virtual corpus id
margarethacfea1ae2018-01-15 20:27:26 +0100143 * @throws KustvaktException
144 */
145 public void deleteVC (String username, int vcId) throws KustvaktException {
146
margarethacfea1ae2018-01-15 20:27:26 +0100147 VirtualCorpus vc = vcDao.retrieveVCById(vcId);
148
margaretha4edc70e2018-03-14 22:34:29 +0100149 if (vc.getCreatedBy().equals(username) || adminDao.isAdmin(username)) {
margaretha44573832018-03-21 16:59:59 +0100150
151 if (vc.getType().equals(VirtualCorpusType.PUBLISHED)) {
152 VirtualCorpusAccess access =
153 accessDao.retrieveHiddenAccess(vcId);
154 accessDao.deleteAccess(access, "system");
155 userGroupService.deleteAutoHiddenGroup(
156 access.getUserGroup().getId(), "system");
157 }
158 vcDao.deleteVirtualCorpus(vc);
margarethacfea1ae2018-01-15 20:27:26 +0100159 }
160 else {
161 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
162 "Unauthorized operation for user: " + username, username);
163 }
164 }
165
166 public void editVC (VirtualCorpusJson vcJson, String username)
167 throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100168 ParameterChecker.checkIntegerValue(vcJson.getId(), "id");
margarethab874ef52018-01-23 20:26:31 +0100169 int vcId = vcJson.getId();
170 VirtualCorpus vc = vcDao.retrieveVCById(vcId);
margarethacfea1ae2018-01-15 20:27:26 +0100171
margaretha4edc70e2018-03-14 22:34:29 +0100172 if (!username.equals(vc.getCreatedBy())
173 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100174 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
175 "Unauthorized operation for user: " + username, username);
176 }
177
178 String koralQuery = null;
179 CorpusAccess requiredAccess = null;
180 if (vcJson.getCorpusQuery() != null
181 && vcJson.getCorpusQuery().isEmpty()) {
182 koralQuery = serializeCorpusQuery(vcJson.getCorpusQuery());
183 requiredAccess = determineRequiredAccess(koralQuery);
184 }
185
margarethab874ef52018-01-23 20:26:31 +0100186 VirtualCorpusType type = vcJson.getType();
187 if (type != null) {
188 if (vc.getType().equals(VirtualCorpusType.PUBLISHED)) {
189 // withdraw from publication
190 if (!type.equals(VirtualCorpusType.PUBLISHED)) {
191 VirtualCorpusAccess hiddenAccess =
192 accessDao.retrieveHiddenAccess(vcId);
193 deleteVCAccess(hiddenAccess.getId(), "system");
194 int groupId = hiddenAccess.getUserGroup().getId();
195 userGroupService.deleteAutoHiddenGroup(groupId, "system");
196 }
197 // else remains the same
198 }
199 else if (type.equals(VirtualCorpusType.PUBLISHED)) {
200 publishVC(vcJson.getId());
201 }
202 }
203
margarethacfea1ae2018-01-15 20:27:26 +0100204 vcDao.editVirtualCorpus(vc, vcJson.getName(), vcJson.getType(),
205 requiredAccess, koralQuery, vcJson.getDefinition(),
206 vcJson.getDescription(), vcJson.getStatus());
margarethab874ef52018-01-23 20:26:31 +0100207 }
margarethacfea1ae2018-01-15 20:27:26 +0100208
margarethab874ef52018-01-23 20:26:31 +0100209 private void publishVC (int vcId) throws KustvaktException {
210
211 VirtualCorpusAccess access = accessDao.retrieveHiddenAccess(vcId);
212 // check if hidden access exists
213 if (access == null) {
214 VirtualCorpus vc = vcDao.retrieveVCById(vcId);
215 // create and assign a hidden group
216 int groupId = userGroupService.createAutoHiddenGroup(vcId);
217 UserGroup autoHidden =
218 userGroupService.retrieveUserGroupById(groupId);
219 accessDao.createAccessToVC(vc, autoHidden, "system",
220 VirtualCorpusAccessStatus.HIDDEN);
221 }
222 else {
223 jlog.error("Cannot publish VC with id: " + vcId
224 + ". There have been hidden accesses for the VC already.");
margarethacfea1ae2018-01-15 20:27:26 +0100225 }
226 }
227
margaretha541b8cc2018-01-10 13:02:46 +0100228 public int storeVC (VirtualCorpusJson vc, String username)
margarethaf093afb2017-11-12 21:38:23 +0100229 throws KustvaktException {
margaretha45dde682018-01-04 21:33:46 +0100230 ParameterChecker.checkStringValue(vc.getName(), "name");
margarethad3bc71f2018-01-03 20:35:06 +0100231 ParameterChecker.checkObjectValue(vc.getType(), "type");
margaretha541b8cc2018-01-10 13:02:46 +0100232 ParameterChecker.checkStringValue(vc.getCorpusQuery(), "corpusQuery");
margaretha45dde682018-01-04 21:33:46 +0100233
margaretha2e1781f2018-08-21 11:45:26 +0200234 String name = vc.getName();
235 if (!wordPattern.matcher(name).matches()) {
236 throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
237 "Virtual corpus name must only contains letters, numbers, underscores and spaces",
238 name);
239 }
margaretha45dde682018-01-04 21:33:46 +0100240
margaretha44573832018-03-21 16:59:59 +0100241 if (vc.getType().equals(VirtualCorpusType.SYSTEM)
margaretha4edc70e2018-03-14 22:34:29 +0100242 && !adminDao.isAdmin(username)) {
margaretha56e8e552017-12-05 16:31:21 +0100243 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
margaretha45dde682018-01-04 21:33:46 +0100244 "Unauthorized operation for user: " + username, username);
margarethaf093afb2017-11-12 21:38:23 +0100245 }
246
margaretha541b8cc2018-01-10 13:02:46 +0100247 String koralQuery = serializeCorpusQuery(vc.getCorpusQuery());
margarethaf093afb2017-11-12 21:38:23 +0100248 CorpusAccess requiredAccess = determineRequiredAccess(koralQuery);
249
margaretha541b8cc2018-01-10 13:02:46 +0100250 int vcId = vcDao.createVirtualCorpus(vc.getName(), vc.getType(),
margaretha45dde682018-01-04 21:33:46 +0100251 requiredAccess, koralQuery, vc.getDefinition(),
margaretha7a25f012018-03-22 19:49:01 +0100252 vc.getDescription(), vc.getStatus(), username);
margaretha45dde682018-01-04 21:33:46 +0100253
margaretha541b8cc2018-01-10 13:02:46 +0100254 if (vc.getType().equals(VirtualCorpusType.PUBLISHED)) {
255 publishVC(vcId);
256 }
margarethaf093afb2017-11-12 21:38:23 +0100257 // EM: should this return anything?
margaretha541b8cc2018-01-10 13:02:46 +0100258 return vcId;
margarethaf093afb2017-11-12 21:38:23 +0100259 }
260
margaretha541b8cc2018-01-10 13:02:46 +0100261 private String serializeCorpusQuery (String corpusQuery)
margarethaf093afb2017-11-12 21:38:23 +0100262 throws KustvaktException {
263 QuerySerializer serializer = new QuerySerializer();
margaretha541b8cc2018-01-10 13:02:46 +0100264 serializer.setCollection(corpusQuery);
margarethaf093afb2017-11-12 21:38:23 +0100265 String koralQuery;
266 try {
267 koralQuery = serializer.convertCollectionToJson();
268 }
269 catch (JsonProcessingException e) {
270 throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
margaretha541b8cc2018-01-10 13:02:46 +0100271 "Invalid argument: " + corpusQuery, corpusQuery);
margarethaf093afb2017-11-12 21:38:23 +0100272 }
273 jlog.debug(koralQuery);
274 return koralQuery;
275 }
276
277 private CorpusAccess determineRequiredAccess (String koralQuery)
278 throws KustvaktException {
279
280 if (findDocWithLicense(koralQuery, config.getAllOnlyRegex())) {
281 return CorpusAccess.ALL;
282 }
283 else if (findDocWithLicense(koralQuery, config.getPublicOnlyRegex())) {
284 return CorpusAccess.PUB;
285 }
286 else {
287 return CorpusAccess.FREE;
288 }
289 }
290
291 private boolean findDocWithLicense (String koralQuery, String license)
292 throws KustvaktException {
293 KoralCollectionQueryBuilder koral = new KoralCollectionQueryBuilder();
294 koral.setBaseQuery(koralQuery);
295 koral.with("availability=/" + license + "/");
296 String json = koral.toJSON();
297
298 String statistics = krill.getStatistics(json);
299 JsonNode node = JsonUtils.readTree(statistics);
300 int numberOfDoc = node.at("/documents").asInt();
301 jlog.debug("License: " + license + ", number of docs: " + numberOfDoc);
302 return (numberOfDoc > 0) ? true : false;
303 }
margaretha351777b2017-12-13 19:55:04 +0100304
margarethafc7d7772018-01-16 17:48:17 +0100305 public List<VirtualCorpusAccess> retrieveAllVCAccess (int vcId)
margarethacfea1ae2018-01-15 20:27:26 +0100306 throws KustvaktException {
margarethafc7d7772018-01-16 17:48:17 +0100307 return accessDao.retrieveAllAccessByVC(vcId);
margarethacfea1ae2018-01-15 20:27:26 +0100308 }
309
310 public void shareVC (String username, int vcId, int groupId)
311 throws KustvaktException {
312
margarethacfea1ae2018-01-15 20:27:26 +0100313 VirtualCorpus vc = vcDao.retrieveVCById(vcId);
margaretha4edc70e2018-03-14 22:34:29 +0100314 if (!username.equals(vc.getCreatedBy())
315 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100316 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
317 "Unauthorized operation for user: " + username, username);
318 }
319
320 UserGroup userGroup = userGroupService.retrieveUserGroupById(groupId);
321
margaretha4edc70e2018-03-14 22:34:29 +0100322 if (!isVCAccessAdmin(userGroup, username)
323 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100324 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
325 "Unauthorized operation for user: " + username, username);
326 }
327 else {
328 accessDao.createAccessToVC(vc, userGroup, username,
329 VirtualCorpusAccessStatus.ACTIVE);
margaretha45667922018-01-25 21:23:03 +0100330 vcDao.editVirtualCorpus(vc, null, VirtualCorpusType.PUBLISHED, null,
331 null, null, null, null);
margarethacfea1ae2018-01-15 20:27:26 +0100332 }
333 }
334
335 private boolean isVCAccessAdmin (UserGroup userGroup, String username)
336 throws KustvaktException {
337 List<UserGroupMember> accessAdmins =
338 userGroupService.retrieveVCAccessAdmins(userGroup);
339 for (UserGroupMember m : accessAdmins) {
340 if (username.equals(m.getUserId())) {
341 return true;
342 }
343 }
344 return false;
345 }
346
margaretha2e1781f2018-08-21 11:45:26 +0200347 // public void editVCAccess (VirtualCorpusAccess access, String
348 // username)
349 // throws KustvaktException {
margaretha4edc70e2018-03-14 22:34:29 +0100350 //
margaretha2e1781f2018-08-21 11:45:26 +0200351 // // get all the VCA admins
352 // UserGroup userGroup = access.getUserGroup();
353 // List<UserGroupMember> accessAdmins =
354 // userGroupService.retrieveVCAccessAdmins(userGroup);
margaretha4edc70e2018-03-14 22:34:29 +0100355 //
margaretha2e1781f2018-08-21 11:45:26 +0200356 // User user = authManager.getUser(username);
357 // if (!user.isSystemAdmin()) {
358 // throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
359 // "Unauthorized operation for user: " + username, username);
360 // }
361 // }
margaretha541b8cc2018-01-10 13:02:46 +0100362
margarethafc7d7772018-01-16 17:48:17 +0100363 public List<VirtualCorpusAccessDto> listVCAccessByVC (String username,
margarethacfea1ae2018-01-15 20:27:26 +0100364 int vcId) throws KustvaktException {
365
margarethafc7d7772018-01-16 17:48:17 +0100366 List<VirtualCorpusAccess> accessList;
margaretha4edc70e2018-03-14 22:34:29 +0100367 if (adminDao.isAdmin(username)) {
margarethafc7d7772018-01-16 17:48:17 +0100368 accessList = accessDao.retrieveAllAccessByVC(vcId);
margarethacfea1ae2018-01-15 20:27:26 +0100369 }
margarethafc7d7772018-01-16 17:48:17 +0100370 else {
371 accessList = accessDao.retrieveActiveAccessByVC(vcId);
372 List<VirtualCorpusAccess> filteredAccessList = new ArrayList<>();
373 for (VirtualCorpusAccess access : accessList) {
374 UserGroup userGroup = access.getUserGroup();
375 if (isVCAccessAdmin(userGroup, username)) {
376 filteredAccessList.add(access);
377 }
margarethacfea1ae2018-01-15 20:27:26 +0100378 }
margarethafc7d7772018-01-16 17:48:17 +0100379 accessList = filteredAccessList;
margarethacfea1ae2018-01-15 20:27:26 +0100380 }
margarethafc7d7772018-01-16 17:48:17 +0100381 return accessConverter.createVCADto(accessList);
margarethacfea1ae2018-01-15 20:27:26 +0100382 }
383
margarethafc7d7772018-01-16 17:48:17 +0100384 public List<VirtualCorpusAccessDto> listVCAccessByGroup (String username,
margarethacfea1ae2018-01-15 20:27:26 +0100385 int groupId) throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100386 UserGroup userGroup = userGroupService.retrieveUserGroupById(groupId);
margarethafc7d7772018-01-16 17:48:17 +0100387
388 List<VirtualCorpusAccess> accessList;
margaretha4edc70e2018-03-14 22:34:29 +0100389 if (adminDao.isAdmin(username)) {
margarethafc7d7772018-01-16 17:48:17 +0100390 accessList = accessDao.retrieveAllAccessByGroup(groupId);
391 }
392 else if (isVCAccessAdmin(userGroup, username)) {
393 accessList = accessDao.retrieveActiveAccessByGroup(groupId);
394 }
395 else {
margarethacfea1ae2018-01-15 20:27:26 +0100396 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
397 "Unauthorized operation for user: " + username, username);
398 }
margarethafc7d7772018-01-16 17:48:17 +0100399
400 return accessConverter.createVCADto(accessList);
margaretha541b8cc2018-01-10 13:02:46 +0100401 }
margarethae8ab51d2018-01-16 19:27:40 +0100402
margarethab874ef52018-01-23 20:26:31 +0100403 public void deleteVCAccess (int accessId, String username)
margarethae8ab51d2018-01-16 19:27:40 +0100404 throws KustvaktException {
405
margarethae8ab51d2018-01-16 19:27:40 +0100406 VirtualCorpusAccess access = accessDao.retrieveAccessById(accessId);
407 UserGroup userGroup = access.getUserGroup();
margaretha4edc70e2018-03-14 22:34:29 +0100408 if (isVCAccessAdmin(userGroup, username)
409 || adminDao.isAdmin(username)) {
margarethab874ef52018-01-23 20:26:31 +0100410 accessDao.deleteAccess(access, username);
margarethae8ab51d2018-01-16 19:27:40 +0100411 }
412 else {
413 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
414 "Unauthorized operation for user: " + username, username);
415 }
416
417 }
418
419 public VirtualCorpusDto searchVCById (String username, int vcId)
420 throws KustvaktException {
margarethae8ab51d2018-01-16 19:27:40 +0100421
422 VirtualCorpus vc = vcDao.retrieveVCById(vcId);
423 VirtualCorpusType type = vc.getType();
424
margaretha4edc70e2018-03-14 22:34:29 +0100425 if (!adminDao.isAdmin(username)
426 && !username.equals(vc.getCreatedBy())) {
margarethae8ab51d2018-01-16 19:27:40 +0100427 if (type.equals(VirtualCorpusType.PRIVATE)
margaretha2c249912018-01-17 20:07:20 +0100428 || (type.equals(VirtualCorpusType.PROJECT)
429 && !hasAccess(username, vcId))) {
margarethae8ab51d2018-01-16 19:27:40 +0100430 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
431 "Unauthorized operation for user: " + username,
432 username);
433 }
margaretha2c249912018-01-17 20:07:20 +0100434
margarethae8ab51d2018-01-16 19:27:40 +0100435 else if (VirtualCorpusType.PUBLISHED.equals(type)) {
margaretha2e1781f2018-08-21 11:45:26 +0200436 // add user in the VC's auto group
margarethab874ef52018-01-23 20:26:31 +0100437 UserGroup userGroup =
margaretha293ee032018-03-20 20:11:52 +0100438 userGroupService.retrieveHiddenUserGroupByVC(vcId);
margaretha45667922018-01-25 21:23:03 +0100439 try {
margaretha18533fd2018-03-28 16:01:06 +0200440 userGroupService.addGroupMember(username, userGroup,
margarethab874ef52018-01-23 20:26:31 +0100441 "system", GroupMemberStatus.ACTIVE);
margaretha18533fd2018-03-28 16:01:06 +0200442 // member roles has not been set (not necessary)
margarethab874ef52018-01-23 20:26:31 +0100443 }
444 catch (KustvaktException e) {
445 // member exists
446 // skip adding user to hidden group
margarethae8ab51d2018-01-16 19:27:40 +0100447 }
448 }
margaretha44573832018-03-21 16:59:59 +0100449 // else VirtualCorpusType.SYSTEM
margarethae8ab51d2018-01-16 19:27:40 +0100450 }
451
452 String json = vc.getCorpusQuery();
453 String statistics = krill.getStatistics(json);
454 return converter.createVirtualCorpusDto(vc, statistics);
455 }
margaretha2c249912018-01-17 20:07:20 +0100456
457 private boolean hasAccess (String username, int vcId)
458 throws KustvaktException {
459 UserGroup userGroup;
460 List<VirtualCorpusAccess> accessList =
461 accessDao.retrieveActiveAccessByVC(vcId);
462 for (VirtualCorpusAccess access : accessList) {
463 userGroup = access.getUserGroup();
464 if (userGroupService.isMember(username, userGroup)) {
465 return true;
466 }
467 }
468 return false;
469 }
margarethaf093afb2017-11-12 21:38:23 +0100470}