Montagem de Relatório/subrepot com JASPER

1 resposta
hmoore

Prezados,

Eu acompanho mais do que escrevo, agora me surgiu um problema enorme para mim e tenho que compartilhar para achar a resposta.

Estou gerando um Relatório em PDF através do JASPER. nele tenho uma lista e dentro dessa lista tenho outra lista. estou tentando passar essa lista para um subreport e não estou tendo exito. segue o código para tentar esclarecer o que estou fazendo.
:?: :shock: :roll:
OBS: o dado que estou passando é uma lista de MAP onde o 1° elemento é uma string e o 2° elemento é uma lista de MAP. Essa segunda lista é que estou tentando passar para o SUBREPORT.

:arrow: GERAÇÃO DO PDF

//	RELATORIO
	private List<Map<String,Object>> liberRecursos;

//	UMA OUTRA CLASSE CHAMA O gerarPDF
private String gerarPDF() throws JRException, SolicitaLiberacaoRecursoException, IOException{
			
		final FacesContext context = FacesContext.getCurrentInstance();  
		final HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();  

		final ServletOutputStream servOutputStream = response.getOutputStream();  
		final InputStream reportStream = context.getExternalContext().getResourceAsStream("/dinamico/report/Relatorio_Lib_rec.jasper");
	   
		
		JasperRunManager.runReportToPdfStream(reportStream, servOutputStream, parametrizar(), new JRBeanCollectionDataSource(liberRecursos));
		reportStream.close();  

		response.setContentType("application/pdf");  
		response.setHeader("Content-Disposition", "attachment;filename=relatorio.pdf;");  
		servOutputStream.flush();
		servOutputStream.close();
		context.responseComplete();

		return Constantes.LIBERAR_DADOS;
	}

:arrow: Montagem da lista.

public List<Map<String,Object>> mapaRelatorio()throws SolicitaLiberacaoRecursoException{
		
		final ArrayList copia = new ArrayList();
		final HashMap mapa= new HashMap();
		
		final List<Map<String,Object>> trabalhadorList = (List<Map<String,Object>>)copia.clone();
		final List<Map<String,Object>> empresasList = (List<Map<String,Object>>)copia.clone();
		Map<String,Object> empresaMap;
		Map<String,Object> trabalhadorMap;
		List<Trabalhador> listaDeTrabalhado;
		
		List<Long> numemp;
		
		numemp = (List<Long>) copia.clone();
		empresaMap = (Map<String, Object>) mapa.clone();
		
		for (Long cnpjEmp : cnpjtas) {
			empresaMap = (Map<String, Object>) mapa.clone();
			numemp = (List<Long>) copia.clone();
			numemp.add(cnpjEmp);
			listaDeTrabalhado = delegate.buscarTrabalhadores(numemp, "N");
			
			empresaMap.put("cnpjEmpresa", cnpjEmp);
			
			trabalhadorMap = (Map<String, Object>) mapa.clone();
			
			for(final Trabalhador trabalhador : listaDeTrabalhado){
				trabalhadorMap = (Map<String, Object>) mapa.clone();
				
				trabalhadorMap.put("numPISPASEP", trabalhador.getNuPisAtvo() );
				trabalhadorMap.put("nomTrab", trabalhador.getNoTrblr() );
				trabalhadorMap.put("dtaNasc", trabalhador.getDtNscmo() );
				trabalhadorMap.put("valorRendimento", trabalhador.getValorRendimento() );
				trabalhadorMap.put("valorRendAbono", trabalhador.getValorAbono() );
				
				trabalhadorList.add(trabalhadorMap);
				
			}
			
			empresaMap.put("trabalhadores", trabalhadorList);
			empresasList.add(empresaMap);
		}
		
		return empresasList;
	}

:arrow: PARAMETROS

