27
27
*/
28
28
package org .hisp .dhis .webapi .controller .validation ;
29
29
30
- import static org .hisp .dhis .dxf2 .webmessage .WebMessageUtils .notFound ;
31
30
import static org .hisp .dhis .webapi .utils .ContextUtils .setNoStore ;
32
31
33
- import com .google .common .collect .Lists ;
34
32
import java .util .List ;
35
33
import javax .servlet .http .HttpServletResponse ;
34
+ import lombok .RequiredArgsConstructor ;
36
35
import org .hisp .dhis .common .DhisApiVersion ;
37
36
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 ;
39
39
import org .hisp .dhis .fieldfilter .FieldFilterParams ;
40
40
import org .hisp .dhis .fieldfilter .FieldFilterService ;
41
- import org .hisp .dhis .fieldfiltering .Preset ;
42
41
import org .hisp .dhis .node .NodeUtils ;
43
42
import org .hisp .dhis .node .types .RootNode ;
44
43
import org .hisp .dhis .schema .descriptors .ValidationResultSchemaDescriptor ;
45
44
import org .hisp .dhis .validation .ValidationResult ;
46
45
import org .hisp .dhis .validation .ValidationResultService ;
47
46
import org .hisp .dhis .validation .ValidationResultsDeletionRequest ;
48
47
import org .hisp .dhis .validation .comparator .ValidationResultQuery ;
48
+ import org .hisp .dhis .webapi .controller .AbstractFullReadOnlyController ;
49
49
import org .hisp .dhis .webapi .mvc .annotation .ApiVersion ;
50
- import org .hisp .dhis .webapi .service .ContextService ;
51
50
import org .springframework .http .HttpStatus ;
52
51
import org .springframework .security .access .prepost .PreAuthorize ;
53
52
import org .springframework .web .bind .annotation .DeleteMapping ;
62
61
* @author Stian Sandvold
63
62
*/
64
63
@ OpenApi .Tags ("data" )
64
+ @ OpenApi .EntityType (ValidationResult .class )
65
65
@ RestController
66
66
@ RequestMapping (value = ValidationResultSchemaDescriptor .API_ENDPOINT )
67
67
@ ApiVersion ({DhisApiVersion .ALL , DhisApiVersion .DEFAULT })
68
+ @ RequiredArgsConstructor
68
69
public class ValidationResultController {
69
- private final FieldFilterService fieldFilterService ;
70
70
71
+ private final FieldFilterService fieldFilterService ;
71
72
private final ValidationResultService validationResultService ;
72
73
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
-
84
74
@ GetMapping
75
+ @ OpenApi .Response (AbstractFullReadOnlyController .ObjectListResponse .class )
85
76
public @ ResponseBody RootNode getObjectList (
86
77
ValidationResultQuery query , HttpServletResponse response ) {
87
- List <String > fields = Lists . newArrayList ( contextService . getParameterValues ( "fields" ) );
78
+ List <String > fields = query . getFields ( );
88
79
89
- if (fields .isEmpty ()) {
90
- fields . addAll ( Preset . ALL . getFields () );
80
+ if (fields == null || fields .isEmpty ()) {
81
+ fields = List . of ( "*" );
91
82
}
92
83
93
84
List <ValidationResult > validationResults = validationResultService .getValidationResults (query );
94
85
95
86
RootNode rootNode = NodeUtils .createMetadata ();
96
87
97
88
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 ())));
100
92
}
101
93
102
94
rootNode .addChild (
@@ -108,18 +100,18 @@ public ValidationResultController(
108
100
}
109
101
110
102
@ GetMapping (value = "/{id}" )
111
- public @ ResponseBody ValidationResult getObject (@ PathVariable int id ) throws WebMessageException {
103
+ public @ ResponseBody ValidationResult getObject (@ PathVariable int id ) throws NotFoundException {
112
104
ValidationResult result = validationResultService .getById (id );
113
- checkFound ( id , result );
105
+ if ( result == null ) throw new NotFoundException ( ValidationResult . class , "" + id );
114
106
return result ;
115
107
}
116
108
117
109
@ PreAuthorize ("hasRole('F_PERFORM_MAINTENANCE')" )
118
110
@ DeleteMapping (value = "/{id}" )
119
111
@ ResponseStatus (value = HttpStatus .NO_CONTENT )
120
- public void delete (@ PathVariable int id ) throws WebMessageException {
112
+ public void delete (@ PathVariable int id ) throws NotFoundException {
121
113
ValidationResult result = validationResultService .getById (id );
122
- checkFound ( id , result );
114
+ if ( result == null ) throw new NotFoundException ( ValidationResult . class , "" + id );
123
115
validationResultService .deleteValidationResult (result );
124
116
}
125
117
@@ -129,10 +121,4 @@ public void delete(@PathVariable int id) throws WebMessageException {
129
121
public void deleteValidationResults (ValidationResultsDeletionRequest request ) {
130
122
validationResultService .deleteValidationResults (request );
131
123
}
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
- }
138
124
}
0 commit comments