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

package org.teiid.translator.jdbc;

import java.sql.SQLException;
import java.util.Arrays;

import org.teiid.core.BundleUtil;
import org.teiid.core.util.ExceptionUtil;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.jdbc.plugin.JDBCPlugin;


public class JDBCExecutionException extends TranslatorException {

	private static final long serialVersionUID = 1758087499488916573L;

	public JDBCExecutionException(BundleUtil.Event event, SQLException error,TranslatedCommand... commands) {
		// EXTENSION
		// ISSUE: DVCORE-630,DVCORE-2408
		// FIXED BY: Salvatore Raunich,Roman Pedchenko
		// DATE: 2013.10.16,2014.10.03
//		super(error, commands == null || commands.length == 0 ? event.toString()+":"+error.getMessage() : event.toString()+":"+JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11004, Arrays.toString(commands))); //$NON-NLS-1$ //$NON-NLS-2$ 
		super(error, commands == null || commands.length == 0 ? event.toString()+":"+ExceptionUtil.getLinkedMessagesVerbose(error) : event.toString()+":"+ExceptionUtil.getLinkedMessagesVerbose(error)+":"+ JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11004, Arrays.toString(commands))); //$NON-NLS-1$ //$NON-NLS-2$
		// END EXTENSION
		setCode(String.valueOf(error.getErrorCode()));
	}

	// EXTENSION
	// ISSUE: DVCORE-2440,DVCORE-2581
	// FIXED BY: Roman Pedchenko
	// DATE: 2014.10.18,2015.01.28
	// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2176,https://issues.jboss.org/browse/TEIID-2177
	public JDBCExecutionException(BundleUtil.Event event, SQLException error, String command) {
		super(error, event.toString()+":"+JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11004, command)+":"+ExceptionUtil.getLinkedMessagesVerbose(error)); //$NON-NLS-1$
		setCode(String.valueOf(error.getErrorCode()));
	}
	
	// END EXTENSION
	// EXTENSION
	// ISSUE: DVCORE-764, DVCORE-1296
	// FIXED BY: Roman Pedchenko, Salvatore Raunich
	// DATE: 2013.10.27, 2014.06.02
	@Override
	public String getMessage() {
		String nextExc = "";
		if (getSuppressed() != null || getSuppressed().length != 0) {
			for (int i = 0; i < getSuppressed().length; i++) {
				nextExc += extractNestedExceptions(getSuppressed()[i]);
			}
		}
		nextExc += extractNextException(this);
		return nextExc + super.getMessage();
	}
	
	private String extractNextException(Throwable exc) {
		if (exc.getCause() instanceof SQLException && ((SQLException) exc.getCause()).getNextException() != null) {
			return "\nNext Exception: " + ((SQLException) exc.getCause()).getNextException().getMessage();
		}
		return "";
	}

	private String extractNestedExceptions(Throwable exc) {
		Throwable parentCause = exc;
		Throwable nestedCause = exc.getCause();
		String result = "";
		while (nestedCause != null && !nestedCause.equals(parentCause)) {
			result += nestedCause.getMessage() + "\n";
			parentCause = nestedCause;
			nestedCause = nestedCause.getCause();
		}
		if (!"".equals(result)) {
			return "\nNested Exceptions: " + result;
		}
		return "";
	}
	// END EXTENSION
}