private Map<String, Object> parametrizar()throws SolicitaLiberacaoRecursoException{
		final Map<String, Object> parametros = new HashMap<String, Object>();
		
		parametros.put("MAT_CONVENIO", "texto00");
		parametros.put("RAZAO_SOCIAL", "texto01");
		parametros.put("CNPJ_CEI_EMPR", "texto02");
		parametros.put("QT_TOTAL_ESTB", "texto03");
		parametros.put("QT_TOTAL_TRAB", "texto04");
		parametros.put("VALOR_SAL_MIN", "texto05");
		
		parametros.put("DT_PREV_PGMTO", "texto06");
		
		parametros.put("QT_TOT_NPAGO_REND", "texto07");
		parametros.put("QT_TOT_NPAGO_ABONO", "texto09");
		parametros.put("VAL_TOT_NPAGO_REND", "texto08");
		
		return parametros;
	}

:arrow: Relatorio_Lib_rec.jasper

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
		 name="Relatorio_Liberacao_recursos"
		 columnCount="1"
		 printOrder="Vertical"
		 orientation="Portrait"
		 pageWidth="595"
		 pageHeight="842"
		 columnWidth="535"
		 columnSpacing="0"
		 leftMargin="30"
		 rightMargin="30"
		 topMargin="20"
		 bottomMargin="20"
		 whenNoDataType="NoPages"
		 isTitleNewPage="false"
		 isSummaryNewPage="false">
	<property name="ireport.scriptlethandling" value="0" />
	<property name="ireport.encoding" value="UTF-8" />
	<import value="java.util.*" />
	<import value="net.sf.jasperreports.engine.*" />
	<import value="net.sf.jasperreports.engine.data.*" />

	<parameter name="MAT_CONVENIO" isForPrompting="false" class="java.lang.String"/>
	<parameter name="RAZAO_SOCIAL" isForPrompting="false" class="java.lang.String"/>
	<parameter name="CNPJ_CEI_EMPR" isForPrompting="false" class="java.lang.String"/>
	<parameter name="QT_TOTAL_ESTB" isForPrompting="false" class="java.lang.String"/>
	<parameter name="QT_TOTAL_TRAB" isForPrompting="false" class="java.lang.String"/>
	<parameter name="VALOR_SAL_MIN" isForPrompting="false" class="java.lang.String"/>
	<parameter name="QT_TOT_NPAGO_REND" isForPrompting="false" class="java.lang.String"/>
	<parameter name="QT_TOT_NPAGO_ABONO" isForPrompting="false" class="java.lang.String"/>
	<parameter name="VAL_TOT_NPAGO_REND" isForPrompting="false" class="java.lang.String"/>
	<parameter name="VAL_TOT_NPAGO_ABONO" isForPrompting="false" class="java.lang.String"/>

	<field name="cnpjEmpresa" class="java.lang.Long"/>
	<field name="trabalhadores" class="java.lang.Object"/>

		<background><band height="0"  isSplitAllowed="true" ></band></background>
		<title><band height="50"  isSplitAllowed="true" ></band></title>
		<pageHeader>
			<band height="50"  isSplitAllowed="true" >
				<staticText>
					<reportElement
						x="40"
						y="7"
						width="220"
						height="24"
						key="staticText-2"/>
					<box></box>
					<textElement><font/></textElement>
				<text><![CDATA[TESTE LIBERACAO DE RECURSOS]]></text>
				</staticText>
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="278"
						y="7"
						width="197"
						height="24"
						key="textField"/>
					<box></box>
					<textElement><font/></textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[$P{MAT_CONVENIO}]]></textFieldExpression>
				</textField>
			</band>
		</pageHeader>
		<columnHeader>
			<band height="88"  isSplitAllowed="true" >
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="40"
						y="20"
						width="114"
						height="18"
						key="textField"/>
					<box></box>
					<textElement><font/></textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[$P{MAT_CONVENIO}]]></textFieldExpression>
				</textField>
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="40"
						y="48"
						width="117"
						height="18"
						key="textField"/>
					<box></box>
					<textElement><font/></textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[$P{RAZAO_SOCIAL}]]></textFieldExpression>
				</textField>
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="205"
						y="20"
						width="116"
						height="18"
						key="textField"/>
					<box></box>
					<textElement><font/></textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[$P{CNPJ_CEI_EMPR}]]></textFieldExpression>
				</textField>
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="205"
						y="48"
						width="116"
						height="18"
						key="textField"/>
					<box></box>
					<textElement><font/></textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[$P{CNPJ_CEI_EMPR}]]></textFieldExpression>
				</textField>
			</band>
		</columnHeader>
		<detail>
			<band height="178"  isSplitAllowed="true" >
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="40"
						y="13"
						width="100"
						height="18"
						key="textField"/>
					<box></box>
					<textElement>
						<font/>
					</textElement>
				<textFieldExpression   class="java.lang.Long"><![CDATA[$F{cnpjEmpresa}]]></textFieldExpression>
				</textField>
				<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
					<reportElement
						x="13"
						y="55"
						width="496"
						height="18"
						key="textField"/>
					<box></box>
					<textElement>
						<font/>
					</textElement>
				<textFieldExpression   class="java.lang.String"><![CDATA[""+$F{trabalhadores}]]></textFieldExpression>
				</textField>
				<subreport  isUsingCache="true">
					<reportElement
						x="13"
						y="96"
						width="496"
						height="67"
						key="subreport-1"/>
					<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource((List) $F{trabalhadores})]]></dataSourceExpression>
					<subreportExpression  class="java.lang.String"><![CDATA[".\\Rel_Lib_rec_subreport_Trab.jasper"]]></subreportExpression>
				</subreport>
			</band>
		</detail>
		<columnFooter><band height="30"  isSplitAllowed="true" ></band></columnFooter>
		<pageFooter><band height="50"  isSplitAllowed="true" ></band></pageFooter>
		<lastPageFooter><band height="50"  isSplitAllowed="true" ></band></lastPageFooter>
		<summary><band height="50"  isSplitAllowed="true" ></band></summary>
