Search in sources :

Example 1 with ParameterValue

use of io.jmix.emailtemplates.entity.ParameterValue in project jmix by jmix-framework.

the class EmailTemplateSendScreen method setParameter.

public void setParameter(String alias, Object value) {
    List<TemplateReport> templateReports = new ArrayList<>();
    if (emailTemplate.getEmailBodyReport() != null) {
        templateReports.add(emailTemplate.getEmailBodyReport());
    }
    templateReports.addAll(emailTemplate.getAttachedTemplateReports());
    for (TemplateReport templateReport : templateReports) {
        ReportInputParameter inputParameter = templateReport.getReport().getInputParameters().stream().filter(e -> alias.equals(e.getAlias())).findFirst().orElse(null);
        if (inputParameter != null) {
            ParameterValue parameterValue = templateReport.getParameterValues().stream().filter(pv -> pv.getAlias().equals(alias)).findFirst().orElse(null);
            if (parameterValue == null) {
                parameterValue = metadata.create(ParameterValue.class);
                parameterValue.setAlias(alias);
                parameterValue.setParameterType(inputParameter.getType());
                parameterValue.setTemplateReport(templateReport);
                templateReport.getParameterValues().add(parameterValue);
            }
            Class parameterClass = classResolver.resolveClass(inputParameter);
            if (!ParameterType.ENTITY_LIST.equals(inputParameter.getType())) {
                String stringValue = objectToStringConverter.convertToString(parameterClass, value);
                parameterValue.setDefaultValue(stringValue);
            }
        }
    }
}
Also used : ParameterValue(io.jmix.emailtemplates.entity.ParameterValue) ArrayList(java.util.ArrayList) ReportInputParameter(io.jmix.reports.entity.ReportInputParameter) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport)

Example 2 with ParameterValue

use of io.jmix.emailtemplates.entity.ParameterValue in project jmix by jmix-framework.

the class TemplateParametersExtractor method getTemplateDefaultValues.

public List<ReportWithParams> getTemplateDefaultValues(EmailTemplate emailTemplate) throws ReportParameterTypeChangedException {
    List<TemplateReport> templateReports = createParamsCollectionByTemplate(emailTemplate);
    List<ReportWithParams> reportWithParams = new ArrayList<>();
    List<String> exceptionMessages = new ArrayList<>();
    for (TemplateReport templateReport : templateReports) {
        try {
            List<ParameterValue> parameterValues = null;
            if (templateReport != null) {
                parameterValues = templateReport.getParameterValues();
                reportWithParams.add(getReportDefaultValues(templateReport.getReport(), parameterValues));
            }
        } catch (ReportParameterTypeChangedException e) {
            exceptionMessages.add(e.getMessage());
        }
    }
    if (!exceptionMessages.isEmpty()) {
        StringBuilder messages = new StringBuilder();
        exceptionMessages.forEach(m -> {
            messages.append(m);
            messages.append("\n");
        });
        throw new ReportParameterTypeChangedException(messages.toString());
    }
    return reportWithParams;
}
Also used : ParameterValue(io.jmix.emailtemplates.entity.ParameterValue) ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport) ReportParameterTypeChangedException(io.jmix.emailtemplates.exception.ReportParameterTypeChangedException)

Example 3 with ParameterValue

use of io.jmix.emailtemplates.entity.ParameterValue in project jmix by jmix-framework.

the class EmailTemplateParametersFragment method updateDefaultValue.

private void updateDefaultValue(ReportInputParameter parameter, Object fieldValue) {
    String alias = parameter.getAlias();
    Report report = parameter.getReport();
    TemplateReport templateReport = templateReports.stream().filter(e -> e.getReport().equals(report)).findFirst().orElse(null);
    if (fieldValue == null && templateReport == null) {
        return;
    }
    ParameterValue parameterValue = templateReport.getParameterValues().stream().filter(pv -> pv.getAlias().equals(alias)).findFirst().orElse(null);
    if (parameterValue == null) {
        parameterValue = metadata.create(ParameterValue.class);
        parameterValue.setAlias(alias);
        parameterValue.setParameterType(parameter.getType());
        parameterValue.setTemplateReport(templateReport);
        templateReport.getParameterValues().add(parameterValue);
    }
    Class parameterClass = parameterClassResolver.resolveClass(parameter);
    String stringValue = templateParametersExtractor.convertToString(parameter.getType(), parameterClass, fieldValue);
    parameterValue.setDefaultValue(stringValue);
}
Also used : ParameterValue(io.jmix.emailtemplates.entity.ParameterValue) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport) Report(io.jmix.reports.entity.Report) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport)

Example 4 with ParameterValue

use of io.jmix.emailtemplates.entity.ParameterValue in project jmix by jmix-framework.

the class TemplateParametersExtractor method getReportDefaultValues.

public ReportWithParams getReportDefaultValues(Report report, List<ParameterValue> parameterValues) throws ReportParameterTypeChangedException {
    ReportWithParams paramsData = new ReportWithParams(report);
    List<String> exceptionMessages = new ArrayList<>();
    if (parameterValues != null) {
        Report reportFromXml = reportsSerialization.convertToReport(report.getXml());
        for (ParameterValue paramValue : parameterValues) {
            String alias = paramValue.getAlias();
            String stringValue = paramValue.getDefaultValue();
            ReportInputParameter inputParameter = reportFromXml.getInputParameters().stream().filter(e -> e.getAlias().equals(alias)).findFirst().orElse(null);
            if (inputParameter != null) {
                try {
                    emailTemplates.checkParameterTypeChanged(inputParameter, paramValue);
                } catch (ReportParameterTypeChangedException e) {
                    exceptionMessages.add(e.getMessage());
                }
                Class parameterClass = resolveClass(inputParameter);
                Object value = convertFromString(inputParameter.getType(), parameterClass, stringValue);
                paramsData.put(alias, value);
            }
        }
    }
    if (!exceptionMessages.isEmpty()) {
        StringBuilder messages = new StringBuilder();
        exceptionMessages.forEach(m -> {
            messages.append(m);
            messages.append("\n");
        });
        throw new ReportParameterTypeChangedException(messages.toString());
    }
    return paramsData;
}
Also used : ParameterValue(io.jmix.emailtemplates.entity.ParameterValue) ReportWithParams(io.jmix.emailtemplates.dto.ReportWithParams) TemplateReport(io.jmix.emailtemplates.entity.TemplateReport) Report(io.jmix.reports.entity.Report) ReportInputParameter(io.jmix.reports.entity.ReportInputParameter) MetaClass(io.jmix.core.metamodel.model.MetaClass) ReportParameterTypeChangedException(io.jmix.emailtemplates.exception.ReportParameterTypeChangedException)

Aggregations

ParameterValue (io.jmix.emailtemplates.entity.ParameterValue)4 TemplateReport (io.jmix.emailtemplates.entity.TemplateReport)4 ReportWithParams (io.jmix.emailtemplates.dto.ReportWithParams)2 ReportParameterTypeChangedException (io.jmix.emailtemplates.exception.ReportParameterTypeChangedException)2 Report (io.jmix.reports.entity.Report)2 ReportInputParameter (io.jmix.reports.entity.ReportInputParameter)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 ArrayList (java.util.ArrayList)1