Example usage for org.joda.time Weeks weeksBetween

List of usage examples for org.joda.time Weeks weeksBetween

Introduction

In this page you can find the example usage for org.joda.time Weeks weeksBetween.

Prototype

public static Weeks weeksBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Weeks representing the number of whole weeks between the two specified partial datetimes.

Usage

From source file:co.malm.mglam_reads.backend.util.DateDiffUtil.java

License:Apache License

/**
 * Calculates time differences using two dates, and stores it into a
 * {@linkplain java.util.Map} object./*w  ww .j a v a 2  s. co  m*/
 *
 * @param map calculations data
 * @param dt1 first date for diff
 * @param dt2 second date for diff
 * @see org.joda.time.DateTime
 * @see org.joda.time.Years
 * @see org.joda.time.Months
 * @see org.joda.time.Weeks
 * @see org.joda.time.Days
 * @see org.joda.time.Hours
 * @see org.joda.time.Minutes
 * @see org.joda.time.Seconds
 */
private void calculate(HashMap<String, Integer> map, DateTime dt1, DateTime dt2) {

    final int SECONDS_IN_MINUTE = 60;
    final int MINUTES_IN_HOUR = 60;
    final int HOURS_IN_DAY = 24;
    final int DAYS_IN_MONTH = 31;
    final int MONTHS_IN_YEAR = 12;
    final int WEEKS_IN_DAY = 7;

    int diffYears = Years.yearsBetween(dt1, dt2).getYears();
    if (diffYears > 0) {
        map.put("years", diffYears);
    }

    int diffMonths = Months.monthsBetween(dt1, dt2).getMonths();
    if (diffMonths >= 1 && diffMonths < MONTHS_IN_YEAR) {
        map.put("months", diffMonths);
    }

    int diffWeeks = Weeks.weeksBetween(dt1, dt2).getWeeks();
    if (diffWeeks >= 1 && diffWeeks < WEEKS_IN_DAY) {
        map.put("weeks", diffWeeks);
    }

    int diffDays = Days.daysBetween(dt1, dt2).getDays();
    if (diffDays >= 1 && diffDays < DAYS_IN_MONTH) {
        map.put("days", diffDays);
    }

    int diffHours = Hours.hoursBetween(dt1, dt2).getHours();
    if (diffHours >= 1 && diffHours < HOURS_IN_DAY) {
        map.put("hours", diffHours);
    }

    int diffMinutes = Minutes.minutesBetween(dt1, dt2).getMinutes();
    if (diffMinutes >= 1 && diffMinutes < MINUTES_IN_HOUR) {
        map.put("minutes", diffMinutes);
    }

    int diffSeconds = Seconds.secondsBetween(dt1, dt2).getSeconds();
    if (diffSeconds >= 1 && diffSeconds < SECONDS_IN_MINUTE) {
        map.put("seconds", diffSeconds);
    }
}

