<commit_before>package uk.ac.ed.inf.Metabolic.excelexport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.pathwayeditor.businessobjectsAPI.IMap;
import org.pathwayeditor.contextadapter.publicapi.ExportServiceException;
import org.pathwayeditor.contextadapter.publicapi.IContext;
import org.pathwayeditor.contextadapter.publicapi.IContextAdapterExportService;
import org.pathwayeditor.contextadapter.publicapi.IContextAdapterServiceProvider;
import org.pathwayeditor.contextadapter.publicapi.IValidationReport;

import uk.ac.ed.inf.Metabolic.ExportAdapterCreationException;
import uk.ac.ed.inf.Metabolic.MetabolicContextValidationService;
import uk.ac.ed.inf.Metabolic.ndomAPI.IModel;

public class ExcelExportService implements IContextAdapterExportService {

	String DISPLAY_NAME = "Excel export";

	String RECOMMENDED_SUFFIX = "xls";
	
	String PROPERTIES_LOCATION = "/templatepath.properties" ;

	final String TYPECODE = "Excel_Export_1.0.0";
	private IContext context;
	private IExcelFileGenerator generator;

	public ExcelExportService(IContextAdapterServiceProvider provider) {
		this.serviceProvider = provider;
		context = provider.getContext();
	}

	private IContextAdapterServiceProvider serviceProvider;

	public IContextAdapterServiceProvider getServiceProvider() {
		return serviceProvider;
	}

	/**
	 * @throws IllegalArgumentException
	 *             if either argument is null
	 * @throws ExportServiceException
	 *             if exportFile:
	 *             <ul>
	 *             <li> Doesn't exist
	 *             <li> Is not writable.
	 *             <li> Cannot produce valid Excel
	 *             </ul>
	 */
	public void exportMap(IMap map, File exportFile)
	throws ExportServiceException {
FileOutputStream fos = null;
try {
	checkArgs(map, exportFile);

	MetabolicContextValidationService validator = (MetabolicContextValidationService) serviceProvider
			.getValidationService();
	validator.setMapToValidate(map);
	IModel ndom = null;
	if (validator.isReadyToValidate()) {
		validator.validateMap();
		IValidationReport report =validator.getValidationReport();
		if(!report.isMapValid()){
			String sb="Map is not valid:\n";
			
			throw new ExportServiceException(sb, report);
		}else{
			ndom=validator.getModel();
		}
		System.out.println(validator.getValidationReport());
		System.out.println(ndom.toString());
		
		generator = getGenerator( ndom );
		
		generator.createNewWorkbook();
		
		if ( !generator.wasWorkBookCreated() )
			throw new ExportServiceException ( "Workbook generator was not created propery" ) ;
		
		try {
		
		generator.populateModelPage() ;
		generator.populateCompoundPage();
		generator.populateReactionsPage();
		generator.populateMacromoleculePage() ;
		}
		catch (IllegalStateException e)
		{
			throw new ExportServiceException ( "There was a problem while generating the Excel file.") ;
		}
		
		generator.saveWorkBook(exportFile) ;
	}
} catch (ExportAdapterCreationException e) {
	throw new ExportServiceException(e);
} catch (IOException e) {
	throw new ExportServiceException(e);
} finally {
	try {
		if (fos != null)
			fos.close();
	} catch (Exception e) {
	}

}

}

