/*******************************************************************************
 * Copyright 2012 University of Southern California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This code was developed by the Information Integration Group as part 
 * of the Karma project at the Information Sciences Institute of the 
 * University of Southern California.  For more information, publications, 
 * and related projects, please see: http://www.isi.edu/integration
 ******************************************************************************/

package edu.isi.karma.imp.csv;

import edu.isi.karma.rep.HNode;
import edu.isi.karma.rep.Row;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.alignment.SemanticType;
import edu.isi.karma.webserver.ServletContextParameterMap;
import edu.isi.karma.webserver.ServletContextParameterMap.ContextParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CSVFileExport {
	private Worksheet worksheet;
	private static final Logger logger = LoggerFactory
			.getLogger(CSVFileExport.class);
	public CSVFileExport(Worksheet worksheet) {
		this.worksheet = worksheet;
	}
	public String publishCSV() throws FileNotFoundException {
		String outputFile = "publish/CSV/" + worksheet.getTitle() + ".csv";
		logger.info("CSV file exported. Location:"
				+ outputFile);
		HashMap<String, String> modeledColumnTable = new HashMap<String, String>();
		for (SemanticType type : worksheet.getSemanticTypes().getListOfTypes()) {
			modeledColumnTable.put(type.getHNodeId(),"");
		}
		if(modeledColumnTable.size()==0) return null;
		int numRows = worksheet.getDataTable().getNumRows();
		if(numRows==0) 
			return "";
		StringBuilder sb = new StringBuilder();
		ArrayList<Row> rows =  worksheet.getDataTable().getRows(0, numRows);
		List<HNode> sortedLeafHNodes = new ArrayList<HNode>();
		List<String> hNodeIdList = new ArrayList<String>();
		worksheet.getHeaders().getSortedLeafHNodes(sortedLeafHNodes);
		for (HNode hNode : sortedLeafHNodes) {
			if(modeledColumnTable.containsKey(hNode.getId())) {
				if (sb.length() != 0)
					sb.append(",");
				sb.append(hNode.getColumnName());
				hNodeIdList.add(hNode.getId());
			}
		}
		sb.append("\n");
		for (Row row : rows) {
			boolean newRow = true;
			try {
				for(String hNodeId : hNodeIdList) {
					if(!newRow) 
						sb.append(",");
					else
						newRow = false;
					String colValue =row.getNode(hNodeId).getValue().asString();
					sb.append("\"");
					sb.append(colValue);
					sb.append("\"");
				}
			} catch (Exception e) {
				logger.error("Error reading a row! Skipping it.", e);
				continue;
			}
			sb.append("\n");
		}
		try {
	        Writer outUTF8=null;
			try {
				outUTF8 = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(ServletContextParameterMap.getParameterValue(ContextParameter.USER_DIRECTORY_PATH) +outputFile), "UTF8"));
				outUTF8.append(sb.toString());
	    		outUTF8.flush();
	    		outUTF8.close();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
				
		} catch (IOException e) {
			e.printStackTrace();
		}
		return outputFile;
	}
}

@@ -1367,24 +1367,25 @@
 tParameter;%0A
+%0A
 import org.s
@@ -1823,67 +1823,301 @@
 ing 
-outputFile = %22publish/CSV/%22 + worksheet.getTitle() + %22.csv%22
+filename = worksheet.getTitle() + %22.csv%22;%0A%09%09String outputFile = ServletContextParameterMap.getParameterValue(ContextParameter.CSV_PUBLISH_DIR) +  %0A%09%09%09%09filename;%0A%09%09String relativeFilename = ServletContextParameterMap.getParameterValue(ContextParameter.CSV_PUBLISH_RELATIVE_DIR) +  %0A%09%09%09%09filename
 ;%0A%09%09
@@ -3630,92 +3630,8 @@
 eam(
-ServletContextParameterMap.getParameterValue(ContextParameter.USER_DIRECTORY_PATH) +
 outp
@@ -3878,25 +3878,31 @@
 %09return 
-outputFil
+relativeFilenam
 e;%0A%09%7D%0A%7D%0A

Fix Filepaths in Export to CSV