package WEB-INF.classes.wenrgise.hrms.webtier.action;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.hrms.bean.HrmAdhocReportHdrBean;
import wenrgise.hrms.ejb.business.HrmAdhocReportBO;
import wenrgise.hrms.webtier.action.BaseAction;
import wenrgise.hrms.webtier.form.HrmAdhocReportForm;

public class HrmAdhocReportAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    response.setContentType("application/vnd.ms-excel");
    WorkbookSettings ws = new WorkbookSettings();
    ws.setLocale(new Locale("en", "EN"));
    ServletOutputStream servletOutputStream = response.getOutputStream();
    HrmAdhocReportForm oForm = (HrmAdhocReportForm)form;
    String sFormName = oForm.getClass().getName();
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oBaseHeaderBean, oForm);
    HrmAdhocReportBO oHrmAdhocReportBO = new HrmAdhocReportBO();
    String reportName = oForm.getCode();
    StringBuffer fileName = new StringBuffer("\\");
    fileName.append(reportName);
    fileName.append(".xls");
    response.setHeader("Content-disposition", String.valueOf(String.valueOf("attachment; filename=\"").concat(String.valueOf(fileName.toString()))).concat(String.valueOf("\"")));
    try {
      WritableWorkbook workbook = Workbook.createWorkbook((OutputStream)servletOutputStream, ws);
      WritableSheet s = workbook.createSheet(reportName, 0);
      oHrmAdhocReportBO.downloadAdhocReport((HrmAdhocReportHdrBean)oBaseHeaderBean, s);
      workbook.write();
      workbook.close();
      servletOutputStream.flush();
    } catch (Exception e) {
      e.printStackTrace();
    } 
    return mapping.findForward("success");
  }
}
