1818import java .util .Set ;
1919
2020import javax .servlet .http .HttpServletRequest ;
21- import javax .servlet .http .HttpServletResponse ;
2221
2322import org .apache .commons .logging .Log ;
2423import org .apache .commons .logging .LogFactory ;
3837import org .openmrs .module .reporting .report .renderer .RenderingMode ;
3938import org .openmrs .module .reporting .report .renderer .ReportRenderer ;
4039import org .openmrs .module .reporting .report .service .ReportService ;
41- import org .openmrs .module .reporting .web .validator .RunReportFormValidator ;
4240import org .openmrs .util .OpenmrsUtil ;
4341import org .quartz .CronExpression ;
44- import org .springframework .beans .factory .annotation .Autowired ;
4542import org .springframework .stereotype .Controller ;
4643import org .springframework .ui .ModelMap ;
4744import org .springframework .util .StringUtils ;
4845import org .springframework .validation .*;
49- import org .springframework .web .bind .ServletRequestDataBinder ;
5046import org .springframework .web .bind .WebDataBinder ;
5147import org .springframework .web .bind .annotation .InitBinder ;
5248import org .springframework .web .bind .annotation .ModelAttribute ;
5349import org .springframework .web .bind .annotation .RequestMapping ;
5450import 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 ;
5951
6052/**
6153 * This controller runs a report (which must be passed in with the reportId parameter) after
7365 * form's response.
7466 */
7567@ Controller
76- public class RunReportFormController {
68+ public class RunReportFormController implements Validator {
7769
7870 private transient Log log = LogFactory .getLog (this .getClass ());
7971
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+
82141
83142 @ InitBinder
84143 private void initBinder (WebDataBinder binder ) throws Exception {
@@ -103,7 +162,7 @@ protected String onSubmit(@ModelAttribute("report") CommandObject commandObject,
103162 CommandObject command = (CommandObject ) commandObject ;
104163 fillCommandObjectData (command , request );
105164
106- validator . validate (commandObject , errors );
165+ validate (commandObject , errors );
107166
108167 if (errors .hasErrors ()) {
109168 return "/module/reporting/run/runReportForm" ;
@@ -230,5 +289,4 @@ private void fillCommandObjectData(CommandObject command, HttpServletRequest req
230289 command .setRenderingModes (reportService .getRenderingModes (command .getReportDefinition ()));
231290 }
232291 }
233-
234292}
0 commit comments