/**
 * 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.contract.download;

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

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.log4j.Logger;

import eu.dnetlib.data.utility.download.DownloadServiceException;
import eu.dnetlib.data.utility.download.IDownloadService;
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
import eu.dnetlib.enabling.tools.JaxwsServiceResolverImpl;
import eu.dnetlib.enabling.tools.ServiceResolver;

/**
 * The DownloadContractsHelper class.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */
public class DownloadContractsHelper {
	
	/** The Constant log. */
	protected static final Logger log = Logger.getLogger(DownloadContractsHelper.class);
	
	/** The index service facade. */
	private IDownloadService downloadServiceFacade;
	
	/** Result Set service location. */
	private String resultSetLocation;
	
	public String createPushRS() throws ResultSetException {
		W3CEndpointReference epr = proccedCreatePushRS();
		return epr.toString();
	}
	
	public String convertEPR (W3CEndpointReference epr){
		return epr.toString();
	}
	
	public W3CEndpointReference downloadURLsFromRS () throws ResultSetException, DownloadServiceException {
		W3CEndpointReference rsEpr = proccedCreatePushRS();
		W3CEndpointReference dowEpr = downloadServiceFacade.downloadURLsFromRS(rsEpr); 
		return dowEpr;
	}
	
	public W3CEndpointReference proccedCreatePushRS() throws ResultSetException{
		System.out.println(">>>>>:"+ resultSetLocation);
		JaxWsProxyFactoryBean factory0 = new JaxWsProxyFactoryBean();
		factory0.setServiceClass(ResultSetService.class);
		factory0.setAddress(resultSetLocation);
		System.out.println(">>>>>:"+ resultSetLocation);
		System.out.println(">>>>>:"+ factory0);
		System.out.println(">>>>>#:"+ factory0.getAddress()+"#");
		ResultSetService resultSetService1 = (ResultSetService) factory0.create();
		System.out.println(">>>>>:"+ resultSetLocation);
		
		W3CEndpointReference rsEPR = resultSetService1.createPushRS(70, 70);
		if (rsEPR == null) {
			log.debug("Could not create push resultset at:"+ resultSetLocation);
			return null;
		}
		
		final ServiceResolver serviceResolver = new JaxwsServiceResolverImpl();
		final String rsId = serviceResolver.getResourceIdentifier(rsEPR);
		List<String> objectsForStoring = new ArrayList<String>();
		objectsForStoring.add("http://technical.wiki.driver.research-infrastructures.eu/index.php/Main_Page");
		objectsForStoring.add("http://technical.wiki.driver.research-infrastructures.eu/index.php/Architecture_Specification");
		resultSetService1.populateRS(rsId, objectsForStoring);
		resultSetService1.closeRS(rsId);
		
		return rsEPR;
	}
	
	
	/**
	 * Gets the index service facade.
	 * 
	 * @return the index service facade
	 */
	public IDownloadService getDownloadServiceFacade() {
		return downloadServiceFacade;
	}

	/**
	 * Sets the index service facade.
	 * 
	 * @param downloadServiceFacade the new index service facade
	 */
	public void setDownloadServiceFacade(IDownloadService downloadServiceFacade) {
		this.downloadServiceFacade = downloadServiceFacade;
	}
	
	/**
	 * Returns target Result Set service location.
	 * 
	 * @return target Result Set service location
	 */
	public String getResultSetLocation() {
		return resultSetLocation;
	}

	/**
	 * Sets target Result Set service location.
	 * 
	 * @param resultSetLocation the result set location
	 */
	public void setResultSetLocation(String resultSetLocation) {
		this.resultSetLocation = resultSetLocation;
	}
}
