/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * 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.index.ws.dataprov;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;

import org.apache.log4j.Logger;

import eu.dnetlib.common.ws.dataprov.IDataProviderExt;
import eu.dnetlib.common.ws.dataprov.DataProviderException;
import eu.dnetlib.common.ws.dataprov.ResultsResponse;

/**
 * Driver's mock data provider.
 * @author mhorst
 *
 */
@WebService(serviceName="DataProvider",
		endpointInterface="eu.dnetlib.common.ws.dataprov.IDataProvider",
		targetNamespace = "http://schemas.xmlsoap.org/soap/envelope/")
public class MockDataProvider implements IDataProviderExt {
	
	protected static final Logger log = Logger.getLogger(MockDataProvider.class);
	
	public int predefinedResultsCount;
	public static String predefinedStatus = "closed";
	public static int sleepTime = 1;
	
	public String resultContent;
	
	/**
	 * Default constructor.
	 * @param resultSet
	 * @param predefinedResultsCount
	 * @param resultContent
	 */
	public MockDataProvider(int predefinedResultsCount,
			String resultContent) {
		
		if (predefinedResultsCount<0)
			this.predefinedResultsCount = 0;
		else
			this.predefinedResultsCount = predefinedResultsCount;
		this.resultContent = resultContent;
		
	}

	/* (non-Javadoc)
	 * @see eu.dnetlib.data.index.ws.dataprov.IDataProvider#getBulkData(java.lang.String, int, int)
	 */
	public List<String> getBulkData(String bd_id, int fromPosition, int toPosition) throws DataProviderException {
		log.debug("getBulkData call received!");
		log.debug("bd_id: "+bd_id);
		log.debug("fromPosition: "+fromPosition);
		log.debug("toPosition: "+toPosition);
		return Arrays.asList(getSimpleBulkData(bd_id, fromPosition, toPosition));
	}

/* (non-Javadoc)
 * @see eu.dnetlib.data.index.ws.dataprov.IDataProvider#getSimpleBulkData(java.lang.String, int, int)
 */
public String[] getSimpleBulkData(String bd_id, int fromPosition, int toPosition) throws DataProviderException {
		return generateBulkData(fromPosition, toPosition);
	}
	
	/* (non-Javadoc)
	 * @see eu.dnetlib.data.index.ws.dataprov.IDataProvider#getNumberOfResults(java.lang.String)
	 */
	public ResultsResponse getNumberOfResults(String bd_id) throws DataProviderException {
		
		log.debug("getNumberOfResults call received!");
		log.debug("bd_id: "+bd_id);
		
		ResultsResponse resultsResponse = new ResultsResponse();
		resultsResponse.setStatus(predefinedStatus);
		resultsResponse.setTotal(predefinedResultsCount);
		return resultsResponse;
	}

	/**
	 * Generates bulk data.
	 * @param fromPosition
	 * @param toPosition
	 * @return generated bulk data
	 */
	private String[] generateBulkData(int fromPosition, int toPosition) {
		String localResultContent = "This is mock data provider result no ";
//		int fromPositionInt = Integer.valueOf(fromPosition).intValue();
//		int toPositionInt = Integer.valueOf(toPosition).intValue();
		int fromPositionInt = fromPosition;
		int toPositionInt = toPosition;
		if (toPositionInt<fromPositionInt) {
			log.error("FromPosition is greater than toPosition!");
			return null;
		}
		
		String[] results = new String[toPositionInt-fromPositionInt+1];
		for (int i=0; i<toPositionInt-fromPositionInt+1; i++) {
			if (resultContent==null || resultContent.trim().length()==0)
				results[i] = localResultContent + (fromPositionInt+i);
			else
				results[i] = resultContent;
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		return results;
	}

}
