/**
 *
 */


package gov.va.med.cds.request;
//TODO: refactor or determine if ReadResult will remain or if Documents will be passed instead

//import gov.va.med.cds.cdm.EntryPointCdmResult;
//import gov.va.med.cds.cdm.EntryPointPersistenceManagerResult;


/**
 * @author vhaislkorte
 *
 */

public class ReadResult
{

    public enum ResultState
    {
        BEFORE_READ, AFTER_READ, AFTER_OUTGOING_RULES, AFTER_FILTER, AFTER_MAPPING, AFTER_MAPPING_BEFORE_FILTERING
    }

//    protected ResultState state = ResultState.BEFORE_READ;
//    protected List<EntryPointPersistenceManagerResult> entryPointPersistenceResults = new ArrayList<EntryPointPersistenceManagerResult>();
//    protected List<EntryPointCdmResult> entryPointCdmResults = new ArrayList<EntryPointCdmResult>();
//    protected List<Patient> filteredPatients;
//    protected Set<String> filterEntryPoints = new HashSet<String>();
//    protected TemplateMetaDataInterface templateMetaData = null;
//
//
//    private void addPersistanceManagerResult( String entryFilterName, String entryType, String persistanceManagerId, List<Patient> dataSourceResult )
//    {
//        if ( dataSourceResult != null )
//        {
//            state = ResultState.AFTER_READ;
//            PersistenceManagerResult pmResult = new PersistenceManagerResult();
//            pmResult.setPersistenceManagerIdentifier( persistanceManagerId );
//
//            pmResult.setPatients( dataSourceResult );
//
//            EntryPointPersistenceManagerResult epResult = getEntryPointPersistenceManagerResult( entryFilterName, entryType );
//            epResult.addPersistenceResult( pmResult );
//        }
//    }
//
//
//    private EntryPointPersistenceManagerResult getEntryPointPersistenceManagerResult( String entryFilterName, String entryType )
//    {
//        for ( EntryPointPersistenceManagerResult result : entryPointPersistenceResults )
//        {
//            if ( result.getEntryPointFilterIdentifier().equals( entryFilterName ) )
//            {
//                return result;
//            }
//        }
//
//        EntryPointPersistenceManagerResult result = new EntryPointPersistenceManagerResult();
//        result.setEntryPointFilterIdentifier( entryFilterName );
//        result.setEntryPointType( entryType );
//        filterEntryPoints.add( entryType );
//        entryPointPersistenceResults.add( result );
//        return result;
//    }
//
//
//    public Set<String> getFilterEntryPoints( )
//    {
//        return filterEntryPoints;
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#readClinicalData(gov.va.med.cds.filter.CdsFilterInterface,
//     *      gov.va.med.cds.filter.schema.generated.EntryFilter, gov.va.med.cds.persistence.PersistenceManagerInterface)
//     */
//    public void assembleClinicalData( EntryFilterInterface aEntryFilter, List<Patient> aPatients, HashMap<String, List<Patient>> dataSourceResults )
//    {
//        if ( ( state != ResultState.BEFORE_READ ) && ( state != ResultState.AFTER_READ ) )
//        {
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//        //        List<Patient> dataSourceResult = persistenceManager.performReadOnClinicalData( aEntryFilter, aPatients );
//
//        for ( String persistanceManagerId : dataSourceResults.keySet() )
//        {
//            String entryFilterId = aEntryFilter.getQueryName();
//            String entryType = aEntryFilter.getDomainEntryPoint();
//            addPersistanceManagerResult( entryFilterId, entryType, persistanceManagerId, dataSourceResults.get( persistanceManagerId ) );
//        }
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#applyOutgoingProcessingRules(gov.va.med.cds.rules.DataRulesProcessorInterface)
//     */
//    public void applyOutgoingProcessingRules( DataRulesProcessorInterface rulesProcessor )
//    {
//        if ( state != ResultState.AFTER_READ )
//        {
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//        for ( EntryPointPersistenceManagerResult eppmResult : entryPointPersistenceResults )
//        {
//            String entryPointType = eppmResult.getEntryPointType();
//            // First apply persistenceManager-specific rules to each patient.
//            for ( PersistenceManagerResult pmResult : eppmResult.getPersistenceResults() )
//            {
//                String persistenceManagerId = pmResult.getPersistenceManagerIdentifier();
//                for ( Patient patient : pmResult.getPatients() )
//                {
//                    rulesProcessor.applyReadTransformationRules( patient, entryPointType, persistenceManagerId );
//                }
//            }
//
//            // Merge/reconcile records retrieved from multiple persistenceManagers.
//            List<Patient> mergedPatients = rulesProcessor.applyReadMergeRules( eppmResult.getPersistenceResults(), entryPointType );
//
//            // Repackage the patient list so it is just grouped by entry point filter.
//            EntryPointCdmResult epcResult = new EntryPointCdmResult();
//            epcResult.setEntryPointFilterIdentifier( eppmResult.getEntryPointFilterIdentifier() );
//            epcResult.setEntryPointType( eppmResult.getEntryPointType() );
//            epcResult.setCdmPatients( mergedPatients );
//            entryPointCdmResults.add( epcResult );
//        }
//
//        state = ResultState.AFTER_OUTGOING_RULES;
//        entryPointPersistenceResults = null;
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#mapCdmToVdm(gov.va.med.cds.mapping.OoMappingInterface)
//     */
//    public void mapCdmToVdm( OoMappingInterface mapper )
//    {
//        switch ( state )
//        {
//        case AFTER_OUTGOING_RULES:
//            for ( EntryPointCdmResult result : entryPointCdmResults )
//            {
//                // List mappedPatients = mapper.mapCdmPatientsToVdm( result.getCdmPatients(), getFilterEntryPoints() );
//                HashSet<String> singleEntrySet = new HashSet<String>();
//                singleEntrySet.add( result.getEntryPointType() );
//                List<Patient> mappedPatients = mapper.mapCdmPatientsToVdm( result.getCdmPatients(), singleEntrySet, templateMetaData.getTemplateId() );
//                result.setVdmPatients( mappedPatients );
//            }
//            state = ResultState.AFTER_MAPPING_BEFORE_FILTERING;
//            break;
//        case AFTER_FILTER:
//            filteredPatients = mapper.mapCdmPatientsToVdm( filteredPatients, getFilterEntryPoints() );
//            state = ResultState.AFTER_MAPPING;
//            break;
//        default:
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#getEntryPointResult(java.lang.String)
//     */
//    public EntryPointCdmResult getEntryPointResult( String entryPointIdentifier )
//    {
//        if ( state != ResultState.AFTER_OUTGOING_RULES && state != ResultState.AFTER_MAPPING_BEFORE_FILTERING )
//        {
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//        for ( EntryPointCdmResult result : entryPointCdmResults )
//        {
//            String resultId = result.getEntryPointFilterIdentifier();
//            if ( resultId.equals( entryPointIdentifier ) )
//            {
//                return result;
//            }
//        }
//        return null;
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#setFilteredPatients(java.util.List)
//     */
//    public void setFilteredPatients( List<Patient> patients )
//    {
//        if ( state != ResultState.AFTER_OUTGOING_RULES && state != ResultState.AFTER_MAPPING_BEFORE_FILTERING )
//        {
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//        state = ResultState.AFTER_FILTER;
//        filteredPatients = patients;
//        entryPointCdmResults = null;
//    }
//
//
//    /*
//     * (non-Javadoc)
//     *
//     * @see gov.va.med.cds.request.ReadResultInterface#getFilteredPatients()
//     */
//    public List<Patient> getFilteredPatients( )
//    {
//        if ( state != ResultState.AFTER_FILTER )
//        {
//            throw new OperationNotSupportedException( ErrorCodeEnum.READ_RESULT_INCORRECTLY_ACCESSED );
//        }
//        return filteredPatients;
//    }
//
//
//    public void setTemplateMetaData( TemplateMetaDataInterface aTemplateMetaData )
//    {
//        templateMetaData = aTemplateMetaData;
//    }
//
//
//    public TemplateMetaDataInterface getTemplateMetaData( )
//    {
//        return templateMetaData;
//    }
}
