<commit_before>package gov.nih.nci.cabig.caaers.resolver;

import edu.duke.cabig.c3pr.esb.Metadata;
import edu.duke.cabig.c3pr.esb.OperationNameEnum;
import edu.duke.cabig.c3pr.esb.ServiceTypeEnum;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.RemoteOrganization;
import gov.nih.nci.cabig.caaers.utils.XMLUtil;
import gov.nih.nci.coppa.po.IdentifiedOrganization;

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

import org.apache.log4j.Logger;
import org.iso._21090.II;

import com.semanticbits.coppa.infrastructure.service.RemoteResolver;
import com.semanticbits.coppasimulator.util.CoppaObjectFactory;

public class OrganizationResolver extends BaseResolver implements RemoteResolver{ 
	private static Logger logger = Logger.getLogger(OrganizationResolver.class);
/*
	public Organization populateRemoteOrganization(gov.nih.nci.coppa.po.Organization coppaOrganization){
		
		// using coppa organization identier and previously obtained id of CTEP (hard coded in CoppaObjectFactory.getIIOfCTEP) get Identified organization 
		IdentifiedOrganization identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganizationSearchCriteriaForCorrelation(coppaOrganization.getIdentifier());
		
		String identifiedOrganizationXml = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrganization);		
		Metadata mData = new Metadata(OperationNameEnum.search.getName(), "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
		String resultXml = broadcastCOPPA(identifiedOrganizationXml, mData);		

		List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
		if (results.size()>0) {
			System.out.println(results.get(0));
			identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(results.get(0));
		}
		Organization remoteOrganization = new RemoteOrganization();
		if (identifiedOrganization.getAssignedId() != null ) {
			// CTEP ID 
			remoteOrganization.setNciInstituteCode(identifiedOrganization.getAssignedId().getExtension());	
		} else {
			return null;
		}
		remoteOrganization.setName(CoppaObjectFactory.getName(coppaOrganization.getName()));
		remoteOrganization.setCity(CoppaObjectFactory.getCity(coppaOrganization.getPostalAddress()));
		remoteOrganization.setCountry(CoppaObjectFactory.getCountry(coppaOrganization.getPostalAddress()));
		remoteOrganization.setExternalId(coppaOrganization.getIdentifier().getExtension());
		return remoteOrganization;
	}*/
	
	public Organization populateRemoteOrganization(gov.nih.nci.coppa.po.Organization coppaOrganization,IdentifiedOrganization identifiedOrganization){
		Organization remoteOrganization = new RemoteOrganization();
		remoteOrganization.setNciInstituteCode(identifiedOrganization.getAssignedId().getExtension());	
		remoteOrganization.setName(CoppaObjectFactory.getName(coppaOrganization.getName()));
		remoteOrganization.setCity(CoppaObjectFactory.getCity(coppaOrganization.getPostalAddress()));
		remoteOrganization.setCountry(CoppaObjectFactory.getCountry(coppaOrganization.getPostalAddress()));
		remoteOrganization.setExternalId(coppaOrganization.getIdentifier().getExtension());
		return remoteOrganization;
	}
	
