/*
 * 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

package org.teiid.translator.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.resource.ResourceException;
import javax.resource.cci.ConnectionFactory;

import com.datavirtuality.dv.core.interfaces.DVCaller;
import org.apache.commons.io.input.BOMInputStream;
import org.teiid.core.BundleUtil;
import org.teiid.core.TeiidRuntimeException;
import org.teiid.core.types.BlobImpl;
import org.teiid.core.types.BlobType;
import org.teiid.core.types.ClobImpl;
import org.teiid.core.types.ClobType;
import org.teiid.core.types.InputStreamFactory.FileInputStreamFactory;
import org.teiid.core.util.ObjectConverterUtil;
import org.teiid.core.util.ReaderInputStream;
import org.teiid.language.Call;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.metadata.MetadataFactory;
import org.teiid.metadata.Procedure;
import org.teiid.metadata.ProcedureParameter;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.ProcedureParameter.Type;
import org.teiid.translator.DataNotAvailableException;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ExecutionFactory;
import org.teiid.translator.FileConnection;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.TranslatorProperty;
import org.teiid.translator.TypeFacility;
import org.teiid.util.CharsetUtils;

@Translator(name="file", description="File Translator, reads contents of files or writes to them")
public class FileExecutionFactory extends ExecutionFactory<ConnectionFactory, FileConnection> {

	enum Event implements BundleUtil.Event {
		DVCORE94089,
	}
	
	private final class FileProcedureExecution implements ProcedureExecution {
		private final Call command;
		private final FileConnection fc;
		private File[] files = null;
		boolean isText = false;
		private int index;

		// EXTENSION
		// ISSUE: DVCORE-730
		// FIXED BY: Salvatore Raunich
		// DATE: 2014.07.29
		private Object value = null;		
		// END EXTENSION
		
		private FileProcedureExecution(Call command, FileConnection fc) {
			this.command = command;
			this.fc = fc;
		}

		@Override
		public void execute() throws TranslatorException {
			String path = (String)command.getArguments().get(0).getArgumentValue().getValue();
			try {
				files = FileConnection.Util.getFiles(path, fc);
			} catch (ResourceException e) {
				throw new TranslatorException(e);
			}
			if (files == null && exceptionIfFileNotFound) {
				throw new TranslatorException(UTIL.getString("file_not_found", path)); //$NON-NLS-1$
			}
			LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Getting", files != null ? files.length : 0, "file(s)"); //$NON-NLS-1$ //$NON-NLS-2$
			String name = command.getProcedureName();
			if (name.equalsIgnoreCase(GETTEXTFILES)) {
				isText = true;
			} else if (!name.equalsIgnoreCase(GETFILES)) {
				throw new TeiidRuntimeException(UTIL.gs(Event.DVCORE94089, name));
			}
		}

		@Override
		public void close() {
			
		}

		@Override
		public void cancel() throws TranslatorException {
			// EXTENSION
			// ISSUE: DVCORE-730
			// FIXED BY: Salvatore Raunich
			// DATE: 2014.07.29
			try {
				if (value instanceof BlobType) {
					BlobType b = (BlobType) value;
					b.free();
				} else if (value instanceof ClobType) {
					ClobType c = (ClobType) value;
					c.free();
				}
			} catch (SQLException e) {
				throw new TranslatorException(e);
			}
			// END EXTENSION
		}

		@Override
		public List<?> next() throws TranslatorException, DataNotAvailableException {
			if (files == null || index >= files.length) {
				return null;
			}
			ArrayList<Object> result = new ArrayList<Object>(10);
			final File file = files[index++];
			LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Getting", file); //$NON-NLS-1$
			// BUG: need to skip UTF8 BOM
			// ISSUE: DVCORE-385
			// SOLUTION: Added using BOMInputStream from apache commons.io
			// FIXED BY: Roman Pedchenko
			// DATE: 2013.04.19
//			FileInputStreamFactory isf = new FileInputStreamFactory(file);
			FileInputStreamFactory isf = new FileInputStreamFactory(file) {
				@Override
				public InputStream getInputStream() throws IOException {
					return new BOMInputStream(super.getInputStream());
				}
			};
			// END BUG
			isf.setLength(file.length());
			// EXTENSION
			// ISSUE: DVCORE-730
			// FIXED BY: Salvatore Raunich
			// DATE: 2014.07.29
//			Object value = null;
			this.value = null;
			// END EXTENSION
			if (isText) {
				ClobImpl clob = new ClobImpl(isf, -1);
				clob.setEncoding(encoding.name());
				value = new ClobType(clob);
			} else {
				value = new BlobType(new BlobImpl(isf));
			}
			result.add(value);
			result.add(file.getName());
			// EXTENSION
			// ISSUE: DVCORE-690
			// FIXED BY: Roman Pedchenko
			// DATE: 2015.02.07
			try {
				BasicFileAttributes attr = Files.readAttributes(Paths.get(file.getCanonicalPath()), BasicFileAttributes.class);
				result.add(new Timestamp(attr.creationTime().toMillis()));
				result.add(new Timestamp(attr.lastAccessTime().toMillis()));
				result.add(new Timestamp(attr.lastModifiedTime().toMillis()));
				result.add(attr.isDirectory());
				result.add(attr.isOther());
				result.add(attr.isRegularFile());
				result.add(attr.isSymbolicLink());
				result.add(attr.size());
			} catch (IOException e) {
				throw new TranslatorException(e);
			}
			// END EXTENSION
			return result;
		}

		@Override
		public List<?> getOutputParameterValues() throws TranslatorException {
			return Collections.emptyList();
		}
	}

	public static BundleUtil UTIL = BundleUtil.getBundleUtil(FileExecutionFactory.class);
	
	public static final String GETTEXTFILES = "getTextFiles"; //$NON-NLS-1$
	public static final String GETFILES = "getFiles"; //$NON-NLS-1$
	public static final String SAVEFILE = "saveFile"; //$NON-NLS-1$
	
	private Charset encoding = Charset.defaultCharset();
	// EXTENSION
	// ISSUE: DVCORE-1120
	// FIXED BY: Roman Pedchenko
	// DATE: 2014.04.10
//	private boolean exceptionIfFileNotFound;
	private boolean exceptionIfFileNotFound = true;
	// END EXTENSION
	
	public FileExecutionFactory() {
		setTransactionSupport(TransactionSupport.NONE);
		setSourceRequiredForMetadata(false);
	}
	
	@TranslatorProperty(display="File Encoding",advanced=true)
	public String getEncoding() {
		return encoding.name();
	}
	
	public void setEncoding(String encoding) {
		this.encoding = CharsetUtils.getCharset(encoding);
	}
	
	@TranslatorProperty(display="Exception if file not found",advanced=true)
	public boolean isExceptionIfFileNotFound() {
		return exceptionIfFileNotFound;
	}
	
	public void setExceptionIfFileNotFound(boolean exceptionIfFileNotFound) {
		this.exceptionIfFileNotFound = exceptionIfFileNotFound;
	}
	
	//@Override
	public ProcedureExecution createProcedureExecution(final Call command,
			final ExecutionContext executionContext, final RuntimeMetadata metadata,
			final FileConnection fc) throws TranslatorException {
		if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
			return new ProcedureExecution() {
				
				@Override
				public void execute() throws TranslatorException {
					String filePath = (String)command.getArguments().get(0).getArgumentValue().getValue();
					Object file = command.getArguments().get(1).getArgumentValue().getValue();
					if (file == null || filePath == null) {
						throw new TranslatorException(UTIL.getString("non_null")); //$NON-NLS-1$
					}
					LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath); //$NON-NLS-1$
					InputStream is = null;
					try {
						Boolean isFileUnzipped = (Boolean) DVCaller.call(222, file, fc.getFile(filePath).getAbsolutePath());
						if (Boolean.TRUE.equals(isFileUnzipped)) {
							return;
						}
						if (file instanceof SQLXML) {
							is = ((SQLXML)file).getBinaryStream();
						} else if (file instanceof Clob) {
							is = new ReaderInputStream(((Clob)file).getCharacterStream(), encoding);
						} else if (file instanceof Blob) {
							is = ((Blob)file).getBinaryStream();
						} else {
							throw new TranslatorException(UTIL.getString("unknown_type")); //$NON-NLS-1$
						}
					
						ObjectConverterUtil.write(is, fc.getFile(filePath));
					} catch (IOException e) {
						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
					} catch (SQLException e) {
						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
					} catch (ResourceException e) {
						throw new TranslatorException(e, UTIL.getString("error_writing")); //$NON-NLS-1$
					}
				}
				
				@Override
				public void close() {
				}
				
				@Override
				public void cancel() throws TranslatorException {
				}
				
				@Override
				public List<?> next() throws TranslatorException, DataNotAvailableException {
					return null;
				}
				
				@Override
				public List<?> getOutputParameterValues() throws TranslatorException {
					return Collections.emptyList();
				}
			};
		}
		return new FileProcedureExecution(command, fc);
	}

	@Override
	public void getMetadata(MetadataFactory metadataFactory, FileConnection connection) throws TranslatorException {
		Procedure p = metadataFactory.addProcedure(GETTEXTFILES);
		p.setRemark("Returns text files that match the given path and pattern as CLOBs"); //$NON-NLS-1$
		ProcedureParameter param = metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$
		param.setRemark("The path and pattern of what files to return.  Currently the only pattern supported is *.<ext>, which returns only the files matching the given extension at the given path."); //$NON-NLS-1$
		metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.CLOB, p); //$NON-NLS-1$
		metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$
		// EXTENSION
		// ISSUE: DVCORE-690
		// FIXED BY: Roman Pedchenko
		// DATE: 2015.02.07
		metadataFactory.addProcedureResultSetColumn("creationTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p);
		metadataFactory.addProcedureResultSetColumn("lastAccessTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p);
		metadataFactory.addProcedureResultSetColumn("lastModifiedTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p);
		metadataFactory.addProcedureResultSetColumn("isDirectory", TypeFacility.RUNTIME_NAMES.BOOLEAN, p);
		metadataFactory.addProcedureResultSetColumn("isOther", TypeFacility.RUNTIME_NAMES.BOOLEAN, p);
		metadataFactory.addProcedureResultSetColumn("isRegularFile", TypeFacility.RUNTIME_NAMES.BOOLEAN, p);
		metadataFactory.addProcedureResultSetColumn("isSymbolicLink", TypeFacility.RUNTIME_NAMES.BOOLEAN, p);
		metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p);
		// END EXTENSION
		
		Procedure p1 = metadataFactory.addProcedure(GETFILES);
		p1.setRemark("Returns text files that match the given path and pattern as BLOBs"); //$NON-NLS-1$
		param = metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p1); //$NON-NLS-1$
		param.setRemark("The path and pattern of what files to return.  Currently the only pattern supported is *.<ext>, which returns only the files matching the given extension at the given path."); //$NON-NLS-1$
		metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.BLOB, p1); //$NON-NLS-1$
		metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p1); //$NON-NLS-1$
		// EXTENSION
		// ISSUE: DVCORE-690
		// FIXED BY: Roman Pedchenko
		// DATE: 2015.02.07
		metadataFactory.addProcedureResultSetColumn("creationTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p1);
		metadataFactory.addProcedureResultSetColumn("lastAccessTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p1);
		metadataFactory.addProcedureResultSetColumn("lastModifiedTime", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p1);
		metadataFactory.addProcedureResultSetColumn("isDirectory", TypeFacility.RUNTIME_NAMES.BOOLEAN, p1);
		metadataFactory.addProcedureResultSetColumn("isOther", TypeFacility.RUNTIME_NAMES.BOOLEAN, p1);
		metadataFactory.addProcedureResultSetColumn("isRegularFile", TypeFacility.RUNTIME_NAMES.BOOLEAN, p1);
		metadataFactory.addProcedureResultSetColumn("isSymbolicLink", TypeFacility.RUNTIME_NAMES.BOOLEAN, p1);
		metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p1);
		// END EXTENSION
		
		Procedure p2 = metadataFactory.addProcedure(SAVEFILE);
		p2.setRemark("Saves the given vale to the given path.  Any existing file will be overriden."); //$NON-NLS-1$
		metadataFactory.addProcedureParameter("filePath", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p2); //$NON-NLS-1$
		param = metadataFactory.addProcedureParameter("file", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p2); //$NON-NLS-1$
		param.setRemark("The contents to save.  Can be one of CLOB, BLOB, or XML"); //$NON-NLS-1$

		File parentDirectory = connection.getParentDirectory();
		metadataFactory.getSchema().setProperty(FileConnection.FILE_SCHEMA_DIRECTORY, parentDirectory != null ? parentDirectory.getAbsolutePath() : "");
	} 
	
	@Override
	public boolean areLobsUsableAfterClose() {
		return true;
	}
	
}
