Tuesday, June 9, 2015


Before jasper report call only insert value and return page show :
    public void saveCustomerDataInfo(){
       
        System.out.println("select call data:");
        UserBlockDao userBlockDao = new UserBlockDao();
        String stopDate = "";
        String fromDate = "";
        if(status=="S"){
            stopDate = statusWiseDate;
        } else {
            fromDate = statusWiseDate;
        }
      userBlockDao.userBlogSubmit(areaExecId,regById,name,userId,userPass,areaId,stopDate,fromDate,status,previousId,previousName,instructionBy,instructionName,designation,dateWithTime,remarks);
       
             return "saveCustomerDataInfo";
       
    }
   
After jasper report call only insert value and Not return page show :
    public void saveCustomerDataInfo(){
       
        System.out.println("select call data:");
        UserBlockDao userBlockDao = new UserBlockDao();
        String stopDate = "";
        String fromDate = "";
        if(status=="S"){
            stopDate = statusWiseDate;
        } else {
            fromDate = statusWiseDate;
        }
        int processId = userBlockDao.userBlogSubmit(areaExecId,regById,name,userId,userPass,areaId,stopDate,fromDate,status,previousId,previousName,instructionBy,instructionName,designation,dateWithTime,remarks);
       
        try {
                genInvoicePDF("processId", String.valueOf(processId),"userStatus",status);
            } catch (JRException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

       
        //return "saveCustomerDataInfo";
       
    }

///////////////////////////////////
query ( int use  instead of void and return[return processId;] use instead of not return) :
 public int insertCustomerInforma(String areaExecId,String regById,String name,String userId,String userPass,String areaId,String stopDate,String fromDate,String status,String previousId,String previousName,String instructionBy,String instructionName,String designation,String dateWithTime,String remarks){
      
        System.out.println("kaniz");
        boolean fg = true;
        con = dbc.connectDB();

         if (con == null) {
             fg = false;
         }
    
    if (fg) {
     try {

         int processId = 0;

      
            st = con.createStatement();
            String unikProcessId = "select Nvl(max(process_id),0)+1 processId from ms_system_user_process";
            rs = st.executeQuery(unikProcessId);
            while(rs.next()) {
                 processId = rs.getInt("processId");
            }
          
             st = con.createStatement();
             query = "Insert into MS_SYSTEM_USER_PROCESS(PROCESS_ID, AREA_EXEC_ID, REQ_BY_ID, EXECUTIVE_NAME, USER_ID,USER_PASS, AREA_ID, START_FROM_DT, STOP_FROM_DT,"
                           + "USER_NEW_REPLACEMENT_STATUS,PRE_REQ_BY_ID, PRE_EXECUTIVE_NAME, INSTRUCTION_BY, "
                           + "INSTRUCTION_BY_NAME, DESIGNATION,INSTRUCTION_DATE_TIME, "
                           + "REMARKS)Values("+processId+", '"+areaExecId+"', '"+regById+"', '"+name+"', '"+userId+"', '"+userPass+"', '"+areaId+"', TO_DATE('"+fromDate+"', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('"+stopDate+"', 'MM/DD/YYYY HH24:MI:SS'), '"+status+"', '"+previousId+"', '"+previousName+"', '"+instructionBy+"', '"+instructionName+"', '"+designation+"', TO_DATE('"+dateWithTime+"', 'MM/DD/YYYY HH24:MI:SS'), '"+remarks+"')";
                    System.out.println("Query" + query);
                    st.executeUpdate(query);
          
    } catch (SQLException sq) {       
      sq.printStackTrace();
    } finally {
      try {
          if (rs != null) {
              rs.close();
          }
    
          con.close();
      } catch (SQLException ex) {
          ex.printStackTrace();
      }
    }
    }
  

   return processId;
  
           // return "getBankInfoPageReturn";  //  page return not query return.Remember insert query notreturn;.Only select query return List.
   }

/////////////////////////////////////////////////////////////////////
    
public void genInvoicePDF(String key, String processId,String key2, String status) throws JRException, ServletException, IOException {
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put(key, processId);
            parameters.put(key2, status);
            AllReports allReports = new AllReports();
            allReports.showReport(processId,parameters,"stopUser.jasper", "pdf");
    }


AllReports.java:

package com.drug.billing.userblock.action.utility;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.drug.billing.userblock.database.DatabaseConnection;


public class AllReports {
    private DatabaseConnection dbc = new DatabaseConnection();
   
    public  void printReport(String accID, String filename){
             Connection con= dbc.connectDB();
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("regNo", accID);
            try {
                ServletContext context = ServletActionContext.getServletContext();
                String path = context.getRealPath("/");
                String printFileName = JasperFillManager.fillReportToFile(path + "/Jasper"+ "/" + filename,parameters,con);
                JasperPrintManager.printReport(printFileName, false);
            } catch (JRException e) {
                e.printStackTrace();
            }
    }
   
   
    public  void showReport(String key,Map<String, String> parameters, String filename, String reportType)
            throws JRException, ServletException, IOException {

        Connection connection = null;
        HttpServletResponse response = ServletActionContext.getResponse();
        String outputFileName = "report_" + parameters.get(key);


/*        parameters.put("regNo", accID);*/
        connection = dbc.connectDB();

        ServletContext context = ServletActionContext.getServletContext();
        String path = context.getRealPath("/");// .getContextPath();

        JasperPrint jasperPrint = JasperFillManager.fillReport(path + "/Jasper"+ "/" + filename, parameters, connection);
        System.out.println("Report Created");

        OutputStream ouputStream = response.getOutputStream();

        JRExporter exporter = null;

        if ("pdf".equalsIgnoreCase(reportType)) {

            response.setContentType("application/pdf");
            // If you want show the report in the browser then write
            // “inline�
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            // If you want show the report out of the browser as a .pdf format
            // then write “attachment�
            // response.setHeader(“Content-disposition�,
            // “attachment;filename=�+outputFileName+�.�+reportType);
            exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                    ouputStream);

        } else if ("rtf".equalsIgnoreCase(reportType)) {

            response.setContentType("application/rtf");
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            exporter = new JRRtfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                    ouputStream);

        } else if ("html".equalsIgnoreCase(reportType)) {

            exporter = new JRHtmlExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                    ouputStream);

        } else if ("xls".equalsIgnoreCase(reportType)) {

            response.setContentType("application/xls");
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            exporter = new JRXlsExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                    ouputStream);

        } else if ("csv".equalsIgnoreCase(reportType)) {

            response.setContentType("application/xls");
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            exporter = new JRCsvExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                    ouputStream);
        }

        try {
            exporter.exportReport();

        } catch (JRException e) {
            throw new ServletException(e);
        } finally {
            if (ouputStream != null) {
                try {
                    ouputStream.close();
                } catch (IOException ex) {
                }
            }
        }
    }
   
   
    public  void showPDFReport( String filename, String reportType)
            throws JRException, ServletException, IOException {
        Connection connection = null;
        HttpServletResponse response = ServletActionContext.getResponse();
        String outputFileName = "PolioceReport";

        Map<String, String> parameters = new HashMap<String, String>();

        connection = dbc.connectDB();

        ServletContext context = ServletActionContext.getServletContext();
        String path = context.getRealPath("/");// .getContextPath();
        System.out.println(path);
        JasperPrint jasperPrint = JasperFillManager.fillReport(path + "/Jasper"+ "/" + filename, parameters, connection);
       
        OutputStream ouputStream = response.getOutputStream();
        OutputStream out2 = null;
        out2 = new FileOutputStream(new File(path += "Jasper\\"+filename+".docx"));
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        try {
            JRExporter exporter = null;
   
            response.setContentType("application/docx");
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            exporter = new JRDocxExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,byteArrayOutputStream2);
            exporter.exportReport();
             out2.write(byteArrayOutputStream2.toByteArray());
                out2.flush();
                out2.close();

        } catch(JRException e){
            throw new ServletException(e);
        } finally {
            if (ouputStream != null) {
                try {
                    ouputStream.close();
                } catch (IOException ex) {
                }
            }
        }
    }
   
   
    public  void showPDFReport(String accID, String filename, String reportType)
            throws JRException, ServletException, IOException {
        Connection connection = null;
        HttpServletResponse response = ServletActionContext.getResponse();
        String outputFileName = "PolioceReport";

        Map<String, String> parameters = new HashMap<String, String>();

        parameters.put("regNo", accID);
        connection = dbc.connectDB();

        ServletContext context = ServletActionContext.getServletContext();
        String path = context.getRealPath("/");// .getContextPath();
        System.out.println(path);
        JasperPrint jasperPrint = JasperFillManager.fillReport(path + "/Jasper"+ "/" + filename, parameters, connection);
       
        OutputStream ouputStream = response.getOutputStream();
        OutputStream out2 = null;
        out2 = new FileOutputStream(new File("C:\\Jasper\\"+accID+".docx"));
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        try {
            JRExporter exporter = null;
   
            response.setContentType("application/docx");
            response.setHeader("Content-disposition", "inline;filename="
                    + outputFileName + "." + reportType);
            exporter = new JRDocxExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,byteArrayOutputStream2);
            exporter.exportReport();
             out2.write(byteArrayOutputStream2.toByteArray());
                out2.flush();
                out2.close();

        } catch(JRException e){
            throw new ServletException(e);
        } finally {
            if (ouputStream != null) {
                try {
                    ouputStream.close();
                } catch (IOException ex) {
                }
            }
        }
    }
   
    public static void genWordToPDF(String vbsPath, String docPath) {
       
        try {
            Process process = Runtime.getRuntime().exec("cscript "+vbsPath+" "+docPath);
            process.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }
       
    }

}


No comments:

Post a Comment