package edu.ucsf.mousedatabase.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import edu.ucsf.mousedatabase.*;
import edu.ucsf.mousedatabase.objects.ImportReport;

public class ReportServlet extends HttpServlet
{

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  public static final String HolderReportName = "Holder Report";
  public static final String RecordsReportName = "Records Report";
  public static final String LarcRecordHolderReportName = "LARC Record Holder Report";
  public static final String PPTChangeRequestImportReportName = "PPT Change Request Data Upload Report";
  public static final String PurchaseChangeRequestImportReportName = "Purchases Change Request Data Upload Report";
  public static final String PurchaseSubmissionsImportReportName = "Purchases Submissions Data Upload Report";
  public static final String OtherInstitutionsChangeRequestImportReportName = "Transfer from other Instutions Change Request Data Upload Report";
  public static final String OtherInstitutionsSubmissionsImportReportName = "Transfer from other Instutions Submissions Data Upload Report";

  public void doGet(HttpServletRequest request, HttpServletResponse  response)
      throws IOException, ServletException {

    Object[] args = null;
    String reportName = request.getParameter("reportName");
    Boolean problem = false;
    String filename = reportName + ".csv";
    if (reportName.equals(LarcRecordHolderReportName))
    {
      filename = "holderreport.csv";
    }
    if (reportName.equals(PPTChangeRequestImportReportName) || reportName.equals(PurchaseChangeRequestImportReportName) || 
        reportName.equals(PurchaseSubmissionsImportReportName) || reportName.equals(OtherInstitutionsChangeRequestImportReportName) ||
        reportName.equals(OtherInstitutionsSubmissionsImportReportName))
    {
      args = new Object[1];
      args[0] = request.getParameter("importReportId");
      ArrayList<ImportReport> reports = DBConnect.getImportReport(Integer.parseInt((String)args[0]));
      filename = reportName + " " + reports.get(0).getName() + ".csv";
    }

    String errorMessage = "Problem running report.  Please contact Mouse Database Administrator\r\n";
    String report = "";
    if (reportName != null && !reportName.equals(""))
    {
      try
      {
        report = DBConnect.RunReport(reportName,args);
      }
      catch (IndexOutOfBoundsException e)
      {
        problem = true;
        errorMessage += e.getMessage();
      }
      catch (Exception e)
      {
        e.printStackTrace();
        problem = true;
        errorMessage += e.getMessage();
      }
    }
    else
    {
      errorMessage += "No report specified!";
      problem = true;
    }

    response.setContentType("application/text");
    filename = filename.replaceAll(" ", "_");
    response.setHeader("Content-disposition","attachment; filename=" +filename);
        if (!problem)
        {
          response.getWriter().write(report);
        }
        else
        {
          response.getWriter().write(errorMessage);
        }


  }


}

@@ -418,25 +418,25 @@
 e = %22Holder 
-R
+r
 eport%22;%0A  pu
@@ -489,17 +489,17 @@
 Records 
-R
+r
 eport%22;%0A
@@ -566,23 +566,23 @@
 ARC 
-R
+r
 ecord 
-H
+h
 older 
-R
+r
 epor
@@ -654,40 +654,28 @@
  = %22
-PPT C
+TDU c
 hange 
-R
+r
 equest 
-Data Upload R
+r
 epor
@@ -753,45 +753,27 @@
 = %22P
-urchases C
+DU c
 hange 
-R
+r
 equest 
-Data Upload R
+r
 epor
@@ -849,18 +849,16 @@
 = %22P
-urchases S
+DU new s
 ubmi
@@ -860,37 +860,25 @@
 submissions 
-Data Upload R
+r
 eport%22;%0A  pu
@@ -956,67 +956,28 @@
  = %22
-Transfer from other Instutions C
+IDU c
 hange 
-R
+r
 equest 
-Data Upload R
+r
 epor
@@ -1061,64 +1061,29 @@
  = %22
-Transfer from other Instutions Submissions Data Upload R
+IDU new submissions r
 epor

Update report names to match updated import names