Showing posts with label write. Show all posts
Showing posts with label write. Show all posts

Monday, October 3, 2016

Write to File from init Bean ADF

  
Make sure you execute the view object & refresh the iterator before generate Print


  public void beforeGeneratePrint() {
        DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iter = bindings.findIteratorBinding("DynamicVO1Iterator");
        iter.executeQuery();

        System.out.println("get Estimated Row Count" + iter.getEstimatedRowCount());
}
---------------------------------------------------------------------------------------------------------------

  public void generatePrint() {
        FileOutputStream fop = null;
        File file;

        try {

          // file = new File("C://FileStore//quarter3Rpt.html");
            file = new File("//home//myapp//Attachments//quarter3Rpt.html");


       

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }


            Map pageFlowScope = AdfFacesContext.getCurrentInstance().getPageFlowScope();
            Map sessionScope = ADFContext.getCurrent().getSessionScope();
            try {

                FileOutputStream fos = new FileOutputStream(file);
           
                OutputStreamWriter w = new OutputStreamWriter(fos);
                w.write("<html>");
                w.write("<head>");
                w.write("<style type=text/css>td{ font-family: Helvetica;font-SIZE: 11; } tr{ height: 18px; }</style>");
                w.write("</head>");
                w.write("<body>");
                //            w.write("<button onclick=\"myFunction()\">Print this page</button>");
                //            w.write("<script> function myFunction() { window.print();}</script>");
                w.write("<table>");
                w.write("<tr HEIGHT=18>");
                w.write("<td BGCOLOR=\"#FFFFCC\" COLSPAN=12 ALIGN=CENTER><b> " + this.getRptDesc() + "</b></td></tr>");

                //                      w.write("<td COLSPAN=6 ALIGN=RIGHT>Report Date: " +
                //                              new SimpleDateFormat("MM/dd/yyyy HH:mm").format(new Date()) + "</td></tr>");

                w.write("<tr HEIGHT=18> <td  BGCOLOR=\"#FFFFCC\" COLSPAN=12 ALIGN=CENTER><b>  Company :" +
                        pageFlowScope.get("company") + " " + "Currency :" + sessionScope.get("currencyCode") +
                        " </b></td></tr>");
                w.write("<tr HEIGHT=18> <td COLSPAN=12 BGCOLOR=\"#FFFFCC\" ALIGN=CENTER><b> Period ending: " +
                        this.getPeriod() + "</b></td></tr>");

                w.write("<tr><td COLSPAN=12 BGCOLOR=\"#FFFFCC\" ALIGN=CENTER><b>Report Run Date: " +
                        new SimpleDateFormat("MM/dd/yyyy HH:mm").format(new Date()) + "</b></td></tr>");


                //            HSSFWorkbook workbook = new HSSFWorkbook();
                //            HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

                DCBindingContainer bindings =
                    (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
                DCIteratorBinding dcIteratorBindings = bindings.findIteratorBinding("DynamicVO1Iterator");
                // HSSFRow excelrow = null;
                // Get all the rows of a iterator

                oracle.jbo.Row[] rows = dcIteratorBindings.getAllRowsInRange();

                int i = 0;


                for (oracle.jbo.Row row : rows) {
                    //print header on first row in excel
                    if (i == 0) {
                        //  excelrow = (HSSFRow) worksheet.createRow((short) i);
                        short j = 0;
                        w.write("<tr HEIGHT=21>");
                        for (String colName : row.getAttributeNames()) {
                            // HSSFCell cellA1 = excelrow.createCell((short) j);
                            if (!(colName.equalsIgnoreCase("SORT_ORDER") || colName.equalsIgnoreCase("P1"))) {
                                if (colName.contains("_")) {
                                    colName = colName.replace("_", " ");
                                    if (colName.toUpperCase().contains("PERCENT")) {
                                        colName = colName.toUpperCase().replace("PERCENT", "%");
                                    }
                                    if (j < 2 || colName.equalsIgnoreCase("ACCOUNT_DESCRIPTION")) {
                                        w.write("<th ALIGN=LEFT BGCOLOR=\"#99CCCC\"><FONT COLOR=BLACK FACE=\"verdana\" SIZE=1 > " +
                                                colName + " </th>");
                                    } else {
                                        w.write("<th ALIGN=RIGHT BGCOLOR=\"#99CCCC\"><FONT COLOR=BLACK FACE=\"verdana\" SIZE=1 > " +
                                                colName + " </th>");
                                    }
                                    //  cellA1.setCellValue(colName);
                                } else {
                                    // cellA1.setCellValue(colName);
                                    if (j < 2 || colName.equalsIgnoreCase("ACCOUNT_DESCRIPTION")) {
                                        w.write("<th ALIGN=LEFT BGCOLOR=\"#99CCCC\"><FONT COLOR=BLACK FACE=\"verdana\" SIZE=1 > " +
                                                colName + " </th>");
                                    } else {
                                        w.write("<th ALIGN=RIGHT BGCOLOR=\"#99CCCC\"><FONT COLOR=BLACK FACE=\"verdana\" SIZE=1 > " +
                                                colName + " </th>");
                                    }
                                }

                            }
                            j++;
                        }
                    }
                    w.write("</tr>");
                    //print data from second row in excel
                    ++i;
                    short j = 0;

                    w.write("<tr HEIGHT=18>");
                    for (String colName : row.getAttributeNames()) {


                        if (!(colName.equalsIgnoreCase("SORT_ORDER") || colName.equalsIgnoreCase("P1"))) {
                            if (row.getAttribute(colName) != null) {


                                if (j < 2 || colName.equalsIgnoreCase("ACCOUNT_DESCRIPTION")) {
                                    if (row.getAttribute(colName).toString().contains("_")) {


                                        w.write("<td  BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                row.getAttribute(colName).toString().replaceAll("_", " ") + "</td>");
                                    } else if (colName.equalsIgnoreCase("ACCOUNT")) {
                                        //numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "US"));
                                        //  System.out.println("row.getAttribute(colName).toString()" +
                                        //   numberFormatter.format(row.getAttribute(colName)).toString());
                                        //  Integer amountOut = new Integer(numberFormatter.format(Integer.parseInt(row.getAttribute(colName).toString())));
                                        System.out.println("this is the number" + row.getAttribute(colName));
                                        w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=LEFT>" + row.getAttribute(colName) +
                                                "</td>");
                                    } else {
                                        w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=LEFT>" + row.getAttribute(colName) +
                                                "</td>");
                                    }
                                }


                                else {

                                    // make sure the description doesnm't have any "_"
                                    if (row.getAttribute(colName).toString().contains("_")) {


                                        w.write("<td  BGCOLOR=\"#FFFFCC\" ALIGN=LEFT>" +
                                                row.getAttribute(colName).toString().replaceAll("_", " ") + "</td>");
                                    }

                                    // check if the String is a number; If so set the locale for positive no and for negative no. remove the - sign and enclose with in "( )"
                                    //For decimal numbers don't format it
                                    else if (this.isNumber(row.getAttribute(colName).toString().substring(1))) {
                                        if (row.getAttribute(colName).toString().contains("-")) {
                                            numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "US"));
                                            //  System.out.println("------------------------------------" +
                                            //           numberFormatter.format(row.getAttribute(colName)).toString());
                                            //  Integer amountOut = new Integer(numberFormatter.format(Integer.parseInt(row.getAttribute(colName).toString())));
                                            try {
                                                w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                        numberFormatter.format(row.getAttribute(colName)).toString().replace("-",
                                                                                                                             "(") +
                                                        ")" + "</td>");

                                            } catch (Exception e) {
                                                w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                        row.getAttribute(colName).toString().replace("-", "(") + ")" +
                                                        "</td>");
                                            }

                                        } else {
                                            if (colName.equalsIgnoreCase("ACCOUNT")) {
                                                //numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "US"));
                                                //  System.out.println("row.getAttribute(colName).toString()" +
                                                //   numberFormatter.format(row.getAttribute(colName)).toString());
                                                //  Integer amountOut = new Integer(numberFormatter.format(Integer.parseInt(row.getAttribute(colName).toString())));
                                                System.out.println("this is the number" + row.getAttribute(colName));
                                                w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=LEFT>" +
                                                        row.getAttribute(colName) + "</td>");
                                            }


                                            else {
                                                numberFormatter =
                                                    NumberFormat.getNumberInstance(new Locale("en", "US"));
                                                //System.out.println("row.getAttribute(colName).toString()" +row.getAttribute(colName).toString());
                                                try {

                                                    w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                            numberFormatter.format(row.getAttribute(colName)).toString() +
                                                            "</td>");
                                                } catch (Exception e) {
                                                    System.out.println("ctch blockkkkkkk" +
                                                                       row.getAttribute(colName).toString());

                                                    w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                            row.getAttribute(colName).toString() + "</td>");
                                                }
                                            }
                                        }
                                        //  cell.setCellValue("(" + row.getAttribute(colName).toString() + ")");

                                    }

                                    //In all other cases print the value as is
                                    else {
                                        if (row.getAttribute(colName).toString().contains(".")) {

                                            if (row.getAttribute(colName).toString().contains("-")) {
                                                numberFormatter =
                                                    NumberFormat.getNumberInstance(new Locale("en", "US"));

                                                w.write("<td   BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                        numberFormatter.format(row.getAttribute(colName)).toString().replace("-",
                                                                                                                             "(") +
                                                        ")" + "</td>");
                                            } else {
                                                w.write("<td  BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                        row.getAttribute(colName).toString() + "</td>");
                                            }
                                        } else {

                                            w.write("<td  BGCOLOR=\"#FFFFCC\" ALIGN=RIGHT>" +
                                                    row.getAttribute(colName).toString() + "</td>");
                                            // cell.setCellValue(row.getAttribute(colName).toString());
                                        }
                                    }
                                }
                            } else {
                                w.write("<td  BGCOLOR=\"#FFFFCC\" ALIGN=LEFT>" + " " + "</td>");
                                //cell.setCellValue("");
                            }
                        }


                        //                    System.out.println("colName " + colName + "row.getAttribute(colName).toString()" +
                        //                                       row.getAttribute(colName));


                        j++;
                    }

                    w.write("</tr>");

                }

                w.write("</table>");
                w.write("</body>");
                w.write("</html>");
                w.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }