blob: cc5f51b83b018e097eaee5c0021b9236eabce21b [file] [log] [blame]
margarethaf093afb2017-11-12 21:38:23 +01001package de.ids_mannheim.korap.service;
2
margarethaf7abb362018-09-18 20:09:37 +02003import java.sql.SQLException;
margaretha351777b2017-12-13 19:55:04 +01004import java.util.ArrayList;
margaretha44573832018-03-21 16:59:59 +01005import java.util.Collections;
margaretha541b8cc2018-01-10 13:02:46 +01006import java.util.Iterator;
margaretha351777b2017-12-13 19:55:04 +01007import java.util.List;
margaretha2e1781f2018-08-21 11:45:26 +02008import java.util.regex.Pattern;
margaretha351777b2017-12-13 19:55:04 +01009
margaretha49cb6882018-07-04 04:19:54 +020010import org.apache.logging.log4j.LogManager;
11import org.apache.logging.log4j.Logger;
margarethaf093afb2017-11-12 21:38:23 +010012import org.springframework.beans.factory.annotation.Autowired;
13import org.springframework.stereotype.Service;
14
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.JsonNode;
17
margaretha7c1f4282021-11-29 17:27:53 +010018import de.ids_mannheim.korap.cache.VirtualCorpusCache;
margaretha56e8e552017-12-05 16:31:21 +010019import de.ids_mannheim.korap.config.FullConfiguration;
margarethab874ef52018-01-23 20:26:31 +010020import de.ids_mannheim.korap.constant.GroupMemberStatus;
margarethab097fb02021-02-22 19:28:33 +010021import de.ids_mannheim.korap.constant.QueryAccessStatus;
margaretha7c1f4282021-11-29 17:27:53 +010022import de.ids_mannheim.korap.constant.QueryType;
Akronda080152020-12-03 13:53:29 +010023import de.ids_mannheim.korap.constant.ResourceType;
margaretha4edc70e2018-03-14 22:34:29 +010024import de.ids_mannheim.korap.dao.AdminDao;
margarethab097fb02021-02-22 19:28:33 +010025import de.ids_mannheim.korap.dao.QueryAccessDao;
26import de.ids_mannheim.korap.dao.QueryDao;
27import de.ids_mannheim.korap.dto.QueryAccessDto;
28import de.ids_mannheim.korap.dto.QueryDto;
29import de.ids_mannheim.korap.dto.converter.QueryAccessConverter;
30import de.ids_mannheim.korap.dto.converter.QueryConverter;
margaretha7c1f4282021-11-29 17:27:53 +010031import de.ids_mannheim.korap.entity.QueryAccess;
32import de.ids_mannheim.korap.entity.QueryDO;
margaretha45dde682018-01-04 21:33:46 +010033import de.ids_mannheim.korap.entity.UserGroup;
margarethacfea1ae2018-01-15 20:27:26 +010034import de.ids_mannheim.korap.entity.UserGroupMember;
margarethaf093afb2017-11-12 21:38:23 +010035import de.ids_mannheim.korap.exceptions.KustvaktException;
36import de.ids_mannheim.korap.exceptions.StatusCodes;
37import de.ids_mannheim.korap.query.serialize.QuerySerializer;
margarethaf093afb2017-11-12 21:38:23 +010038import de.ids_mannheim.korap.user.User.CorpusAccess;
39import de.ids_mannheim.korap.utils.JsonUtils;
40import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
margarethad3bc71f2018-01-03 20:35:06 +010041import de.ids_mannheim.korap.utils.ParameterChecker;
margarethaf093afb2017-11-12 21:38:23 +010042import de.ids_mannheim.korap.web.SearchKrill;
margaretha89bd8f52021-02-26 17:08:01 +010043import de.ids_mannheim.korap.web.controller.QueryReferenceController;
margaretha0b63de42017-12-20 18:48:09 +010044import de.ids_mannheim.korap.web.controller.VirtualCorpusController;
Akronda080152020-12-03 13:53:29 +010045import de.ids_mannheim.korap.web.input.QueryJson;
margaretha96c309d2023-08-16 12:24:12 +020046import jakarta.ws.rs.core.Response.Status;
margarethaf093afb2017-11-12 21:38:23 +010047
margaretha2e1781f2018-08-21 11:45:26 +020048/**
margaretha89bd8f52021-02-26 17:08:01 +010049 * QueryService handles the logic behind
50 * {@link VirtualCorpusController} and
51 * {@link QueryReferenceController}. Virtual corpora and
52 * stored-queries are both treated as queries of different types.
53 * Thus, they are handled logically similarly.
54 *
55 * QueryService communicates with {@link QueryDao}, handles
56 * {@link QueryDO} and
57 * returns
58 * {@link QueryDto} to {@link VirtualCorpusController} and
59 * {@link QueryReferenceController}.
margaretha0b63de42017-12-20 18:48:09 +010060 *
61 * @author margaretha
62 *
63 */
margarethaf093afb2017-11-12 21:38:23 +010064@Service
margarethab097fb02021-02-22 19:28:33 +010065public class QueryService {
margarethaf093afb2017-11-12 21:38:23 +010066
margaretha923c2072022-01-31 14:45:47 +010067 public static Logger jlog = LogManager.getLogger(QueryService.class);
margarethaf093afb2017-11-12 21:38:23 +010068
margarethadda4ef72018-12-06 14:20:51 +010069 public static boolean DEBUG = false;
margaretha8c203962019-01-14 17:01:33 +010070
margaretha9645ab02022-03-31 11:33:03 +020071 public static Pattern queryNamePattern = Pattern
72 .compile("[a-zA-Z0-9]+[a-zA-Z_0-9-.]+");
margaretha2e1781f2018-08-21 11:45:26 +020073
margarethaf093afb2017-11-12 21:38:23 +010074 @Autowired
margarethab097fb02021-02-22 19:28:33 +010075 private QueryDao queryDao;
margaretha541b8cc2018-01-10 13:02:46 +010076 @Autowired
margarethab097fb02021-02-22 19:28:33 +010077 private QueryAccessDao accessDao;
margarethaf093afb2017-11-12 21:38:23 +010078 @Autowired
margaretha4edc70e2018-03-14 22:34:29 +010079 private AdminDao adminDao;
80 @Autowired
margaretha45dde682018-01-04 21:33:46 +010081 private UserGroupService userGroupService;
82 @Autowired
margarethaf093afb2017-11-12 21:38:23 +010083 private SearchKrill krill;
margarethaf093afb2017-11-12 21:38:23 +010084 @Autowired
margaretha56e8e552017-12-05 16:31:21 +010085 private FullConfiguration config;
margaretha351777b2017-12-13 19:55:04 +010086 @Autowired
margarethab097fb02021-02-22 19:28:33 +010087 private QueryConverter converter;
margarethafc7d7772018-01-16 17:48:17 +010088 @Autowired
margarethab097fb02021-02-22 19:28:33 +010089 private QueryAccessConverter accessConverter;
margarethaf093afb2017-11-12 21:38:23 +010090
margaretha3ccaeb72019-02-28 18:40:22 +010091 private void verifyUsername (String contextUsername, String pathUsername)
margarethacfea1ae2018-01-15 20:27:26 +010092 throws KustvaktException {
margaretha3ccaeb72019-02-28 18:40:22 +010093 if (!contextUsername.equals(pathUsername)
94 && !adminDao.isAdmin(contextUsername)) {
95 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
96 "Unauthorized operation for user: " + contextUsername,
97 contextUsername);
98 }
99 }
100
margaretha35e1ca22023-11-16 22:00:01 +0100101 public List<QueryDto> listOwnerQuery (String username, QueryType queryType)
102 throws KustvaktException {
margarethab097fb02021-02-22 19:28:33 +0100103 List<QueryDO> list = queryDao.retrieveOwnerQuery(username, queryType);
104 return createQueryDtos(list, queryType);
margarethacfea1ae2018-01-15 20:27:26 +0100105 }
margaretha923c2072022-01-31 14:45:47 +0100106
margarethaaec6b412021-12-03 15:42:33 +0100107 public List<QueryDto> listSystemQuery (QueryType queryType)
108 throws KustvaktException {
109 List<QueryDO> list = queryDao.retrieveQueryByType(ResourceType.SYSTEM,
110 null, queryType);
111 return createQueryDtos(list, queryType);
112 }
margarethacfea1ae2018-01-15 20:27:26 +0100113
margarethacf869e12023-04-03 15:47:45 +0200114 public List<QueryDto> listAvailableQueryForUser (String username,
115 QueryType queryType) throws KustvaktException {
margaretha923c2072022-01-31 14:45:47 +0100116 List<QueryDO> list = queryDao.retrieveQueryByUser(username, queryType);
margarethab097fb02021-02-22 19:28:33 +0100117 return createQueryDtos(list, queryType);
margarethacfea1ae2018-01-15 20:27:26 +0100118 }
margarethab874ef52018-01-23 20:26:31 +0100119
margarethabdde7f42023-02-10 08:24:03 +0100120 public List<QueryDto> listQueryByType (String createdBy, ResourceType type,
121 QueryType queryType) throws KustvaktException {
margaretha44573832018-03-21 16:59:59 +0100122
margaretha35e1ca22023-11-16 22:00:01 +0100123 List<QueryDO> virtualCorpora = queryDao.retrieveQueryByType(type,
124 createdBy, queryType);
margarethabdde7f42023-02-10 08:24:03 +0100125 Collections.sort(virtualCorpora);
126 return createQueryDtos(virtualCorpora, queryType);
margaretha44573832018-03-21 16:59:59 +0100127
margaretha44573832018-03-21 16:59:59 +0100128 }
129
margaretha923c2072022-01-31 14:45:47 +0100130 private ArrayList<QueryDto> createQueryDtos (List<QueryDO> queryList,
131 QueryType queryType) throws KustvaktException {
margarethab097fb02021-02-22 19:28:33 +0100132 ArrayList<QueryDto> dtos = new ArrayList<>(queryList.size());
133 QueryDO query;
134 Iterator<QueryDO> i = queryList.iterator();
margarethacfea1ae2018-01-15 20:27:26 +0100135 while (i.hasNext()) {
margarethab097fb02021-02-22 19:28:33 +0100136 query = i.next();
margaretha923c2072022-01-31 14:45:47 +0100137 // String json = query.getKoralQuery();
margaretha652c4dc2021-02-12 17:07:44 +0100138 String statistics = null;
margaretha923c2072022-01-31 14:45:47 +0100139 // if (queryType.equals(QueryType.VIRTUAL_CORPUS)) {
140 // statistics = krill.getStatistics(json);
141 // }
142 QueryDto dto = converter.createQueryDto(query, statistics);
margarethab097fb02021-02-22 19:28:33 +0100143 dtos.add(dto);
margarethacfea1ae2018-01-15 20:27:26 +0100144 }
145 return dtos;
146 }
147
margarethab097fb02021-02-22 19:28:33 +0100148 public void deleteQueryByName (String username, String queryName,
margaretha89bd8f52021-02-26 17:08:01 +0100149 String createdBy, QueryType type) throws KustvaktException {
margaretha8c203962019-01-14 17:01:33 +0100150
margarethab097fb02021-02-22 19:28:33 +0100151 QueryDO query = queryDao.retrieveQueryByName(queryName, createdBy);
margaretha8c203962019-01-14 17:01:33 +0100152
margarethab097fb02021-02-22 19:28:33 +0100153 if (query == null) {
154 String code = createdBy + "/" + queryName;
margarethab5e1e0a2019-01-29 22:11:57 +0100155 throw new KustvaktException(StatusCodes.NO_RESOURCE_FOUND,
margaretha923c2072022-01-31 14:45:47 +0100156 "Query " + code + " is not found.", String.valueOf(code));
margaretha4af3f1e2019-01-16 17:53:26 +0100157 }
margarethab097fb02021-02-22 19:28:33 +0100158 else if (query.getCreatedBy().equals(username)
margaretha4af3f1e2019-01-16 17:53:26 +0100159 || adminDao.isAdmin(username)) {
margaretha8c203962019-01-14 17:01:33 +0100160
margarethab097fb02021-02-22 19:28:33 +0100161 if (query.getType().equals(ResourceType.PUBLISHED)) {
margaretha35e1ca22023-11-16 22:00:01 +0100162 QueryAccess access = accessDao
163 .retrieveHiddenAccess(query.getId());
margaretha8c203962019-01-14 17:01:33 +0100164 accessDao.deleteAccess(access, "system");
165 userGroupService.deleteAutoHiddenGroup(
166 access.getUserGroup().getId(), "system");
167 }
margaretha89bd8f52021-02-26 17:08:01 +0100168 if (type.equals(QueryType.VIRTUAL_CORPUS)
margaretha7c1f4282021-11-29 17:27:53 +0100169 && VirtualCorpusCache.contains(queryName)) {
margaretha923c2072022-01-31 14:45:47 +0100170 VirtualCorpusCache.delete(queryName);
margaretha47a72a82019-07-03 16:00:54 +0200171 }
margarethab097fb02021-02-22 19:28:33 +0100172 queryDao.deleteQuery(query);
margaretha8c203962019-01-14 17:01:33 +0100173 }
174 else {
175 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
176 "Unauthorized operation for user: " + username, username);
177 }
178 }
179
margarethab097fb02021-02-22 19:28:33 +0100180 public Status handlePutRequest (String username, String queryCreator,
181 String queryName, QueryJson queryJson) throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100182
margarethab097fb02021-02-22 19:28:33 +0100183 verifyUsername(username, queryCreator);
184 QueryDO query = queryDao.retrieveQueryByName(queryName, queryCreator);
margaretha923c2072022-01-31 14:45:47 +0100185
margarethab097fb02021-02-22 19:28:33 +0100186 if (query == null) {
margarethab3ecbe32021-08-16 12:55:54 +0200187 storeQuery(queryJson, queryName, queryCreator, username);
margarethabdd47ac2019-08-15 14:22:38 +0200188 return Status.CREATED;
margaretha4af3f1e2019-01-16 17:53:26 +0100189 }
190 else {
margarethab097fb02021-02-22 19:28:33 +0100191 editQuery(query, queryJson, queryName, username);
margarethabdd47ac2019-08-15 14:22:38 +0200192 return Status.NO_CONTENT;
margaretha4af3f1e2019-01-16 17:53:26 +0100193 }
194 }
195
margarethab097fb02021-02-22 19:28:33 +0100196 public void editQuery (QueryDO existingQuery, QueryJson newQuery,
197 String queryName, String username) throws KustvaktException {
margaretha4af3f1e2019-01-16 17:53:26 +0100198
margarethab097fb02021-02-22 19:28:33 +0100199 if (!username.equals(existingQuery.getCreatedBy())
margaretha4edc70e2018-03-14 22:34:29 +0100200 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100201 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
202 "Unauthorized operation for user: " + username, username);
203 }
204
205 String koralQuery = null;
206 CorpusAccess requiredAccess = null;
margarethab097fb02021-02-22 19:28:33 +0100207 String corpusQuery = newQuery.getCorpusQuery();
margaretha1703c162021-08-13 17:13:42 +0200208 String query = newQuery.getQuery();
209 String queryLanguage = newQuery.getQueryLanguage();
margarethaed053fb2019-04-11 15:15:13 +0200210 if (corpusQuery != null && !corpusQuery.isEmpty()) {
margaretha4af3f1e2019-01-16 17:53:26 +0100211 koralQuery = serializeCorpusQuery(corpusQuery);
margaretha923c2072022-01-31 14:45:47 +0100212 requiredAccess = determineRequiredAccess(newQuery.isCached(),
213 queryName, koralQuery);
margarethacfea1ae2018-01-15 20:27:26 +0100214 }
margaretha1703c162021-08-13 17:13:42 +0200215 else if (query != null && !query.isEmpty() && queryLanguage != null
216 && !queryLanguage.isEmpty()) {
217 koralQuery = serializeQuery(query, queryLanguage);
218 }
margarethacfea1ae2018-01-15 20:27:26 +0100219
margarethab097fb02021-02-22 19:28:33 +0100220 ResourceType type = newQuery.getType();
margarethab874ef52018-01-23 20:26:31 +0100221 if (type != null) {
margarethab097fb02021-02-22 19:28:33 +0100222 if (existingQuery.getType().equals(ResourceType.PUBLISHED)) {
margarethab874ef52018-01-23 20:26:31 +0100223 // withdraw from publication
Akronda080152020-12-03 13:53:29 +0100224 if (!type.equals(ResourceType.PUBLISHED)) {
margaretha923c2072022-01-31 14:45:47 +0100225 QueryAccess hiddenAccess = accessDao
226 .retrieveHiddenAccess(existingQuery.getId());
margarethab097fb02021-02-22 19:28:33 +0100227 deleteQueryAccess(hiddenAccess.getId(), "system");
margarethab874ef52018-01-23 20:26:31 +0100228 int groupId = hiddenAccess.getUserGroup().getId();
229 userGroupService.deleteAutoHiddenGroup(groupId, "system");
margaretha652c4dc2021-02-12 17:07:44 +0100230 // EM: should the users within the hidden group
231 // receive
232 // notifications?
margarethab874ef52018-01-23 20:26:31 +0100233 }
234 // else remains the same
235 }
Akronda080152020-12-03 13:53:29 +0100236 else if (type.equals(ResourceType.PUBLISHED)) {
margarethab097fb02021-02-22 19:28:33 +0100237 publishQuery(existingQuery.getId());
margarethab874ef52018-01-23 20:26:31 +0100238 }
239 }
240
margarethab097fb02021-02-22 19:28:33 +0100241 queryDao.editQuery(existingQuery, queryName, type, requiredAccess,
242 koralQuery, newQuery.getDefinition(), newQuery.getDescription(),
margaretha923c2072022-01-31 14:45:47 +0100243 newQuery.getStatus(), newQuery.isCached(), query,
244 queryLanguage);
margarethab874ef52018-01-23 20:26:31 +0100245 }
margarethacfea1ae2018-01-15 20:27:26 +0100246
margarethab097fb02021-02-22 19:28:33 +0100247 private void publishQuery (int queryId) throws KustvaktException {
margarethab874ef52018-01-23 20:26:31 +0100248
margarethab097fb02021-02-22 19:28:33 +0100249 QueryAccess access = accessDao.retrieveHiddenAccess(queryId);
margarethab874ef52018-01-23 20:26:31 +0100250 // check if hidden access exists
251 if (access == null) {
margarethab097fb02021-02-22 19:28:33 +0100252 QueryDO query = queryDao.retrieveQueryById(queryId);
margarethaf7abb362018-09-18 20:09:37 +0200253 // create and assign a new hidden group
margarethaa18ab2b2019-11-11 12:55:26 +0100254 int groupId = userGroupService.createAutoHiddenGroup();
margaretha35e1ca22023-11-16 22:00:01 +0100255 UserGroup autoHidden = userGroupService
256 .retrieveUserGroupById(groupId);
margarethab097fb02021-02-22 19:28:33 +0100257 accessDao.createAccessToQuery(query, autoHidden, "system",
258 QueryAccessStatus.HIDDEN);
margarethab874ef52018-01-23 20:26:31 +0100259 }
260 else {
margarethaf7abb362018-09-18 20:09:37 +0200261 // should not happened
margarethab097fb02021-02-22 19:28:33 +0100262 jlog.error("Cannot publish query with id: " + queryId
margarethaf7abb362018-09-18 20:09:37 +0200263 + ". Hidden access exists! Access id: " + access.getId());
margarethacfea1ae2018-01-15 20:27:26 +0100264 }
265 }
margaretha923c2072022-01-31 14:45:47 +0100266
margarethab3ecbe32021-08-16 12:55:54 +0200267 public void storeQuery (QueryJson query, String queryName,
268 String queryCreator, String username) throws KustvaktException {
margaretha9e73c0e2023-05-05 16:51:49 +0200269 QueryType queryType = query.getQueryType();
270 if (!checkNumberOfQueryLimit(username, queryType)) {
271 String type = queryType.displayName().toLowerCase();
margaretha35e1ca22023-11-16 22:00:01 +0100272 throw new KustvaktException(StatusCodes.NOT_ALLOWED,
273 "Cannot create " + type + ". The maximum number " + "of "
274 + type + " has been reached.");
Akronda080152020-12-03 13:53:29 +0100275 }
margaretha35e1ca22023-11-16 22:00:01 +0100276
margaretha9e73c0e2023-05-05 16:51:49 +0200277 String koralQuery = computeKoralQuery(query);
margarethab3ecbe32021-08-16 12:55:54 +0200278 storeQuery(username, queryName, query.getType(), query.getQueryType(),
279 koralQuery, query.getDefinition(), query.getDescription(),
280 query.getStatus(), query.isCached(), queryCreator,
margaretha479a4472021-02-18 12:00:55 +0100281 query.getQuery(), query.getQueryLanguage());
margarethaf7abb362018-09-18 20:09:37 +0200282 }
margaretha35e1ca22023-11-16 22:00:01 +0100283
margaretha9e73c0e2023-05-05 16:51:49 +0200284 private boolean checkNumberOfQueryLimit (String username,
285 QueryType queryType) throws KustvaktException {
286 Long num = queryDao.countNumberOfQuery(username, queryType);
margaretha35e1ca22023-11-16 22:00:01 +0100287 if (num < config.getMaxNumberOfUserQueries())
288 return true;
289 else
290 return false;
margaretha9e73c0e2023-05-05 16:51:49 +0200291 }
margaretha35e1ca22023-11-16 22:00:01 +0100292
293 private String computeKoralQuery (QueryJson query)
294 throws KustvaktException {
margaretha9e73c0e2023-05-05 16:51:49 +0200295 if (query.getQueryType().equals(QueryType.VIRTUAL_CORPUS)) {
296 ParameterChecker.checkStringValue(query.getCorpusQuery(),
297 "corpusQuery");
298 return serializeCorpusQuery(query.getCorpusQuery());
299 }
margaretha35e1ca22023-11-16 22:00:01 +0100300
margaretha9e73c0e2023-05-05 16:51:49 +0200301 if (query.getQueryType().equals(QueryType.QUERY)) {
302 ParameterChecker.checkStringValue(query.getQuery(), "query");
303 ParameterChecker.checkStringValue(query.getQueryLanguage(),
304 "queryLanguage");
margaretha35e1ca22023-11-16 22:00:01 +0100305 return serializeQuery(query.getQuery(), query.getQueryLanguage());
margaretha9e73c0e2023-05-05 16:51:49 +0200306 }
margaretha35e1ca22023-11-16 22:00:01 +0100307
margaretha9e73c0e2023-05-05 16:51:49 +0200308 return null;
309 }
margarethaf7abb362018-09-18 20:09:37 +0200310
margarethab3ecbe32021-08-16 12:55:54 +0200311 public void storeQuery (String username, String queryName,
312 ResourceType type, QueryType queryType, String koralQuery,
313 String definition, String description, String status,
314 boolean isCached, String queryCreator, String query,
margaretha5213ced2021-02-17 12:27:59 +0100315 String queryLanguage) throws KustvaktException {
margarethaa8c364b2021-02-19 13:00:31 +0100316 ParameterChecker.checkNameValue(queryName, "queryName");
margarethaf7abb362018-09-18 20:09:37 +0200317 ParameterChecker.checkObjectValue(type, "type");
318
margaretha479a4472021-02-18 12:00:55 +0100319 if (!queryNamePattern.matcher(queryName).matches()) {
margaretha923c2072022-01-31 14:45:47 +0100320 throw new KustvaktException(StatusCodes.INVALID_ARGUMENT, queryType
margaretha35e1ca22023-11-16 22:00:01 +0100321 .displayName()
322 + " must consists of alphanumerical characters "
323 + "(limited to ASCII), underscores, dashes and periods. "
324 + "The name has to start with an alphanumerical character.",
margaretha479a4472021-02-18 12:00:55 +0100325 queryName);
margaretha2e1781f2018-08-21 11:45:26 +0200326 }
margaretha45dde682018-01-04 21:33:46 +0100327
margaretha923c2072022-01-31 14:45:47 +0100328 if (type.equals(ResourceType.SYSTEM)) {
margarethab3ecbe32021-08-16 12:55:54 +0200329 if (adminDao.isAdmin(username)) {
margaretha923c2072022-01-31 14:45:47 +0100330 queryCreator = "system";
margarethab3ecbe32021-08-16 12:55:54 +0200331 }
332 else if (!username.equals("system")) {
333 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
margaretha923c2072022-01-31 14:45:47 +0100334 "Unauthorized operation for user: " + username,
335 username);
margarethab3ecbe32021-08-16 12:55:54 +0200336 }
margarethaf093afb2017-11-12 21:38:23 +0100337 }
338
margaretha479a4472021-02-18 12:00:55 +0100339 CorpusAccess requiredAccess = CorpusAccess.PUB;
340 if (queryType.equals(QueryType.VIRTUAL_CORPUS)) {
margaretha35e1ca22023-11-16 22:00:01 +0100341 requiredAccess = determineRequiredAccess(isCached, queryName,
342 koralQuery);
margaretha479a4472021-02-18 12:00:55 +0100343 }
margaretha8c203962019-01-14 17:01:33 +0100344
margaretha923c2072022-01-31 14:45:47 +0100345 if (DEBUG) {
margarethab097fb02021-02-22 19:28:33 +0100346 jlog.debug("Storing query: " + queryName + "in the database ");
347 }
margaretha923c2072022-01-31 14:45:47 +0100348
margaretha479a4472021-02-18 12:00:55 +0100349 int queryId = 0;
margarethaf7abb362018-09-18 20:09:37 +0200350 try {
margarethab097fb02021-02-22 19:28:33 +0100351 queryId = queryDao.createQuery(queryName, type, queryType,
Akronda080152020-12-03 13:53:29 +0100352 requiredAccess, koralQuery, definition, description, status,
margarethab3ecbe32021-08-16 12:55:54 +0200353 isCached, queryCreator, query, queryLanguage);
margaretha45dde682018-01-04 21:33:46 +0100354
margarethaf7abb362018-09-18 20:09:37 +0200355 }
356 catch (Exception e) {
357 Throwable cause = e;
358 Throwable lastCause = null;
359 while ((cause = cause.getCause()) != null
360 && !cause.equals(lastCause)) {
361 if (cause instanceof SQLException) {
362 break;
363 }
364 lastCause = cause;
365 }
366 throw new KustvaktException(StatusCodes.DB_INSERT_FAILED,
367 cause.getMessage());
368 }
Akronda080152020-12-03 13:53:29 +0100369 if (type.equals(ResourceType.PUBLISHED)) {
margarethab097fb02021-02-22 19:28:33 +0100370 publishQuery(queryId);
margaretha541b8cc2018-01-10 13:02:46 +0100371 }
margarethaf093afb2017-11-12 21:38:23 +0100372 }
373
margaretha541b8cc2018-01-10 13:02:46 +0100374 private String serializeCorpusQuery (String corpusQuery)
margarethaf093afb2017-11-12 21:38:23 +0100375 throws KustvaktException {
376 QuerySerializer serializer = new QuerySerializer();
margaretha541b8cc2018-01-10 13:02:46 +0100377 serializer.setCollection(corpusQuery);
margarethaf093afb2017-11-12 21:38:23 +0100378 String koralQuery;
379 try {
380 koralQuery = serializer.convertCollectionToJson();
381 }
382 catch (JsonProcessingException e) {
383 throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
margaretha541b8cc2018-01-10 13:02:46 +0100384 "Invalid argument: " + corpusQuery, corpusQuery);
margarethaf093afb2017-11-12 21:38:23 +0100385 }
margarethadda4ef72018-12-06 14:20:51 +0100386 if (DEBUG) {
387 jlog.debug(koralQuery);
388 }
margarethaf093afb2017-11-12 21:38:23 +0100389 return koralQuery;
390 }
margaretha652c4dc2021-02-12 17:07:44 +0100391
Akronda080152020-12-03 13:53:29 +0100392 private String serializeQuery (String query, String queryLanguage)
393 throws KustvaktException {
394 QuerySerializer serializer = new QuerySerializer();
395 String koralQuery;
margaretha652c4dc2021-02-12 17:07:44 +0100396 koralQuery = serializer.setQuery(query, queryLanguage).toJSON();
Akronda080152020-12-03 13:53:29 +0100397 if (DEBUG) {
398 jlog.debug(koralQuery);
399 }
400 return koralQuery;
401 }
margarethaf093afb2017-11-12 21:38:23 +0100402
margaretha4af3f1e2019-01-16 17:53:26 +0100403 public CorpusAccess determineRequiredAccess (boolean isCached, String name,
404 String koralQuery) throws KustvaktException {
405
406 if (isCached) {
margaretha35e1ca22023-11-16 22:00:01 +0100407 KoralCollectionQueryBuilder koral = new KoralCollectionQueryBuilder();
margaretha4af3f1e2019-01-16 17:53:26 +0100408 koral.with("referTo " + name);
409 koralQuery = koral.toJSON();
410 if (DEBUG) {
411 jlog.debug("Determine vc access with vc ref: " + koralQuery);
412 }
413
414 }
margarethaf093afb2017-11-12 21:38:23 +0100415
416 if (findDocWithLicense(koralQuery, config.getAllOnlyRegex())) {
417 return CorpusAccess.ALL;
418 }
419 else if (findDocWithLicense(koralQuery, config.getPublicOnlyRegex())) {
420 return CorpusAccess.PUB;
421 }
422 else {
423 return CorpusAccess.FREE;
424 }
425 }
426
427 private boolean findDocWithLicense (String koralQuery, String license)
428 throws KustvaktException {
429 KoralCollectionQueryBuilder koral = new KoralCollectionQueryBuilder();
430 koral.setBaseQuery(koralQuery);
431 koral.with("availability=/" + license + "/");
432 String json = koral.toJSON();
433
434 String statistics = krill.getStatistics(json);
435 JsonNode node = JsonUtils.readTree(statistics);
436 int numberOfDoc = node.at("/documents").asInt();
margarethadda4ef72018-12-06 14:20:51 +0100437 if (DEBUG) {
438 jlog.debug(
439 "License: " + license + ", number of docs: " + numberOfDoc);
440 }
margarethaf093afb2017-11-12 21:38:23 +0100441 return (numberOfDoc > 0) ? true : false;
442 }
margaretha351777b2017-12-13 19:55:04 +0100443
margarethab097fb02021-02-22 19:28:33 +0100444 public void shareQuery (String username, String createdBy, String queryName,
margaretha3ccaeb72019-02-28 18:40:22 +0100445 String groupName) throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100446
margarethab097fb02021-02-22 19:28:33 +0100447 QueryDO query = queryDao.retrieveQueryByName(queryName, createdBy);
448 if (query == null) {
449 String code = createdBy + "/" + queryName;
margarethaed053fb2019-04-11 15:15:13 +0200450 throw new KustvaktException(StatusCodes.NO_RESOURCE_FOUND,
margaretha923c2072022-01-31 14:45:47 +0100451 "Query " + code + " is not found.", String.valueOf(code));
margarethaed053fb2019-04-11 15:15:13 +0200452 }
margarethab097fb02021-02-22 19:28:33 +0100453 if (!username.equals(query.getCreatedBy())
margaretha4edc70e2018-03-14 22:34:29 +0100454 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100455 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
456 "Unauthorized operation for user: " + username, username);
457 }
458
margaretha35e1ca22023-11-16 22:00:01 +0100459 UserGroup userGroup = userGroupService
460 .retrieveUserGroupByName(groupName);
margarethacfea1ae2018-01-15 20:27:26 +0100461
margarethab097fb02021-02-22 19:28:33 +0100462 if (!isQueryAccessAdmin(userGroup, username)
margaretha4edc70e2018-03-14 22:34:29 +0100463 && !adminDao.isAdmin(username)) {
margarethacfea1ae2018-01-15 20:27:26 +0100464 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
465 "Unauthorized operation for user: " + username, username);
466 }
467 else {
margarethaf7abb362018-09-18 20:09:37 +0200468 try {
margarethab097fb02021-02-22 19:28:33 +0100469 accessDao.createAccessToQuery(query, userGroup, username,
470 QueryAccessStatus.ACTIVE);
margarethaf7abb362018-09-18 20:09:37 +0200471 }
472 catch (Exception e) {
473 Throwable cause = e;
474 Throwable lastCause = null;
475 while ((cause = cause.getCause()) != null
476 && !cause.equals(lastCause)) {
477 if (cause instanceof SQLException) {
478 break;
479 }
480 lastCause = cause;
481 }
482 throw new KustvaktException(StatusCodes.DB_INSERT_FAILED,
483 cause.getMessage());
484 }
margaretha652c4dc2021-02-12 17:07:44 +0100485
margarethab097fb02021-02-22 19:28:33 +0100486 queryDao.editQuery(query, null, ResourceType.PROJECT, null, null,
margaretha1703c162021-08-13 17:13:42 +0200487 null, null, null, query.isCached(), null, null);
margarethacfea1ae2018-01-15 20:27:26 +0100488 }
489 }
490
margarethab097fb02021-02-22 19:28:33 +0100491 private boolean isQueryAccessAdmin (UserGroup userGroup, String username)
margarethacfea1ae2018-01-15 20:27:26 +0100492 throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100493 List<UserGroupMember> accessAdmins = userGroupService
494 .retrieveQueryAccessAdmins(userGroup);
margarethacfea1ae2018-01-15 20:27:26 +0100495 for (UserGroupMember m : accessAdmins) {
496 if (username.equals(m.getUserId())) {
497 return true;
498 }
499 }
500 return false;
501 }
502
margaretha2e1781f2018-08-21 11:45:26 +0200503 // public void editVCAccess (VirtualCorpusAccess access, String
504 // username)
505 // throws KustvaktException {
margaretha4edc70e2018-03-14 22:34:29 +0100506 //
margaretha2e1781f2018-08-21 11:45:26 +0200507 // // get all the VCA admins
508 // UserGroup userGroup = access.getUserGroup();
509 // List<UserGroupMember> accessAdmins =
510 // userGroupService.retrieveVCAccessAdmins(userGroup);
margaretha4edc70e2018-03-14 22:34:29 +0100511 //
margaretha2e1781f2018-08-21 11:45:26 +0200512 // User user = authManager.getUser(username);
513 // if (!user.isSystemAdmin()) {
514 // throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
515 // "Unauthorized operation for user: " + username, username);
516 // }
517 // }
margaretha541b8cc2018-01-10 13:02:46 +0100518
margarethab097fb02021-02-22 19:28:33 +0100519 public List<QueryAccessDto> listQueryAccessByUsername (String username)
margaretha3ccaeb72019-02-28 18:40:22 +0100520 throws KustvaktException {
margarethab097fb02021-02-22 19:28:33 +0100521 List<QueryAccess> accessList = new ArrayList<>();
margaretha4edc70e2018-03-14 22:34:29 +0100522 if (adminDao.isAdmin(username)) {
margaretha3ccaeb72019-02-28 18:40:22 +0100523 accessList = accessDao.retrieveAllAccess();
margarethacfea1ae2018-01-15 20:27:26 +0100524 }
margarethafc7d7772018-01-16 17:48:17 +0100525 else {
margaretha35e1ca22023-11-16 22:00:01 +0100526 List<UserGroup> groups = userGroupService
527 .retrieveUserGroup(username);
margaretha652c4dc2021-02-12 17:07:44 +0100528 for (UserGroup g : groups) {
margarethab097fb02021-02-22 19:28:33 +0100529 if (isQueryAccessAdmin(g, username)) {
margaretha652c4dc2021-02-12 17:07:44 +0100530 accessList.addAll(
531 accessDao.retrieveActiveAccessByGroup(g.getId()));
532 }
margarethacfea1ae2018-01-15 20:27:26 +0100533 }
534 }
margarethab097fb02021-02-22 19:28:33 +0100535 return accessConverter.createQueryAccessDto(accessList);
margarethacfea1ae2018-01-15 20:27:26 +0100536 }
margaretha652c4dc2021-02-12 17:07:44 +0100537
margarethab097fb02021-02-22 19:28:33 +0100538 public List<QueryAccessDto> listQueryAccessByQuery (String username,
539 String queryCreator, String queryName) throws KustvaktException {
margaretha4af3f1e2019-01-16 17:53:26 +0100540
margarethab097fb02021-02-22 19:28:33 +0100541 List<QueryAccess> accessList;
margaretha4af3f1e2019-01-16 17:53:26 +0100542 if (adminDao.isAdmin(username)) {
margaretha35e1ca22023-11-16 22:00:01 +0100543 accessList = accessDao.retrieveAllAccessByQuery(queryCreator,
544 queryName);
margaretha4af3f1e2019-01-16 17:53:26 +0100545 }
546 else {
margaretha923c2072022-01-31 14:45:47 +0100547 accessList = accessDao.retrieveActiveAccessByQuery(queryCreator,
548 queryName);
margarethab097fb02021-02-22 19:28:33 +0100549 List<QueryAccess> filteredAccessList = new ArrayList<>();
550 for (QueryAccess access : accessList) {
margaretha4af3f1e2019-01-16 17:53:26 +0100551 UserGroup userGroup = access.getUserGroup();
margarethab097fb02021-02-22 19:28:33 +0100552 if (isQueryAccessAdmin(userGroup, username)) {
margaretha4af3f1e2019-01-16 17:53:26 +0100553 filteredAccessList.add(access);
554 }
555 }
556 accessList = filteredAccessList;
557 }
margarethab097fb02021-02-22 19:28:33 +0100558 return accessConverter.createQueryAccessDto(accessList);
margaretha4af3f1e2019-01-16 17:53:26 +0100559 }
560
margaretha3ccaeb72019-02-28 18:40:22 +0100561 @Deprecated
margarethab097fb02021-02-22 19:28:33 +0100562 public List<QueryAccessDto> listVCAccessByGroup (String username,
margarethacfea1ae2018-01-15 20:27:26 +0100563 int groupId) throws KustvaktException {
margarethacfea1ae2018-01-15 20:27:26 +0100564 UserGroup userGroup = userGroupService.retrieveUserGroupById(groupId);
margarethafc7d7772018-01-16 17:48:17 +0100565
margarethab097fb02021-02-22 19:28:33 +0100566 List<QueryAccess> accessList;
margaretha4edc70e2018-03-14 22:34:29 +0100567 if (adminDao.isAdmin(username)) {
margarethafc7d7772018-01-16 17:48:17 +0100568 accessList = accessDao.retrieveAllAccessByGroup(groupId);
569 }
margarethab097fb02021-02-22 19:28:33 +0100570 else if (isQueryAccessAdmin(userGroup, username)) {
margarethafc7d7772018-01-16 17:48:17 +0100571 accessList = accessDao.retrieveActiveAccessByGroup(groupId);
572 }
573 else {
margarethacfea1ae2018-01-15 20:27:26 +0100574 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
575 "Unauthorized operation for user: " + username, username);
576 }
margarethafc7d7772018-01-16 17:48:17 +0100577
margarethab097fb02021-02-22 19:28:33 +0100578 return accessConverter.createQueryAccessDto(accessList);
margaretha541b8cc2018-01-10 13:02:46 +0100579 }
margaretha652c4dc2021-02-12 17:07:44 +0100580
margarethab097fb02021-02-22 19:28:33 +0100581 public List<QueryAccessDto> listQueryAccessByGroup (String username,
margaretha3ccaeb72019-02-28 18:40:22 +0100582 String groupName) throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100583 UserGroup userGroup = userGroupService
584 .retrieveUserGroupByName(groupName);
margaretha3ccaeb72019-02-28 18:40:22 +0100585
margarethab097fb02021-02-22 19:28:33 +0100586 List<QueryAccess> accessList;
margaretha3ccaeb72019-02-28 18:40:22 +0100587 if (adminDao.isAdmin(username)) {
588 accessList = accessDao.retrieveAllAccessByGroup(userGroup.getId());
589 }
margarethab097fb02021-02-22 19:28:33 +0100590 else if (isQueryAccessAdmin(userGroup, username)) {
margaretha35e1ca22023-11-16 22:00:01 +0100591 accessList = accessDao
592 .retrieveActiveAccessByGroup(userGroup.getId());
margaretha3ccaeb72019-02-28 18:40:22 +0100593 }
594 else {
595 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
596 "Unauthorized operation for user: " + username, username);
597 }
margarethab097fb02021-02-22 19:28:33 +0100598 return accessConverter.createQueryAccessDto(accessList);
margaretha3ccaeb72019-02-28 18:40:22 +0100599 }
margarethae8ab51d2018-01-16 19:27:40 +0100600
margarethab097fb02021-02-22 19:28:33 +0100601 public void deleteQueryAccess (int accessId, String username)
margarethae8ab51d2018-01-16 19:27:40 +0100602 throws KustvaktException {
603
margarethab097fb02021-02-22 19:28:33 +0100604 QueryAccess access = accessDao.retrieveAccessById(accessId);
margarethae8ab51d2018-01-16 19:27:40 +0100605 UserGroup userGroup = access.getUserGroup();
margarethab097fb02021-02-22 19:28:33 +0100606 if (isQueryAccessAdmin(userGroup, username)
margaretha4edc70e2018-03-14 22:34:29 +0100607 || adminDao.isAdmin(username)) {
margarethab874ef52018-01-23 20:26:31 +0100608 accessDao.deleteAccess(access, username);
margarethae8ab51d2018-01-16 19:27:40 +0100609 }
610 else {
611 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
612 "Unauthorized operation for user: " + username, username);
613 }
614
615 }
margaretha923c2072022-01-31 14:45:47 +0100616
margaretha86c9be32021-12-08 19:09:18 +0100617 public JsonNode retrieveKoralQuery (String username, String queryName,
618 String createdBy, QueryType queryType) throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100619 QueryDO query = searchQueryByName(username, queryName, createdBy,
620 queryType);
margaretha86c9be32021-12-08 19:09:18 +0100621 String koralQuery = query.getKoralQuery();
margaretha923c2072022-01-31 14:45:47 +0100622 JsonNode kq = JsonUtils.readTree(koralQuery);
margaretha86c9be32021-12-08 19:09:18 +0100623 return kq;
624 }
margarethae8ab51d2018-01-16 19:27:40 +0100625
margaretha923c2072022-01-31 14:45:47 +0100626 public JsonNode retrieveFieldValues (String username, String queryName,
627 String createdBy, QueryType queryType, String fieldName)
628 throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100629
margaretha21a83862022-03-07 16:56:08 +0100630 ParameterChecker.checkStringValue(fieldName, "fieldName");
margaretha35e1ca22023-11-16 22:00:01 +0100631
632 // if (!adminDao.isAdmin(username)) {
633 // throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
634 // "Unauthorized operation for user: " + username, username);
635 // }
636
margaretha923c2072022-01-31 14:45:47 +0100637 if (fieldName.equals("tokens") || fieldName.equals("base")) {
638 throw new KustvaktException(StatusCodes.NOT_ALLOWED,
margaretha35e1ca22023-11-16 22:00:01 +0100639 "Retrieving values of field " + fieldName
640 + " is not allowed.");
margaretha923c2072022-01-31 14:45:47 +0100641 }
642 else {
643 QueryDO query = searchQueryByName(username, queryName, createdBy,
644 queryType);
645 String koralQuery = query.getKoralQuery();
646 return krill.getFieldValuesForVC(koralQuery, fieldName);
647 }
648 }
649
margarethab097fb02021-02-22 19:28:33 +0100650 public QueryDO searchQueryByName (String username, String queryName,
margaretha652c4dc2021-02-12 17:07:44 +0100651 String createdBy, QueryType queryType) throws KustvaktException {
margarethab097fb02021-02-22 19:28:33 +0100652 QueryDO query = queryDao.retrieveQueryByName(queryName, createdBy);
653 if (query == null) {
654 String code = createdBy + "/" + queryName;
margarethab5e1e0a2019-01-29 22:11:57 +0100655 throw new KustvaktException(StatusCodes.NO_RESOURCE_FOUND,
margaretha923c2072022-01-31 14:45:47 +0100656 queryType.displayName() + " " + code + " is not found.",
margarethab097fb02021-02-22 19:28:33 +0100657 String.valueOf(code));
margaretha4af3f1e2019-01-16 17:53:26 +0100658 }
margarethab097fb02021-02-22 19:28:33 +0100659 checkQueryAccess(query, username);
660 return query;
margaretha563aabe2018-09-13 20:39:45 +0200661 }
662
margarethab097fb02021-02-22 19:28:33 +0100663 public QueryDto retrieveQueryByName (String username, String queryName,
margaretha652c4dc2021-02-12 17:07:44 +0100664 String createdBy, QueryType queryType) throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100665 QueryDO query = searchQueryByName(username, queryName, createdBy,
666 queryType);
margaretha923c2072022-01-31 14:45:47 +0100667 // String json = query.getKoralQuery();
Akronda080152020-12-03 13:53:29 +0100668 String statistics = null;
margaretha923c2072022-01-31 14:45:47 +0100669 // long start,end;
670 // start = System.currentTimeMillis();
671 // if (query.getQueryType().equals(QueryType.VIRTUAL_CORPUS))
672 // {
673 // statistics = krill.getStatistics(json);
674 // }
675 // end = System.currentTimeMillis();
676 // jlog.debug("{} statistics duration: {}", queryName, (end -
677 // start));
margarethab097fb02021-02-22 19:28:33 +0100678 return converter.createQueryDto(query, statistics);
margaretha8c203962019-01-14 17:01:33 +0100679 }
680
margarethab097fb02021-02-22 19:28:33 +0100681 public QueryDto searchQueryById (String username, int queryId)
margarethae8ab51d2018-01-16 19:27:40 +0100682 throws KustvaktException {
margarethae8ab51d2018-01-16 19:27:40 +0100683
margarethab097fb02021-02-22 19:28:33 +0100684 QueryDO query = queryDao.retrieveQueryById(queryId);
685 checkQueryAccess(query, username);
margaretha923c2072022-01-31 14:45:47 +0100686 // String json = query.getKoralQuery();
687 // String statistics = krill.getStatistics(json);
margaretha326520b2021-12-08 17:58:09 +0100688 return converter.createQueryDto(query, null);
margaretha563aabe2018-09-13 20:39:45 +0200689 }
690
margarethab097fb02021-02-22 19:28:33 +0100691 private void checkQueryAccess (QueryDO query, String username)
margaretha563aabe2018-09-13 20:39:45 +0200692 throws KustvaktException {
margarethab097fb02021-02-22 19:28:33 +0100693 ResourceType type = query.getType();
margarethae8ab51d2018-01-16 19:27:40 +0100694
margaretha4edc70e2018-03-14 22:34:29 +0100695 if (!adminDao.isAdmin(username)
margarethab097fb02021-02-22 19:28:33 +0100696 && !username.equals(query.getCreatedBy())) {
Akronda080152020-12-03 13:53:29 +0100697 if (type.equals(ResourceType.PRIVATE)
698 || (type.equals(ResourceType.PROJECT)
margarethab097fb02021-02-22 19:28:33 +0100699 && !hasAccess(username, query.getId()))) {
margarethae8ab51d2018-01-16 19:27:40 +0100700 throw new KustvaktException(StatusCodes.AUTHORIZATION_FAILED,
701 "Unauthorized operation for user: " + username,
702 username);
703 }
margaretha2c249912018-01-17 20:07:20 +0100704
Akronda080152020-12-03 13:53:29 +0100705 else if (ResourceType.PUBLISHED.equals(type)
margaretha0e1fc552019-08-08 15:31:01 +0200706 && !username.equals("guest")) {
margarethab097fb02021-02-22 19:28:33 +0100707 // add user in the query's auto group
margaretha563aabe2018-09-13 20:39:45 +0200708 UserGroup userGroup = userGroupService
margarethab097fb02021-02-22 19:28:33 +0100709 .retrieveHiddenUserGroupByQuery(query.getId());
margaretha45667922018-01-25 21:23:03 +0100710 try {
margaretha18533fd2018-03-28 16:01:06 +0200711 userGroupService.addGroupMember(username, userGroup,
margarethab874ef52018-01-23 20:26:31 +0100712 "system", GroupMemberStatus.ACTIVE);
margaretha0e1fc552019-08-08 15:31:01 +0200713 // member roles are not set (not necessary)
margarethab874ef52018-01-23 20:26:31 +0100714 }
715 catch (KustvaktException e) {
716 // member exists
717 // skip adding user to hidden group
margarethae8ab51d2018-01-16 19:27:40 +0100718 }
719 }
margaretha44573832018-03-21 16:59:59 +0100720 // else VirtualCorpusType.SYSTEM
margarethae8ab51d2018-01-16 19:27:40 +0100721 }
margarethae8ab51d2018-01-16 19:27:40 +0100722 }
margaretha2c249912018-01-17 20:07:20 +0100723
margarethab097fb02021-02-22 19:28:33 +0100724 private boolean hasAccess (String username, int queryId)
margaretha2c249912018-01-17 20:07:20 +0100725 throws KustvaktException {
726 UserGroup userGroup;
margaretha35e1ca22023-11-16 22:00:01 +0100727 List<QueryAccess> accessList = accessDao
728 .retrieveActiveAccessByQuery(queryId);
margarethab097fb02021-02-22 19:28:33 +0100729 for (QueryAccess access : accessList) {
margaretha2c249912018-01-17 20:07:20 +0100730 userGroup = access.getUserGroup();
731 if (userGroupService.isMember(username, userGroup)) {
732 return true;
733 }
734 }
735 return false;
736 }
margarethaf093afb2017-11-12 21:38:23 +0100737}