Skip to content

Commit 4a5f591

Browse files
authored
fix: validation results paging [DHIS2-19173] (2.41) (#20232)
* fix: validation results paging [DHIS2-19173] (#20229) * fix: undo OpenApi.Description * fix: imported class name * fix: import
1 parent adec9ba commit 4a5f591

File tree

4 files changed

+39
-134
lines changed

4 files changed

+39
-134
lines changed

dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/comparator/ValidationResultQuery.java

+18-97
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,25 @@
2727
*/
2828
package org.hisp.dhis.validation.comparator;
2929

30-
import com.google.common.base.MoreObjects;
3130
import java.util.Date;
3231
import java.util.List;
33-
import org.apache.commons.lang3.BooleanUtils;
32+
import lombok.Getter;
33+
import lombok.Setter;
34+
import lombok.ToString;
3435
import org.hisp.dhis.common.OpenApi;
3536
import org.hisp.dhis.common.Pager;
3637
import org.hisp.dhis.common.PagerUtils;
38+
import org.hisp.dhis.common.UID;
39+
import org.hisp.dhis.organisationunit.OrganisationUnit;
40+
import org.hisp.dhis.validation.ValidationRule;
3741

3842
/**
3943
* @author Stian Sandvold
4044
*/
45+
@Setter
46+
@Getter
47+
@ToString
4148
public class ValidationResultQuery {
42-
public static final ValidationResultQuery EMPTY = new ValidationResultQuery();
4349

4450
private Boolean skipPaging;
4551

@@ -49,117 +55,32 @@ public class ValidationResultQuery {
4955

5056
private int pageSize = Pager.DEFAULT_PAGE_SIZE;
5157

52-
private long total;
53-
5458
/**
55-
* Optional list of validation rule uids to filter. If empty the list is not restricting the
56-
* query.
59+
* Optional list of validation rule UIDs to filter. If empty the list is not restricting the query
5760
*/
61+
@OpenApi.Property({UID.class, ValidationRule.class})
5862
private List<String> vr;
5963

6064
/**
61-
* Optional list of organisation unit uids to filter. If empty the list is not restricting the
62-
* query.
65+
* Optional list of organisation unit UIDs to filter. If empty the list is not restricting the
66+
* query
6367
*/
68+
@OpenApi.Property({UID.class, OrganisationUnit.class})
6469
private List<String> ou;
6570

6671
/**
67-
* Optional list of ISO-Date expressions to filter. If empty the list is not restricting the
68-
* query.
72+
* Optional list of ISO-Date expressions to filter. If empty the list is not restricting the query
6973
*/
7074
private List<String> pe;
7175

72-
/** Optional filter to select only results that have been created on or after the given date. */
76+
/** Optional filter to select only results that have been created on or after the given date */
7377
private Date createdDate;
7478

79+
private List<String> fields;
80+
7581
public ValidationResultQuery() {}
7682

7783
public boolean isSkipPaging() {
7884
return PagerUtils.isSkipPaging(skipPaging, paging);
7985
}
80-
81-
public void setSkipPaging(Boolean skipPaging) {
82-
this.skipPaging = skipPaging;
83-
}
84-
85-
public boolean isPaging() {
86-
return BooleanUtils.toBoolean(paging);
87-
}
88-
89-
public void setPaging(Boolean paging) {
90-
this.paging = paging;
91-
}
92-
93-
public int getPage() {
94-
return page;
95-
}
96-
97-
public void setPage(int page) {
98-
this.page = page;
99-
}
100-
101-
public int getPageSize() {
102-
return pageSize;
103-
}
104-
105-
public void setPageSize(int pageSize) {
106-
this.pageSize = pageSize;
107-
}
108-
109-
public long getTotal() {
110-
return total;
111-
}
112-
113-
public void setTotal(long total) {
114-
this.total = total;
115-
}
116-
117-
@OpenApi.Ignore
118-
public Pager getPager() {
119-
return PagerUtils.isSkipPaging(skipPaging, paging) ? null : new Pager(page, total, pageSize);
120-
}
121-
122-
public List<String> getVr() {
123-
return vr;
124-
}
125-
126-
public void setVr(List<String> vr) {
127-
this.vr = vr;
128-
}
129-
130-
public List<String> getOu() {
131-
return ou;
132-
}
133-
134-
public void setOu(List<String> ou) {
135-
this.ou = ou;
136-
}
137-
138-
public List<String> getPe() {
139-
return pe;
140-
}
141-
142-
public void setPe(List<String> pe) {
143-
this.pe = pe;
144-
}
145-
146-
public Date getCreatedDate() {
147-
return createdDate;
148-
}
149-
150-
public void setCreatedDate(Date createdDate) {
151-
this.createdDate = createdDate;
152-
}
153-
154-
@Override
155-
public String toString() {
156-
return MoreObjects.toStringHelper(this)
157-
.add("page", page)
158-
.add("pageSize", pageSize)
159-
.add("total", total)
160-
.add("ou", ou)
161-
.add("vr", vr)
162-
.add("pe", pe)
163-
.toString();
164-
}
16586
}

dhis-2/dhis-services/dhis-service-validation/src/main/java/org/hisp/dhis/validation/hibernate/HibernateValidationResultStore.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.hisp.dhis.category.CategoryOptionGroupSet;
4646
import org.hisp.dhis.common.IdentifiableObject;
4747
import org.hisp.dhis.common.IdentifiableObjectUtils;
48-
import org.hisp.dhis.common.Pager;
4948
import org.hisp.dhis.commons.util.SqlHelper;
5049
import org.hisp.dhis.hibernate.HibernateGenericStore;
5150
import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -169,9 +168,8 @@ public List<ValidationResult> query(ValidationResultQuery query) {
169168
addQueryParameters(query, hibernateQuery);
170169

171170
if (!query.isSkipPaging()) {
172-
Pager pager = query.getPager();
173-
hibernateQuery.setFirstResult(pager.getOffset());
174-
hibernateQuery.setMaxResults(pager.getPageSize());
171+
hibernateQuery.setFirstResult((query.getPage() - 1) * query.getPageSize());
172+
hibernateQuery.setMaxResults(query.getPageSize());
175173
}
176174

177175
return hibernateQuery.getResultList();

dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractFullReadOnlyController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected void forceFiltering(final WebOptions webOptions, final List<String> fi
170170

171171
@Value
172172
@OpenApi.Shared(value = false)
173-
protected static class ObjectListResponse {
173+
public static class ObjectListResponse {
174174
@OpenApi.Property Pager pager;
175175

176176
@OpenApi.Property(name = "path$", value = OpenApi.EntityType[].class)

dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/validation/ValidationResultController.java

+18-32
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,26 @@
2727
*/
2828
package org.hisp.dhis.webapi.controller.validation;
2929

30-
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound;
3130
import static org.hisp.dhis.webapi.utils.ContextUtils.setNoStore;
3231

33-
import com.google.common.collect.Lists;
3432
import java.util.List;
3533
import javax.servlet.http.HttpServletResponse;
34+
import lombok.RequiredArgsConstructor;
3635
import org.hisp.dhis.common.DhisApiVersion;
3736
import org.hisp.dhis.common.OpenApi;
38-
import org.hisp.dhis.dxf2.webmessage.WebMessageException;
37+
import org.hisp.dhis.common.Pager;
38+
import org.hisp.dhis.feedback.NotFoundException;
3939
import org.hisp.dhis.fieldfilter.FieldFilterParams;
4040
import org.hisp.dhis.fieldfilter.FieldFilterService;
41-
import org.hisp.dhis.fieldfiltering.Preset;
4241
import org.hisp.dhis.node.NodeUtils;
4342
import org.hisp.dhis.node.types.RootNode;
4443
import org.hisp.dhis.schema.descriptors.ValidationResultSchemaDescriptor;
4544
import org.hisp.dhis.validation.ValidationResult;
4645
import org.hisp.dhis.validation.ValidationResultService;
4746
import org.hisp.dhis.validation.ValidationResultsDeletionRequest;
4847
import org.hisp.dhis.validation.comparator.ValidationResultQuery;
48+
import org.hisp.dhis.webapi.controller.AbstractFullReadOnlyController;
4949
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
50-
import org.hisp.dhis.webapi.service.ContextService;
5150
import org.springframework.http.HttpStatus;
5251
import org.springframework.security.access.prepost.PreAuthorize;
5352
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -62,41 +61,34 @@
6261
* @author Stian Sandvold
6362
*/
6463
@OpenApi.Tags("data")
64+
@OpenApi.EntityType(ValidationResult.class)
6565
@RestController
6666
@RequestMapping(value = ValidationResultSchemaDescriptor.API_ENDPOINT)
6767
@ApiVersion({DhisApiVersion.ALL, DhisApiVersion.DEFAULT})
68+
@RequiredArgsConstructor
6869
public class ValidationResultController {
69-
private final FieldFilterService fieldFilterService;
7070

71+
private final FieldFilterService fieldFilterService;
7172
private final ValidationResultService validationResultService;
7273

73-
private final ContextService contextService;
74-
75-
public ValidationResultController(
76-
FieldFilterService fieldFilterService,
77-
ValidationResultService validationResultService,
78-
ContextService contextService) {
79-
this.fieldFilterService = fieldFilterService;
80-
this.validationResultService = validationResultService;
81-
this.contextService = contextService;
82-
}
83-
8474
@GetMapping
75+
@OpenApi.Response(AbstractFullReadOnlyController.ObjectListResponse.class)
8576
public @ResponseBody RootNode getObjectList(
8677
ValidationResultQuery query, HttpServletResponse response) {
87-
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
78+
List<String> fields = query.getFields();
8879

89-
if (fields.isEmpty()) {
90-
fields.addAll(Preset.ALL.getFields());
80+
if (fields == null || fields.isEmpty()) {
81+
fields = List.of("*");
9182
}
9283

9384
List<ValidationResult> validationResults = validationResultService.getValidationResults(query);
9485

9586
RootNode rootNode = NodeUtils.createMetadata();
9687

9788
if (!query.isSkipPaging()) {
98-
query.setTotal(validationResultService.countValidationResults(query));
99-
rootNode.addChild(NodeUtils.createPager(query.getPager()));
89+
long total = validationResultService.countValidationResults(query);
90+
rootNode.addChild(
91+
NodeUtils.createPager(new Pager(query.getPage(), total, query.getPageSize())));
10092
}
10193

10294
rootNode.addChild(
@@ -108,18 +100,18 @@ public ValidationResultController(
108100
}
109101

110102
@GetMapping(value = "/{id}")
111-
public @ResponseBody ValidationResult getObject(@PathVariable int id) throws WebMessageException {
103+
public @ResponseBody ValidationResult getObject(@PathVariable int id) throws NotFoundException {
112104
ValidationResult result = validationResultService.getById(id);
113-
checkFound(id, result);
105+
if (result == null) throw new NotFoundException(ValidationResult.class, "" + id);
114106
return result;
115107
}
116108

117109
@PreAuthorize("hasRole('F_PERFORM_MAINTENANCE')")
118110
@DeleteMapping(value = "/{id}")
119111
@ResponseStatus(value = HttpStatus.NO_CONTENT)
120-
public void delete(@PathVariable int id) throws WebMessageException {
112+
public void delete(@PathVariable int id) throws NotFoundException {
121113
ValidationResult result = validationResultService.getById(id);
122-
checkFound(id, result);
114+
if (result == null) throw new NotFoundException(ValidationResult.class, "" + id);
123115
validationResultService.deleteValidationResult(result);
124116
}
125117

@@ -129,10 +121,4 @@ public void delete(@PathVariable int id) throws WebMessageException {
129121
public void deleteValidationResults(ValidationResultsDeletionRequest request) {
130122
validationResultService.deleteValidationResults(request);
131123
}
132-
133-
private void checkFound(int id, ValidationResult result) throws WebMessageException {
134-
if (result == null) {
135-
throw new WebMessageException(notFound("Validation result with id " + id + " was not found"));
136-
}
137-
}
138124
}

0 commit comments

Comments
 (0)