Package com.tapestry5book.services.impl

Source Code of com.tapestry5book.services.impl.ReportServiceImpl

package com.tapestry5book.services.impl;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.tapestry5book.entities.DateParameter;
import com.tapestry5book.entities.NumericParameter;
import com.tapestry5book.entities.ReportParameter;
import com.tapestry5book.entities.StringParameter;
import com.tapestry5book.services.ReportService;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

public class ReportServiceImpl implements ReportService {

    public InputStream getReportData(String reportName,
                                     List<ReportParameter> parameters) {

        Document document = new Document();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, stream);
            document.open();

            document.add(new Paragraph(String.format("Report '%s'", reportName)));

            for (ReportParameter param : parameters) {
                document.add(new Paragraph(String.format("Parameter %s:%s", param.getName(), param.getValue())));
            }

        } catch (final Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (document.isOpen()) {
                document.close();
            }
        }
        return new ByteArrayInputStream(stream.toByteArray());
    }

    public List<ReportParameter> getReportParameters(String reportName) {

        ReportParameter date = new StringParameter("Cost Center");
        ReportParameter number = new NumericParameter("Account Number");
        ReportParameter text = new DateParameter("Date");

        return Arrays.asList(date, number, text);
    }

    public ReportParameter findReportParameter(String reportName, String name) {
        List<ReportParameter> parameters = getReportParameters(reportName);

        for (ReportParameter next : parameters) {
            if (next.getName().equals(name)) {
                return next;
            }
        }
        return null;
    }

}
TOP

Related Classes of com.tapestry5book.services.impl.ReportServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.