Search in sources :

Example 1 with GlobalHoursReportBean

use of com.autentia.tnt.bean.activity.GlobalHoursReportBean in project TNTConcept by autentia.

the class ReportGeneratorStandardImpl method buildGlobalHoursReportJrxml.

private byte[] buildGlobalHoursReportJrxml(String reportCategory) {
    String reportHeaderFileName = ReportUtil.REPORT_PREFIX + reportCategory + ReportUtil.PARTIAL_REPORT_PREFIX + ReportUtil.GLOBAL_HOUR_REPORT_HEADER_FILE;
    String reportInterFileName = ReportUtil.REPORT_PREFIX + reportCategory + ReportUtil.PARTIAL_REPORT_PREFIX + ReportUtil.GLOBAL_HOUR_REPORT_INTER_FILE;
    String reportFooterFileName = ReportUtil.REPORT_PREFIX + reportCategory + ReportUtil.PARTIAL_REPORT_PREFIX + ReportUtil.GLOBAL_HOUR_REPORT_FOOTER_FILE;
    Reader jrxmlHeader = null;
    Reader jrxmlFooter = null;
    Reader jrxmlInter = null;
    ByteArrayOutputStream report = new ByteArrayOutputStream();
    byte[] output = null;
    int posX = GLOBAL_HOUR_REPORT_INITIAL_X_POS;
    int colNum = GLOBAL_HOUR_REPORT_ALWAYS_PRESENT_COLS_NUM;
    int pageWidth;
    StringBuilder columnHeader = new StringBuilder();
    StringBuilder detail = new StringBuilder();
    StringBuilder summary = new StringBuilder();
    StringBuilder vars = new StringBuilder();
    GlobalHoursReportBean ghReportBean = new GlobalHoursReportBean();
    List<User> users = ghReportBean.getUsers();
    colNum += users.size();
    pageWidth = colNum * (GLOBAL_HOUR_REPORT_COLUMN_WIDTH + GLOBAL_HOUR_REPORT_SPACE_BETWEEN_COLS);
    summary.append("<summary>");
    summary.append("<band height='25'  isSplitAllowed='true' >");
    for (int l = 0; l < users.size(); l++) {
        User user = users.get(l);
        columnHeader.append("<staticText><reportElement x='").append(posX).append("' y='5' width='").append(GLOBAL_HOUR_REPORT_COLUMN_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_HEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_HEADER_FIELD_KEY + l).append("'/>");
        columnHeader.append("<box></box><textElement textAlignment='Center' verticalAlignment='Middle'>").append("<font pdfFontName='Helvetica-Bold' size='14' isBold='true'/></textElement>");
        columnHeader.append("<text><![CDATA[").append(user.getName()).append("]]></text></staticText>");
        columnHeader.append("<staticText><reportElement x='").append(posX).append("' y='74' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_HOURS_FIELD_KEY + l).append("'/>");
        columnHeader.append("<box></box><textElement textAlignment='Center'>").append("<font /></textElement>");
        columnHeader.append("<text><![CDATA[Horas]]></text></staticText>");
        columnHeader.append("<staticText><reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH + GLOBAL_HOUR_REPORT_HOURS_COL_SPACE).append("' y='74' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("'").append(" key='").append(GLOBAL_HOUR_REPORT_COST_FIELD_KEY + l).append("'/>");
        columnHeader.append("<box></box><textElement textAlignment='Center'>").append("<font /></textElement>");
        columnHeader.append("<text><![CDATA[Coste (€)]]></text></staticText>");
        detail.append("<subreport isUsingCache='true'>");
        detail.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN).append("' y='8' ").append("width='").append(GLOBAL_HOUR_REPORT_SUBREPORT_WIDTH).append("' ").append("height='").append(GLOBAL_HOUR_REPORT_SUBREPORT_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_SUBREPORT_KEY + l).append("'/>");
        detail.append("<subreportParameter  name='eurosPerHour'>").append("<subreportParameterExpression><![CDATA[new java.lang.Double(").append(new Double(0)).append(")]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<subreportParameter  name='idUser'>").append("<subreportParameterExpression><![CDATA[new java.lang.Integer(").append(user.getId()).append(")]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<subreportParameter  name='idProject'>").append("<subreportParameterExpression><![CDATA[$F{project_id}]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<subreportParameter  name='startDate'>").append("<subreportParameterExpression><![CDATA[$P{Fecha inicio}]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<subreportParameter  name='endDate'>").append("<subreportParameterExpression><![CDATA[$P{Fecha fin}]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<subreportParameter  name='billable'>").append("<subreportParameterExpression><![CDATA[$V{billable}]]></subreportParameterExpression>").append("</subreportParameter>");
        detail.append("<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>");
        detail.append("<returnValue subreportVariable='Horas' toVariable='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_KEY + l).append("' calculation='Sum'/>");
        detail.append("<returnValue subreportVariable='Coste' toVariable='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_KEY + l).append("' calculation='Sum'/>");
        detail.append("<subreportExpression  class='java.lang.String'><![CDATA[$P{SUBREPORT_ACTIVITIES}]]></subreportExpression>");
        detail.append("</subreport>");
        summary.append("<textField isStretchWithOverflow='false' pattern='#,##0.00' isBlankWhenNull='true' evaluationTime='Now' hyperlinkType='None'  hyperlinkTarget='Self' >");
        summary.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN).append("' y='5' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_KEY + l).append("'/><box></box>");
        summary.append("<textElement></textElement>");
        summary.append("<textFieldExpression   class='java.lang.Double'><![CDATA[$V{").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_VAR + l).append("}]]></textFieldExpression></textField>");
        summary.append("<textField isStretchWithOverflow='false' pattern='#,##0.00' isBlankWhenNull='true' evaluationTime='Now' hyperlinkType='None'  hyperlinkTarget='Self' >");
        summary.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN + GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH + GLOBAL_HOUR_REPORT_HOURS_COL_SPACE).append("' y='5' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_KEY + l).append("'/><box></box>");
        summary.append("<textElement ></textElement>");
        summary.append("<textFieldExpression   class='java.lang.Double'><![CDATA[$V{").append(GLOBAL_HOUR_REPORT_TOTAL_COST_VAR + l).append("}]]></textFieldExpression></textField>");
        vars.append("<variable name='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_VAR + l).append("' class='java.lang.Double' resetType='Report' calculation='Sum'></variable>");
        vars.append("<variable name='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_VAR + l).append("' class='java.lang.Double' resetType='Report' calculation='Sum'></variable>");
        posX = posX + GLOBAL_HOUR_REPORT_COLUMN_WIDTH + GLOBAL_HOUR_REPORT_SPACE_BETWEEN_COLS;
    }
    columnHeader.append("<staticText><reportElement y='5' x='").append(posX).append("' key='staticText-3' ");
    columnHeader.append("width='").append(GLOBAL_HOUR_REPORT_COLUMN_WIDTH).append("' ").append("height='").append(GLOBAL_HOUR_REPORT_HEADER_HEIGHT).append("' />");
    columnHeader.append("<box></box><textElement textAlignment='Center' verticalAlignment='Middle'><font pdfFontName='Helvetica-Bold' size='14' isBold='true'/></textElement>");
    columnHeader.append("<text><![CDATA[TOTAL ]]></text></staticText>");
    columnHeader.append("<staticText><reportElement x='").append(posX).append("' y='74' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_HOURS_FIELD_KEY + users.size()).append("'/>");
    columnHeader.append("<box></box><textElement textAlignment='Center'>").append("<font /></textElement>");
    columnHeader.append("<text><![CDATA[Horas]]></text></staticText>");
    columnHeader.append("<staticText><reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH + GLOBAL_HOUR_REPORT_HOURS_COL_SPACE).append("' y='74' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("'").append(" key='").append(GLOBAL_HOUR_REPORT_COST_FIELD_KEY + users.size()).append("'/>");
    columnHeader.append("<box></box><textElement textAlignment='Center'>").append("<font /></textElement>");
    columnHeader.append("<text><![CDATA[Coste (€)]]></text></staticText>");
    detail.append("<subreport isUsingCache='true'>");
    detail.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN).append("' y='8' ").append("width='").append(GLOBAL_HOUR_REPORT_SUBREPORT_WIDTH).append("' ").append("height='").append(GLOBAL_HOUR_REPORT_SUBREPORT_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_SUBREPORT_KEY + users.size()).append("'/>");
    detail.append("<subreportParameter  name='idProject'>").append("<subreportParameterExpression><![CDATA[$F{project_id}]]></subreportParameterExpression>").append("</subreportParameter>");
    detail.append("<subreportParameter  name='billable'>").append("<subreportParameterExpression><![CDATA[$V{billable}]]></subreportParameterExpression>").append("</subreportParameter>");
    detail.append("<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>");
    detail.append("<returnValue subreportVariable='Horas' toVariable='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_KEY + users.size()).append("' calculation='Sum'/>");
    detail.append("<returnValue subreportVariable='Coste' toVariable='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_KEY + users.size()).append("' calculation='Sum'/>");
    detail.append("<subreportExpression  class='java.lang.String'><![CDATA[$P{SUBREPORT_TOTAL_COST}]]></subreportExpression>");
    detail.append("</subreport>");
    detail.append("<line direction='TopDown'>");
    detail.append("<reportElement x='2' y='1' height='0' key='line-1' width='").append((pageWidth - GLOBAL_HOUR_REPORT_SPACE_BETWEEN_COLS)).append("' />");
    detail.append("<graphicElement stretchType='NoStretch'/></line>");
    summary.append("<textField isStretchWithOverflow='false' pattern='#,##0.00' isBlankWhenNull='true' evaluationTime='Now' hyperlinkType='None'  hyperlinkTarget='Self' >");
    summary.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN).append("' y='5' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_KEY + users.size()).append("'/><box></box>");
    summary.append("<textElement ></textElement>");
    summary.append("<textFieldExpression   class='java.lang.Double'><![CDATA[$V{").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_VAR + users.size()).append("}]]></textFieldExpression></textField>");
    summary.append("<textField isStretchWithOverflow='false' pattern='#,##0.00' isBlankWhenNull='true' evaluationTime='Now' hyperlinkType='None'  hyperlinkTarget='Self' >");
    summary.append("<reportElement x='").append(posX + GLOBAL_HOUR_REPORT_SUBREPORT_X_MARGIN + GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH + GLOBAL_HOUR_REPORT_HOURS_COL_SPACE).append("' y='5' width='").append(GLOBAL_HOUR_REPORT_SUBHEADER_WIDTH).append("' height='").append(GLOBAL_HOUR_REPORT_SUBHEADER_HEIGHT).append("' ").append("key='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_KEY + users.size()).append("'/><box></box>");
    summary.append("<textElement ></textElement>");
    summary.append("<textFieldExpression   class='java.lang.Double'><![CDATA[$V{").append(GLOBAL_HOUR_REPORT_TOTAL_COST_VAR + users.size()).append("}]]></textFieldExpression></textField>");
    summary.append("</band>");
    summary.append("</summary>");
    summary.append("</jasperReport>");
    vars.append("<variable name='").append(GLOBAL_HOUR_REPORT_TOTAL_HOURS_VAR + users.size()).append("' class='java.lang.Double' resetType='Report' calculation='Sum'></variable>");
    vars.append("<variable name='").append(GLOBAL_HOUR_REPORT_TOTAL_COST_VAR + users.size()).append("' class='java.lang.Double' resetType='Report' calculation='Sum'></variable>");
    try {
        pageWidth = (pageWidth < REPORT_A4_LANDSCAPE_MIN_WIDTH) ? REPORT_A4_LANDSCAPE_MIN_WIDTH : pageWidth;
        Hashtable<String, String> replacements = new Hashtable<String, String>();
        replacements.put("@PAGE_WIDTH@", String.valueOf(pageWidth));
        replacements.put("@COL_COUNT@", String.valueOf(colNum));
        replacements.put("@BG_WIDTH@", String.valueOf(pageWidth - 4));
        replacements.put("@DynamicVars@", vars.toString());
        reportHeaderFileName = getResourceAsURL(reportHeaderFileName).getFile();
        reportFooterFileName = getResourceAsURL(reportFooterFileName).getFile();
        reportInterFileName = getResourceAsURL(reportInterFileName).getFile();
        jrxmlHeader = new FileReader(reportHeaderFileName);
        jrxmlFooter = new FileReader(reportFooterFileName);
        jrxmlInter = new FileReader(reportInterFileName);
        // out = new PrintWriter(report);
        sendData(jrxmlHeader, report, replacements);
        sendText(columnHeader.toString(), report);
        sendData(jrxmlInter, report, replacements);
        sendText(detail.toString(), report);
        sendData(jrxmlFooter, report, replacements);
        sendText(summary.toString(), report);
        output = report.toByteArray();
    } catch (Exception t) {
        log.error("Error generando JRXML de parte de horas global", t);
    } finally {
        if (jrxmlHeader != null) {
            try {
                jrxmlHeader.close();
            } catch (IOException e) {
                log.error("Error cerrando fichero de cabecera", e);
            }
        }
        if (jrxmlFooter != null) {
            try {
                jrxmlFooter.close();
            } catch (IOException e) {
                log.error("Error cerrando fichero de pie", e);
            }
        }
        if (jrxmlInter != null) {
            try {
                jrxmlInter.close();
            } catch (IOException e) {
                log.error("Error cerrando fichero intermedio", e);
            }
        }
        if (report != null) {
            try {
                report.close();
            } catch (IOException e) {
                log.error("Error cerrando report", e);
            }
        }
    }
    return output;
}
Also used : User(com.autentia.tnt.businessobject.User) GlobalHoursReportBean(com.autentia.tnt.bean.activity.GlobalHoursReportBean) Hashtable(java.util.Hashtable) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JRException(net.sf.jasperreports.engine.JRException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReportException(com.autentia.tnt.error.ReportException) FileReader(java.io.FileReader)

Example 2 with GlobalHoursReportBean

use of com.autentia.tnt.bean.activity.GlobalHoursReportBean in project TNTConcept by autentia.

the class GlobalHoursReportCSVServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String pStartDate = req.getParameter("startDate");
    String pEndDate = req.getParameter("endDate");
    String pBillable = req.getParameter("billable");
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    StringBuilder csvReport = new StringBuilder();
    GlobalHoursReportBean ghReportBean = (GlobalHoursReportBean) req.getSession().getAttribute("globalHoursReportBean");
    PrintWriter writer = resp.getWriter();
    try {
        Date startDate = sdf.parse(pStartDate);
        Date endDate = sdf.parse(pEndDate);
        boolean billable = Boolean.valueOf(pBillable);
        ghReportBean.setEndDate(endDate);
        ghReportBean.setStartDate(startDate);
        ghReportBean.setBillable(billable);
        List<User> users = ghReportBean.getUsers();
        List<GlobalHourReport> globalHourReports = ghReportBean.getAll();
        csvReport.append(getCSVHeader(users));
        csvReport.append(getCSVBody(globalHourReports));
        csvReport.append(getCSVFooter(users, globalHourReports));
        resp.setContentType("text/csv");
        resp.setContentLength(csvReport.length());
        writer.append(csvReport.toString());
    } catch (ParseException e) {
        Log.error("Error en el parseo ", e);
    } finally {
        writer.close();
    }
}
Also used : User(com.autentia.tnt.businessobject.User) GlobalHourReport(com.autentia.tnt.businessobject.GlobalHourReport) GlobalHoursReportBean(com.autentia.tnt.bean.activity.GlobalHoursReportBean) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Aggregations

GlobalHoursReportBean (com.autentia.tnt.bean.activity.GlobalHoursReportBean)2 User (com.autentia.tnt.businessobject.User)2 GlobalHourReport (com.autentia.tnt.businessobject.GlobalHourReport)1 ReportException (com.autentia.tnt.error.ReportException)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 JRException (net.sf.jasperreports.engine.JRException)1