/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * 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 eu.dnetlib.data.sts.das;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.log4j.Logger;

import org.springframework.util.StringUtils;

import eu.dnetlib.common.ws.dataprov.DataProviderException;
import eu.dnetlib.common.ws.dataprov.ResultsResponse;
import eu.dnetlib.data.sts.das.utils.StoreUtils;
import eu.dnetlib.data.sts.das.ws.deliver.IDataAccessServiceDeliver;
import eu.dnetlib.data.sts.dataprov.IDataAccessServiceDataProvider;

/**
 * Store Data Access Service Facade.
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 * @version
 */
@WebService
public class DataAccessServiceFacade implements IDataAccessService {

	protected static final Logger log = Logger
			.getLogger(DataAccessServiceFacade.class);

	/**
	 * Current Store service version.
	 */
	private String serviceVersion;

	/**
	 * Data provider interface
	 */
	private IDataAccessServiceDataProvider dataProvider;

	/**
	 * StoreDeliver interface
	 */
	private IDataAccessServiceDeliver stsDeliver;
	
	/**
	 * Spring init method.
	 */
	public void init() {}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessService#identify()
	 */
	public String identify() {
		return getServiceVersion();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessService#getListOfStoreDataStructures()
	 */
	@Deprecated
	public List<String> getListOfStoreDataStructures() {

		ArrayList<String> stds = StoreUtils.getListOfStoreDataStructures(null);

		if (stds == null || stds.size() == 0)
			return null;
		else
			return stds;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessService#getListOfStoreDataStructuresCSV()
	 */
	@Deprecated
	public String getListOfStoreDataStructuresCSV() {
		ArrayList<String> stds = StoreUtils.getListOfStoreDataStructures(null);

		if (stds == null || stds.size() == 0)
			return null;
		else
			return StringUtils.arrayToCommaDelimitedString(stds
					.toArray(new String[0]));
	}

	/* StS-DELIVER FUNCTIONALITY */
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessServiceDeliver#storeLookUpRS()
	 */
	public W3CEndpointReference storeLookUpRS(String stId, 
			List<String> objectType) 
		throws DataAccessServiceException {
		
		if (stId == null)
			throw new IllegalArgumentException("Wrong parameter! " +
				"StDS identifier can not be null!");
		
		return stsDeliver.storeLookUpRS(stId, objectType);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessServiceDeliver#storeLookUpDataRS()
	 */
	public W3CEndpointReference storeLookUpDataRS(String stId, 
			List<String> objectType) 
		throws DataAccessServiceException {

		if (stId == null)
			throw new IllegalArgumentException("Wrong parameter! " +
				"StDS identifier can not be null!");
		
		return stsDeliver.storeLookUpDataRS(stId, objectType);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see eu.dnetlib.data.sts.das.IDataAccessServiceDeliver#storeLookUpSDORS()
	 */
	public W3CEndpointReference storeLookUpSDORS(String stId, 
			List<String> objectType) 
		throws DataAccessServiceException {

		if (stId == null)
			throw new IllegalArgumentException("Wrong parameter! " +
				"StDS identifier can not be null!");
		
		return stsDeliver.storeLookUpSDORS(stId, objectType);
	}
	
	/* DATA PROVIDER FUNCTIONALITY */
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.unibielefeld.driver.is.dataprov.IDataProvider#getBulkData(java.lang.String,
	 *      int, int)
	 */
	public List<String> getBulkData(String bkid,
			int fromPosition, int toPosition) throws DataProviderException {
		try {
			int i = 0;
			if (i == 1)
				throw new DataProviderException("bdbd");

			return this.dataProvider.getBulkData(bkid, fromPosition,
					toPosition);

		} catch (DataProviderException e) {
			log.error("Exception occured when retrieving bulk data for bdId "
					+ bkid, e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.unibielefeld.driver.is.dataprov.IDataProvider#getSimpleBulkData(java.lang.String,
	 *      int, int)
	 */
	public String[] getSimpleBulkData(String bkId, int fromPosition,
			int toPosition) throws DataProviderException {
		try {
			int i = 0;
			if (i == 1)
				throw new DataProviderException("bdbd");
			return this.dataProvider.getSimpleBulkData(bkId,
					fromPosition, toPosition);
		} catch (DataProviderException e) {
			log.error("Exception occured when retrieving bulk data for bdId "
					+ bkId, e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.unibielefeld.driver.is.dataprov.IDataProvider#getNumberOfResults(java.lang.String)
	 */
	public ResultsResponse getNumberOfResults(String bkId)
			throws DataProviderException {
		try {
			int i = 0;
			if (i == 1)
				throw new DataProviderException("bdbd");

			return this.dataProvider.getNumberOfResults(bkId);
		} catch (DataProviderException e) {
			log.error(
					"Exception occured when retrieving number of results for bdId "
							+ bkId, e);
			throw e;
		}
	}

	/**
	 * Returns current service version.
	 * 
	 * @return current service version
	 */
	public String getServiceVersion() {
		return this.serviceVersion;
	}

	/**
	 * Sets current service version.
	 * 
	 * @param serviceVersion
	 */
	public void setServiceVersion(String serviceVersion) {
		this.serviceVersion = serviceVersion;
	}
	
	/**
	 * Returns data provider reference.
	 * 
	 * @return dataProvider
	 */
	public IDataAccessServiceDataProvider getDataProvider() {
		return this.dataProvider;
	}

	/**
	 * Sets data provider reference.
	 * 
	 * @param dataProvider
	 */
	public void setDataProvider(
			IDataAccessServiceDataProvider storeDataProvider) {
		this.dataProvider = storeDataProvider;
	}

	/**
	 * Returns deliver interface reference.
	 * 
	 * @return stsDeliver
	 */
	public IDataAccessServiceDeliver getStsDeliver() {
		return this.stsDeliver;
	}

	/**
	 * Sets deliver interface reference.
	 * 
	 * @param stsDeliver
	 */
	public void setstsDeliver(
			IDataAccessServiceDeliver stsDeliver) {
		this.stsDeliver = stsDeliver;
	}

}
