use of org.motechproject.mots.domain.JasperTemplateParameter in project mots by motech-implementations.
the class JasperTemplateServiceTest method mapRequestParametersToTemplateShouldReturnEmptyMapIfNoRequestParameters.
@Test
public void mapRequestParametersToTemplateShouldReturnEmptyMapIfNoRequestParameters() {
JasperTemplateParameter templateParameter = new JasperTemplateParameter();
templateParameter.setTemplate(template);
templateParameter.setName(PARAM1);
when(request.getParameterMap()).thenReturn(Collections.emptyMap());
when(template.getTemplateParameters()).thenReturn(Collections.singletonList(templateParameter));
Map<String, Object> resultMap = jasperTemplateService.mapRequestParametersToTemplate(request, template);
assertThat(resultMap.size(), is(0));
}
use of org.motechproject.mots.domain.JasperTemplateParameter in project mots by motech-implementations.
the class JasperTemplateServiceTest method mapRequestParametersToTemplateShouldReturnMatchingParameters.
@Test
public void mapRequestParametersToTemplateShouldReturnMatchingParameters() {
JasperTemplateParameter param1 = new JasperTemplateParameter();
param1.setTemplate(template);
param1.setName(PARAM1);
JasperTemplateParameter param2 = new JasperTemplateParameter();
param2.setTemplate(template);
param2.setName(PARAM2);
Map<String, String[]> requestParameterMap = new HashMap<>();
requestParameterMap.put(PARAM1, new String[] { "value1" });
requestParameterMap.put(PARAM3, new String[] { "value3" });
List<JasperTemplateParameter> templateParameterList = Arrays.asList(param1, param2);
when(request.getParameterMap()).thenReturn(requestParameterMap);
when(template.getTemplateParameters()).thenReturn(templateParameterList);
Map<String, Object> resultMap = jasperTemplateService.mapRequestParametersToTemplate(request, template);
assertThat(resultMap.size(), is(1));
assertTrue(resultMap.containsKey(PARAM1));
assertEquals("value1", resultMap.get(PARAM1));
}
use of org.motechproject.mots.domain.JasperTemplateParameter in project mots by motech-implementations.
the class JasperTemplateService method createParameter.
/**
* Create new report parameter of report which is not defined in Jasper system.
*/
private JasperTemplateParameter createParameter(JRParameter jrParameter) throws ReportingException {
String displayName = jrParameter.getPropertiesMap().getProperty("displayName");
if (isBlank(displayName)) {
throw new ReportingException(ERROR_REPORTING_PARAMETER_MISSING, "displayName");
}
String dataType = jrParameter.getValueClassName();
if (isNotBlank(dataType)) {
try {
Class.forName(dataType);
} catch (ClassNotFoundException err) {
throw new ReportingException(err, ERROR_REPORTING_PARAMETER_INCORRECT_TYPE, jrParameter.getName(), dataType);
}
}
// Set parameters.
JasperTemplateParameter jasperTemplateParameter = new JasperTemplateParameter();
jasperTemplateParameter.setName(jrParameter.getName());
jasperTemplateParameter.setDisplayName(displayName);
jasperTemplateParameter.setDescription(jrParameter.getDescription());
jasperTemplateParameter.setDataType(dataType);
jasperTemplateParameter.setSelectExpression(jrParameter.getPropertiesMap().getProperty("selectExpression"));
jasperTemplateParameter.setSelectProperty(jrParameter.getPropertiesMap().getProperty("selectProperty"));
jasperTemplateParameter.setDisplayProperty(jrParameter.getPropertiesMap().getProperty("displayProperty"));
String required = jrParameter.getPropertiesMap().getProperty("required");
if (required != null) {
jasperTemplateParameter.setRequired(Boolean.parseBoolean(jrParameter.getPropertiesMap().getProperty("required")));
}
if (jrParameter.getDefaultValueExpression() != null) {
jasperTemplateParameter.setDefaultValue(jrParameter.getDefaultValueExpression().getText().replace("\"", "").replace("\'", ""));
}
jasperTemplateParameter.setOptions(extractOptions(jrParameter));
return jasperTemplateParameter;
}
use of org.motechproject.mots.domain.JasperTemplateParameter in project mots by motech-implementations.
the class JasperTemplateService method mapRequestParametersToTemplate.
/**
* Map request parameters to the template parameters in the template. If there are no template
* parameters, returns an empty Map.
*
* @param request request with parameters
* @param template template with parameters
* @return Map of matching parameters, empty Map if none match
*/
public Map<String, Object> mapRequestParametersToTemplate(HttpServletRequest request, JasperTemplate template) {
List<JasperTemplateParameter> templateParameters = template.getTemplateParameters();
if (templateParameters == null) {
return new HashMap<>();
}
Map<String, String[]> requestParameterMap = request.getParameterMap();
Map<String, Object> map = new HashMap<>();
for (JasperTemplateParameter templateParameter : templateParameters) {
String templateParameterName = templateParameter.getName();
for (String requestParamName : requestParameterMap.keySet()) {
if (templateParameterName.equalsIgnoreCase(requestParamName)) {
String requestParamValue = "";
if (requestParameterMap.get(templateParameterName).length > 0) {
requestParamValue = requestParameterMap.get(templateParameterName)[0];
}
if (!(isBlank(requestParamValue) || "null".equals(requestParamValue) || "undefined".equals(requestParamValue))) {
map.put(templateParameterName, requestParamValue);
}
}
}
}
return map;
}
use of org.motechproject.mots.domain.JasperTemplateParameter in project mots by motech-implementations.
the class JasperTemplateService method processJrParameters.
private void processJrParameters(JasperTemplate jasperTemplate, JRParameter[] jrParameters) throws ReportingException {
ArrayList<JasperTemplateParameter> parameters = new ArrayList<>();
for (JRParameter jrParameter : jrParameters) {
if (!jrParameter.isSystemDefined() && jrParameter.isForPrompting()) {
JasperTemplateParameter jasperTemplateParameter = createParameter(jrParameter);
jasperTemplateParameter.setTemplate(jasperTemplate);
parameters.add(jasperTemplateParameter);
}
}
jasperTemplate.setTemplateParameters(parameters);
}
Aggregations