/*
 *  Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 *  Licensed under the Apache License, Version 2.0 the "License";
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.culturegraph.mf.morph.maps;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.culturegraph.mf.exceptions.MorphException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author Daniel
 * 
 */
public final class JndiSqlMap extends AbstractReadOnlyMap<String, String> implements Closeable {

	private static final Logger LOG = LoggerFactory.getLogger(JndiSqlMap.class);
	private DataSource datasource;
	private String query;
	private Connection connection;
	

	@Override
	public void close() throws IOException {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			throw new MorphException(e);
		}
	}

	protected DataSource getDatasource() {
		return datasource;
	}

	public void setDatasource(final String name) {
		try {
			this.datasource = (DataSource) new InitialContext().lookup(name);
		} catch (NamingException e) {
			throw new MorphException(e);
		}
	}

	public void setQuery(final String query) {
		this.query = query;
	}

	@Override
	public String get(final Object key) {
		String resultString = null;
		final ResultSet resultSet;
		PreparedStatement stmt = null;
		Connection con = null;
		try {
			con = datasource.getConnection();
			stmt = con.prepareStatement(query);
			stmt.setString(1, key.toString());
			resultSet = stmt.executeQuery();
			if (resultSet.first()) {
				resultString = resultSet.getString(1);
			}
			resultSet.close();
		} catch (SQLException e) {
			throw new MorphException(e);
		} finally {
			if(stmt != null){
				try {
					stmt.close();
				} catch (SQLException e) {
					LOG.error("Can't close SQL-Statement.", e);
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					LOG.error("Can't close Connection.", e);
				}
			}
		}
		return resultString;
	}

}

@@ -837,35 +837,9 @@
 on;%0A
-import java.sql.Statement;
 %0A
+
 impo
@@ -1074,17 +1074,16 @@
 aniel%0A *
- 
 %0A */%0Apub
@@ -1313,230 +1313,8 @@
 ery;
-%0A%09private Connection connection;%0A%09%0A%0A%09@Override%0A%09public void close() throws IOException %7B%0A%09%09try %7B%0A%09%09%09if (connection != null) %7B%0A%09%09%09%09connection.close();%0A%09%09%09%7D%0A%09%09%7D catch (SQLException e) %7B%0A%09%09%09throw new MorphException(e);%0A%09%09%7D%0A%09%7D
 %0A%0A%09p
@@ -1512,16 +1512,22 @@
  catch (
+final 
 NamingEx
@@ -2075,32 +2075,38 @@
 e();%0A%09%09%7D catch (
+final 
 SQLException e) 
@@ -2208,32 +2208,38 @@
 );%0A%09%09%09%09%7D catch (
+final 
 SQLException e) 
@@ -2361,16 +2361,22 @@
  catch (
+final 
 SQLExcep
@@ -2446,16 +2446,16 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09return
@@ -2473,10 +2473,86 @@
 ng;%0A%09%7D%0A%0A
+%09@Override%0A%09public void close() throws IOException %7B%0A%09%09// Nothing to do%0A%09%7D%0A%0A
 %7D%0A

Remove dead code from `JndiSqlMap`