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

import static org.junit.Assert.assertNotNull;

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

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Before;
import org.junit.Test;

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 Class SimpleClient.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */
public class SimpleClient {

	private ResultSetService downloadRS;
	
	private ResultSetService pushRS;
	
	private IDownloadService download;

	/**
	 * Sets the up.
	 * 
	 */
	@Before 
	public void setUp() {
		
		JaxWsProxyFactoryBean factory0 = new JaxWsProxyFactoryBean();
		factory0.setServiceClass(ResultSetService.class);
		factory0.setAddress(
				"http://129.70.40.102:8280/is/services/resultSet");
		pushRS = (ResultSetService) factory0.create();
		assertNotNull(pushRS.identify());
		System.out.println("Identify Push RS: " + pushRS.identify() );
		
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ResultSetService.class);
		factory.setAddress(
				"http://localhost:8880/dnet-download/services/DownloadResultSet");
		downloadRS = (ResultSetService) factory.create();
		assertNotNull(downloadRS.identify());
		System.out.println("Identify Read from RS: " + downloadRS.identify() );
		
		JaxWsProxyFactoryBean factory1 = new JaxWsProxyFactoryBean();
		factory1.setServiceClass(IDownloadService.class);
		factory1.setAddress(
				"http://localhost:8880/dnet-download/services/DownloadService");
		download = (IDownloadService) factory1.create();
		assertNotNull(download.identify());
		System.out.println("Identify Download Service: " + download.identify() );
			
	}
	
	@Test
	public void  testResultSet() throws ResultSetException, DownloadServiceException {
		
		/** Prepare Push ResultSet **/
		W3CEndpointReference pushRSEpr = pushRS.createPushRS(400, 400);
		final ServiceResolver serviceResolver = new JaxwsServiceResolverImpl();
		final String exrsid = serviceResolver.getResourceIdentifier(pushRSEpr);
		List<String> elements = new ArrayList<String>();
		elements.add("http://dobreprogramy.pl/index.php");
		elements.add("http://www.onet.pl/index.html");
		elements.add("http://www.cs.bham.ac.uk/~axs/misc/talks/goldsmiths.pdf");
	
		elements.add("http://de.wikipedia.org/wiki/Wikipedia:Hauptseite");
		
		elements.add("http://www.wolframalpha.com/index.html");
		
		elements.add("http://www.apple.com/pl/index.html");
		elements.add("http://www.robertnz.net/ftp/newmat10.tar.gz");
		elements.add("http://clgiles.ist.psu.edu/");
		elements.add("http://www.gfu.net/s406.html");
		elements.add("http://www.ventura.org/RMA/envhealth/EHD%20FACILITY%20LISTS/backflow%20testers.pdf");
		
		//elements.add("http://www.limeconnect.com/pdf/2009/IBM%20Software%20Tester%20May%202009.pdf");
		
		elements.add("http://www.xilinx.com/support/documentation/application_notes/xapp713.pdf");
		elements.add("http://pixdata.lbl.gov/html/docs/Module_Testers_Examples_v0.3.doc");
		elements.add("http://www.nikhef.nl/pub/experiments/bfys/lhcb/outerTracker/Electronics/FE-Electronics/Tester_%20for_HV_board.doc");
		elements.add("http://www.pd.infn.it/spd/noprot/slides/hard-html/hardware.ppt");
		elements.add("http://www.scripps.edu/rc/softwaredocs/msi/catalyst45/tutorials/graphics/9_shapeSearch.doc.anc3.gif");
 		
		pushRS.populateRS(exrsid, elements);
		pushRS.closeRS(exrsid);		
		
		/**Sent to download **/
		
		W3CEndpointReference downloadEPR = download.downloadURLsFromRS(pushRSEpr);
		
		final ServiceResolver serviceResolver2 = new JaxwsServiceResolverImpl();
		final String downloadServiceRsId = serviceResolver2.getResourceIdentifier(downloadEPR);
		
		int resultsNumber = downloadRS.getNumberOfElements(downloadServiceRsId);
		System.out.println(downloadServiceRsId);
		System.out.println("Number of results in Download RS: "+ resultsNumber);
		System.out.println("RS Status:" + downloadRS.getRSStatus(downloadServiceRsId));
		
		List<String> results = downloadRS.getResult(downloadServiceRsId,
				1, resultsNumber , "waiting");
		
		if (results != null ){
			Iterator<String> iter = results.iterator();
	
			while (iter.hasNext())
				System.out.println("Downloaded object path: " + iter.next());	
		}
		

		System.out.println("Sleeping to give some time for finishing junit job!");
		try {
			Thread.sleep(70000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("WakeUp!");
		
		List<String> results2 = downloadRS.getResult(downloadServiceRsId,
				1, resultsNumber , "waiting");
		System.out.println("Second try");
		if (results2 != null ){
			Iterator<String> iter2 = results2.iterator();
	
			while (iter2.hasNext())
				System.out.println("Downloaded object path: " + iter2.next());	
		}
		
		
	}
  
	
	
}
