
package edu.wustl.catissuecore.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.wustl.catissuecore.bizlogic.ExportCollectionProtocolBizLogic;
import edu.wustl.common.action.SecureAction;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.common.xml2excel.CSVWriter;

/**
 * This action class receives and forwards all the bulk operations
 * request done from UI.
 * @author sagar_baldwa
 *
 */
public class ExportCollectionProtocolAction extends SecureAction
{

	/**
	 * logger.
	 */
	private transient final Logger logger = Logger.getCommonLogger(ExportCollectionProtocolAction.class);

	@Override
	/**
	 * Execute Secure Action.
	 */
	protected ActionForward executeSecureAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String mappingForward = null;
		try 
		{
			
			ExportCollectionProtocolBizLogic exportCP=new ExportCollectionProtocolBizLogic();
			StringBuffer downloadFile = exportCP.getCPXMLFile(request.getParameter("title"));
			CSVWriter writer = new CSVWriter();
			writer.generate(downloadFile.toString(),"\""+request.getParameter("title")+".csv\"", response);
            
            
		} catch (Exception exp) {
			logger.error(exp.getMessage(), exp);
		}
		return mapping.findForward(mappingForward);
	}
}
@@ -33,24 +33,210 @@
 .action;%0D%0A%0D%0A
+import java.io.BufferedInputStream;%0D%0Aimport java.io.File;%0D%0Aimport java.io.FileInputStream;%0D%0Aimport java.io.FileOutputStream;%0D%0Aimport java.io.FileWriter;%0D%0Aimport java.io.OutputStream;%0D%0A%0D%0A
 import javax
@@ -459,24 +459,69 @@
 Mapping;%0D%0A%0D%0A
+import au.com.bytecode.opencsv.CSVWriter;%0D%0A%0D%0A
 import edu.w
@@ -661,20 +661,34 @@
 til.
+g
 lo
-gger.Logge
+bal.CommonServiceLocato
 r;%0D%0A
@@ -715,25 +715,24 @@
 mon.
-xml2excel.CSVWrit
+util.logger.Logg
 er;%0D
@@ -1513,16 +1513,214 @@
 le%22));%0D%0A
+%09%09%09%0D%0A%09%09%09String%5B%5D headerNValue=downloadFile.toString().split(%22%5Cn%22);%0D%0A%09%09%09 %0D%0A%09%09%09File file = new File(CommonServiceLocator.getInstance().getAppHome() + %22/%22 +request.getParameter(%22title%22)+%22.csv%22);%0D%0A%09%09%09%0D%0A
 %09%09%09CSVWr
@@ -1728,17 +1728,18 @@
 ter 
-w
+csvW
 riter
- = 
+=
 new 
@@ -1752,124 +1752,835 @@
 ter(
-);%0D%0A%09%09%09writer.generate(downloadFile.toString(),%22%5C%22%22+request.getParameter(%22title%22)+%22.csv%5C%22%22, response);%0D%0A            
+new FileWriter(CommonServiceLocator.getInstance().getAppHome() + %22/%22 +request.getParameter(%22title%22)+%22.csv%22));%0D%0A%09%09%09csvWriter.writeNext(headerNValue%5B0%5D.split(%22,%22));%0D%0A%09%09%09csvWriter.writeNext(headerNValue%5B1%5D.split(%22,%22));%0D%0A%09%09%09csvWriter.flush();%0D%0A%09%09%09response.setContentType(%22application/download%22);%0D%0A            response.setHeader(%22Content-Disposition%22, %22attachment;filename=%5C%22%22+request.getParameter(%22title%22)+%22.csv%5C%22%22);%0D%0A            response.setContentLength((int) file.length());%0D%0A            OutputStream outputStream = response.getOutputStream();%0D%0A%09%09%09BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));%0D%0A%09%09%09int count;%0D%0A%09%09%09byte buf%5B%5D = new byte%5B4096%5D;%0D%0A%09%09%09while ((count = bis.read(buf)) %3E -1)%0D%0A%09%09%09%7B%0D%0A%09%09%09%09outputStream.write(buf, 0, count);%0D%0A%09%09%09%7D%0D%0A%09%09%09outputStream.flush();%0D%0A%09%09%09bis.close();%0D%0A%09%09%09file.delete();
 %0D%0A  

Fix for bug 184.