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,7 +3,7 @@ package br.com.centralit.esi.api.enumerated;
3 public enum ReportParameterEnum { 3 public enum ReportParameterEnum {
4 DATAOBJECT("ESI.ENUMERADO.DATASOURCE_OBJETO_DADOS"), 4 DATAOBJECT("ESI.ENUMERADO.DATASOURCE_OBJETO_DADOS"),
5 FLOW("ESI.ENUMERADO.DATASOURCE_FLUXO_ESI"), 5 FLOW("ESI.ENUMERADO.DATASOURCE_FLUXO_ESI"),
6 - VARIABLE("ESI.ENUMERADO.DATASOURCE_VARIAVEL"), 6 + EXPRESSION("ESI.ENUMERADO.DATASOURCE_EXPRESSAO"),
7 IMAGE("ESI.ENUMERADO.DATASOURCE_IMAGEM"); 7 IMAGE("ESI.ENUMERADO.DATASOURCE_IMAGEM");
8 8
9 private String id; 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,6 +11,7 @@ import javax.persistence.Lob;
11 import org.hibernate.annotations.Type; 11 import org.hibernate.annotations.Type;
12 12
13 import br.com.centralit.esi.api.design.model.ScriptCode; 13 import br.com.centralit.esi.api.design.model.ScriptCode;
  14 +import br.com.centralit.esi.api.design.model.Variable;
14 import br.com.centralit.esi.api.enumerated.ExpressionTypeEnum; 15 import br.com.centralit.esi.api.enumerated.ExpressionTypeEnum;
15 import br.com.centralit.framework.json.Views; 16 import br.com.centralit.framework.json.Views;
16 17
@@ -24,17 +25,21 @@ public class ReportExpression implements Serializable { @@ -24,17 +25,21 @@ public class ReportExpression implements Serializable {
24 */ 25 */
25 private static final long serialVersionUID = 1795388490044351994L; 26 private static final long serialVersionUID = 1795388490044351994L;
26 27
27 - @JsonView({Views.GenericView.class }) 28 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
28 private ExpressionTypeEnum expressionType; 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 @Lob 36 @Lob
32 @Basic(fetch = FetchType.LAZY) 37 @Basic(fetch = FetchType.LAZY)
33 @Type(type="org.hibernate.type.StringClobType") 38 @Type(type="org.hibernate.type.StringClobType")
34 private String constantValue; 39 private String constantValue;
35 40
36 @Embedded 41 @Embedded
37 - @JsonView({ Views.GenericView.class}) 42 + @JsonView({ Views.EsiResourceEditView.class, Views.EsiPackageExportView.class})
38 private ScriptCode scriptCode; 43 private ScriptCode scriptCode;
39 44
40 /** 45 /**
@@ -86,4 +91,12 @@ public class ReportExpression implements Serializable { @@ -86,4 +91,12 @@ public class ReportExpression implements Serializable {
86 return serialVersionUID; 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,6 +7,6 @@ import br.com.centralit.esi.api.resource.model.ReportVersion;
7 7
8 public interface ReportManager extends Serializable { 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,13 +10,19 @@ import net.sf.jasperreports.engine.JRException;
10 import net.sf.jasperreports.engine.JasperExportManager; 10 import net.sf.jasperreports.engine.JasperExportManager;
11 import net.sf.jasperreports.engine.JasperFillManager; 11 import net.sf.jasperreports.engine.JasperFillManager;
12 import net.sf.jasperreports.engine.JasperPrint; 12 import net.sf.jasperreports.engine.JasperPrint;
  13 +import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
13 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 14 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
14 15
15 import org.springframework.stereotype.Component; 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 import br.com.centralit.esi.api.resource.model.ReportDataSource; 20 import br.com.centralit.esi.api.resource.model.ReportDataSource;
  21 +import br.com.centralit.esi.api.resource.model.ReportExpression;
18 import br.com.centralit.esi.api.resource.model.ReportVersion; 22 import br.com.centralit.esi.api.resource.model.ReportVersion;
19 import br.com.centralit.esi.api.resource.service.JasperReportManager; 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 import br.com.centralit.esi.api.runtime.RuntimeEnvironmentOutput; 26 import br.com.centralit.esi.api.runtime.RuntimeEnvironmentOutput;
21 import br.com.centralit.esi.api.util.ConvertUtilsESI; 27 import br.com.centralit.esi.api.util.ConvertUtilsESI;
22 import br.com.centralit.esi.exception.EsiExecutionException; 28 import br.com.centralit.esi.exception.EsiExecutionException;
@@ -30,7 +36,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper @@ -30,7 +36,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
30 private static final long serialVersionUID = 1L; 36 private static final long serialVersionUID = 1L;
31 37
32 @Override 38 @Override
33 - public byte[] execute(ReportVersion report, HashMap<String, Object> parameters) { 39 + public byte[] execute(ReportVersion report, HashMap<String, Object> inputMap) {
34 JasperPrint print = null; 40 JasperPrint print = null;
35 byte[] buffer = null; 41 byte[] buffer = null;
36 42
@@ -39,7 +45,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper @@ -39,7 +45,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
39 Connection connection = dataSourceService.connect(report.getDataSource().getConnection()); 45 Connection connection = dataSourceService.connect(report.getDataSource().getConnection());
40 46
41 try { 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 } catch (JRException e) { 49 } catch (JRException e) {
44 e.printStackTrace(); 50 e.printStackTrace();
45 try { 51 try {
@@ -53,7 +59,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper @@ -53,7 +59,7 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
53 59
54 default: 60 default:
55 try { 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 } catch (JRException e) { 63 } catch (JRException e) {
58 throw new EsiExecutionException(e); 64 throw new EsiExecutionException(e);
59 } 65 }
@@ -74,27 +80,35 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper @@ -74,27 +80,35 @@ public class JasperReportManagerImpl extends ReportManagerImpl implements Jasper
74 } 80 }
75 81
76 @Override 82 @Override
77 - protected JRDataSource buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> parameters) { 83 + protected JRDataSource buildDataSource(ReportDataSource reportDataSource, HashMap<String, Object> inputMap) {
78 JRDataSource dataSource = null; 84 JRDataSource dataSource = null;
79 - List<HashMap<String, Object>> result = null;  
80 85
81 switch (reportDataSource.getType()) { 86 switch (reportDataSource.getType()) {
82 case DATAOBJECT: 87 case DATAOBJECT:
83 String SQLName = reportDataSource.getCustomSQL() != null ? reportDataSource.getCustomSQL().getName() : null; 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 break; 91 break;
87 92
88 case FLOW: 93 case FLOW:
89 - RuntimeEnvironmentOutput output = runtimeManager.execute(reportDataSource.getFlow().getName(), parameters); 94 + RuntimeEnvironmentOutput output = runtimeManager.execute(reportDataSource.getFlow().getName(), inputMap);
90 if (output.getOutputVariables() != null) { 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 break; 100 break;
96 101
97 case EXPRESSION: 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 break; 112 break;
99 113
100 default: 114 default:
cit-esi-api/src/main/java/br/com/centralit/esi/api/resource/service/impl/ReportManagerImpl.java
1 package br.com.centralit.esi.api.resource.service.impl; 1 package br.com.centralit.esi.api.resource.service.impl;
2 2
3 import java.util.HashMap; 3 import java.util.HashMap;
  4 +import java.util.List;
4 import java.util.Map; 5 import java.util.Map;
5 6
6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
@@ -8,10 +9,18 @@ import org.springframework.stereotype.Component; @@ -8,10 +9,18 @@ import org.springframework.stereotype.Component;
8 9
9 import br.com.centralit.esi.api.data.service.DataSourceService; 10 import br.com.centralit.esi.api.data.service.DataSourceService;
10 import br.com.centralit.esi.api.data.service.impl.DataManager; 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 import br.com.centralit.esi.api.resource.model.ReportDataSource; 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 import br.com.centralit.esi.api.resource.model.ReportVersion; 17 import br.com.centralit.esi.api.resource.model.ReportVersion;
13 import br.com.centralit.esi.api.resource.service.ReportManager; 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 import br.com.centralit.esi.api.runtime.service.RuntimeManager; 22 import br.com.centralit.esi.api.runtime.service.RuntimeManager;
  23 +import br.com.centralit.esi.api.util.ConvertUtilsESI;
15 24
16 @Component("reportManager") 25 @Component("reportManager")
17 public abstract class ReportManagerImpl implements ReportManager { 26 public abstract class ReportManagerImpl implements ReportManager {
@@ -30,11 +39,68 @@ public abstract class ReportManagerImpl implements ReportManager { @@ -30,11 +39,68 @@ public abstract class ReportManagerImpl implements ReportManager {
30 @Autowired 39 @Autowired
31 protected RuntimeManager runtimeManager; 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 \ No newline at end of file 107 \ No newline at end of file