Commit 470a2361edc7dc410c7d7fd40df74ef7924a2948

Authored by Carlos Alberto
1 parent 8bc915a6
Exists in master

Implementação dos recursos de relatório

cit-esi-api/src/main/java/br/com/centralit/esi/api/enumerated/ReportParameterEnum.java
... ... @@ -3,7 +3,7 @@ package br.com.centralit.esi.api.enumerated;
3 3 public enum ReportParameterEnum {
4 4 DATAOBJECT("ESI.ENUMERADO.DATASOURCE_OBJETO_DADOS"),
5 5 FLOW("ESI.ENUMERADO.DATASOURCE_FLUXO_ESI"),
6   - VARIABLE("ESI.ENUMERADO.DATASOURCE_VARIAVEL"),
  6 + EXPRESSION("ESI.ENUMERADO.DATASOURCE_EXPRESSAO"),
7 7 IMAGE("ESI.ENUMERADO.DATASOURCE_IMAGEM");
8 8  
9 9 private String id;
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/resource/model/ReportExpression.java
... ... @@ -11,6 +11,7 @@ import javax.persistence.Lob;
11 11 import org.hibernate.annotations.Type;
12 12  
13 13 import br.com.centralit.esi.api.design.model.ScriptCode;
  14 +import br.com.centralit.esi.api.design.model.Variable;
14 15 import br.com.centralit.esi.api.enumerated.ExpressionTypeEnum;
15 16 import br.com.centralit.framework.json.Views;
16 17  
... ... @@ -24,17 +25,21 @@ public class ReportExpression implements Serializable {
24 25 */
25 26 private static final long serialVersionUID = 1795388490044351994L;
26 27  
27   - @JsonView({Views.GenericView.class })
  28 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
28 29 private ExpressionTypeEnum expressionType;
  30 +
  31 + @Embedded
  32 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
  33 + private Variable variable;
29 34  
30   - @JsonView({ Views.GenericView.class})
  35 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
31 36 @Lob
32 37 @Basic(fetch = FetchType.LAZY)
33 38 @Type(type="org.hibernate.type.StringClobType")
34 39 private String constantValue;
35 40  
36 41 @Embedded
37   - @JsonView({ Views.GenericView.class})
  42 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
38 43 private ScriptCode scriptCode;
39 44  
40 45 /**
... ... @@ -86,4 +91,12 @@ public class ReportExpression implements Serializable {
86 91 return serialVersionUID;
87 92 }
88 93  
  94 + public Variable getVariable() {
  95 + return variable;
  96 + }
  97 +
  98 + public void setVariable(Variable variable) {
  99 + this.variable = variable;
  100 + }
  101 +
89 102 }
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/resource/service/ReportManager.java
... ... @@ -7,6 +7,6 @@ import br.com.centralit.esi.api.resource.model.ReportVersion;
7 7  
8 8 public interface ReportManager extends Serializable {
9 9  
10   - public byte[] execute(ReportVersion reportVersion, HashMap<String, Object> parameters);
  10 + public byte[] execute(ReportVersion reportVersion, HashMap<String, Object> inputMap);
11 11  
12 12 }
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/resource/service/impl/JasperReportManagerImpl.java
... ... @@ -10,13 +10,19 @@ import net.sf.jasperreports.engine.JRException;
10 10 import net.sf.jasperreports.engine.JasperExportManager;
11 11 import net.sf.jasperreports.engine.JasperFillManager;
12 12 import net.sf.jasperreports.engine.JasperPrint;
  13 +import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
13 14 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
14 15  
15 16 import org.springframework.stereotype.Component;
16 17  
  18 +import br.com.centralit.esi.api.enumerated.ExpressionTypeEnum;
  19 +import br.com.centralit.esi.api.execution.component.ExecuteScript;
17 20 import br.com.centralit.esi.api.resource.model.ReportDataSource;
  21 +import br.com.centralit.esi.api.resource.model.ReportExpression;
18 22 import br.com.centralit.esi.api.resource.model.ReportVersion;
19 23 import br.com.centralit.esi.api.resource.service.JasperReportManager;
  24 +import br.com.centralit.esi.api.runtime.RuntimeEnvironment;
  25 +import br.com.centralit.esi.api.runtime.RuntimeEnvironmentImpl;
20 26 import br.com.centralit.esi.api.runtime.RuntimeEnvironmentOutput;
21 27 import br.com.centralit.esi.api.util.ConvertUtilsESI;
22 28 import br.com.centralit.esi.exception.EsiExecutionException;
... ... @@ -30,7 +36,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
30 36 private static final long serialVersionUID = 1L;
31 37  
32 38 @Override
33   - public byte[] execute(ReportVersion report, HashMap<String, Object> parameters) {
  39 + public byte[] execute(ReportVersion report, HashMap<String, Object> inputMap) {
34 40 JasperPrint print = null;
35 41 byte[] buffer = null;
36 42  
... ... @@ -39,7 +45,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
39 45 Connection connection = dataSourceService.connect(report.getDataSource().getConnection());
40 46  
41 47 try {
42   - print = JasperFillManager.fillReport(report.getFileName(), this.buildParams(report, parameters), connection);
  48 + print = JasperFillManager.fillReport(report.getFileName(), this.buildParams(report, inputMap), connection);
43 49 } catch (JRException e) {
44 50 e.printStackTrace();
45 51 try {
... ... @@ -53,7 +59,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
53 59  
54 60 default:
55 61 try {
56   - print = JasperFillManager.fillReport(report.getFileName(), this.buildParams(report, parameters), this.buildDataSource(report.getDataSource(), parameters));
  62 + print = JasperFillManager.fillReport(report.getFileName(), this.buildParams(report, inputMap), this.buildDataSource(report.getDataSource(), inputMap));
57 63 } catch (JRException e) {
58 64 throw new EsiExecutionException(e);
59 65 }
... ... @@ -74,27 +80,35 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
74 80 }
75 81  
76 82 @Override
77   - protected JRDataSource buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> parameters) {
  83 + protected JRDataSource buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> inputMap) {
78 84 JRDataSource dataSource = null;
79   - List<HashMap<String, Object>> result = null;
80 85  
81 86 switch (reportDataSource.getType()) {
82 87 case DATAOBJECT:
83 88 String SQLName = reportDataSource.getCustomSQL() != null ? reportDataSource.getCustomSQL().getName() : null;
84   - result = dataManager.executeQuery(reportDataSource.getDataObject(), SQLName, parameters);
85   - dataSource = new JRBeanCollectionDataSource(result);
  89 + List<HashMap<String, Object>> resultSet = dataManager.executeQuery(reportDataSource.getDataObject(), SQLName, inputMap);
  90 + dataSource = new JRBeanCollectionDataSource(resultSet);
86 91 break;
87 92  
88 93 case FLOW:
89   - RuntimeEnvironmentOutput output = runtimeManager.execute(reportDataSource.getFlow().getName(), parameters);
  94 + RuntimeEnvironmentOutput output = runtimeManager.execute(reportDataSource.getFlow().getName(), inputMap);
90 95 if (output.getOutputVariables() != null) {
91   - result = new ArrayList<HashMap<String, Object>>();
92   - result.add(ConvertUtilsESI.environmentVariablesToMap(output.getOutputVariables()));
93   - dataSource = new JRBeanCollectionDataSource(result);
  96 + List<HashMap<String, Object>> variablesMap = new ArrayList<HashMap<String, Object>>();
  97 + variablesMap.add(ConvertUtilsESI.environmentVariablesToMap(output.getOutputVariables()));
  98 + dataSource = new JRBeanCollectionDataSource(variablesMap);
94 99 }
95 100 break;
96 101  
97 102 case EXPRESSION:
  103 + ReportExpression expression = reportDataSource.getExpression();
  104 + if (expression.getExpressionType().equals(ExpressionTypeEnum.CONSTANT) && expression.getConstantValue() != null) {
  105 + Object object = ConvertUtilsESI.convertVariable(expression.getVariable().getVariableType(), false, expression.getVariable().getClassName(), expression.getConstantValue());
  106 + dataSource = new JRBeanArrayDataSource(new Object[]{object});
  107 + }else if (expression.getExpressionType().equals(ExpressionTypeEnum.SCRIPT) && expression.getScriptCode() != null) {
  108 + RuntimeEnvironment runtimeEnvironment = new RuntimeEnvironmentImpl(null, inputMap);
  109 + Object object = ExecuteScript.execute(runtimeEnvironment, expression.getVariable().getName(), expression.getScriptCode());
  110 + dataSource = new JRBeanArrayDataSource(new Object[]{object});
  111 + }
98 112 break;
99 113  
100 114 default:
... ...
cit-esi-api/src/main/java/br/com/centralit/esi/api/resource/service/impl/ReportManagerImpl.java
1 1 package br.com.centralit.esi.api.resource.service.impl;
2 2  
3 3 import java.util.HashMap;
  4 +import java.util.List;
4 5 import java.util.Map;
5 6  
6 7 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -8,10 +9,18 @@ import org.springframework.stereotype.Component;
8 9  
9 10 import br.com.centralit.esi.api.data.service.DataSourceService;
10 11 import br.com.centralit.esi.api.data.service.impl.DataManager;
  12 +import br.com.centralit.esi.api.enumerated.ExpressionTypeEnum;
  13 +import br.com.centralit.esi.api.execution.component.ExecuteScript;
11 14 import br.com.centralit.esi.api.resource.model.ReportDataSource;
  15 +import br.com.centralit.esi.api.resource.model.ReportExpression;
  16 +import br.com.centralit.esi.api.resource.model.ReportParameter;
12 17 import br.com.centralit.esi.api.resource.model.ReportVersion;
13 18 import br.com.centralit.esi.api.resource.service.ReportManager;
  19 +import br.com.centralit.esi.api.runtime.RuntimeEnvironment;
  20 +import br.com.centralit.esi.api.runtime.RuntimeEnvironmentImpl;
  21 +import br.com.centralit.esi.api.runtime.RuntimeEnvironmentOutput;
14 22 import br.com.centralit.esi.api.runtime.service.RuntimeManager;
  23 +import br.com.centralit.esi.api.util.ConvertUtilsESI;
15 24  
16 25 @Component("reportManager")
17 26 public abstract class ReportManagerImpl implements ReportManager {
... ... @@ -30,11 +39,68 @@ public abstract class ReportManagerImpl implements ReportManager {
30 39 @Autowired
31 40 protected RuntimeManager runtimeManager;
32 41  
33   - protected abstract Object buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> parameters);
  42 + protected abstract Object buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> inputMap);
34 43  
35   - protected Map<String, Object> buildParams(ReportVersion report, HashMap<String, Object> parameters) {
36   - // TODO Auto-generated method stub
37   - return null;
  44 + protected Map<String, Object> buildParams(ReportVersion report, HashMap<String, Object> inputMap) {
  45 + HashMap<String, Object> outputMap = new HashMap<String, Object>();
  46 +
  47 + if (report.getParameters() != null) {
  48 + for (ReportParameter parameter : report.getParameters()) {
  49 + this.buildParam(parameter, inputMap, outputMap);
  50 + }
  51 + }
  52 +
  53 + return outputMap;
  54 + }
  55 +
  56 + protected void buildParam(ReportParameter parameter, HashMap<String, Object> inputMap, HashMap<String, Object> outputMap) {
  57 + switch (parameter.getType()) {
  58 + case DATAOBJECT:
  59 + String SQLName = parameter.getCustomSQL() != null ? parameter.getCustomSQL().getName() : null;
  60 + List<HashMap<String, Object>> resultSet = dataManager.executeQuery(parameter.getDataObject(), SQLName, inputMap);
  61 + if (resultSet != null) {
  62 + for (HashMap<String, Object> map : resultSet) {
  63 + Object value = map.get(parameter.getOutputAttributeName());
  64 + if (value != null) {
  65 + outputMap.put(parameter.getOutputAttributeName(), value);
  66 + break;
  67 + }
  68 + }
  69 + }
  70 + break;
  71 +
  72 + case FLOW:
  73 + RuntimeEnvironmentOutput output = runtimeManager.execute(parameter.getFlow().getName(), inputMap);
  74 + if (output.getOutputVariables() != null) {
  75 + Object value = output.getValue(parameter.getOutputAttributeName());
  76 + if (value != null) {
  77 + outputMap.put(parameter.getOutputAttributeName(), value);
  78 + }
  79 + }
  80 + break;
  81 +
  82 + case EXPRESSION:
  83 + ReportExpression expression = parameter.getExpression();
  84 + if (expression.getExpressionType().equals(ExpressionTypeEnum.CONSTANT) && expression.getConstantValue() != null) {
  85 + Object value = ConvertUtilsESI.convertVariable(expression.getVariable().getVariableType(), false, expression.getVariable().getClassName(), expression.getConstantValue());
  86 + if (value != null) {
  87 + outputMap.put(expression.getVariable().getName(), value);
  88 + }
  89 + }else if (expression.getExpressionType().equals(ExpressionTypeEnum.SCRIPT) && expression.getScriptCode() != null) {
  90 + RuntimeEnvironment runtimeEnvironment = new RuntimeEnvironmentImpl(null, inputMap);
  91 + Object value = ExecuteScript.execute(runtimeEnvironment, expression.getVariable().getName(), expression.getScriptCode());
  92 + if (value != null) {
  93 + outputMap.put(expression.getVariable().getName(), value);
  94 + }
  95 + }
  96 + break;
  97 +
  98 + case IMAGE:
  99 + break;
  100 +
  101 + default:
  102 + break;
  103 + }
38 104 }
39 105  
40 106 }
41 107 \ No newline at end of file
... ...