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;

public class TimeWatcher {
static Logger logger = Logger.getLogger(SVNCollector.class);
	
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		//Set the project you want to test
		String project = "weberp";
		
		//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 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"));
		
		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);	
			for (Iterator entries = logEntries.iterator(); entries.hasNext();) {
				SVNLogEntry logEntry = (SVNLogEntry) entries.next();
				System.out.println("Revision " + logEntry.getRevision() + ", Date: " + logEntry.getDate());
				
			}
		} catch (SVNException e) {
			logger.error(e);
		}
	
	}
}
