<commit_before>package org.openmrs.module.dhisreport.api.syncmodel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.openmrs.api.context.Context;
import org.openmrs.module.dhisreport.api.DHIS2ReportingService;
import org.openmrs.module.dhisreport.api.model.DataElement;
import org.openmrs.module.dhisreport.api.model.DataValueTemplate;
import org.openmrs.module.dhisreport.api.model.Disaggregation;
import org.openmrs.module.dhisreport.api.model.ReportDefinition;
import org.openmrs.module.dhisreport.api.model.ReportTemplates;

public class ReportSynchronizer
{

    public void fullSync()
        throws UnsupportedEncodingException
    {

        //Fetch DataElement group page

        String webPage = "http://localhost:8081/dhis/api/dataElementGroups.xml";
        String OpenmrsdeURL = "";
        String html = fetchURL( webPage );
        InputStream stream = new ByteArrayInputStream( html.getBytes( "UTF-8" ) );
        System.out.println( html );
        StaXParser read = new StaXParser();
        List<SyncDataElementGroup> readDEG = read.readConfig( stream );
        for ( SyncDataElementGroup item : readDEG )
        {
            //Search for "openmrsde" dataelement group
            if ( item.getName().equals( "openmrsde" ) )
            {
                OpenmrsdeURL = item.getHref();
            }
            System.out.println( item );
        }

        //Fetch Openmrsde page 
        OpenmrsdeURL = OpenmrsdeURL + ".xml";
        html = fetchURL( OpenmrsdeURL );

        System.out.println( html );
        //get the list of data elements 
        List<SyncDataElement> readDE = read.readDe( html );
        for ( SyncDataElement item : readDE )
        {
            System.out.println( item );

            String DeURL = "";
            DeURL = item.getHref() + ".xml";
            //fetch every dataelement page	      
            html = fetchURL( DeURL );

            //	      System.out.println(html);
            //fetch the set of datasets for each element
            List<SyncDataSet> datasets = read.readDS( html );
            //fetch the list of dissagregations for each element
            List<SyncCategoryCombo> categoryCombo = read.readCategoryCombo( html );

            String CategoryComboURL = categoryCombo.get( 0 ).getHref().concat( ".xml" );
            html = fetchURL( CategoryComboURL );

            List<SyncCategoryOptionCombo> disaggregations = read.readCategoryOptionCombo( html );

            item.setDataSet( datasets );
            item.setDisaggregation( disaggregations );

            for ( SyncCategoryOptionCombo d : disaggregations )
            {
                System.out.println( d );
            }

            for ( SyncDataSet ds : datasets )
            {
                System.out.println( ds );
            }

            updateReferences( item, datasets, disaggregations );

        }

    }

