|
17 | 17 | package de.terrestris.shogun.lib.controller; |
18 | 18 |
|
19 | 19 | import com.github.fge.jsonpatch.mergepatch.JsonMergePatch; |
| 20 | +import com.jayway.jsonpath.Criteria; |
| 21 | +import com.jayway.jsonpath.Filter; |
| 22 | +import com.jayway.jsonpath.JsonPath; |
| 23 | +import com.jayway.jsonpath.internal.Path; |
| 24 | +import com.jayway.jsonpath.internal.filter.FilterCompiler; |
| 25 | +import com.jayway.jsonpath.internal.path.PathCompiler; |
20 | 26 | import de.terrestris.shogun.lib.controller.security.permission.BasePermissionController; |
21 | 27 | import de.terrestris.shogun.lib.model.BaseEntity; |
22 | | -import de.terrestris.shogun.lib.model.User; |
23 | 28 | import de.terrestris.shogun.lib.service.BaseService; |
24 | 29 | import io.swagger.v3.oas.annotations.Operation; |
25 | 30 | import io.swagger.v3.oas.annotations.Parameter; |
26 | 31 | import io.swagger.v3.oas.annotations.media.Content; |
27 | | -import io.swagger.v3.oas.annotations.media.Schema; |
28 | 32 | import io.swagger.v3.oas.annotations.responses.ApiResponse; |
29 | 33 | import io.swagger.v3.oas.annotations.responses.ApiResponses; |
30 | 34 | import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
31 | 35 | import lombok.extern.log4j.Log4j2; |
| 36 | +import org.apache.commons.lang3.StringUtils; |
32 | 37 | import org.springdoc.api.annotations.ParameterObject; |
33 | 38 | import org.springframework.beans.factory.annotation.Autowired; |
34 | 39 | import org.springframework.context.MessageSource; |
|
45 | 50 | import org.springframework.web.bind.annotation.*; |
46 | 51 | import org.springframework.web.server.ResponseStatusException; |
47 | 52 |
|
48 | | -import java.lang.reflect.ParameterizedType; |
49 | 53 | import java.time.OffsetDateTime; |
50 | | -import java.util.List; |
51 | 54 | import java.util.Optional; |
52 | 55 |
|
53 | 56 | // TODO Specify and type extension of BaseService |
@@ -78,26 +81,47 @@ public abstract class BaseController<T extends BaseService<?, S>, S extends Base |
78 | 81 | description = "Unauthorized: You need to provide a bearer token", |
79 | 82 | content = @Content |
80 | 83 | ), |
81 | | - @ApiResponse( |
82 | | - responseCode = "404", |
83 | | - description = "Not found: The provided ID does not exist (or you don't have the permission to delete it)" |
84 | | - ), |
| 84 | +// @ApiResponse( |
| 85 | +// responseCode = "404", |
| 86 | +// description = "Not found: The provided ID does not exist (or you don't have the permission to delete it)" |
| 87 | +// ), |
85 | 88 | @ApiResponse( |
86 | 89 | responseCode = "500", |
87 | | - description = "Internal Server Error: Something internal went wrong while deleting the entity" |
| 90 | + description = "Internal Server Error: Something internal went wrong while getting the entity list" |
88 | 91 | ) |
89 | 92 | }) |
90 | | - public Page<S> findAll(@PageableDefault(Integer.MAX_VALUE) @ParameterObject Pageable pageable) { |
| 93 | + // TODO Custom annotation to get filterobject directly, similiar to @ParameterObect |
| 94 | + public Page<S> findAll(@PageableDefault(Integer.MAX_VALUE) @ParameterObject Pageable pageable, @RequestParam(required = false) String filter) { |
91 | 95 | log.trace("Requested to return all entities of type {}", getGenericClassName()); |
92 | 96 |
|
| 97 | +// Path compiledPath = PathCompiler.compile("$.phoneNumbers[?(@.type=='home')]"); |
| 98 | +// |
| 99 | +// Filter parsedFilter = Filter.parse("$.phoneNumbers[?(@.type=='home')]"); |
| 100 | +// |
| 101 | +// Filter compiled = FilterCompiler.compile("$.phoneNumbers[?(@.type=='home')]"); |
| 102 | +// new FilterCompiler("$.phoneNumbers[?(@.type=='home')]"); |
| 103 | +// |
| 104 | +// Criteria crit = Criteria.parse("$.phoneNumbers[?(@.type=='home')]"); |
| 105 | + |
93 | 106 | try { |
94 | | - Page<S> persistedEntities = service.findAll(pageable); |
| 107 | + // https://github.com/json-path/JsonPath |
| 108 | + if (StringUtils.isNotEmpty(filter)) { |
| 109 | + // TODO Parse and validate filter |
| 110 | + //JsonPath peter = JsonPath.compile("$.phoneNumbers[?(@.type=='home')]"); |
| 111 | + JsonPath compiledFilter = JsonPath.compile(filter); |
| 112 | +// Filter compiledFilter = Filter.parse(filter); |
| 113 | + |
| 114 | + log.trace("Got filter " + compiledFilter.toString()); |
| 115 | + } |
| 116 | + |
| 117 | + Page<S> persistedEntities = service.findAll(pageable, filter); |
95 | 118 |
|
96 | 119 | log.trace("Successfully got all entities of type {} (count: {})", |
97 | 120 | getGenericClassName(), persistedEntities.getTotalElements()); |
98 | 121 |
|
99 | 122 | return persistedEntities; |
100 | 123 | } catch (AccessDeniedException ade) { |
| 124 | + // TODO I don't think we'll ever get into this exception here |
101 | 125 | log.warn("Access to entity of type {} is denied", getGenericClassName()); |
102 | 126 |
|
103 | 127 | throw new ResponseStatusException( |
|
0 commit comments