/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *
 */
// https://issues.jboss.org/browse/TEIID-4076
// https://issues.jboss.org/browse/TEIID-2904
// https://issues.jboss.org/browse/TEIID-4995
package org.teiid.translator.excel;

import javax.resource.cci.ConnectionFactory;

import org.apache.poi.ss.usermodel.DataFormatter;
import org.teiid.language.Command;
import org.teiid.language.Insert;
import org.teiid.language.QueryExpression;
import org.teiid.language.Select;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.*;

@Translator(name="excel", description="Excel file translator")
public class ExcelExecutionFactory extends ExecutionFactory<ConnectionFactory, FileConnection> {

	private boolean formatStrings;
	private boolean formatStringsOverrided;
	private boolean allColumnsAsString;
	private ExcelMetadataProcessor metadataProcessor;

	public ExcelExecutionFactory() {
		setSourceRequiredForMetadata(true);
	}

	@Override
	public void start() throws TranslatorException {
		super.start();
		setTransactionSupport(TransactionSupport.NONE);
	}

	@Override
	public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, FileConnection connection)
			throws TranslatorException {
		ExcelExecution ex = new ExcelExecution((Select)command, executionContext, metadata, connection);
		if (metadataProcessor != null) {
			allColumnsAsString = metadataProcessor.getAllColumnsAsString();
		}
		if (allColumnsAsString && !formatStringsOverrided) {
			this.formatStrings = true;
		}
		if (formatStrings) {
			ex.setDataFormatter(new DataFormatter()); //assume default locale
		}
		ex.setAllColumnsAsString(allColumnsAsString);
		return ex;
	}

	@Override
	public MetadataProcessor<FileConnection> getMetadataProcessor() {
		metadataProcessor = new ExcelMetadataProcessor();
		return metadataProcessor;
	}

	@Override
	public boolean supportsCompareCriteriaEquals() {
		return true; // only on ROW_ID
	}

	@Override
	public boolean supportsCompareCriteriaOrdered() {
		return true; //Only on ROW_ID
	}

	@Override
	public boolean supportsRowLimit() {
		return true;
	}

	@Override
	public boolean supportsRowOffset() {
		return true;
	}

	@Override
	public boolean supportsOnlyLiteralComparison() {
		return true;
	}

	@Override
	public boolean supportsInCriteria() {
		return true;
	}
	@TranslatorProperty(display="Format Strings", category= TranslatorProperty.PropertyType.IMPORT, description="Format non-string cell values in a string column according to the worksheet format.", advanced=true)
	public boolean isFormatStrings() {
		return formatStrings;
	}

	public void setFormatStrings(boolean formatStrings) {
		this.formatStrings = formatStrings;
		this.formatStringsOverrided = true;
	}

	public void setAllColumnsAsString(boolean allColumnsAsString) {
		this.allColumnsAsString = allColumnsAsString;
	}
}
