Search in sources :

Example 1 with JRParameter

use of net.sf.jasperreports.engine.JRParameter in project midpoint by Evolveum.

the class JasperReportDto method initFileds.

private void initFileds(boolean onlyForPromptingParams) {
    if (jasperReportXml == null) {
        return;
    }
    try {
        design = ReportTypeUtil.loadJasperDesign(jasperReportXml);
        query = design.getQuery().getText();
        fields = new ArrayList<JasperReportFieldDto>();
        for (JRField field : design.getFieldsList()) {
            fields.add(new JasperReportFieldDto(field.getName(), field.getValueClass(), field.getValueClassName()));
        }
        for (JasperReportFieldDto field : fields) {
            design.removeField(field.getName());
        }
        parameters = new ArrayList<JasperReportParameterDto>();
        for (JRParameter parameter : design.getParametersList()) {
            if (parameter.isSystemDefined()) {
                continue;
            }
            if (onlyForPromptingParams && !parameter.isForPrompting()) {
                continue;
            }
            JasperReportParameterDto p = new JasperReportParameterDto(parameter);
            parameters.add(p);
        }
        for (JasperReportParameterDto param : parameters) {
            design.removeParameter(param.getName());
        }
        detail = new String(Base64.decodeBase64(jasperReportXml));
    } catch (SchemaException e) {
        // TODO Auto-generated catch block
        throw new IllegalArgumentException(e);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) JRParameter(net.sf.jasperreports.engine.JRParameter) JRField(net.sf.jasperreports.engine.JRField)

Example 2 with JRParameter

use of net.sf.jasperreports.engine.JRParameter in project midpoint by Evolveum.

the class MidPointQueryExecutor method getParameters.

protected Map<QName, Object> getParameters() {
    JRParameter[] params = dataset.getParameters();
    Map<QName, Object> expressionParameters = new HashMap<QName, Object>();
    for (JRParameter param : params) {
        if (param.isSystemDefined()) {
            continue;
        }
        //LOGGER.trace(((JRBaseParameter)param).getName());
        Object v = getParameterValue(param.getName());
        try {
            expressionParameters.put(new QName(param.getName()), new PrismPropertyValue(v));
        } catch (Exception e) {
        //just skip properties that are not important for midpoint
        }
        LOGGER.trace("p.val: {}", v);
    }
    return expressionParameters;
}
Also used : JRParameter(net.sf.jasperreports.engine.JRParameter) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PrismObject(com.evolveum.midpoint.prism.PrismObject) JRException(net.sf.jasperreports.engine.JRException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SystemException(com.evolveum.midpoint.util.exception.SystemException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 3 with JRParameter

use of net.sf.jasperreports.engine.JRParameter in project mots by motech-implementations.

the class JasperTemplateServiceTest method shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull.

@Test
public void shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull() throws Exception {
    MultipartFile file = mock(MultipartFile.class);
    when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);
    mockStatic(JasperCompileManager.class);
    JasperReport report = mock(JasperReport.class);
    InputStream inputStream = mock(InputStream.class);
    when(file.getInputStream()).thenReturn(inputStream);
    JRParameter param1 = mock(JRParameter.class);
    JRParameter param2 = mock(JRParameter.class);
    JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
    JRExpression jrExpression = mock(JRExpression.class);
    String[] propertyNames = { DISPLAY_NAME };
    when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2 });
    when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
    when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
    when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);
    when(param1.getPropertiesMap()).thenReturn(propertiesMap);
    when(param1.getValueClassName()).thenReturn("java.lang.String");
    when(param1.isForPrompting()).thenReturn(true);
    when(param1.getDefaultValueExpression()).thenReturn(jrExpression);
    when(jrExpression.getText()).thenReturn("text");
    when(param2.getPropertiesMap()).thenReturn(propertiesMap);
    when(param2.getValueClassName()).thenReturn("java.lang.Integer");
    when(param2.isForPrompting()).thenReturn(true);
    when(param2.getDefaultValueExpression()).thenReturn(null);
    ByteArrayOutputStream byteOutputStream = mock(ByteArrayOutputStream.class);
    whenNew(ByteArrayOutputStream.class).withAnyArguments().thenReturn(byteOutputStream);
    ObjectOutputStream objectOutputStream = spy(new ObjectOutputStream(byteOutputStream));
    whenNew(ObjectOutputStream.class).withArguments(byteOutputStream).thenReturn(objectOutputStream);
    doNothing().when(objectOutputStream).writeObject(report);
    byte[] byteData = new byte[1];
    when(byteOutputStream.toByteArray()).thenReturn(byteData);
    JasperTemplate jasperTemplate = new JasperTemplate();
    jasperTemplateService.validateFileAndInsertTemplate(jasperTemplate, file);
    verify(jasperTemplateRepository).save(jasperTemplate);
    assertThat(jasperTemplate.getTemplateParameters().get(0).getDisplayName(), is(PARAM_DISPLAY_NAME));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) JRParameter(net.sf.jasperreports.engine.JRParameter) InputStream(java.io.InputStream) JRExpression(net.sf.jasperreports.engine.JRExpression) JasperReport(net.sf.jasperreports.engine.JasperReport) JRPropertiesMap(net.sf.jasperreports.engine.JRPropertiesMap) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with JRParameter

