18
18
import java .util .Set ;
19
19
20
20
import javax .servlet .http .HttpServletRequest ;
21
- import javax .servlet .http .HttpServletResponse ;
22
21
23
22
import org .apache .commons .logging .Log ;
24
23
import org .apache .commons .logging .LogFactory ;
38
37
import org .openmrs .module .reporting .report .renderer .RenderingMode ;
39
38
import org .openmrs .module .reporting .report .renderer .ReportRenderer ;
40
39
import org .openmrs .module .reporting .report .service .ReportService ;
41
- import org .openmrs .module .reporting .web .validator .RunReportFormValidator ;
42
40
import org .openmrs .util .OpenmrsUtil ;
43
41
import org .quartz .CronExpression ;
44
- import org .springframework .beans .factory .annotation .Autowired ;
45
42
import org .springframework .stereotype .Controller ;
46
43
import org .springframework .ui .ModelMap ;
47
44
import org .springframework .util .StringUtils ;
48
45
import org .springframework .validation .*;
49
- import org .springframework .web .bind .ServletRequestDataBinder ;
50
46
import org .springframework .web .bind .WebDataBinder ;
51
47
import org .springframework .web .bind .annotation .InitBinder ;
52
48
import org .springframework .web .bind .annotation .ModelAttribute ;
53
49
import org .springframework .web .bind .annotation .RequestMapping ;
54
50
import org .springframework .web .bind .annotation .RequestMethod ;
55
- import org .springframework .web .servlet .ModelAndView ;
56
- import org .springframework .web .servlet .mvc .BaseCommandController ;
57
- import org .springframework .web .servlet .mvc .SimpleFormController ;
58
- import org .springframework .web .servlet .view .RedirectView ;
59
51
60
52
/**
61
53
* This controller runs a report (which must be passed in with the reportId parameter) after
73
65
* form's response.
74
66
*/
75
67
@ Controller
76
- public class RunReportFormController {
68
+ public class RunReportFormController implements Validator {
77
69
78
70
private transient Log log = LogFactory .getLog (this .getClass ());
79
71
80
- @ Autowired
81
- RunReportFormValidator validator ;
72
+ @ SuppressWarnings ("rawtypes" )
73
+ public boolean supports (Class c ) {
74
+ return c == CommandObject .class ;
75
+ }
76
+
77
+ @ Override
78
+ public void validate (Object commandObject , Errors errors ) {
79
+ CommandObject command = (CommandObject ) commandObject ;
80
+ ValidationUtils .rejectIfEmpty (errors , "reportDefinition" , "reporting.Report.run.error.missingReportID" );
81
+ if (command .getReportDefinition () != null ) {
82
+ ReportDefinition reportDefinition = command .getReportDefinition ();
83
+ Set <String > requiredParams = new HashSet <String >();
84
+ if (reportDefinition .getParameters () != null ) {
85
+ for (Parameter parameter : reportDefinition .getParameters ()) {
86
+ if (parameter .isRequired ()) {
87
+ requiredParams .add (parameter .getName ());
88
+ }
89
+ }
90
+ }
91
+
92
+ for (Map .Entry <String , Object > e : command .getUserEnteredParams ().entrySet ()) {
93
+ if (e .getValue () instanceof Iterable || e .getValue () instanceof Object []) {
94
+ Object iterable = e .getValue ();
95
+ if (e .getValue () instanceof Object []) {
96
+ iterable = Arrays .asList ((Object []) e .getValue ());
97
+ }
98
+
99
+ boolean hasNull = true ;
100
+
101
+ for (Object value : (Iterable <Object >) iterable ) {
102
+ hasNull = !ObjectUtil .notNull (value );
103
+ }
104
+
105
+ if (!hasNull ) {
106
+ requiredParams .remove (e .getKey ());
107
+ }
108
+ } else if (ObjectUtil .notNull (e .getValue ())) {
109
+ requiredParams .remove (e .getKey ());
110
+ }
111
+ }
112
+ if (requiredParams .size () > 0 ) {
113
+ for (Iterator <String > iterator = requiredParams .iterator (); iterator .hasNext ();) {
114
+ String parameterName = (String ) iterator .next ();
115
+ if (StringUtils .hasText (command .getExpressions ().get (parameterName ))) {
116
+ String expression = command .getExpressions ().get (parameterName );
117
+ if (!EvaluationUtil .isExpression (expression )){
118
+ errors .rejectValue ("expressions[" + parameterName + "]" ,
119
+ "reporting.Report.run.error.invalidParamExpression" );
120
+ }
121
+ } else {
122
+ errors .rejectValue ("userEnteredParams[" + parameterName + "]" , "error.required" ,
123
+ new Object [] { "This parameter" }, "{0} is required" );
124
+ }
125
+ }
126
+ }
127
+
128
+ if (reportDefinition .getDataSetDefinitions () == null || reportDefinition .getDataSetDefinitions ().size () == 0 ) {
129
+ errors .reject ("reporting.Report.run.error.definitionNotDeclared" );
130
+ }
131
+
132
+ if (ObjectUtil .notNull (command .getSchedule ())) {
133
+ if (!CronExpression .isValidExpression (command .getSchedule ())) {
134
+ errors .rejectValue ("schedule" , "reporting.Report.run.error.invalidCronExpression" );
135
+ }
136
+ }
137
+ }
138
+ ValidationUtils .rejectIfEmpty (errors , "selectedRenderer" , "reporting.Report.run.error.noRendererSelected" );
139
+ }
140
+
82
141
83
142
@ InitBinder
84
143
private void initBinder (WebDataBinder binder ) throws Exception {
@@ -103,7 +162,7 @@ protected String onSubmit(@ModelAttribute("report") CommandObject commandObject,
103
162
CommandObject command = (CommandObject ) commandObject ;
104
163
fillCommandObjectData (command , request );
105
164
106
- validator . validate (commandObject , errors );
165
+ validate (commandObject , errors );
107
166
108
167
if (errors .hasErrors ()) {
109
168
return "/module/reporting/run/runReportForm" ;
@@ -230,5 +289,4 @@ private void fillCommandObjectData(CommandObject command, HttpServletRequest req
230
289
command .setRenderingModes (reportService .getRenderingModes (command .getReportDefinition ()));
231
290
}
232
291
}
233
-
234
292
}
0 commit comments