</jasperReport>

:arrow: // SUB RELATORIO. Rel_Lib_rec_subreport_Trab.jasper

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
		 name="classic"
		 columnCount="1"
		 printOrder="Vertical"
		 orientation="Portrait"
		 pageWidth="535"
		 pageHeight="802"
		 columnWidth="535"
		 columnSpacing="0"
		 leftMargin="0"
		 rightMargin="0"
		 topMargin="0"
		 bottomMargin="0"
		 whenNoDataType="AllSectionsNoDetail"
		 isTitleNewPage="false"
		 isSummaryNewPage="false">
	<property name="ireport.scriptlethandling" value="0" />
	<property name="ireport.encoding" value="UTF-8" />
	<import value="java.util.*" />
	<import value="net.sf.jasperreports.engine.*" />
	<import value="net.sf.jasperreports.engine.data.*" />


		<background><band height="0"  isSplitAllowed="true" ></band></background>
		<title><band height="0"  isSplitAllowed="true" ></band></title>
		<pageHeader><band height="0"  isSplitAllowed="true" ></band></pageHeader>
		<columnHeader>
			<band height="20"  isSplitAllowed="true" >
				<rectangle radius="0" >
					<reportElement
						mode="Opaque"
						x="1"
						y="1"
						width="534"
						height="17"
						forecolor="#000000"
						backcolor="#999999"
						key="element-22"/>
					<graphicElement stretchType="NoStretch">
					<pen lineWidth="0.25" lineStyle="Solid"/>
</graphicElement>
				</rectangle>
			</band>
		</columnHeader>
		<detail>
			<band height="51"  isSplitAllowed="true" >
				<line direction="TopDown">
					<reportElement
						x="0"
						y="17"
						width="535"
						height="0"
						forecolor="#808080"
						key="line"
						positionType="FixRelativeToBottom"/>
					<graphicElement stretchType="NoStretch">
					<pen lineWidth="0.25" lineStyle="Solid"/>
</graphicElement>
				</line>
			</band>
		</detail>
		<columnFooter><band height="0"  isSplitAllowed="true" ></band></columnFooter>
		<pageFooter><band height="0"  isSplitAllowed="true" ></band></pageFooter>
		<summary><band height="0"  isSplitAllowed="true" ></band></summary>
</jasperReport>

Desde Já agradeço.

1 Resposta

erico_kl

Por quê você não manda diretamente uma lista de empresas com seus respectivos trabalhadores dentro?
É uma relação 1xN certo? Faça essa relação na sua classe de negócios também

e depois passe essas empresas para o relatório e no relatório crie um subrelatório passando a lista de trabalhadores como parâmetro num DataSource

Criado 17 de agosto de 2012
Ultima resposta 17 de ago. de 2012
Respostas 1
Participantes 2