use of net.sf.jasperreports.engine.JRParameter in project mots by motech-implementations.

the class JasperTemplateService method validateFileAndSetData.

/**
 * Validate ".jrxml" report file with JasperCompileManager. If report is valid create additional
 * report parameters. Save additional report parameters as JasperTemplateParameter list. Save
 * report file as ".jasper" in byte array in Template class. If report is not valid throw
 * exception.
 */
private void validateFileAndSetData(JasperTemplate jasperTemplate, MultipartFile file) throws ReportingException {
    ReportingValidationHelper.throwIfFileIsNull(file);
    ReportingValidationHelper.throwIfIncorrectFileType(file, ALLOWED_FILETYPES);
    ReportingValidationHelper.throwIfFileIsEmpty(file);
    try {
        JasperReport report = JasperCompileManager.compileReport(file.getInputStream());
        String reportType = report.getProperty(REPORT_TYPE_PROPERTY);
        if (reportType != null) {
            jasperTemplate.setType(reportType);
        }
        String formats = report.getProperty(SUPPORTED_FORMATS_PROPERTY);
        if (formats != null) {
            jasperTemplate.setSupportedFormats(extractListProperties(formats));
        }
        JRParameter[] jrParameters = report.getParameters();
        if (jrParameters != null && jrParameters.length > 0) {
            processJrParameters(jasperTemplate, jrParameters);
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(report);
        jasperTemplate.setData(bos.toByteArray());
    } catch (JRException ex) {
        throw new ReportingException(ex, ERROR_REPORTING_FILE_INVALID);
    } catch (IOException ex) {
        throw new ReportingException(ex, ex.getMessage());
    }
}
Also used : JRParameter(net.sf.jasperreports.engine.JRParameter) JRException(net.sf.jasperreports.engine.JRException) ReportingException(org.motechproject.mots.exception.ReportingException) JasperReport(net.sf.jasperreports.engine.JasperReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 5 with JRParameter

use of net.sf.jasperreports.engine.JRParameter in project opennms by OpenNMS.

the class JasperReportService method buildJRparameters.

private Map<String, Object> buildJRparameters(final Map<String, Object> onmsReportParms, final JRParameter[] reportParms) throws ReportException {
    final Map<String, Object> jrReportParms = new HashMap<String, Object>();
    for (final JRParameter reportParm : reportParms) {
        if (apply(getParameterFilters(), reportParm)) {
            final String parmName = reportParm.getName();
            if (!onmsReportParms.containsKey(parmName)) {
                throw new ReportException("Required parameter " + parmName + " not supplied to JasperReports by OpenNMS");
            }
            if (reportParm.getValueClassName().equals("java.lang.String")) {
                jrReportParms.put(parmName, (String) onmsReportParms.get(parmName));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.lang.Integer")) {
                jrReportParms.put(parmName, (Integer) onmsReportParms.get(parmName));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.lang.Float")) {
                jrReportParms.put(parmName, (Float) onmsReportParms.get(parmName));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.lang.Double")) {
                jrReportParms.put(parmName, (Double) onmsReportParms.get(parmName));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.util.Date")) {
                jrReportParms.put(parmName, (Date) onmsReportParms.get(parmName));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.sql.Date")) {
                final Date date = (Date) onmsReportParms.get(parmName);
                jrReportParms.put(parmName, new java.sql.Date(date.getTime()));
                continue;
            }
            if (reportParm.getValueClassName().equals("java.sql.Timestamp")) {
                final Date date = (Date) onmsReportParms.get(parmName);
                jrReportParms.put(parmName, new java.sql.Timestamp(date.getTime()));
                continue;
            }
            throw new ReportException("Unsupported report parameter type " + reportParm.getValueClassName());
        }
    }
    return jrReportParms;
}
Also used : JRParameter(net.sf.jasperreports.engine.JRParameter) HashMap(java.util.HashMap) ReportException(org.opennms.api.reporting.ReportException) Date(java.util.Date)

Aggregations

JRParameter (net.sf.jasperreports.engine.JRParameter)11 HashMap (java.util.HashMap)4 JRException (net.sf.jasperreports.engine.JRException)4 JasperReport (net.sf.jasperreports.engine.JasperReport)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 JRPropertiesMap (net.sf.jasperreports.engine.JRPropertiesMap)3 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 JasperTemplate (org.motechproject.mots.domain.JasperTemplate)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)2 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)2 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)2 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)2