	public Object getRemoteEntityByUniqueId(String externalId) {
		logger.info("Entering OrganizationResolver.getRemoteEntityByUniqueId(externalId : " + externalId + ")"  );
		
		gov.nih.nci.coppa.po.Organization coppaOrganization = null;
		gov.nih.nci.coppa.po.IdentifiedOrganization identifiedOrganization = null;
		
		// using external id (coppa id ) 
		II ii = CoppaObjectFactory.getIISearchCriteria(externalId);

		try {
			String iiXml = CoppaObjectFactory.getCoppaIIXml(ii);
			Metadata mData = new Metadata(OperationNameEnum.getById.getName(), "externalId", ServiceTypeEnum.ORGANIZATION.getName());
			String resultXml = broadcastCOPPA(iiXml, mData);		
			//System.out.println(resultXml);
			List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
			if (results.size()>0) {
				coppaOrganization = CoppaObjectFactory.getCoppaOrganization(results.get(0));
				II ident =  coppaOrganization.getIdentifier();
				String playerId = CoppaObjectFactory.getCoppaIIXml(ident);
				// refactored to get by getByPlayerIds
				mData = new Metadata("getByPlayerIds", "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
				String identifiedOrgsResultXml = broadcastCOPPA(playerId,mData);
				List<String> identifiedOrgsResult = XMLUtil.getObjectsFromCoppaResponse(identifiedOrgsResultXml);
				if (identifiedOrgsResult.size()>0) {
					identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrgsResult.get(0));				
				}
				if (identifiedOrganization == null) {
					logger.info("Exiting OrganizationResolver.getRemoteEntityByUniqueId() - identifiedOrganization is NULL");
					return null;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("Exiting OrganizationResolver.getRemoteEntityByUniqueId()");

		return populateRemoteOrganization(coppaOrganization,identifiedOrganization);
	}
	private List<Object> getRemoteOrganizationByNciIdentifier(String nciInstituteCode){
			// get by nci-id
			List<Object> remoteOrganizations = new ArrayList<Object>();
			IdentifiedOrganization identifiedOrganizationSearchCriteria = CoppaObjectFactory.getCoppaIdentfiedOrganizationSearchCriteriaOnCTEPId(nciInstituteCode);
			String payload = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrganizationSearchCriteria);
			//System.out.println(payload);
			Metadata mData = new Metadata(OperationNameEnum.search.getName(), "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
			String results =broadcastCOPPA(payload, mData);//
			List<String> resultObjects = XMLUtil.getObjectsFromCoppaResponse(results);
			for (String resultObj:resultObjects) {
				//System.out.println(resultObj);
				IdentifiedOrganization coppaIdOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(resultObj);
				II organizationIdentifier = coppaIdOrganization.getPlayerIdentifier();
				String iiXml = CoppaObjectFactory.getCoppaIIXml(organizationIdentifier);
				//Get Organization based on player id of above.
				mData = new Metadata(OperationNameEnum.getById.getName(), "externalId", ServiceTypeEnum.ORGANIZATION.getName());
				String organizationResults = broadcastCOPPA(iiXml, mData);//
				List<String> organizationResultObjects = XMLUtil.getObjectsFromCoppaResponse(organizationResults);
				for (String organizationResultObject:organizationResultObjects) {
					gov.nih.nci.coppa.po.Organization coppaOrganizationResult = CoppaObjectFactory.getCoppaOrganization(organizationResultObject);
					Organization remoteOrganization = populateRemoteOrganization(coppaOrganizationResult,coppaIdOrganization);
					remoteOrganizations.add(remoteOrganization);
				}
			}
			return remoteOrganizations;
	}
	
	private List<Object> getRemoteOrganizationByNameAndMore(Organization remoteOrgExample){
		List<Object> remoteOrganizations = new ArrayList<Object>();
		// get by name
		String payLoad = 
			CoppaObjectFactory.getCoppaOrganizationXml(remoteOrgExample.getName(), null, remoteOrgExample.getCity(),
					null, null, remoteOrgExample.getCountry());
	
		//System.out.println(payLoad);
		
		// call caXchange and get results ... 
		Metadata mData = new Metadata(OperationNameEnum.search.getName(), "externalId", ServiceTypeEnum.ORGANIZATION.getName());
		String resultXml =broadcastCOPPA(payLoad, mData);//
		//System.out.println(resultXml);
		
		List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
		List<gov.nih.nci.coppa.po.Organization> coppaOrganizations = new ArrayList<gov.nih.nci.coppa.po.Organization>();
		List<String> searchStrs = new ArrayList<String>();
		for (String result:results) {
			gov.nih.nci.coppa.po.Organization coppaOrganization = CoppaObjectFactory.getCoppaOrganization(result);
			coppaOrganizations.add(coppaOrganization);
			II ident =  coppaOrganization.getIdentifier();
			String xml1 = CoppaObjectFactory.getCoppaIIXml(ident);
			searchStrs.add(xml1);<commit_msg>Organization query with 500 limit <commit_after>package gov.nih.nci.cabig.caaers.resolver;

import edu.duke.cabig.c3pr.esb.Metadata;
import edu.duke.cabig.c3pr.esb.OperationNameEnum;
import edu.duke.cabig.c3pr.esb.ServiceTypeEnum;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.RemoteOrganization;
import gov.nih.nci.cabig.caaers.utils.XMLUtil;
import gov.nih.nci.coppa.po.IdentifiedOrganization;

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

import org.apache.log4j.Logger;
import org.iso._21090.II;

import com.semanticbits.coppa.infrastructure.service.RemoteResolver;
import com.semanticbits.coppasimulator.util.CoppaObjectFactory;
import com.semanticbits.coppasimulator.util.CoppaPAObjectFactory;

public class OrganizationResolver extends BaseResolver implements RemoteResolver{ 
	private static Logger logger = Logger.getLogger(OrganizationResolver.class);
/*
	public Organization populateRemoteOrganization(gov.nih.nci.coppa.po.Organization coppaOrganization){
		
		// using coppa organization identier and previously obtained id of CTEP (hard coded in CoppaObjectFactory.getIIOfCTEP) get Identified organization 
		IdentifiedOrganization identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganizationSearchCriteriaForCorrelation(coppaOrganization.getIdentifier());
		
		String identifiedOrganizationXml = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrganization);		
		Metadata mData = new Metadata(OperationNameEnum.search.getName(), "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
		String resultXml = broadcastCOPPA(identifiedOrganizationXml, mData);		

		List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
		if (results.size()>0) {
			System.out.println(results.get(0));
			identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(results.get(0));
		}
		Organization remoteOrganization = new RemoteOrganization();
		if (identifiedOrganization.getAssignedId() != null ) {
			// CTEP ID 
			remoteOrganization.setNciInstituteCode(identifiedOrganization.getAssignedId().getExtension());	
		} else {
			return null;
		}
		remoteOrganization.setName(CoppaObjectFactory.getName(coppaOrganization.getName()));
		remoteOrganization.setCity(CoppaObjectFactory.getCity(coppaOrganization.getPostalAddress()));
		remoteOrganization.setCountry(CoppaObjectFactory.getCountry(coppaOrganization.getPostalAddress()));
		remoteOrganization.setExternalId(coppaOrganization.getIdentifier().getExtension());
		return remoteOrganization;
	}*/
	
	public Organization populateRemoteOrganization(gov.nih.nci.coppa.po.Organization coppaOrganization,IdentifiedOrganization identifiedOrganization){
		Organization remoteOrganization = new RemoteOrganization();
		remoteOrganization.setNciInstituteCode(identifiedOrganization.getAssignedId().getExtension());	
		remoteOrganization.setName(CoppaObjectFactory.getName(coppaOrganization.getName()));
		remoteOrganization.setCity(CoppaObjectFactory.getCity(coppaOrganization.getPostalAddress()));
		remoteOrganization.setCountry(CoppaObjectFactory.getCountry(coppaOrganization.getPostalAddress()));
		remoteOrganization.setExternalId(coppaOrganization.getIdentifier().getExtension());
		return remoteOrganization;
	}
	
	public Object getRemoteEntityByUniqueId(String externalId) {
		logger.info("Entering OrganizationResolver.getRemoteEntityByUniqueId(externalId : " + externalId + ")"  );
		
		gov.nih.nci.coppa.po.Organization coppaOrganization = null;
		gov.nih.nci.coppa.po.IdentifiedOrganization identifiedOrganization = null;
		
		// using external id (coppa id ) 
		II ii = CoppaObjectFactory.getIISearchCriteria(externalId);

		try {
			String iiXml = CoppaObjectFactory.getCoppaIIXml(ii);
			Metadata mData = new Metadata(OperationNameEnum.getById.getName(), "externalId", ServiceTypeEnum.ORGANIZATION.getName());
			String resultXml = broadcastCOPPA(iiXml, mData);		
			//System.out.println(resultXml);
			List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
			if (results.size()>0) {
				coppaOrganization = CoppaObjectFactory.getCoppaOrganization(results.get(0));
				II ident =  coppaOrganization.getIdentifier();
				String playerId = CoppaObjectFactory.getCoppaIIXml(ident);
				// refactored to get by getByPlayerIds
				mData = new Metadata("getByPlayerIds", "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
				String identifiedOrgsResultXml = broadcastCOPPA(playerId,mData);
				List<String> identifiedOrgsResult = XMLUtil.getObjectsFromCoppaResponse(identifiedOrgsResultXml);
				if (identifiedOrgsResult.size()>0) {
					identifiedOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrgsResult.get(0));				
				}
				if (identifiedOrganization == null) {
					logger.info("Exiting OrganizationResolver.getRemoteEntityByUniqueId() - identifiedOrganization is NULL");
					return null;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("Exiting OrganizationResolver.getRemoteEntityByUniqueId()");

		return populateRemoteOrganization(coppaOrganization,identifiedOrganization);
	}
	private List<Object> getRemoteOrganizationByNciIdentifier(String nciInstituteCode){
			// get by nci-id
			List<Object> remoteOrganizations = new ArrayList<Object>();
			IdentifiedOrganization identifiedOrganizationSearchCriteria = CoppaObjectFactory.getCoppaIdentfiedOrganizationSearchCriteriaOnCTEPId(nciInstituteCode);
			String payload = CoppaObjectFactory.getCoppaIdentfiedOrganization(identifiedOrganizationSearchCriteria);
			//System.out.println(payload);
			Metadata mData = new Metadata(OperationNameEnum.search.getName(), "externalId", ServiceTypeEnum.IDENTIFIED_ORGANIZATION.getName());
			String results =broadcastCOPPA(payload, mData);//
			List<String> resultObjects = XMLUtil.getObjectsFromCoppaResponse(results);
			for (String resultObj:resultObjects) {
				//System.out.println(resultObj);
				IdentifiedOrganization coppaIdOrganization = CoppaObjectFactory.getCoppaIdentfiedOrganization(resultObj);
				II organizationIdentifier = coppaIdOrganization.getPlayerIdentifier();
				String iiXml = CoppaObjectFactory.getCoppaIIXml(organizationIdentifier);
				//Get Organization based on player id of above.
				mData = new Metadata(OperationNameEnum.getById.getName(), "externalId", ServiceTypeEnum.ORGANIZATION.getName());
				String organizationResults = broadcastCOPPA(iiXml, mData);//
				List<String> organizationResultObjects = XMLUtil.getObjectsFromCoppaResponse(organizationResults);
				for (String organizationResultObject:organizationResultObjects) {
					gov.nih.nci.coppa.po.Organization coppaOrganizationResult = CoppaObjectFactory.getCoppaOrganization(organizationResultObject);
					Organization remoteOrganization = populateRemoteOrganization(coppaOrganizationResult,coppaIdOrganization);
					remoteOrganizations.add(remoteOrganization);
				}
			}
			return remoteOrganizations;
	}
	
	private List<Object> getRemoteOrganizationByNameAndMore(Organization remoteOrgExample){
		List<Object> remoteOrganizations = new ArrayList<Object>();
		// get by name
		String payLoad = 
			CoppaObjectFactory.getCoppaOrganizationXml(remoteOrgExample.getName(), null, remoteOrgExample.getCity(),
					null, null, remoteOrgExample.getCountry());
	
		//System.out.println(payLoad);
		
		// call caXchange and get results ... 
		Metadata mData = new Metadata("query", "externalId", ServiceTypeEnum.ORGANIZATION.getName());
		List<String> payLoads = new ArrayList<String>();
        int limit = 500;
        String limitOffsetPayload = CoppaPAObjectFactory.getLimitOffsetXML(limit, 0);
		payLoads.add(payLoad);
		payLoads.add(limitOffsetPayload);        
	
		String resultXml =broadcastCOPPA(payLoads, mData);//
		//System.out.println(resultXml);
		
		List<String> results = XMLUtil.getObjectsFromCoppaResponse(resultXml);
		List<gov.nih.nci.coppa.po.Organization> coppaOrganizations = new ArrayList<gov.nih.nci.coppa.po.Organization>();
		List<String> searchStrs = new ArrayList<String>();
		for (String result:results) {
			gov.nih.nci.coppa.po.Organization coppaOrganization = CoppaObjectFactory.getCoppaOrganization(result);
			coppaOrganizations.add(coppaOrganization);
			II ident =  coppaOrganization.getIdentifier();
			String xml1 = CoppaObjectFactory.getCoppaIIXml(ident);
			searchStrs.add(xml1);
{'fraction_non_alphanumeric': 0.09706342520158864, 'fraction_numerical': 0.002587555662534601, 'mean_word_length': 6.516508367254636, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6992400', 'n_tokens_mistral': 5314, 'n_tokens_neox': 4753, 'n_words': 723}