

package gov.va.med.cds.ars.persistence;


import gov.va.med.cds.ars.requestresponse.generated.CensusReportDatesReportDataType;
import gov.va.med.cds.ars.requestresponse.generated.CensusReportDatesReportFilterType;
import gov.va.med.cds.ars.requestresponse.generated.HTReportFilterType;
import gov.va.med.cds.ars.requestresponse.generated.HTResponseType;
import gov.va.med.cds.ars.requestresponse.generated.CensusReportDatesDataType;
import gov.va.med.cds.ars.requestresponse.generated.ObjectFactory;
import gov.va.med.cds.exception.PersistenceException;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;


public class CensusStartEndDatesReportPersistenceManager extends AbstractReadableReportsPersistenceManager
	implements CensusStartEndDatesReportPersistenceManagerInterface {
    protected String censusStartEndDatesQuery = "SurveyReports.CensusStartEndDates";
    
    
    @Override
    public HTResponseType performReadReportAggregate( HTReportFilterType aReportFilter )
        throws PersistenceException
    {

        ObjectFactory objectFactory = new ObjectFactory();
        CensusReportDatesReportDataType data = objectFactory.createCensusReportDatesReportDataType();
        
        CensusReportDatesReportDataType.ReportDates censusReportDates = objectFactory.createCensusReportDatesReportDataTypeReportDates();
        data.setReportDates (censusReportDates);
        CensusReportDatesReportFilterType filter = aReportFilter.getCensusReportDatesReportFilter();
        Session session = this.sessionFactory.getCurrentSession();     
        
        try
        {
            List<CensusReportDatesDataType> results = null;            
            results = censusStartEndDatesReport( session, filter );

            data.getReportDates().getReportDate().addAll( results );
            data.setReportID( filter.getReportID() );
        }
        finally
        {
            session.close();
        }
        
        responseType.setCensusReportDatesReportData ( data );
        
        return responseType;
        
        
    }
    

    @SuppressWarnings( "unchecked" )
    private List<CensusReportDatesDataType> censusStartEndDatesReport( Session aSession, CensusReportDatesReportFilterType aCensusReportDatesReportFilter )
        throws PersistenceException
    {        
        Query query = aSession.getNamedQuery( censusStartEndDatesQuery );
        
        if ( LOGGER.isDebugEnabled() )
        {
            LOGGER.debug("+++++ Calling CENSUS_START_END_DATES_REPORT stored proc: *****(  " + censusStartEndDatesQuery + "  )*****" );
        }
        
        List<CensusReportDatesDataType> results = ( List<CensusReportDatesDataType> )queryForReportData( query );
        
        if ( LOGGER.isDebugEnabled() )
        {
            LOGGER.debug( "----- Done with CENSUS_START_END_DATES Report stored proc" );
        }

        return results;
    }


    public void setCensusStartEndDatesQuery( String aCensusStartEndDatesQuery )
    {
        this.censusStartEndDatesQuery= aCensusStartEndDatesQuery ;
    }

}