Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 Michael Ruflin, Andr´┐Ż Locher, Claudia von Bastian. This file is part of Tatool. Tatool is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Tatool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Tatool. If not, see <http://www.gnu.org/licenses/>. /
 
 package ch.tatool.app.export;
 
 import java.io.File;
 import java.util.List;
 
Exports session data into a CSV file. This is a helper class used by the various exporters

Author(s):
Andre Locher
 
 public class CSVSessionDataExport {
 
 	Logger logger = LoggerFactory.getLogger(CSVSessionDataExport.class);
 
 
 	private int basePropertiesSize;
 	private int sessionPropertiesSize;
 
 	public CSVSessionDataExport(DataService dataService) {
 		this. = dataService;
 
 		// this won't change
 	}

Exports the session data and stores the results in the provided Writer

Parameters:
module
file the file to write the data to
Returns:
a file containing the module data or null in case an error occured
 
 	public File exportData(Module moduleint fromSessionIndex) {
 		// initialize the trial properties
 
 		// create a writer
 		try {
 			File tmpFile = File.createTempFile("sessionData""csv");
 			FileOutputStream fos = new FileOutputStream(tmpFilefalse);
 			CSVWriter writer = new CSVWriter(new OutputStreamWriter(fos,
 					"ISO-8859-1"), ';');
 
 			// write the csv file header
 			List<Stringheaders = new ArrayList<String>();
 			writer.writeNext(headers.toArray(new String[headers.size()]));
 
 			// fetch each session and trial, write one line for each trial
 			List<ModuleSessionsessions = .getSessions(module);
 			for (ModuleSession session : sessions) {
 				// skip if we only export some of the session
 				if (fromSessionIndex >= 0
 						&& session.getIndex() < fromSessionIndex) {
 					continue;
 				}
 				List<String[]> sessionData = getDataForSession(session);
 				writer.writeAll(sessionData);
 			}
 
 			// close the writer and return the file
			writer.close();
			return tmpFile;
catch (IOException ioe) {
			.error("Unable to write csv file"ioe);
			return null;
		}
	}

Returns a list with all to be written values for a given trial object.
	private List<String[]> getDataForSession(ModuleSession session) {
		// add trial properties
		addSessionProperties(sessiondata);
		// done already
		return data;
	}
	// Base properties management
Basic properties which are always written.
	private void initBaseProperties() {
		// Module related
		.add("Module id");
		.add("Module name");
		// Session related
		.add("Session id");
		.add("Session index");
		.add("Session start time");
		.add("Session end time");
		.add("Session completed");
	}
	private String[] getBaseProperties(ModuleSession session) {
		// session related
		String[] baseProps = new String[7];
		baseProps[0] = session.getModule().getId().toString();
		baseProps[1] = session.getModule().getName();
		baseProps[2] = session.getId().toString();
		baseProps[3] = session.getIndex() + "";
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		StringBuilder sessionStartTime = new StringBuilder("");
		StringBuilder sessionEndTime = new StringBuilder("");
		if (session.getStartTime() != null) {
			sessionStartTime = new StringBuilder(dateformat.format(session
		}
		if (session.getEndTime() != null) {
			sessionEndTime = new StringBuilder(dateformat.format(session
		}
		baseProps[4] = sessionStartTime.toString();
		baseProps[5] = sessionEndTime.toString();
		baseProps[6] = String.valueOf(session.getCompleted());
		return baseProps;
	}
	private void addToData(List<String[]> dataString[] values) {
		if (values != null) {
			data.add(values);
		}
	}
	// Session properties management
	private void initSessionProperties(Module module) {
		// add additional headers for session properties
		.add("ELEMENT_ID");
		.add("PROPERTY_NAME");
		.add("PROPERTY_VALUE");
	}

Adds the session properties to the data object
	private void addSessionProperties(ModuleSession sessionList<String[]> data) {
		List<StringsessionProps = null;
		if (session.getEntries().isEmpty()) {
			sessionProps = new ArrayList<String>();
			sessionProps.addAll(Arrays.asList(getBaseProperties(session)));
			addToData(datasessionProps.toArray(new String[sessionProps.size()]));
else {
			for (Entry p : session.getEntries()) {
				sessionProps = new ArrayList<String>();
				sessionProps.addAll(Arrays.asList(getBaseProperties(session)));
				sessionProps.add(p.getNodeId());
				sessionProps.add(p.getName());
				sessionProps.add(p.getValue());
				addToData(datasessionProps.toArray(new String[sessionProps
						.size()]));
			}
		}
	}
New to GrepCode? Check out our FAQ X