    private void updateReferences( SyncDataElement de, List<SyncDataSet> datasets,
        List<SyncCategoryOptionCombo> disaggregations )
    {

        DHIS2ReportingService service = Context.getService( DHIS2ReportingService.class );

        DataElement jaxde = new DataElement();

        jaxde.setCode( de.getCode() );
        jaxde.setName( de.getName() );
        jaxde.setUid( de.getId() );

        List<DataElement> rtde = new ArrayList<DataElement>();
        rtde.add( jaxde );
        List<Disaggregation> rtdisag;
        List<ReportDefinition> rtrepdef = null;
        System.out.println( "hello1" );
        if ( service.getDataElementByUid( jaxde.getUid() ) == null )
        {
            service.saveDataElement( jaxde );
            System.out.println( "hello2" );
        }

        for ( SyncCategoryOptionCombo sdisag : disaggregations )
        {
            System.out.println( "hello3" );
            Disaggregation disag = new Disaggregation();
            disag.setCode( sdisag.getCode() );
            disag.setName( sdisag.getName() );
            disag.setUid( sdisag.getId() );

            rtdisag = new ArrayList<Disaggregation>();
            rtdisag.add( disag );
            service.saveDisaggregation( disag );
            System.out.println( "hello4" );
            //					JAXBContext jaxbContextDisag = JAXBContext.newInstance(Disaggregation.class);
            //					Marshaller jaxbDisagMarshaller = jaxbContextDisag.createMarshaller();
            //					jaxbDisagMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //					jaxbDisagMarshaller.marshal(disag, System.out);
            DataValueTemplate dvt = new DataValueTemplate();
            dvt.setDataelement( jaxde );
            dvt.setDisaggregation( disag );
            System.out.println( "hello5" + jaxde.getCode() );
            //            Set<DataValueTemplate> dvts = new HashSet<DataValueTemplate>();
            //            dvts.add( dvt );

            System.out.println( "hello6" );
            for ( SyncDataSet syds : datasets )
            {
                System.out.println( "hello7" );
                ReportDefinition ds = new ReportDefinition();
                ds.setCode( syds.getCode() );
                ds.setName( syds.getName() );
                ds.setUid( syds.getId() );
                //                ds.addDataValueTemplate( dvt );
                dvt.setReportDefinition( ds );
                //                dvt.setId( 4300 );
                //                ds.setDataValueTemplates( dvts );
                System.out.println( "hello8" );
                service.saveReportDefinition( ds );
                System.out.println( "hello9" );
                service.saveDataValueTemplateTest( dvt );

                rtrepdef = new ArrayList<ReportDefinition>();
                rtrepdef.add( ds );
                //                System.out.println( "hello9.5" );

            }
            //            service.saveDataValueTemplate( dvt );
            ReportTemplates rt = new ReportTemplates();
            rt.setDataElements( rtde );
            rt.setDisaggregations( rtdisag );
            rt.setReportDefinitions( rtrepdef );
            //            service.saveReportTemplates( rt );
            System.out.println( "hello10" );
        }//disag for

        System.out.println( "hello11" );
    }

