package gov.nih.nci.ncia.remotesearch;

import gov.nih.nci.cagrid.ncia.client.NCIACoreServiceClient;
import gov.nih.nci.ncia.search.DrillDown;
import gov.nih.nci.ncia.search.ImageSearchResult;
import gov.nih.nci.ncia.search.ImageSearchResultEx;
import gov.nih.nci.ncia.search.ImageSearchResultExImpl;
import gov.nih.nci.ncia.search.NBIANode;
import gov.nih.nci.ncia.search.PatientSearchResult;
import gov.nih.nci.ncia.search.SeriesSearchResult;
import gov.nih.nci.ncia.search.StudySearchResult;

/**
 * This implementation will issue grid requests to retrieve
 * the necessary drilldown results.
 */
public class RemoteDrillDown implements DrillDown {
	/**
	 * {@inheritDoc}
	 */
	public StudySearchResult[] retrieveStudyAndSeriesForPatient(PatientSearchResult patientSearchResult) {
		NBIANode nbiaNode = patientSearchResult.associatedLocation();
		assert nbiaNode.isLocal() == false;

		RemoteNode remoteNode = (RemoteNode)nbiaNode;
		String url = remoteNode.getEndpointReferenceType().getAddress().toString();
		try {
			NCIACoreServiceClient nbiaServiceClient = new NCIACoreServiceClient(url);
			StudySearchResult[] results = nbiaServiceClient.retrieveStudyAndSeriesForPatient(patientSearchResult);
			for(StudySearchResult result : results) {
				result.associateLocation(nbiaNode);
			}
			return results;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public ImageSearchResultEx[] retrieveImagesForSeriesEx(SeriesSearchResult seriesSearchResult) {
		NBIANode nbiaNode = seriesSearchResult.associatedLocation();
		assert nbiaNode.isLocal() == false;

		RemoteNode remoteNode = (RemoteNode)nbiaNode;
		String url = remoteNode.getEndpointReferenceType().getAddress().toString();
		try {
			NCIACoreServiceClient nbiaServiceClient = new NCIACoreServiceClient(url);
			ImageSearchResultEx[] results =  nbiaServiceClient.retrieveImagesForSeriesEx(seriesSearchResult);
			for(ImageSearchResultEx result : results) {
				result.associateLocation(nbiaNode);
			}
			return results;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	public ImageSearchResultEx[] retrieveImagesForSeriesForAllVersion(SeriesSearchResult seriesSearchResult) {
		NBIANode nbiaNode = seriesSearchResult.associatedLocation();
		assert nbiaNode.isLocal() == false;

		RemoteNode remoteNode = (RemoteNode)nbiaNode;
		String url = remoteNode.getEndpointReferenceType().getAddress().toString();
		Boolean hasEx = false;
		if (remoteNode.getUsAvailableSearchTerms() != null){
			hasEx = true;
		}

		if (hasEx){
			return retrieveImagesForSeriesEx(seriesSearchResult);
		}
		else {
			ImageSearchResult[] results =retrieveImagesForSeries(seriesSearchResult);
			int len = results.length;
			ImageSearchResultEx[] resultsEx = new ImageSearchResultEx[len];
			int i=0;
			for(ImageSearchResult result : results) {
				result.associateLocation(nbiaNode);
				ImageSearchResultEx isrei = new ImageSearchResultExImpl(result);
			/*	Integer id = result.getId();
				isrei.setId1(result.getId());
				isrei.setInstanceNumber(result.getInstanceNumber());
				isrei.setSeriesId(result.getSeriesId());
				isrei.setSeriesInstanceUid(result.getSeriesInstanceUid());
				isrei.setSize(result.getSize());
				isrei.setSopInstanceUid(result.getSopInstanceUid());
				isrei.setThumbnailURL(result.getThumbnailURL());
				isrei.associateLocation(result.associatedLocation());
				*/
				resultsEx[i++] = isrei;
			}
			return resultsEx;

		}

	}
	public ImageSearchResult[] retrieveImagesForSeries(SeriesSearchResult seriesSearchResult) {
		NBIANode nbiaNode = seriesSearchResult.associatedLocation();
		assert nbiaNode.isLocal() == false;

		RemoteNode remoteNode = (RemoteNode)nbiaNode;
		String url = remoteNode.getEndpointReferenceType().getAddress().toString();
		try {
			NCIACoreServiceClient nbiaServiceClient = new NCIACoreServiceClient(url);
			ImageSearchResult[] results =  nbiaServiceClient.retrieveImagesForSeries(seriesSearchResult);
			for(ImageSearchResult result : results) {
				result.associateLocation(nbiaNode);
			}
			return results;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
}
@@ -2478,85 +2478,8 @@
 e;%0D%0A
-%09%09String url = remoteNode.getEndpointReferenceType().getAddress().toString();
 %0D%0A%09%09
@@ -3021,462 +3021,8 @@
 );%0D%0A
-%09%09%09/*%09Integer id = result.getId();%0D%0A%09%09%09%09isrei.setId1(result.getId());%0D%0A%09%09%09%09isrei.setInstanceNumber(result.getInstanceNumber());%0D%0A%09%09%09%09isrei.setSeriesId(result.getSeriesId());%0D%0A%09%09%09%09isrei.setSeriesInstanceUid(result.getSeriesInstanceUid());%0D%0A%09%09%09%09isrei.setSize(result.getSize());%0D%0A%09%09%09%09isrei.setSopInstanceUid(result.getSopInstanceUid());%0D%0A%09%09%09%09isrei.setThumbnailURL(result.getThumbnailURL());%0D%0A%09%09%09%09isrei.associateLocation(result.associatedLocation());%0D%0A%09%09%09%09*/
 %0D%0A%09%09

remove unused local variable