	private void checkArgs(IMap map, File exportFile)
			throws ExportServiceException, IOException {

		if (map == null || exportFile == null
				|| map.getTheSingleRootMapObject() == null) {
			throw new IllegalArgumentException("Arguments must not be null");
		}
		exportFile.createNewFile();

		if (!exportFile.canWrite()) {<commit_msg>Rule-based error reporting system in Metabolic context<commit_after>package uk.ac.ed.inf.Metabolic.excelexport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.pathwayeditor.businessobjectsAPI.IMap;
import org.pathwayeditor.contextadapter.publicapi.ExportServiceException;
import org.pathwayeditor.contextadapter.publicapi.IContext;
import org.pathwayeditor.contextadapter.publicapi.IContextAdapterExportService;
import org.pathwayeditor.contextadapter.publicapi.IContextAdapterServiceProvider;
import org.pathwayeditor.contextadapter.publicapi.IContextAdapterValidationService;
import org.pathwayeditor.contextadapter.publicapi.IValidationReport;
import org.pathwayeditor.contextadapter.toolkit.validation.ContextValidationService;

import uk.ac.ed.inf.Metabolic.ExportAdapterCreationException;
import uk.ac.ed.inf.Metabolic.MetabolicNDOMValidationService;
import uk.ac.ed.inf.Metabolic.ndomAPI.IModel;

public class ExcelExportService implements IContextAdapterExportService {

	String DISPLAY_NAME = "Excel export";

	String RECOMMENDED_SUFFIX = "xls";
	
	String PROPERTIES_LOCATION = "/templatepath.properties" ;

	final String TYPECODE = "Excel_Export_1.0.0";
	private IContext context;
	private IExcelFileGenerator generator;

	public ExcelExportService(IContextAdapterServiceProvider provider) {
		this.serviceProvider = provider;
		context = provider.getContext();
	}

	private IContextAdapterServiceProvider serviceProvider;

	public IContextAdapterServiceProvider getServiceProvider() {
		return serviceProvider;
	}

	/**
	 * @throws IllegalArgumentException
	 *             if either argument is null
	 * @throws ExportServiceException
	 *             if exportFile:
	 *             <ul>
	 *             <li> Doesn't exist
	 *             <li> Is not writable.
	 *             <li> Cannot produce valid Excel
	 *             </ul>
	 */
	public void exportMap(IMap map, File exportFile)
	throws ExportServiceException {
FileOutputStream fos = null;
try {
	checkArgs(map, exportFile);

//	MetabolicContextValidationService validator = (MetabolicContextValidationService) serviceProvider
//			.getValidationService();
	
	ContextValidationService validator = (ContextValidationService) serviceProvider
	.getValidationService();
	validator.setMapToValidate(map);
	IModel ndom = null;
	if (validator.isReadyToValidate()) {
		validator.validateMap();
		IValidationReport report =validator.getValidationReport();
		if(!report.isMapValid()){
			String sb="Map is not valid:\n";
			
			throw new ExportServiceException(sb, report);
		}else{
			ndom=getModel(validator);
		}
		System.out.println(validator.getValidationReport());
		System.out.println(ndom.toString());
		
		generator = getGenerator( ndom );
		
		generator.createNewWorkbook();
		
		if ( !generator.wasWorkBookCreated() )
			throw new ExportServiceException ( "Workbook generator was not created propery" ) ;
		
		try {
		
		generator.populateModelPage() ;
		generator.populateCompoundPage();
		generator.populateReactionsPage();
		generator.populateMacromoleculePage() ;
		}
		catch (IllegalStateException e)
		{
			throw new ExportServiceException ( "There was a problem while generating the Excel file.") ;
		}
		
		generator.saveWorkBook(exportFile) ;
	}
} catch (ExportAdapterCreationException e) {
	throw new ExportServiceException(e);
} catch (IOException e) {
	throw new ExportServiceException(e);
} finally {
	try {
		if (fos != null)
			fos.close();
	} catch (Exception e) {
	}

}

}

	IModel getModel(IContextAdapterValidationService validator) {
		if(validator.getValidationReport().isMapValid()){
		return (IModel) MetabolicNDOMValidationService.getInstance(serviceProvider).getNDOM();
		}else{
			return null;
		}
	}

	private void checkArgs(IMap map, File exportFile)
			throws ExportServiceException, IOException {

		if (map == null || exportFile == null
				|| map.getTheSingleRootMapObject() == null) {
			throw new IllegalArgumentException("Arguments must not be null");
		}
		exportFile.createNewFile();

		if (!exportFile.canWrite()) {
{'fraction_non_alphanumeric': 0.09567021000777806, 'fraction_numerical': 0.00077780658542909, 'mean_word_length': 4.757462686567164, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226764', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2159, 'n_words': 469}