    public String fetchURL( String webPage )
    {
        String result = "";
        try
        {
            String name = "admin";
            String password = "<PASSWORD>";<commit_msg>-- Bug fix to sync model for picking name from global properties<commit_after>package org.openmrs.module.dhisreport.api.syncmodel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.openmrs.api.context.Context;
import org.openmrs.module.dhisreport.api.DHIS2ReportingService;
import org.openmrs.module.dhisreport.api.model.DataElement;
import org.openmrs.module.dhisreport.api.model.DataValueTemplate;
import org.openmrs.module.dhisreport.api.model.Disaggregation;
import org.openmrs.module.dhisreport.api.model.ReportDefinition;
import org.openmrs.module.dhisreport.api.model.ReportTemplates;

public class ReportSynchronizer
{

    DHIS2ReportingService service = null;

    public void fullSync()
        throws UnsupportedEncodingException
    {
        service = Context.getService( DHIS2ReportingService.class );
        //Fetch DataElement group page
        URL dhisurl = service.getDhis2Server().getUrl();
        String url = dhisurl.toString();
        String webPage = url + "/api/dataElementGroups.xml";
        String OpenmrsdeURL = "";
        String html = fetchURL( webPage );
        InputStream stream = new ByteArrayInputStream( html.getBytes( "UTF-8" ) );
        System.out.println( html );
        StaXParser read = new StaXParser();
        List<SyncDataElementGroup> readDEG = read.readConfig( stream );
        for ( SyncDataElementGroup item : readDEG )
        {
            //Search for "openmrsde" dataelement group
            if ( item.getName().equals( "openmrsde" ) )
            {
                OpenmrsdeURL = item.getHref();
            }
            System.out.println( item );
        }

        //Fetch Openmrsde page 
        OpenmrsdeURL = OpenmrsdeURL + ".xml";
        html = fetchURL( OpenmrsdeURL );

        System.out.println( html );
        //get the list of data elements 
        List<SyncDataElement> readDE = read.readDe( html );
        for ( SyncDataElement item : readDE )
        {
            System.out.println( item );

            String DeURL = "";
            DeURL = item.getHref() + ".xml";
            //fetch every dataelement page	      
            html = fetchURL( DeURL );

            //	      System.out.println(html);
            //fetch the set of datasets for each element
            List<SyncDataSet> datasets = read.readDS( html );
            //fetch the list of dissagregations for each element
            List<SyncCategoryCombo> categoryCombo = read.readCategoryCombo( html );

            String CategoryComboURL = categoryCombo.get( 0 ).getHref().concat( ".xml" );
            html = fetchURL( CategoryComboURL );

            List<SyncCategoryOptionCombo> disaggregations = read.readCategoryOptionCombo( html );

            item.setDataSet( datasets );
            item.setDisaggregation( disaggregations );

            for ( SyncCategoryOptionCombo d : disaggregations )
            {
                System.out.println( d );
            }

            for ( SyncDataSet ds : datasets )
            {
                System.out.println( ds );
            }

            updateReferences( item, datasets, disaggregations );

        }

    }

    private void updateReferences( SyncDataElement de, List<SyncDataSet> datasets,
        List<SyncCategoryOptionCombo> disaggregations )
    {

        DHIS2ReportingService service = Context.getService( DHIS2ReportingService.class );

        DataElement jaxde = new DataElement();

        jaxde.setCode( de.getCode() );
        jaxde.setName( de.getName() );
        jaxde.setUid( de.getId() );

        List<DataElement> rtde = new ArrayList<DataElement>();
        rtde.add( jaxde );
        List<Disaggregation> rtdisag;
        List<ReportDefinition> rtrepdef = null;
        System.out.println( "hello1" );
        if ( service.getDataElementByUid( jaxde.getUid() ) == null )
        {
            service.saveDataElement( jaxde );
            System.out.println( "hello2" );
        }

        for ( SyncCategoryOptionCombo sdisag : disaggregations )
        {
            System.out.println( "hello3" );
            Disaggregation disag = new Disaggregation();
            disag.setCode( sdisag.getCode() );
            disag.setName( sdisag.getName() );
            disag.setUid( sdisag.getId() );

            rtdisag = new ArrayList<Disaggregation>();
            rtdisag.add( disag );
            service.saveDisaggregation( disag );
            System.out.println( "hello4" );
            //					JAXBContext jaxbContextDisag = JAXBContext.newInstance(Disaggregation.class);
            //					Marshaller jaxbDisagMarshaller = jaxbContextDisag.createMarshaller();
            //					jaxbDisagMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //					jaxbDisagMarshaller.marshal(disag, System.out);
            DataValueTemplate dvt = new DataValueTemplate();
            dvt.setDataelement( jaxde );
            dvt.setDisaggregation( disag );
            System.out.println( "hello5" + jaxde.getCode() );
            //            Set<DataValueTemplate> dvts = new HashSet<DataValueTemplate>();
            //            dvts.add( dvt );

            System.out.println( "hello6" );
            for ( SyncDataSet syds : datasets )
            {
                System.out.println( "hello7" );
                ReportDefinition ds = new ReportDefinition();
                ds.setCode( syds.getCode() );
                ds.setName( syds.getName() );
                ds.setUid( syds.getId() );
                //                ds.addDataValueTemplate( dvt );
                dvt.setReportDefinition( ds );
                //                dvt.setId( 4300 );
                //                ds.setDataValueTemplates( dvts );
                System.out.println( "hello8" );
                service.saveReportDefinition( ds );
                System.out.println( "hello9" );
                service.saveDataValueTemplateTest( dvt );

                rtrepdef = new ArrayList<ReportDefinition>();
                rtrepdef.add( ds );
                //                System.out.println( "hello9.5" );

            }
            //            service.saveDataValueTemplate( dvt );
            ReportTemplates rt = new ReportTemplates();
            rt.setDataElements( rtde );
            rt.setDisaggregations( rtdisag );
            rt.setReportDefinitions( rtrepdef );
            //            service.saveReportTemplates( rt );
            System.out.println( "hello10" );
        }//disag for

        System.out.println( "hello11" );
    }

    public String fetchURL( String webPage )
    {
        String result = "";
        try
        {
            String name = service.getDhis2Server().getUsername();
            String password = service.getDhis2Server().getPassword();
{'fraction_non_alphanumeric': 0.1029878752886836, 'fraction_numerical': 0.00440242494226328, 'mean_word_length': 1.950809199318569, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1120290', 'n_tokens_mistral': 3949, 'n_tokens_neox': 3815, 'n_words': 741}