TSK-478: custom field filter in classification REST service.
This commit is contained in:
parent
2970edd119
commit
c2b0ba5daf
|
@ -59,6 +59,21 @@ public class ClassificationControllerIntTest {
|
|||
assertNotNull(response.getBody().getLink(Link.REL_SELF));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllClassificationsFilterByCustomAttribute() {
|
||||
RestTemplate template = getRestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
|
||||
HttpEntity<String> request = new HttpEntity<String>(headers);
|
||||
ResponseEntity<PagedResources<ClassificationSummaryResource>> response = template.exchange(
|
||||
"http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&custom-1-like=RVNR", HttpMethod.GET,
|
||||
request,
|
||||
new ParameterizedTypeReference<PagedResources<ClassificationSummaryResource>>() {
|
||||
});
|
||||
assertNotNull(response.getBody().getLink(Link.REL_SELF));
|
||||
assertEquals(13, response.getBody().getContent().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllClassificationsKeepingFilters() {
|
||||
RestTemplate template = getRestTemplate();
|
||||
|
@ -66,7 +81,7 @@ public class ClassificationControllerIntTest {
|
|||
headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
|
||||
HttpEntity<String> request = new HttpEntity<String>(headers);
|
||||
ResponseEntity<PagedResources<ClassificationSummaryResource>> response = template.exchange(
|
||||
"http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sortBy=key&order=asc", HttpMethod.GET,
|
||||
"http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc", HttpMethod.GET,
|
||||
request,
|
||||
new ParameterizedTypeReference<PagedResources<ClassificationSummaryResource>>() {
|
||||
});
|
||||
|
@ -74,7 +89,7 @@ public class ClassificationControllerIntTest {
|
|||
assertTrue(response.getBody()
|
||||
.getLink(Link.REL_SELF)
|
||||
.getHref()
|
||||
.endsWith("/v1/classifications?domain=DOMAIN_A&sortBy=key&order=asc"));
|
||||
.endsWith("/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc"));
|
||||
assertEquals(17, response.getBody().getContent().size());
|
||||
assertEquals("A12", response.getBody().getContent().iterator().next().key);
|
||||
}
|
||||
|
@ -86,7 +101,7 @@ public class ClassificationControllerIntTest {
|
|||
headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x");
|
||||
HttpEntity<String> request = new HttpEntity<String>(headers);
|
||||
ResponseEntity<PagedResources<ClassificationSummaryResource>> response = template.exchange(
|
||||
"http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sortBy=key&order=asc&page=2&pagesize=5",
|
||||
"http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc&page=2&page-size=5",
|
||||
HttpMethod.GET,
|
||||
request,
|
||||
new ParameterizedTypeReference<PagedResources<ClassificationSummaryResource>>() {
|
||||
|
@ -97,7 +112,7 @@ public class ClassificationControllerIntTest {
|
|||
assertTrue(response.getBody()
|
||||
.getLink(Link.REL_SELF)
|
||||
.getHref()
|
||||
.endsWith("/v1/classifications?domain=DOMAIN_A&sortBy=key&order=asc&page=2&pagesize=5"));
|
||||
.endsWith("/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc&page=2&page-size=5"));
|
||||
assertNotNull(response.getBody().getLink("allClassifications"));
|
||||
assertTrue(response.getBody()
|
||||
.getLink("allClassifications")
|
||||
|
@ -112,7 +127,6 @@ public class ClassificationControllerIntTest {
|
|||
@Test
|
||||
public void testCreateClassification() throws IOException {
|
||||
String newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_A\",\"key\":\"NEW_CLASS\",\"name\":\"new classification\",\"type\":\"TASK\"}";
|
||||
// {"classificationId": "","key": "aaaaa","parentId": "","category": "EXTERN","type": "TASK","domain": ""
|
||||
URL url = new URL("http://127.0.0.1:" + port + "/v1/classifications");
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType
|
|||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
@ -20,7 +21,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import pro.taskana.BaseQuery;
|
||||
import pro.taskana.BaseQuery.SortDirection;
|
||||
import pro.taskana.Classification;
|
||||
import pro.taskana.ClassificationQuery;
|
||||
import pro.taskana.ClassificationService;
|
||||
|
@ -47,10 +48,25 @@ public class ClassificationController extends AbstractPagingController {
|
|||
|
||||
private static final String LIKE = "%";
|
||||
private static final String NAME = "name";
|
||||
private static final String NAME_LIKE = "name-like";
|
||||
private static final String KEY = "key";
|
||||
private static final String DOMAIN = "domain";
|
||||
private static final String CATEGORY = "category";
|
||||
private static final String DESC = "desc";
|
||||
private static final String TYPE = "type";
|
||||
private static final String CUSTOM_1_LIKE = "custom-1-like";
|
||||
private static final String CUSTOM_2_LIKE = "custom-2-like";
|
||||
private static final String CUSTOM_3_LIKE = "custom-3-like";
|
||||
private static final String CUSTOM_4_LIKE = "custom-4-like";
|
||||
private static final String CUSTOM_5_LIKE = "custom-5-like";
|
||||
private static final String CUSTOM_6_LIKE = "custom-6-like";
|
||||
private static final String CUSTOM_7_LIKE = "custom-7-like";
|
||||
private static final String CUSTOM_8_LIKE = "custom-8-like";
|
||||
|
||||
private static final String SORT_BY = "sort-by";
|
||||
private static final String SORT_DIRECTION = "order";
|
||||
|
||||
private static final String PAGING_PAGE = "page";
|
||||
private static final String PAGING_PAGE_SIZE = "page-size";
|
||||
|
||||
@Autowired
|
||||
private ClassificationService classificationService;
|
||||
|
@ -61,23 +77,19 @@ public class ClassificationController extends AbstractPagingController {
|
|||
@GetMapping
|
||||
@Transactional(readOnly = true, rollbackFor = Exception.class)
|
||||
public ResponseEntity<PagedResources<ClassificationSummaryResource>> getClassifications(
|
||||
@RequestParam(value = "sortBy", defaultValue = "key", required = false) String sortBy,
|
||||
@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
|
||||
@RequestParam(value = "name", required = false) String name,
|
||||
@RequestParam(value = "nameLike", required = false) String nameLike,
|
||||
@RequestParam(value = "key", required = false) String key,
|
||||
@RequestParam(value = "category", required = false) String category,
|
||||
@RequestParam(value = "domain", required = false) String domain,
|
||||
@RequestParam(value = "type", required = false) String type,
|
||||
@RequestParam(value = "page", required = false) String page,
|
||||
@RequestParam(value = "pagesize", required = false) String pageSize) throws InvalidArgumentException {
|
||||
@RequestParam MultiValueMap<String, String> params) throws InvalidArgumentException {
|
||||
|
||||
ClassificationQuery query = classificationService.createClassificationQuery();
|
||||
addSortingToQuery(query, sortBy, order);
|
||||
addAttributeFilter(query, name, nameLike, key, category, type, domain);
|
||||
query = applySortingParams(query, params);
|
||||
query = applyFilterParams(query, params);
|
||||
|
||||
PageMetadata pageMetadata = null;
|
||||
List<ClassificationSummary> classificationSummaries = null;
|
||||
String page = params.getFirst(PAGING_PAGE);
|
||||
String pageSize = params.getFirst(PAGING_PAGE_SIZE);
|
||||
params.remove(PAGING_PAGE);
|
||||
params.remove(PAGING_PAGE_SIZE);
|
||||
validateNoInvalidParameterIsLeft(params);
|
||||
if (page != null && pageSize != null) {
|
||||
// paging
|
||||
long totalElements = query.count();
|
||||
|
@ -148,57 +160,103 @@ public class ClassificationController extends AbstractPagingController {
|
|||
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
||||
}
|
||||
|
||||
private void addSortingToQuery(ClassificationQuery query, String sortBy, String order)
|
||||
private ClassificationQuery applySortingParams(ClassificationQuery query, MultiValueMap<String, String> params)
|
||||
throws IllegalArgumentException {
|
||||
BaseQuery.SortDirection sortDirection = getSortDirection(order);
|
||||
|
||||
switch (sortBy) {
|
||||
case CATEGORY:
|
||||
query.orderByCategory(sortDirection);
|
||||
break;
|
||||
case DOMAIN:
|
||||
query.orderByDomain(sortDirection);
|
||||
break;
|
||||
case KEY:
|
||||
query.orderByKey(sortDirection);
|
||||
break;
|
||||
case NAME:
|
||||
query.orderByName(sortDirection);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown order '" + sortBy + "'");
|
||||
// sorting
|
||||
String sortBy = params.getFirst(SORT_BY);
|
||||
if (sortBy != null) {
|
||||
SortDirection sortDirection;
|
||||
if (params.getFirst(SORT_DIRECTION) != null && "desc".equals(params.getFirst(SORT_DIRECTION))) {
|
||||
sortDirection = SortDirection.DESCENDING;
|
||||
} else {
|
||||
sortDirection = SortDirection.ASCENDING;
|
||||
}
|
||||
switch (sortBy) {
|
||||
case (CATEGORY):
|
||||
query = query.orderByCategory(sortDirection);
|
||||
break;
|
||||
case (DOMAIN):
|
||||
query = query.orderByDomain(sortDirection);
|
||||
break;
|
||||
case (KEY):
|
||||
query = query.orderByKey(sortDirection);
|
||||
break;
|
||||
case (NAME):
|
||||
query = query.orderByName(sortDirection);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown order '" + sortBy + "'");
|
||||
}
|
||||
}
|
||||
params.remove(SORT_BY);
|
||||
params.remove(SORT_DIRECTION);
|
||||
return query;
|
||||
}
|
||||
|
||||
private BaseQuery.SortDirection getSortDirection(String order) {
|
||||
if (order.equals(DESC)) {
|
||||
return BaseQuery.SortDirection.DESCENDING;
|
||||
private ClassificationQuery applyFilterParams(ClassificationQuery query,
|
||||
MultiValueMap<String, String> params) throws InvalidArgumentException {
|
||||
if (params.containsKey(NAME)) {
|
||||
String[] names = extractCommaSeparatedFields(params.get(NAME));
|
||||
query.nameIn(names);
|
||||
params.remove(NAME);
|
||||
}
|
||||
return BaseQuery.SortDirection.ASCENDING;
|
||||
}
|
||||
|
||||
private void addAttributeFilter(ClassificationQuery query,
|
||||
String name, String nameLike,
|
||||
String key, String category,
|
||||
String type, String domain) throws InvalidArgumentException {
|
||||
if (name != null) {
|
||||
query.nameIn(name);
|
||||
if (params.containsKey(NAME_LIKE)) {
|
||||
query.nameLike(LIKE + params.get(NAME_LIKE).get(0) + LIKE);
|
||||
params.remove(NAME_LIKE);
|
||||
}
|
||||
if (nameLike != null) {
|
||||
query.nameLike(LIKE + nameLike + LIKE);
|
||||
if (params.containsKey(KEY)) {
|
||||
String[] names = extractCommaSeparatedFields(params.get(KEY));
|
||||
query.keyIn(names);
|
||||
params.remove(KEY);
|
||||
}
|
||||
if (key != null) {
|
||||
query.keyIn(key);
|
||||
if (params.containsKey(CATEGORY)) {
|
||||
String[] names = extractCommaSeparatedFields(params.get(CATEGORY));
|
||||
query.categoryIn(names);
|
||||
params.remove(CATEGORY);
|
||||
}
|
||||
if (category != null) {
|
||||
query.categoryIn(category);
|
||||
if (params.containsKey(DOMAIN)) {
|
||||
String[] names = extractCommaSeparatedFields(params.get(DOMAIN));
|
||||
query.domainIn(names);
|
||||
params.remove(DOMAIN);
|
||||
}
|
||||
if (type != null) {
|
||||
query.typeIn(type);
|
||||
if (params.containsKey(TYPE)) {
|
||||
String[] names = extractCommaSeparatedFields(params.get(TYPE));
|
||||
query.typeIn(names);
|
||||
params.remove(TYPE);
|
||||
}
|
||||
if (domain != null) {
|
||||
query.domainIn(domain);
|
||||
if (params.containsKey(CUSTOM_1_LIKE)) {
|
||||
query.custom1Like(LIKE + params.get(CUSTOM_1_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_1_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_2_LIKE)) {
|
||||
query.custom2Like(LIKE + params.get(CUSTOM_2_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_2_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_3_LIKE)) {
|
||||
query.custom3Like(LIKE + params.get(CUSTOM_3_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_3_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_4_LIKE)) {
|
||||
query.custom4Like(LIKE + params.get(CUSTOM_4_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_4_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_5_LIKE)) {
|
||||
query.custom5Like(LIKE + params.get(CUSTOM_5_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_5_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_6_LIKE)) {
|
||||
query.custom6Like(LIKE + params.get(CUSTOM_6_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_6_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_7_LIKE)) {
|
||||
query.custom7Like(LIKE + params.get(CUSTOM_7_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_7_LIKE);
|
||||
}
|
||||
if (params.containsKey(CUSTOM_8_LIKE)) {
|
||||
query.custom8Like(LIKE + params.get(CUSTOM_8_LIKE).get(0) + LIKE);
|
||||
params.remove(CUSTOM_8_LIKE);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -63,9 +63,6 @@ public class ClassificationResourceAssembler {
|
|||
resource.add(
|
||||
linkTo(methodOn(ClassificationController.class).getClassification(classification.getId()))
|
||||
.withSelfRel());
|
||||
resource.add(
|
||||
linkTo(methodOn(ClassificationController.class).getClassifications(null, null, null, null, null, null, null,
|
||||
null, null, null)).withRel("getAllClassifications"));
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue