<commit_before>package gov.nih.nci.calab.service.remote;

import gov.nih.nci.cagrid.cananolab.client.CaNanoLabSvcClient;
import gov.nih.nci.calab.domain.AssociatedFile;
import gov.nih.nci.calab.domain.Report;
import gov.nih.nci.calab.domain.Source;
import gov.nih.nci.calab.domain.nano.characterization.Characterization;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.ComposingElement;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.DendrimerComposition;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.ParticleComposition;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.SurfaceGroup;
import gov.nih.nci.calab.domain.nano.function.Function;
import gov.nih.nci.calab.domain.nano.particle.Nanoparticle;
import gov.nih.nci.calab.dto.characterization.CharacterizationBean;
import gov.nih.nci.calab.dto.common.ReportBean;
import gov.nih.nci.calab.dto.function.FunctionBean;
import gov.nih.nci.calab.dto.particle.ParticleBean;
import gov.nih.nci.calab.dto.remote.GridNodeBean;
import gov.nih.nci.calab.exception.GridQueryException;
import gov.nih.nci.calab.service.particle.NanoparticleCharacterizationService;
import gov.nih.nci.calab.service.util.CaNanoLabConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Remote search calls across the grid.
 * 
 * @author pansu
 * 
 */
public class GridSearchService {
	private static Logger logger = Logger.getLogger(GridSearchService.class);

	/**
	 * Retrieve remote reports files from the given grid node.
	 * 
	 * @param reportTitle
	 * @param reportType
	 * @param particleType
	 * @param functionTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public List<ReportBean> getRemoteReports(String reportTitle,
			String reportType, String particleType, String[] functionTypes,
			GridNodeBean gridNode) throws GridQueryException {
		List<ReportBean> reports = new ArrayList<ReportBean>();

		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			if (reportType.equals(CaNanoLabConstants.REPORT)) {
				Report[] gridReports = gridClient.getReports(reportTitle,
						particleType, functionTypes);
				if (gridReports != null) {
					for (Report report : gridReports) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean.setInstanceType(CaNanoLabConstants.REPORT);
						reports.add(fileBean);
					}
				}
			} else if (reportType.equals(CaNanoLabConstants.ASSOCIATED_FILE)) {
				AssociatedFile[] gridAssociatedFiles = gridClient
						.getOtherAssociatedFiles(reportTitle, particleType,
								functionTypes);
				if (gridAssociatedFiles != null) {
					for (AssociatedFile report : gridAssociatedFiles) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean
								.setInstanceType(CaNanoLabConstants.ASSOCIATED_FILE);
						reports.add(fileBean);
					}
				}
			} else {
				Report[] gridReports = gridClient.getReports(reportTitle,
						particleType, functionTypes);
				if (gridReports != null) {
					for (Report report : gridReports) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean.setInstanceType(CaNanoLabConstants.REPORT);
						reports.add(fileBean);
					}
				}
				AssociatedFile[] gridAssociatedFiles = gridClient
						.getOtherAssociatedFiles(reportTitle, particleType,
								functionTypes);
				if (gridAssociatedFiles != null) {
					for (AssociatedFile report : gridAssociatedFiles) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean
								.setInstanceType(CaNanoLabConstants.ASSOCIATED_FILE);
						reports.add(fileBean);
					}
				}
			}
		} catch (Exception e) {
			logger.error("Error in searching remote reports: " + e);
			throw new GridQueryException();
		}
		return reports;
	}

	/**
	 * Retrieve nanoparticles from the given grid node
	 * 
	 * @param particleType
	 * @param functionTypes
	 * @param characterizationTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public List<ParticleBean> getRemoteNanoparticles(String particleType,
			String[] functionTypes, String[] characterizationTypes,
			GridNodeBean gridNode) throws GridQueryException {
		List<ParticleBean> particles = new ArrayList<ParticleBean>();
		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			Nanoparticle[] gridParticles = gridClient.getNanoparticles(
					particleType, characterizationTypes, functionTypes);
			if (gridParticles != null) {
				for (Nanoparticle particle : gridParticles) {
					Characterization[] gridCharacterizations = gridClient
							.getCharacterizationsByParticleName(particle
									.getName());
					if (gridCharacterizations != null) {
						particle.setCharacterizationCollection(Arrays
								.asList(gridCharacterizations));
					}
					Function[] gridFunctions = gridClient
							.getFunctionsByParticleName(particle.getName());
					if (gridFunctions != null) {
						particle.setFunctionCollection(Arrays
								.asList(gridFunctions));
					}
					Source gridSource = gridClient
							.getSourceByParticleName(particle.getName());
					if (gridSource != null)
						particle.setSource(gridSource);
					particles.add(new ParticleBean(particle, gridNode
							.getHostName()));
				}
			}
		} catch (Exception e) {
			logger.error("Error in searching remote particles: " + e);
			throw new GridQueryException();
		}
		return particles;
	}

	public Nanoparticle getRemoteNanoparticle(String particleName,
			GridNodeBean gridNode) throws GridQueryException {
		Nanoparticle particle = null;
		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			particle = gridClient.getNanoparticle(particleName);
			Characterization[] gridCharacterizations = gridClient
					.getCharacterizationsByParticleName(particle.getName());
			if (gridCharacterizations != null) {
				particle.setCharacterizationCollection(Arrays
						.asList(gridCharacterizations));
			}
			Function[] gridFunctions = gridClient
					.getFunctionsByParticleName(particle.getName());
			if (gridFunctions != null) {
				particle.setFunctionCollection(Arrays.asList(gridFunctions));
			}
			Source gridSource = gridClient.getSourceByParticleName(particle
					.getName());<commit_msg>minor midification<commit_after>package gov.nih.nci.calab.service.remote;

import gov.nih.nci.cagrid.cananolab.client.CaNanoLabSvcClient;
import gov.nih.nci.calab.domain.AssociatedFile;
import gov.nih.nci.calab.domain.Report;
import gov.nih.nci.calab.domain.Source;
import gov.nih.nci.calab.domain.nano.characterization.Characterization;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.ComposingElement;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.DendrimerComposition;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.ParticleComposition;
import gov.nih.nci.calab.domain.nano.characterization.physical.composition.SurfaceGroup;
import gov.nih.nci.calab.domain.nano.function.Function;
import gov.nih.nci.calab.domain.nano.particle.Nanoparticle;
import gov.nih.nci.calab.dto.characterization.CharacterizationBean;
import gov.nih.nci.calab.dto.common.ReportBean;
import gov.nih.nci.calab.dto.function.FunctionBean;
import gov.nih.nci.calab.dto.particle.ParticleBean;
import gov.nih.nci.calab.dto.remote.GridNodeBean;
import gov.nih.nci.calab.exception.GridQueryException;
import gov.nih.nci.calab.service.particle.NanoparticleCharacterizationService;
import gov.nih.nci.calab.service.util.CaNanoLabConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Remote search calls across the grid.
 * 
 * @author pansu
 * 
 */
public class GridSearchService {
	private static Logger logger = Logger.getLogger(GridSearchService.class);

	/**
	 * Retrieve remote reports files from the given grid node.
	 * 
	 * @param reportTitle
	 * @param reportType
	 * @param particleType
	 * @param functionTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public int getRemoteReportCount(String reportTitle,
			String particleType, String[] functionTypes,
			GridNodeBean gridNode) throws GridQueryException {
		
		int reportCount = 0;

		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			
			Report[] gridReports = gridClient.getReports(reportTitle,
						particleType, functionTypes);
			if(gridReports != null)
				reportCount += gridReports.length;
				
			AssociatedFile[] gridAssociatedFiles = gridClient
						.getOtherAssociatedFiles(reportTitle, particleType,
								functionTypes);
			if(gridAssociatedFiles != null)
				reportCount += gridAssociatedFiles.length;
			
		} catch (Exception e) {
			logger.error("Error in searching remote reports: " + e);
			throw new GridQueryException();
		}
		return reportCount;
	}
	
	/**
	 * Retrieve remote reports files from the given grid node.
	 * 
	 * @param reportTitle
	 * @param reportType
	 * @param particleType
	 * @param functionTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public List<ReportBean> getRemoteReports(String reportTitle,
			String reportType, String particleType, String[] functionTypes,
			GridNodeBean gridNode) throws GridQueryException {
		List<ReportBean> reports = new ArrayList<ReportBean>();

		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			if (reportType.equals(CaNanoLabConstants.REPORT)) {
				Report[] gridReports = gridClient.getReports(reportTitle,
						particleType, functionTypes);
				if (gridReports != null) {
					for (Report report : gridReports) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean.setInstanceType(CaNanoLabConstants.REPORT);
						reports.add(fileBean);
					}
				}
			} else if (reportType.equals(CaNanoLabConstants.ASSOCIATED_FILE)) {
				AssociatedFile[] gridAssociatedFiles = gridClient
						.getOtherAssociatedFiles(reportTitle, particleType,
								functionTypes);
				if (gridAssociatedFiles != null) {
					for (AssociatedFile report : gridAssociatedFiles) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean
								.setInstanceType(CaNanoLabConstants.ASSOCIATED_FILE);
						reports.add(fileBean);
					}
				}
			} else {
				Report[] gridReports = gridClient.getReports(reportTitle,
						particleType, functionTypes);
				if (gridReports != null) {
					for (Report report : gridReports) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean.setInstanceType(CaNanoLabConstants.REPORT);
						reports.add(fileBean);
					}
				}
				AssociatedFile[] gridAssociatedFiles = gridClient
						.getOtherAssociatedFiles(reportTitle, particleType,
								functionTypes);
				if (gridAssociatedFiles != null) {
					for (AssociatedFile report : gridAssociatedFiles) {
						ReportBean fileBean = new ReportBean(report, gridNode
								.getHostName());
						fileBean
								.setInstanceType(CaNanoLabConstants.ASSOCIATED_FILE);
						reports.add(fileBean);
					}
				}
			}
		} catch (Exception e) {
			logger.error("Error in searching remote reports: " + e);
			throw new GridQueryException();
		}
		return reports;
	}

	/**
	 * Retrieve nanoparticles from the given grid node
	 * 
	 * @param particleType
	 * @param functionTypes
	 * @param characterizationTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public List<ParticleBean> getRemoteNanoparticles(String particleType,
			String[] functionTypes, String[] characterizationTypes,
			GridNodeBean gridNode) throws GridQueryException {
		List<ParticleBean> particles = new ArrayList<ParticleBean>();
		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			Nanoparticle[] gridParticles = gridClient.getNanoparticles(
					particleType, characterizationTypes, functionTypes);
			if (gridParticles != null) {
				for (Nanoparticle particle : gridParticles) {
					Characterization[] gridCharacterizations = gridClient
							.getCharacterizationsByParticleName(particle
									.getName());
					if (gridCharacterizations != null) {
						particle.setCharacterizationCollection(Arrays
								.asList(gridCharacterizations));
					}
					Function[] gridFunctions = gridClient
							.getFunctionsByParticleName(particle.getName());
					if (gridFunctions != null) {
						particle.setFunctionCollection(Arrays
								.asList(gridFunctions));
					}
					Source gridSource = gridClient
							.getSourceByParticleName(particle.getName());
					if (gridSource != null)
						particle.setSource(gridSource);
					particles.add(new ParticleBean(particle, gridNode
							.getHostName()));
				}
			}
		} catch (Exception e) {
			logger.error("Error in searching remote particles: " + e);
			throw new GridQueryException();
		}
		return particles;
	}
	
	/**
	 * Retrieve nanoparticles counts from the given grid node
	 * 
	 * @param particleType
	 * @param functionTypes
	 * @param characterizationTypes
	 * @param gridNode
	 * @return
	 * @throws GridQueryException
	 */
	public int getRemoteNanoparticleCount(String particleType,
			String[] functionTypes, String[] characterizationTypes,
			GridNodeBean gridNode) throws GridQueryException {
		List<ParticleBean> particles = new ArrayList<ParticleBean>();
		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			Nanoparticle[] gridParticles = gridClient.getNanoparticles(
					particleType, characterizationTypes, functionTypes);
			if (gridParticles != null) {
				return gridParticles.length;
			}
		} catch (Exception e) {
			logger.error("Error in searching remote particles: " + e);
			throw new GridQueryException();
		}
		return 0;
	}
	
	public Nanoparticle getRemoteNanoparticle(String particleName,
			GridNodeBean gridNode) throws GridQueryException {
		Nanoparticle particle = null;
		try {
			CaNanoLabSvcClient gridClient = new CaNanoLabSvcClient(gridNode
					.getAddress());
			particle = gridClient.getNanoparticle(particleName);
			Characterization[] gridCharacterizations = gridClient
					.getCharacterizationsByParticleName(particle.getName());
			if (gridCharacterizations != null) {
				particle.setCharacterizationCollection(Arrays
						.asList(gridCharacterizations));
			}
			Function[] gridFunctions = gridClient
					.getFunctionsByParticleName(particle.getName());
			if (gridFunctions != null) {
				particle.setFunctionCollection(Arrays.asList(gridFunctions));
			}
			Source gridSource = gridClient.getSourceByParticleName(particle
					.getName());
{'fraction_non_alphanumeric': 0.09763738415350476, 'fraction_numerical': 0.0002610625244746117, 'mean_word_length': 4.183694181326117, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415170', 'n_tokens_mistral': 5607, 'n_tokens_neox': 4443, 'n_words': 862}