package cn.edu.seu.ise.dca.analysis.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.io.SVNRepository;

import cn.edu.seu.ise.dca.analysis.RepositoryFactory;
import cn.edu.seu.ise.dca.analysis.SVNCollector;
import cn.edu.seu.ise.dca.global.Global;
import cn.edu.seu.ise.dca.util.ExcelWriter;

public class Generator {
	
	static Logger logger = Logger.getLogger(SVNCollector.class);
	
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		//Set the project you want to test
		String project = "tikiwiki";
		
		//Set the log properties
		PropertyConfigurator.configure( "log4j.properties" );
		
		//Get the properties defined for project settings
		String settings = "cn/edu/seu/ise/dca/properties/projects.properties";
		Properties properties = new Properties();
		InputStream in = ClassLoader.getSystemResourceAsStream(settings);  
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		//Get the properties of the selected project
		String projectSettings = properties.getProperty(project);
		in = ClassLoader.getSystemResourceAsStream(projectSettings);
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//Get the information of selected project
		String projectName = properties.getProperty("name");
		String url = properties.getProperty("url");
		String schemaUrl = properties.getProperty("schema");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		long startRevision = Long.parseLong(properties.getProperty("start"));
		long endRevision = Long.parseLong(properties.getProperty("end"));
		
		
		//Set the output path for checkout and file path
		Global.setOutputDir(projectName);
		//Set the file path of generated statistical file
		String reportPath = Global.OUTPUT_DIR + projectName + "_Rev" + startRevision + "_" + endRevision + "_schema_template.xls";
		
		//Connect to the selected SVN repository
		SVNRepository repository = RepositoryFactory.getRepository(url, username, password);
		Collection logEntries = null;
		/*
		 * Set the paths that mean only those revisions at which they were
		 * changed
		 */
		try {
			logEntries = repository.log(new String[]{schemaUrl}, null, startRevision, endRevision, true, true);		
		} catch (SVNException e) {
			logger.error(e);
		}
		
		//Create a writer to write the content into the excel
		ExcelWriter writer = new ExcelWriter(reportPath);
		writer.addCaption(1, 1, "Revision");
		int row = 1;
		for (Iterator entries = logEntries.iterator(); entries.hasNext();) {
			SVNLogEntry logEntry = (SVNLogEntry) entries.next();
			row ++;
			writer.addNumber(1, row, logEntry.getRevision());
		}
		writer.close();
	}

}
