Search in sources :

Example 1 with ReportException

use of com.autentia.tnt.error.ReportException in project TNTConcept by autentia.

the class ReportGeneratorStandardImpl method generate.

public void generate(ReportInfo parameters, OutputStream outputStream) throws ReportException {
    JasperReport report = null;
    Session session = null;
    Connection con = null;
    try {
        report = initReport(parameters.name, parameters.category);
        debug("ReportGenerator - report: " + report);
        debug("ReportGenerator - iniciando lista de parametros");
        if (report == null) {
            debug("ReportGenerator - SC_NOT_FOUND");
            // response.sendError(HttpServletResponse.SC_NOT_FOUND);
            this.throwError("Null report");
            return;
        }
        Map args = getParametersAsMapAndSubReport(parameters.parameters, parameters.category);
        // establecemos el resource bundle correspondiente al locale actual
        // del usuario al JasperReport
        final Principal principal = AuthenticationManager.getDefault().getCurrentPrincipal();
        args.put(JRParameter.REPORT_RESOURCE_BUNDLE, ResourceBundle.getBundle("com.autentia.tnt.resources.report", principal.getLocale()));
        debug("ReportGenerator - argc " + args.size());
        debug("ReportGenerator - iniciando conexion and BD");
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        con = session.connection();
        debug("ReportGenerator - generación del informe en función del formato");
        GeneratorFactory.getFactory().getGeneratorByFormat(parameters.format).generate(outputStream, report, args, con);
        debug("ReportGenerator - finalización del informe");
    } catch (Exception e) {
        log.error("ReportGenerator - exception", e);
        this.throwError("Exception");
    /*
			 * try {
			 * response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
			 * e.getMessage()); } catch (IOException e1) { // TODO
			 * Auto-generated catch block e1.printStackTrace(); }
			 */
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e1) {
                log.error("ReportGenerator - exception cerrando conexion", e1);
            }
            con = null;
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JasperReport(net.sf.jasperreports.engine.JasperReport) HashMap(java.util.HashMap) Map(java.util.Map) Principal(com.autentia.tnt.manager.security.Principal) JRException(net.sf.jasperreports.engine.JRException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReportException(com.autentia.tnt.error.ReportException) Session(org.hibernate.Session)

Example 2 with ReportException

use of com.autentia.tnt.error.ReportException in project TNTConcept by autentia.

the class ReportGeneratorStandardImpl method getParametersAsMapAndSubReport.

private Map getParametersAsMapAndSubReport(Map<String, Object> parameters, String reportCategory) throws Exception, ReportException {
    JasperReport subReport;
    Map args = new HashMap();
    Enumeration e = new IteratorEnumeration(parameters.keySet().iterator());
    while (e.hasMoreElements()) {
        String arg = (String) e.nextElement();
        final String value = (String) parameters.get(arg);
        Object obj = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date value_date = sdf.parse(value);
            java.sql.Timestamp ts = new Timestamp(value_date.getTime());
            obj = ts;
        } catch (Exception ex) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date value_date = sdf.parse(value);
                obj = value_date;
            } catch (Exception ex2) {
                try {
                    Integer value_int = Integer.parseInt(value);
                    obj = value_int;
                } catch (Exception ex3) {
                    if ("true".equals(value) || "false".equals(value)) {
                        obj = Boolean.parseBoolean(value);
                    } else {
                        obj = value;
                    }
                }
            }
        }
        if (arg.startsWith("SUBREPORT")) {
            obj = loadSubreportByCategory(reportCategory, obj);
        }
        if (obj.equals("allItemsSelected")) {
            obj = "%";
        }
        args.put(arg, obj);
    }
    return args;
}
Also used : Enumeration(java.util.Enumeration) IteratorEnumeration(org.apache.commons.collections.iterators.IteratorEnumeration) HashMap(java.util.HashMap) IteratorEnumeration(org.apache.commons.collections.iterators.IteratorEnumeration) Timestamp(java.sql.Timestamp) JasperReport(net.sf.jasperreports.engine.JasperReport) Timestamp(java.sql.Timestamp) Date(java.util.Date) JRException(net.sf.jasperreports.engine.JRException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReportException(com.autentia.tnt.error.ReportException) HashMap(java.util.HashMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with ReportException

use of com.autentia.tnt.error.ReportException in project TNTConcept by autentia.

the class ReportServlet method generateReport.

private void generateReport(HttpServletRequest request, HttpServletResponse response, String reportName, String ext, String reportCategory) throws IOException {
    ReportInfoBuilder builder = new ReportInfoBuilder();
    builder.name(reportName).in(ReportFormat.valueOf(ext)).withCategory(reportCategory);
    Enumeration e = request.getParameterNames();
    while (e.hasMoreElements()) {
        String arg = (String) e.nextElement();
        final String value = request.getParameter(arg);
        builder.andParameter(arg, value);
    }
    ReportGeneratorStandardImpl generator = new ReportGeneratorStandardImpl();
    try {
        generator.generate(builder.build(), response.getOutputStream());
    } catch (ReportException e1) {
        log.error(e1);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.getMessage());
    }
    response.setContentType(ReportFormat.valueOf(ext).getResponseType());
}
Also used : Enumeration(java.util.Enumeration) ReportGeneratorStandardImpl(com.autentia.tnt.report.ReportGeneratorStandardImpl) ReportInfoBuilder(com.autentia.tnt.report.ReportInfo.ReportInfoBuilder) ReportException(com.autentia.tnt.error.ReportException)

Example 4 with ReportException

use of com.autentia.tnt.error.ReportException in project TNTConcept by autentia.

the class ReportGeneratorStandardImpl method loadSubreportByCategory.

private Object loadSubreportByCategory(String reportCategory, Object obj) throws JRException, ReportException {
    JasperReport subReport;
    final String subreportPath = ConfigurationUtil.getDefault().getReportPath();
    String targetFilePath;
    String sourceFilePath;
    if (reportCategory.equals("personal/")) {
        sourceFilePath = subreportPath + ReportUtil.SUBREPORT_PREFIX + obj + ReportUtil.REPORT_DEFINITION_SUFFIX;
        targetFilePath = subreportPath + ReportUtil.SUBREPORT_PREFIX + obj + ReportUtil.REPORT_SUFFIX;
    } else {
        sourceFilePath = ReportUtil.REPORT_PREFIX + reportCategory + ReportUtil.SUBREPORT_PREFIX + obj + ReportUtil.REPORT_DEFINITION_SUFFIX;
        targetFilePath = ReportUtil.REPORT_PREFIX + reportCategory + ReportUtil.SUBREPORT_PREFIX + obj + ReportUtil.REPORT_SUFFIX;
    }
    if (null == getResourceAsURL(targetFilePath)) {
        if (null != getResourceAsString(sourceFilePath))
            JasperCompileManager.compileReportToFile(getResourceAsString(sourceFilePath));
    }
    int timeSpentWhile = 0;
    while (isNotSubreportAvailable(getResourceAsURL(targetFilePath), timeSpentWhile)) {
        try {
            Thread.currentThread().sleep(SLEEP_TIME_IN_MILLIS_WHEN_CREATING_SUBREPORTS);
            timeSpentWhile += SLEEP_TIME_IN_MILLIS_WHEN_CREATING_SUBREPORTS;
        } catch (Exception ex) {
            this.throwError("Subreport waiter sleep exception", ex);
        }
    }
    subReport = (JasperReport) JRLoader.loadObject(getResourceAsURL(targetFilePath));
    obj = targetFilePath;
    return obj;
}
Also used : JasperReport(net.sf.jasperreports.engine.JasperReport) JRException(net.sf.jasperreports.engine.JRException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReportException(com.autentia.tnt.error.ReportException)

Aggregations

ReportException (com.autentia.tnt.error.ReportException)4 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 SQLException (java.sql.SQLException)3 JRException (net.sf.jasperreports.engine.JRException)3 JasperReport (net.sf.jasperreports.engine.JasperReport)3 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Principal (com.autentia.tnt.manager.security.Principal)1 ReportGeneratorStandardImpl (com.autentia.tnt.report.ReportGeneratorStandardImpl)1 ReportInfoBuilder (com.autentia.tnt.report.ReportInfo.ReportInfoBuilder)1 Connection (java.sql.Connection)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 IteratorEnumeration (org.apache.commons.collections.iterators.IteratorEnumeration)1 Session (org.hibernate.Session)1