From source file:code.excelreport.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {/*from   ww  w  . ja  va  2  s  .  co m*/
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' ) ";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and (id not like '%_weekly%' )  ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "')  and (id not like '%_weekly%' ) ";

        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,cur.testing_target_child,cur.testing_target_adult,cur.testing_target_total,cur.test_child,cur.test_adult,cur.test_total,cur.tested_perc_child,cur.tested_perc_adult,cur.tested_perc_all,cur.tested_cmts,cur.hiv_pos_target_child,cur.hiv_pos_target_adult,cur.hiv_pos_target_total,cur.hiv_pos_child,cur.hiv_pos_adult,cur.hiv_pos_total,cur.hiv_pos_yield_perc_child,cur.hiv_pos_yield_perc_adult,cur.hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,cur.new_care_child,cur.new_care_adult,cur.new_care_total,cur.hiv_pos_care_perc_child,cur.hiv_pos_care_perc_adult,cur.hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,cur.new_art_target_child,cur.new_art_target_adult,cur.new_art_target_total,cur.started_art_child,cur.started_art_adult,cur.started_art_total,cur.started_art_perc_child,cur.started_art_perc_adult,cur.started_art_perc_all,cur.started_art_cmts,cur.viral_load_target_child,cur.viral_load_target_adult,cur.viral_load_target_total,cur.viral_load_done_child,cur.viral_load_done_adult,cur.viral_load_done_total,cur.viral_test_perc_child,cur.viral_test_perc_adult,cur.viral_test_perc_all,cur.viral_test_cmts,cur.ipt_target_child,cur.ipt_target_adult,cur.ipt_target_total,cur.ipt_child,cur.ipt_adult,cur.ipt_total,cur.ipt_done_perc_child,cur.ipt_done_perc_adult,cur.ipt_done_perc_all,cur.ipt_done_cmts,cur.pmtct_hiv_pos_target,cur.pmtct_hiv_pos,cur.pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,cur.eid_target,cur.eid_done,cur.eid_done_perc,cur.eid_done_cmts,cur.viral_load_mothers_target,cur.viral_load_mothers_done,cur.viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join facility on cur.facilityname=facility.facility_name where  "
                + where + " "
                + " and not exists ( select * from weekly_data_new high join facility on high.facilityname=facility.facility_name  where high.facilityname = cur.facilityname and high.enddate > cur.enddate and "
                + where + ")";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {
                        ce.setCellValue(conn.rs.getString(a));
                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // write it as an excel attachment
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            wb.write(outByteStream);
            byte[] outArray = outByteStream.toByteArray();
            response.setContentType("application/ms-excel");
            response.setContentLength(outArray.length);
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename=MOIS_Cum_Report_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

        } catch (SQLException ex) {
            Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (InvalidFormatException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:code.excelreport_cumulative.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {/*from  w  w  w .  ja  va2 s  .c om*/
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' )";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and cur.id  like '%_weekly%' ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "') and weekly_data_new.id  like '%_weekly%'  ";

        String period = "1";
        String yr = enddate.substring(0, 4);
        String tar[] = enddate.split("-");

        int mwaka = new Integer(yr);
        if (new Integer(tar[1]) >= 10 && new Integer(tar[1]) <= 12) {
            mwaka = mwaka + 1;
        }
        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,case when  ROUND(targets.testing_target_child/"
                + period + ") >0 then ROUND(targets.testing_target_child/" + period
                + ") else 1 end as testing_target_child,case when  ROUND(targets.testing_target_adult/" + period
                + ") >0 then ROUND(targets.testing_target_adult/" + period
                + ") else 1 end as testing_target_adult,case when  ROUND(targets.testing_target_total/" + period
                + ") >0 then ROUND(targets.testing_target_total/" + period
                + ") else 1 end as testing_target_total,SUM(cur.test_child) as test_child,SUM(cur.test_adult) as test_adult,SUM(cur.test_total) as test_total,AVG(cur.tested_perc_child) as tested_perc_child,AVG(cur.tested_perc_adult) as tested_perc_adult,AVG(cur.tested_perc_all) as tested_perc_all,cur.tested_cmts,case when  ROUND(targets.hiv_pos_target_child/"
                + period + ") >0 then ROUND(targets.hiv_pos_target_child/" + period
                + ") else 1 end as hiv_pos_target_child,case when  ROUND(targets.hiv_pos_target_adult/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_adult/" + period
                + ") else 1 end as hiv_pos_target_adult,case when  ROUND(targets.hiv_pos_target_total/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_total/" + period
                + ") else 1 end as hiv_pos_target_total,SUM(cur.hiv_pos_child) as hiv_pos_child,SUM(cur.hiv_pos_adult) as hiv_pos_adult,SUM(cur.hiv_pos_total) as hiv_pos_total,AVG(cur.hiv_pos_yield_perc_child) as hiv_pos_yield_perc_child,AVG(cur.hiv_pos_yield_perc_adult) as hiv_pos_yield_perc_adult,AVG(cur.hiv_pos_yield_perc_all) as hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,SUM(cur.new_care_child) as new_care_child,SUM(cur.new_care_adult) as new_care_adult,SUM(cur.new_care_total) as new_care_total,AVG(cur.hiv_pos_care_perc_child) as hiv_pos_care_perc_child,AVG(cur.hiv_pos_care_perc_adult) as hiv_pos_care_perc_adult,AVG(cur.hiv_pos_care_perc_all) as hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,case when  ROUND(targets.new_art_target_child/"
                + period + ") >0 then ROUND(targets.new_art_target_child/" + period
                + ") else 1 end as new_art_target_child,case when  ROUND(targets.new_art_target_adult/" + period
                + ") >0 then ROUND(targets.new_art_target_adult/" + period
                + ") else 1 end as new_art_target_adult,case when  ROUND(targets.new_art_target_total/" + period
                + ") >0 then ROUND(targets.new_art_target_total/" + period
                + ") else 1 end as new_art_target_total,SUM(cur.started_art_child) as started_art_child,SUM(cur.started_art_adult) as started_art_adult,SUM(cur.started_art_total) as started_art_total,AVG(cur.started_art_perc_child) as started_art_perc_child,AVG(cur.started_art_perc_adult) as started_art_perc_adult, AVG(cur.started_art_perc_all) as started_art_perc_all,cur.started_art_cmts,case when  ROUND(targets.viral_load_target_child/"
                + period + ") >0 then ROUND(targets.viral_load_target_child/" + period
                + ") else 1 end as viral_load_target_child,case when  ROUND(targets.viral_load_target_adult/"
                + period + ") >0 then ROUND(targets.viral_load_target_adult/" + period
                + ") else 1 end as viral_load_target_adult,case when  ROUND(targets.viral_load_target_total/"
                + period + ") >0 then ROUND(targets.viral_load_target_total/" + period
                + ") else 1 end as viral_load_target_total,SUM(cur.viral_load_done_child) as viral_load_done_child,SUM(cur.viral_load_done_adult) as viral_load_done_adult, SUM(cur.viral_load_done_total) as viral_load_done_total,AVG(cur.viral_test_perc_child) as viral_test_perc_child,AVG(cur.viral_test_perc_adult) as viral_test_perc_adult, AVG(cur.viral_test_perc_all) as viral_test_perc_all,cur.viral_test_cmts,case when  ROUND(targets.ipt_target_child/"
                + period + ") >0 then ROUND(targets.ipt_target_child/" + period
                + ") else 1 end as ipt_target_child,case when  ROUND(targets.ipt_target_adult/" + period
                + ") >0 then ROUND(targets.ipt_target_adult/" + period
                + ") else 1 end as ipt_target_adult,case when  ROUND(targets.ipt_target_total/" + period
                + ") >0 then ROUND(targets.ipt_target_total/" + period
                + ") else 1 end as ipt_target_total,SUM(cur.ipt_child) as ipt_child,SUM(cur.ipt_adult) as ipt_adult ,SUM(cur.ipt_total) as ipt_total,AVG(cur.ipt_done_perc_child) as ipt_done_perc_child,AVG(cur.ipt_done_perc_adult) as ipt_done_perc_adult, AVG(cur.ipt_done_perc_all) as ipt_done_perc_all,cur.ipt_done_cmts,case when ROUND(targets.pmtct_hiv_pos_target/"
                + period + ") >0 then ROUND(targets.pmtct_hiv_pos_target/" + period
                + ") else 1 end as pmtct_hiv_pos_target,SUM(cur.pmtct_hiv_pos) as pmtct_hiv_pos,AVG(cur.pmtct_hiv_pos_perc) as pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,case when  ROUND(targets.eid_target/"
                + period + ") >0 then ROUND(targets.eid_target/" + period
                + ") else 1 end as eid_target,SUM(cur.eid_done) as eid_done,AVG(cur.eid_done_perc) as eid_done_perc,cur.eid_done_cmts,case when  ROUND(targets.viral_load_mothers_target/"
                + period + ") >0 then ROUND(targets.viral_load_mothers_target/" + period
                + ") else 1 end as viral_load_mothers_target,SUM(cur.viral_load_mothers_done) as viral_load_mothers_done,AVG(cur.viral_load_mothers_perc) as viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join (facility join targets on (facility.mflcode=targets.facility and targets.year='"
                + mwaka + "' ) ) on cur.facilityname=facility.facility_name where  " + where + " "
                + " group by facilityname ";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {

                        ce.setCellValue(conn.rs.getString(a));

                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // write it as an excel attachment
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            wb.write(outByteStream);
            byte[] outArray = outByteStream.toByteArray();
            response.setContentType("application/ms-excel");
            response.setContentLength(outArray.length);
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename=MOIS_Cum_Rpt_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

        } catch (SQLException ex) {
            Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (InvalidFormatException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:code.excelreport_weekly.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {//from   w  w  w  .  j  a  v a2  s . c  o  m
        /* TODO output your page here. You may use following sample code. */

        String allpath = getServletContext().getRealPath("/MOIS_WEEKLY.xlsm");

        XSSFWorkbook wb1;

        String pathtodelete = null;

        Date da = new Date();
        String dat2 = da.toString().replace(" ", "_");

        dat2 = dat2.toString().replace(":", "_");

        String mydrive = allpath.substring(0, 1);

        String np = mydrive + ":\\APHIAPLUS\\MOIS\\MACROS\\MOIS_REPORT" + dat2 + ".xlsm";
        //check if file exists
        String sourcepath = getServletContext().getRealPath("/MOIS_WEEKLY.xlsm");

        File f = new File(np);
        if (!f.exists() && !f.isFile()) {
            /* do something */

            copytemplates ct = new copytemplates();
            System.out.println("Copying macros..");
            ct.transfermacros(sourcepath, np);

        } else
        //copy the file alone  
        {

            copytemplates ct = new copytemplates();
            //copy the agebased file only
            ct.copymacros(sourcepath, np);

        }

        File allpathfile = new File(np);

        OPCPackage pkg = OPCPackage.open(allpathfile);

        pathtodelete = np;

        //wb = new XSSFWorkbook( OPCPackage.open(allpath) );
        wb1 = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(wb1, 100);

        Sheet rawdata = wb.getSheet("Facility Report");
        String startdate = "2016-10-01";

        String enddate = "2016-10-30";
        String county = "";

        if (request.getParameter("startdate") != null) {
            startdate = request.getParameter("startdate");
        }
        if (request.getParameter("county") != null) {
            if (!request.getParameter("county").equals("")) {

                county = request.getParameter("county");

            }
        }

        if (request.getParameter("enddate") != null) {
            enddate = request.getParameter("enddate");
        }
        String countywhere = " and 1=1";
        if (!county.equals("")) {
            countywhere = "  and ( county like '" + county + "' )";
        }

        DateTime dateTime1 = new DateTime(startdate);
        DateTime dateTime2 = new DateTime(enddate);

        int weeks = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();
        System.out.println("___Weeks____" + weeks);

        if (weeks == 0) {
            weeks = 1;
        }
        //String header[]="county,subcounty,facilityname,startdate,enddate,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,new_care_child,new_care_adult,new_care_total,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,pmtct_hiv_pos_target,pmtct_hiv_pos,eid_target,eid_done,viral_load_mothers_target,viral_load_mothers_done,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,started_art_perc_child,started_art_perc_adult,started_art_perc_all,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,tested_perc_child,tested_perc_adult,tested_perc_all,viral_load_mothers_perc,eid_done_perc,pmtct_hiv_pos_perc,hiv_pos_yield_cmts,hiv_pos_care_cmts,started_art_cmts,viral_test_cmts,ipt_done_cmts,tested_cmts,viral_load_mothers_cmts,eid_done_cmts,pmtct_hiv_pos_cmts".split(",");
        //String headername[]="COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,EID TARGET,EID DONE,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,HIV POSITIVE YIELD CHILDREN ,HIV POSITIVE YIELD ADULT,HIV POSITIVE YIELD ALL,HIV POSITIVE CARE CHILDREN,HIV POSITIVE CARE ADULT,HIV POSITIVE CARE ALL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART ,VIRAL TEST CHILDREN,VIRAL TEST ADULT,VIRAL TEST ALL,IPT DONE CHILD,IPT DONE ADULT,IPT DONE ALL,TESTED CHILD,TESTED ADULT,TESTED ALL,VIRAL LOAD MOTHERS ,EID DONE,PMTCT HIV POSITIVE,HIV POSITIVE YIELD COMMENTS,HIV POSITIVE CARE COMMENTS,STARTED ART COMMENTS,VIRAL TEST COMMENTS,IPT DONE COMMENTS ,TESTED COMMENTS,VIRAL LOAD MOTHERS COMMENTS,EID DONE COMMENTS,PMTCT HIV POSITIVE COMMENTS,REPORTING RATE".split(",");

        String header[] = "county,subcounty,facilityname,startdate,enddate,testing_target_child,testing_target_adult,testing_target_total,test_child,test_adult,test_total,tested_perc_child,tested_perc_adult,tested_perc_all,tested_cmts,hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total,hiv_pos_child,hiv_pos_adult,hiv_pos_total,hiv_pos_yield_perc_child,hiv_pos_yield_perc_adult,hiv_pos_yield_perc_all,hiv_pos_yield_cmts,new_care_child,new_care_adult,new_care_total,hiv_pos_care_perc_child,hiv_pos_care_perc_adult,hiv_pos_care_perc_all,hiv_pos_care_cmts,new_art_target_child,new_art_target_adult,new_art_target_total,started_art_child,started_art_adult,started_art_total,started_art_perc_child,started_art_perc_adult,started_art_perc_all,started_art_cmts,viral_load_target_child,viral_load_target_adult,viral_load_target_total,viral_load_done_child,viral_load_done_adult,viral_load_done_total,viral_test_perc_child,viral_test_perc_adult,viral_test_perc_all,viral_test_cmts,ipt_target_child,ipt_target_adult,ipt_target_total,ipt_child,ipt_adult,ipt_total,ipt_done_perc_child,ipt_done_perc_adult,ipt_done_perc_all,ipt_done_cmts,pmtct_hiv_pos_target,pmtct_hiv_pos,pmtct_hiv_pos_perc,pmtct_hiv_pos_cmts,eid_target,eid_done,eid_done_perc,eid_done_cmts,viral_load_mothers_target,viral_load_mothers_done,viral_load_mothers_perc,viral_load_mothers_cmts"
                .split(",");
        String headername[] = "COUNTY,SUB-COUNTY,FACILITY,START DATE,END DATE,TESTING TARGET CHILDREN,TESTING TARGET ADULT,TESTING TARGET TOTAL,TESTING CHILDREN,TESTING ADULT,TESTING TOTAL,PERCENTAGE TESTED AGAINST TARGET CHILDREN,PERCENTAGE TESTED AGAINST TARGET ADULT,PERCENTAGE TESTED AGAINST TARGET ALL,PERCENTAGE TESTED AGAINST TARGET COMMENTS,HIV POSITIVE TARGET CHILDREN,HIV POSITIVE TARGET ADULT,HIV POSITIVE TARGET TOTAL,HIV POSITIVE CHILDREN,HIV POSITIVE ADULT,HIV POSITIVE TOTAL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED CHILDREN,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ADULT,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED ALL,PERCENTAGE HIV POSITIVE TARGET YIELD ACHIEVED COMMENTS, NEW CARE CHILDREN,NEW CARE ADULT,NEW CARE TOTAL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE CHILDREN,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ADULT,PERCENTAGE HIV POSITIVE ENROLLED ON CARE ALL,PERCENTAGE HIV POSITIVE ENROLLED ON CARE COMMENTS,NEW ART TARGET CHILDREN,NEW ART TARGET ADULT,NEW ART TARGET TOTAL,STARTED ART CHILDREN,STARTED ART ADULT,STARTED ART TOTAL,PERCENTAGE OF TARGET STARTED ON ART CHILDREN,PERCENTAGE OF TARGET STARTED ON ART ADULT,PERCENTAGE OF TARGET STARTED ON ART ALL,PERCENTAGE OF TARGET STARTED ON ART COMMENTS,VIRAL LOAD TARGET CHILDREN,VIRAL LOAD TARGET ADULT,VIRAL LOAD TARGET TOTAL,VIRAL LOAD DONE CHILDREN,VIRAL LOAD DONE ADULT,VIRAL LOAD DONE TOTAL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET CHILDREN,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ADULT,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET ALL,PERCENTAGE OF VIRAL LOAD TESTS DONE AGAINST TARGET COMMENTS,IPT TARGET CHILDREN,IPT TARGET ADULT,IPT TARGET TOTAL,IPT CHILDREN,IPT ADULT,IPT TOTAL,PERCENTAGE OF IPT DONE AGAINST TARGET CHILDREN,PERCENTAGE OF IPT DONE AGAINST TARGET ADULT,PERCENTAGE OF IPT DONE AGAINST TARGET ALL,PERCENTAGE OF IPT DONE AGAINST TARGET COMMENTS,PMTCT HIV POSITIVE TARGET,PMTCT HIV POSITIVE ,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET ALL,PERCENTAGE PMTCT HIV POSITIVE YIELD ACHIEVED AGAINST TARGET COMMENTS,EID TARGET,EID DONE,PERCENTAGE EID DONE AGAINST TARGET ALL,PERCENTAGE EID DONE AGAINST TARGET COMMENTS,VIRAL LOAD MOTHERS TARGET,VIRAL LOAD MOTHERS DONE,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET ALL,PERCENTAGE VIRAL LOAD TESTS DONE FOR MOTHERS AGAINST TARGET COMMENTS,REPORTING RATE"
                .split(",");
        //
        //
        Row rw0 = rawdata.createRow(0);
        rw0.setHeightInPoints(25);

        for (int d = 0; d < headername.length; d++) {
            Cell ce = rw0.createCell(d);
            ce.setCellValue(headername[d]);

        }

        dbConnweb conn = new dbConnweb();

        conn.st.executeUpdate("SET GLOBAL max_allowed_packet = 209715200");
        conn.rs = conn.st.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet' ");
        if (conn.rs.next()) {
            System.out.println("Generating report | Max_allowed_connection_" + conn.rs.getString(2));

        }

        String where = " (enddate between '" + startdate + "' and '" + enddate + "')  " + countywhere
                + " and cur.id  like '%_weekly%' ";
        String where1 = " (enddate between '" + startdate + "' and '" + enddate
                + "') and weekly_data_new.id  like '%_weekly%'  ";

        String period = "52";
        String yr = enddate.substring(0, 4);

        String tar[] = enddate.split("-");

        int mwaka = new Integer(yr);
        if (new Integer(tar[1]) >= 10 && new Integer(tar[1]) <= 12) {
            mwaka = mwaka + 1;
        }

        //get data in report form and add into the various json macros.
        //String query="select facilityname,startdate,enddate, hiv_pos_target_child,hiv_pos_target_adult,hiv_pos_target_total ,hiv_pos_child as hiv_pos_child , hiv_pos_adult as hiv_pos_adult  ,hiv_pos_total as hiv_pos_total  ,new_care_child as new_care_child ,new_care_adult as new_care_adult ,new_care_total as new_care_total  , new_art_target_child  ,new_art_target_adult  ,new_art_target_total  ,started_art_child as started_art_child, started_art_adult as started_art_adult ,started_art_total as started_art_total ,viral_load_target_child  ,viral_load_target_adult  ,viral_load_target_total  ,viral_load_done_child as viral_load_done_child ,viral_load_done_adult as viral_load_done_adult ,viral_load_done_total as viral_load_done_total  ,ipt_target_child  ,ipt_target_adult  ,ipt_target_total  ,ipt_child as ipt_child ,ipt_adult as ipt_adult ,ipt_total as ipt_total ,testing_target_child  ,testing_target_adult  ,testing_target_total  ,test_child as test_child ,test_adult as test_adult   ,test_total as test_total , pmtct_hiv_pos_target,pmtct_hiv_pos as pmtct_hiv_pos, eid_target  , eid_done as eid_done, viral_load_mothers_target, viral_load_mothers_done as viral_load_mothers_done  from weekly_data_new join facility on weekly_data_new.facilityname=facility.facility_name ";

        String query = " select facility.county,facility.subcounty,cur.facilityname,cur.startdate,cur.enddate,case when  ROUND(targets.testing_target_child/"
                + period + ") >0 then ROUND(targets.testing_target_child/" + period
                + ") else 1 end as testing_target_child,case when  ROUND(targets.testing_target_adult/" + period
                + ") >0 then ROUND(targets.testing_target_adult/" + period
                + ") else 1 end as testing_target_adult,case when  ROUND(targets.testing_target_total/" + period
                + ") >0 then ROUND(targets.testing_target_total/" + period
                + ") else 1 end as testing_target_total,cur.test_child,cur.test_adult,cur.test_total,cur.tested_perc_child,cur.tested_perc_adult,cur.tested_perc_all,cur.tested_cmts,case when  ROUND(targets.hiv_pos_target_child/"
                + period + ") >0 then ROUND(targets.hiv_pos_target_child/" + period
                + ") else 1 end as hiv_pos_target_child,case when  ROUND(targets.hiv_pos_target_adult/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_adult/" + period
                + ") else 1 end as hiv_pos_target_adult,case when  ROUND(targets.hiv_pos_target_total/" + period
                + ") >0 then ROUND(targets.hiv_pos_target_total/" + period
                + ") else 1 end as hiv_pos_target_total,cur.hiv_pos_child,cur.hiv_pos_adult,cur.hiv_pos_total,cur.hiv_pos_yield_perc_child,cur.hiv_pos_yield_perc_adult,cur.hiv_pos_yield_perc_all,cur.hiv_pos_yield_cmts,cur.new_care_child,cur.new_care_adult,cur.new_care_total,cur.hiv_pos_care_perc_child,cur.hiv_pos_care_perc_adult,cur.hiv_pos_care_perc_all,cur.hiv_pos_care_cmts,case when  ROUND(targets.new_art_target_child/"
                + period + ") >0 then ROUND(targets.new_art_target_child/" + period
                + ") else 1 end as new_art_target_child,case when  ROUND(targets.new_art_target_adult/" + period
                + ") >0 then ROUND(targets.new_art_target_adult/" + period
                + ") else 1 end as new_art_target_adult,case when  ROUND(targets.new_art_target_total/" + period
                + ") >0 then ROUND(targets.new_art_target_total/" + period
                + ") else 1 end as new_art_target_total,cur.started_art_child,cur.started_art_adult,cur.started_art_total,cur.started_art_perc_child,cur.started_art_perc_adult,cur.started_art_perc_all,cur.started_art_cmts,case when  ROUND(targets.viral_load_target_child/"
                + period + ") >0 then ROUND(targets.viral_load_target_child/" + period
                + ") else 1 end as viral_load_target_child,case when  ROUND(targets.viral_load_target_adult/"
                + period + ") >0 then ROUND(targets.viral_load_target_adult/" + period
                + ") else 1 end as viral_load_target_adult,case when  ROUND(targets.viral_load_target_total/"
                + period + ") >0 then ROUND(targets.viral_load_target_total/" + period
                + ") else 1 end as viral_load_target_total,cur.viral_load_done_child,cur.viral_load_done_adult,cur.viral_load_done_total,cur.viral_test_perc_child,cur.viral_test_perc_adult,cur.viral_test_perc_all,cur.viral_test_cmts,case when  ROUND(targets.ipt_target_child/"
                + period + ") >0 then ROUND(targets.ipt_target_child/" + period
                + ") else 1 end as ipt_target_child,case when  ROUND(targets.ipt_target_adult/" + period
                + ") >0 then ROUND(targets.ipt_target_adult/" + period
                + ") else 1 end as ipt_target_adult,case when  ROUND(targets.ipt_target_total/" + period
                + ") >0 then ROUND(targets.ipt_target_total/" + period
                + ") else 1 end as ipt_target_total,cur.ipt_child,cur.ipt_adult,cur.ipt_total,cur.ipt_done_perc_child,cur.ipt_done_perc_adult,cur.ipt_done_perc_all,cur.ipt_done_cmts,case when  ROUND(targets.pmtct_hiv_pos_target/"
                + period + ") >0 then ROUND(targets.pmtct_hiv_pos_target/" + period
                + ") else 1 end as pmtct_hiv_pos_target,cur.pmtct_hiv_pos,cur.pmtct_hiv_pos_perc,cur.pmtct_hiv_pos_cmts,case when  ROUND(targets.eid_target/"
                + period + ") >0 then ROUND(targets.eid_target/" + period
                + ") else 1 end as eid_target,cur.eid_done,cur.eid_done_perc,cur.eid_done_cmts,case when  ROUND(targets.viral_load_mothers_target/"
                + period + ") >0 then ROUND(targets.viral_load_mothers_target/" + period
                + ") else 1 end as viral_load_mothers_target,cur.viral_load_mothers_done,cur.viral_load_mothers_perc,cur.viral_load_mothers_cmts  from weekly_data_new cur join (facility join targets on (facility.mflcode=targets.facility and targets.year='"
                + mwaka + "' ) ) on cur.facilityname=facility.facility_name where  " + where + " "
                + " group by cur.id ";

        System.out.println("" + query);
        try {

            conn.rs = conn.st.executeQuery(query);
            String facilname = "";
            String facilnamecopy = "";

            String id = "";

            //percentages

            String viral_load_mothers_perc = null;
            String eid_done_perc = null;
            String pmtct_hiv_pos_perc = null;

            //new percenatages
            String hiv_pos_yield_perc_child = null;
            String hiv_pos_yield_perc_adult = null;
            String hiv_pos_yield_perc_all = null;
            String hiv_pos_care_perc_child = null;
            String hiv_pos_care_perc_adult = null;
            String hiv_pos_care_perc_all = null;
            String started_art_perc_child = null;
            String started_art_perc_adult = null;
            String started_art_perc_all = null;
            String viral_test_perc_child = null;
            String viral_test_perc_adult = null;
            String viral_test_perc_all = null;
            String ipt_done_perc_child = null;
            String ipt_done_perc_adult = null;
            String ipt_done_perc_all = null;
            String tested_perc_child = null;
            String tested_perc_adult = null;
            String tested_perc_all = null;
            //comments
            String viral_load_mothers_cmts = null;
            String eid_done_cmts = null;
            String pmtct_hiv_pos_cmts = null;
            String hiv_pos_yield_cmts = null;
            String hiv_pos_care_cmts = null;
            String started_art_cmts = null;
            String viral_test_cmts = null;
            String ipt_done_cmts = null;
            String tested_cmts = null;

            Row rwx = null;

            int rowno = 0;

            while (conn.rs.next()) {

                rowno++;

                rwx = rawdata.createRow(rowno);
                rwx.setHeightInPoints(23);

                //do a per facility 

                //System.out.println("___FACILITY : "+conn.rs.getString("facilityname")+" => hiv_pos_total "+conn.rs.getString("hiv_pos_total"));

                for (int a = 1; a <= header.length; a++) {

                    System.out.print(header[a - 1] + " _ " + conn.rs.getString(a));
                    Cell ce = rwx.createCell(a - 1);

                    if (a > 5 && a <= 73) {
                        if (header[a - 1].contains("cmts")) {
                            ce.setCellValue(conn.rs.getString(a));
                        } else if (header[a - 1].contains("_perc")) {
                            ce.setCellValue(conn.rs.getInt(a));
                            System.out.println("__Percent");
                        } else {
                            ce.setCellValue(conn.rs.getInt(a));
                        }
                    } else {
                        ce.setCellValue(conn.rs.getString(a));
                    }

                }
                //reporting rate
                int reportingrate = 0;

                String getrates = "select ROUND(((count(facilityname))/(" + weeks
                        + ")*100)) as count from weekly_data_new where facilityname like '"
                        + conn.rs.getString("facilityname") + "' and " + where1 + " ";

                conn.rs1 = conn.st1.executeQuery(getrates);

                if (conn.rs1.next()) {

                    reportingrate = conn.rs1.getInt(1);
                }

                Cell ce = rwx.createCell(header.length);
                ce.setCellValue(reportingrate);

            }

            if (conn.rs != null) {
                conn.rs.close();
            }
            if (conn.rs1 != null) {
                conn.rs1.close();
            }
            if (conn.st != null) {
                conn.st.close();
            }
            if (conn.st1 != null) {
                conn.st1.close();
            }
            if (conn.conne != null) {
                conn.conne.close();
            }

            Date dat = new Date();

            String dat1 = dat.toString().replace(" ", "_");

            // write it as an excel attachment
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            wb.write(outByteStream);
            byte[] outArray = outByteStream.toByteArray();
            response.setContentType("application/ms-excel");
            response.setContentLength(outArray.length);
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename=MOIS_Weekly_Report_From"
                    + startdate.replace(" ", "-") + "_To_" + enddate.replace(" ", "_") + ".xlsm");
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            pkg.close();
            wb.dispose();
            // response.sendRedirect("index.jsp");

            File file = new File(pathtodelete);

            if (file.delete()) {
                System.out.println(file.getName() + " is deleted!");
            } else {
                System.out.println("Delete operation is failed.");
            }

        } catch (SQLException ex) {
            Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (InvalidFormatException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(excelreport.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.billing.ng.entities.CurrentBillingCycle.java

License:Open Source License

/**
 * Calculates the number of complete cycles between the given billing start date and
 * the given "today's" date.//from   w  w w .  j a  v a 2  s. c  o  m
 *
 * @param period billing period
 * @param billingStart billing cycle start
 * @param today end date
 * @return number of complete cycles
 */
public static Integer calculateCycleNumber(BillingPeriod period, DateMidnight billingStart,
        DateMidnight today) {
    Integer interval = period.getInterval();

    switch (period.getType()) {
    case DAY:
        return Days.daysBetween(billingStart, today).getDays() / interval;

    case WEEK:
        return Weeks.weeksBetween(billingStart, today).getWeeks() / interval;

    case MONTH:
        return Months.monthsBetween(billingStart, today).getMonths() / interval;

    case YEAR:
        return Years.yearsBetween(billingStart, today).getYears() / interval;
    }
    return null;
}

From source file:com.cisco.dvbu.ps.utils.date.DateDiffDate.java

License:Open Source License

/**
 * Called to invoke the stored procedure.  Will only be called a
 * single time per instance.  Can throw CustomProcedureException or
 * SQLException if there is an error during invoke.
 *///from   www .j  a v a  2s. c o m
public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException {
    java.util.Date startDate = null;
    java.util.Date endDate = null;
    Calendar startCal = null;
    Calendar endCal = null;
    DateTime startDateTime = null;
    DateTime endDateTime = null;
    String datePart = null;
    long dateLength = 0;

    try {
        result = null;
        if (inputValues[0] == null) {
            result = new Long(dateLength);
            return;
        }

        if (inputValues[1] == null) {
            result = new Long(dateLength);
            return;
        }

        if (inputValues[2] == null) {
            result = new Long(dateLength);
            return;
        }

        datePart = (String) inputValues[0];
        startDate = (java.util.Date) inputValues[1];
        startCal = Calendar.getInstance();
        startCal.setTime(startDate);

        endDate = (java.util.Date) inputValues[2];
        endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1,
                startCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0);
        endDateTime = new DateTime(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH) + 1,
                endCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0);

        if (datePart.equalsIgnoreCase("second")) {
            Seconds seconds = Seconds.secondsBetween(startDateTime, endDateTime);
            dateLength = seconds.getSeconds();
        }

        if (datePart.equalsIgnoreCase("minute")) {
            Minutes minutes = Minutes.minutesBetween(startDateTime, endDateTime);
            dateLength = minutes.getMinutes();
        }

        if (datePart.equalsIgnoreCase("hour")) {
            Hours hours = Hours.hoursBetween(startDateTime, endDateTime);
            dateLength = hours.getHours();
        }

        if (datePart.equalsIgnoreCase("day")) {
            Days days = Days.daysBetween(startDateTime, endDateTime);
            dateLength = days.getDays();
        }

        if (datePart.equalsIgnoreCase("week")) {
            Weeks weeks = Weeks.weeksBetween(startDateTime, endDateTime);
            dateLength = weeks.getWeeks();
        }

        if (datePart.equalsIgnoreCase("month")) {
            Months months = Months.monthsBetween(startDateTime, endDateTime);
            dateLength = months.getMonths();
        }

        if (datePart.equalsIgnoreCase("year")) {
            Years years = Years.yearsBetween(startDateTime, endDateTime);
            dateLength = years.getYears();
        }

        result = new Long(dateLength);
    } catch (Throwable t) {
        throw new CustomProcedureException(t);
    }
}

From source file:com.google.android.apps.paco.NonESMSignalGenerator.java

License:Open Source License

private DateTime nextRepeatWeekly(DateTime midnightNextDay) {
    if (schedule.getRepeatRate() == 1) {
        return midnightNextDay;
    }/*from w w w  .j av a 2 s  .c om*/
    int distanceBetweenStartAndTomorrow = Weeks
            .weeksBetween(new DateTime(schedule.getBeginDate()).toDateMidnight(), midnightNextDay).getWeeks();
    if (distanceBetweenStartAndTomorrow == 0 || distanceBetweenStartAndTomorrow == schedule.getRepeatRate()) {
        if ((distanceBetweenStartAndTomorrow == 0 && midnightNextDay
                .getDayOfWeek() <= new DateMidnight(schedule.getBeginDate()).getDayOfWeek())) {
            // we crossed a week boundary, so add one week.
            return midnightNextDay.plusWeeks(schedule.getRepeatRate() - 1);
        }
        return midnightNextDay;
    } else if (distanceBetweenStartAndTomorrow > schedule.getRepeatRate()) {
        int remainder = distanceBetweenStartAndTomorrow % schedule.getRepeatRate();
        return midnightNextDay.plusWeeks(schedule.getRepeatRate() - remainder);
    } else {
        return midnightNextDay.plusWeeks(schedule.getRepeatRate() - distanceBetweenStartAndTomorrow);
    }
}

From source file:com.gst.portfolio.interestratechart.domain.InterestRateChartSlabFields.java

License:Apache License

public Integer depositPeriod(final LocalDate periodStartDate, final LocalDate periodEndDate) {
    Integer actualDepositPeriod = 0;
    final SavingsPeriodFrequencyType periodFrequencyType = SavingsPeriodFrequencyType.fromInt(periodType());
    switch (periodFrequencyType) {
    case DAYS:/*from w w w.  j a v a2 s.co m*/
        actualDepositPeriod = Days.daysBetween(periodStartDate, periodEndDate).getDays();
        break;
    case WEEKS:
        actualDepositPeriod = Weeks.weeksBetween(periodStartDate, periodEndDate).getWeeks();
        break;
    case MONTHS:
        actualDepositPeriod = Months.monthsBetween(periodStartDate, periodEndDate).getMonths();
        break;
    case YEARS:
        actualDepositPeriod = Years.yearsBetween(periodStartDate, periodEndDate).getYears();
        break;
    case INVALID:
        actualDepositPeriod = 0;// default value
        break;
    }
    return actualDepositPeriod;
}

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.DefaultScheduledDateGenerator.java

License:Apache License

@Override
public Boolean isDateFallsInSchedule(final PeriodFrequencyType frequency, final int repaidEvery,
        final LocalDate startDate, final LocalDate date) {
    boolean isScheduledDate = false;
    switch (frequency) {
    case DAYS://from  w ww .  java  2s  .  c  o m
        int diff = Days.daysBetween(startDate, date).getDays();
        isScheduledDate = (diff % repaidEvery) == 0;
        break;
    case WEEKS:
        int weekDiff = Weeks.weeksBetween(startDate, date).getWeeks();
        isScheduledDate = (weekDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusWeeks(weekDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case MONTHS:
        int monthDiff = Months.monthsBetween(startDate, date).getMonths();
        isScheduledDate = (monthDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusMonths(monthDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case YEARS:
        int yearDiff = Years.yearsBetween(startDate, date).getYears();
        isScheduledDate = (yearDiff % repaidEvery) == 0;
        if (isScheduledDate) {
            LocalDate modifiedDate = startDate.plusYears(yearDiff);
            isScheduledDate = modifiedDate.isEqual(date);
        }
        break;
    case INVALID:
        break;
    }
    return isScheduledDate;
}

From source file:com.gst.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final LocalDate endDate) {
    BigDecimal numberOfPeriods = BigDecimal.ZERO;
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:/*w  w w.j a  v  a2s.  c  o  m*/
        int numOfDays = Days.daysBetween(startDate, endDate).getDays();
        numberOfPeriods = BigDecimal.valueOf((double) numOfDays);
        break;
    case WEEKS:
        int numberOfWeeks = Weeks.weeksBetween(startDate, endDate).getWeeks();
        int daysLeftAfterWeeks = Days.daysBetween(startDate.plusWeeks(numberOfWeeks), endDate).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfWeeks))
                .add(BigDecimal.valueOf((double) daysLeftAfterWeeks / 7));
        break;
    case MONTHS:
        int numberOfMonths = Months.monthsBetween(startDate, endDate).getMonths();
        LocalDate startDateAfterConsideringMonths = null;
        LocalDate endDateAfterConsideringMonths = null;
        int diffDays = 0;
        if (this.loanCalendar == null) {
            startDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths);
            startDateAfterConsideringMonths = CalendarUtils.adjustDate(startDateAfterConsideringMonths,
                    getSeedDate(), this.repaymentPeriodFrequencyType);
            endDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths + 1);
            endDateAfterConsideringMonths = CalendarUtils.adjustDate(endDateAfterConsideringMonths,
                    getSeedDate(), this.repaymentPeriodFrequencyType);
        } else {
            LocalDate expectedStartDate = startDate;
            if (!CalendarUtils.isValidRedurringDate(loanCalendar.getRecurrence(),
                    loanCalendar.getStartDateLocalDate().minusMonths(getRepaymentEvery()), startDate)) {
                expectedStartDate = CalendarUtils.getNewRepaymentMeetingDate(loanCalendar.getRecurrence(),
                        startDate.minusMonths(getRepaymentEvery()), startDate.minusMonths(getRepaymentEvery()),
                        getRepaymentEvery(),
                        CalendarUtils
                                .getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()),
                        this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays);
            }
            if (!expectedStartDate.isEqual(startDate)) {
                diffDays = Days.daysBetween(startDate, expectedStartDate).getDays();
            }
            if (numberOfMonths == 0) {
                startDateAfterConsideringMonths = expectedStartDate;
            } else {
                startDateAfterConsideringMonths = CalendarUtils.getNewRepaymentMeetingDate(
                        loanCalendar.getRecurrence(), expectedStartDate,
                        expectedStartDate.plusMonths(numberOfMonths), getRepaymentEvery(),
                        CalendarUtils
                                .getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()),
                        this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays);
            }
            endDateAfterConsideringMonths = CalendarUtils.getNewRepaymentMeetingDate(
                    loanCalendar.getRecurrence(), startDateAfterConsideringMonths,
                    startDateAfterConsideringMonths.plusDays(1), getRepaymentEvery(),
                    CalendarUtils.getMeetingFrequencyFromPeriodFrequencyType(getLoanTermPeriodFrequencyType()),
                    this.holidayDetailDTO.getWorkingDays(), isSkipRepaymentOnFirstDayOfMonth, numberOfDays);
        }
        int daysLeftAfterMonths = Days.daysBetween(startDateAfterConsideringMonths, endDate).getDays()
                + diffDays;
        int daysInPeriodAfterMonths = Days
                .daysBetween(startDateAfterConsideringMonths, endDateAfterConsideringMonths).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfMonths))
                .add(BigDecimal.valueOf((double) daysLeftAfterMonths / daysInPeriodAfterMonths));
        break;
    case YEARS:
        int numberOfYears = Years.yearsBetween(startDate, endDate).getYears();
        LocalDate startDateAfterConsideringYears = startDate.plusYears(numberOfYears);
        LocalDate endDateAfterConsideringYears = startDate.plusYears(numberOfYears + 1);
        int daysLeftAfterYears = Days.daysBetween(startDateAfterConsideringYears, endDate).getDays();
        int daysInPeriodAfterYears = Days
                .daysBetween(startDateAfterConsideringYears, endDateAfterConsideringYears).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfYears))
                .add(BigDecimal.valueOf((double) daysLeftAfterYears / daysInPeriodAfterYears));
        break;
    default:
        break;
    }
    return numberOfPeriods;
}