diff --git a/src/main/java/de/thm/arsnova/controller/AbstractEntityController.java b/src/main/java/de/thm/arsnova/controller/AbstractEntityController.java index bf692e5be5e8a086aa31fd51f9910dc4b9f25893..fb8d4e042858fbe253c968925e335aef0e4d19b8 100644 --- a/src/main/java/de/thm/arsnova/controller/AbstractEntityController.java +++ b/src/main/java/de/thm/arsnova/controller/AbstractEntityController.java @@ -18,7 +18,12 @@ package de.thm.arsnova.controller; import de.thm.arsnova.entities.Entity; +import de.thm.arsnova.entities.FindQuery; import de.thm.arsnova.services.EntityService; +import de.thm.arsnova.services.FindQueryService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; @@ -27,8 +32,10 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import javax.naming.OperationNotSupportedException; import java.io.IOException; import java.util.Map; +import java.util.Set; /** * Base type for Entity controllers which provides basic CRUD operations and supports Entity patching. @@ -37,14 +44,18 @@ import java.util.Map; * @author Daniel Gerhardt */ public abstract class AbstractEntityController<E extends Entity> { + private static final Logger logger = LoggerFactory.getLogger(AbstractEntityController.class); protected static final String DEFAULT_ROOT_MAPPING = "/"; protected static final String DEFAULT_ID_MAPPING = "/{id}"; + protected static final String DEFAULT_FIND_MAPPING = "/find"; protected static final String GET_MAPPING = DEFAULT_ID_MAPPING; protected static final String PUT_MAPPING = DEFAULT_ID_MAPPING; protected static final String POST_MAPPING = DEFAULT_ROOT_MAPPING; protected static final String PATCH_MAPPING = DEFAULT_ID_MAPPING; protected static final String DELETE_MAPPING = DEFAULT_ID_MAPPING; + protected static final String FIND_MAPPING = DEFAULT_FIND_MAPPING; protected final EntityService<E> entityService; + protected FindQueryService<E> findQueryService; protected AbstractEntityController(final EntityService<E> entityService) { this.entityService = entityService; @@ -78,4 +89,22 @@ public abstract class AbstractEntityController<E extends Entity> { E entity = entityService.get(id); entityService.delete(entity); } + + @PostMapping(FIND_MAPPING) + public Iterable<E> find(@RequestBody final FindQuery<E> findQuery) throws OperationNotSupportedException { + if (findQueryService != null) { + logger.debug("Resolving find query: {}", findQuery); + Set<String> ids = findQueryService.resolveQuery(findQuery); + logger.debug("Resolved find query to IDs: {}", ids); + + return entityService.get(ids); + } else { + throw new OperationNotSupportedException("Find is not supported for this entity type."); + } + } + + @Autowired(required = false) + public void setFindQueryService(final FindQueryService<E> findQueryService) { + this.findQueryService = findQueryService; + } } diff --git a/src/main/java/de/thm/arsnova/entities/FindQuery.java b/src/main/java/de/thm/arsnova/entities/FindQuery.java new file mode 100644 index 0000000000000000000000000000000000000000..73f0cd0d3d810b101934435d6cdcf093974eaaeb --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/FindQuery.java @@ -0,0 +1,55 @@ +/* + * This file is part of ARSnova Backend. + * Copyright (C) 2012-2018 The ARSnova Team and Contributors + * + * ARSnova Backend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova Backend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.entities; + +import com.fasterxml.jackson.annotation.JsonView; +import de.thm.arsnova.entities.serialization.View; +import org.springframework.core.style.ToStringCreator; + +import java.util.Map; + +public class FindQuery<E extends Entity> { + private E properties; + private Map<String, Object> externalFilters; + + public E getProperties() { + return properties; + } + + @JsonView(View.Public.class) + public void setProperties(final E properties) { + this.properties = properties; + } + + public Map<String, Object> getExternalFilters() { + return externalFilters; + } + + @JsonView(View.Public.class) + public void setExternalFilters(final Map<String, Object> externalFilters) { + this.externalFilters = externalFilters; + } + + @Override + public String toString() { + return new ToStringCreator(getClass()) + .append("properties", properties) + .append("externalFilters", externalFilters) + .toString(); + } +} diff --git a/src/main/java/de/thm/arsnova/services/DefaultEntityServiceImpl.java b/src/main/java/de/thm/arsnova/services/DefaultEntityServiceImpl.java index 69222b3f039c40ef67b6b977d381fd9b6370e727..0027ffa0a234875493bdf6ab25c7abc345a6d42d 100644 --- a/src/main/java/de/thm/arsnova/services/DefaultEntityServiceImpl.java +++ b/src/main/java/de/thm/arsnova/services/DefaultEntityServiceImpl.java @@ -56,6 +56,12 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService return repository.findOne(id); } + @Override + @PreFilter(value = "hasPermission(filterObject, #this.this.getTypeName(), 'read')", filterTarget = "ids") + public Iterable<T> get(final Collection<String> ids) { + return repository.findAll(ids); + } + @Override @PreAuthorize("hasPermission(#entity, 'create')") public T create(final T entity) { diff --git a/src/main/java/de/thm/arsnova/services/EntityService.java b/src/main/java/de/thm/arsnova/services/EntityService.java index 6e8e61fa638c635ed2885f10dbe87f9372b7c784..16681f0e5eeb67a66739ae0a403f989a8ff41347 100644 --- a/src/main/java/de/thm/arsnova/services/EntityService.java +++ b/src/main/java/de/thm/arsnova/services/EntityService.java @@ -36,6 +36,9 @@ public interface EntityService<T extends Entity> { @PreAuthorize("hasPermission(#id, #this.this.getTypeName(), 'read')") T get(String id); + @PreFilter(value = "hasPermission(filterObject, #this.this.getTypeName(), 'read')", filterTarget = "ids") + Iterable<T> get(Collection<String> ids); + @PreAuthorize("hasPermission(#entity, 'create')") T create(T entity); diff --git a/src/main/java/de/thm/arsnova/services/FindQueryService.java b/src/main/java/de/thm/arsnova/services/FindQueryService.java new file mode 100644 index 0000000000000000000000000000000000000000..2cf1399c9756fd326456ba01e179c02d852e9a4b --- /dev/null +++ b/src/main/java/de/thm/arsnova/services/FindQueryService.java @@ -0,0 +1,32 @@ +/* + * This file is part of ARSnova Backend. + * Copyright (C) 2012-2018 The ARSnova Team and Contributors + * + * ARSnova Backend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova Backend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.services; + +import de.thm.arsnova.entities.Entity; +import de.thm.arsnova.entities.FindQuery; + +import java.util.Set; + +/** + * + * @param <E> Entity type + * @author Daniel Gerhardt + */ +public interface FindQueryService<E extends Entity> { + Set<String> resolveQuery(FindQuery<E> findQuery); +}