<?xml version="1.0" encoding="UTF-8"?>
<xsp:page language="java" xmlns:xsp="http://apache.org/xsp" xmlns:util="http://www.apache.org/1999/XSP/Util" xmlns:oai="http://www.culture.gouv.fr/ns/oai/">
    <xsp:structure>
        <xsp:include>fr.gouv.culture.oai.*</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.framework.Framework</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.framework.FrameworkImpl</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.application.Application</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.documentbase.DocumentBase</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.utils.Utilities</xsp:include>
        <xsp:include>org.apache.cocoon.xml.IncludeXMLConsumer</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.exception.SDXException</xsp:include>
        <xsp:include>fr.gouv.culture.sdx.exception.SDXExceptionCode</xsp:include>
        <xsp:include>org.apache.avalon.framework.service.ServiceManager</xsp:include>
        <xsp:include>org.apache.avalon.framework.service.WrapperServiceManager</xsp:include>
    </xsp:structure>
    <xsp:init-page>
        <xsp:logic>
    		ServiceManager cocoon_serviceManager = new WrapperServiceManager(manager);   
    	</xsp:logic>
    </xsp:init-page>
    <oai:harvest>
        <xsp:logic>
            try{
                FrameworkImpl frame = (FrameworkImpl) cocoon_serviceManager.lookup(Framework.ROLE);
                OAIComponentImpl oaiComp = (OAIComponentImpl) cocoon_serviceManager.lookup(OAIComponent.ROLE);
    
                OAIHarvester oaiHarv = null;
                String harvApp = "sdxtest2";//TODO: allow user input to choose APP to which harvesting xml will be directed
                String harvBase = "sdxworld";//TODO: allow user input to choose BASE to which harvesting xml will be directed
                if (Utilities.checkString(harvApp ) &amp;&amp; Utilities.checkString(harvBase ))
                {
                    Application appli = null;
                    DocumentBase docbase = null;

                    if (frame != null)
                        appli = frame.getApplicationByPath(harvApp);
                        //appli = frame.getApplicationById(harvApp);

                    if (appli != null)
                        docbase = appli.getDocumentBase(harvBase);
                    if (docbase != null)
                        oaiHarv = docbase.getOAIHarvester();

                    if (oaiComp != null)
                    {

                        //TODO:the below links could be to any oai repository
                        String requestUrl =  null;
                        requestUrl = "http://localhost:8080/sdx/sdx/oai/sdxtest/sdxworld?verb=ListRecords&amp;metadataPrefix=oai_dc";
                        //an external source
                        //requestUrl = "http://memory.loc.gov/cgi-bin/oai2_0?verb=GetRecord&amp;metadataPrefix=oai_dc&amp;identifier=oai%3Alcoa1.loc.gov%3Ahttp%3A%2F%2Fhdl.loc.gov.%2Floc.pnp%2Fppmsca.02282";
                        //an external error generating source
                        //requestUrl = "http://memory.loc.gov/cgi-bin/oai2_0?verb=GetRecord&amp;identifier=oai%3Alcoa1.loc.gov%3Ahttp%3A%2F%2Fhdl.loc.gov.%2Floc.pnp%2Fppmsca.02282";
                        //setting the consumer of the harvester to be this xsp page
                        oaiHarv.setConsumer(new IncludeXMLConsumer(this.contentHandler));
                        //setting up the harvester to receive the events from the requestUrl
                        oaiComp.receiveResponse(requestUrl, oaiHarv, "identifier");//TODO: allow user input here for "identifier" variable


                        /*TODO:
                        *OAI-PMH harvesters should follow the standard practices for HTTP robotic agents.
                        *In particular, they should supply HTTP User-Agent and From headers. The User-Agent
                        *header field should contain information about the user agent originating the request,
                        *it is described in section 14.43 of the HTTP specification. The From header field
                        *should contain an Internet e-mail address for the human user who controls the harvested,
                        *it is is described in section 14.22 of the HTTP specification. The email address in the
                        *From header will provide a point of contact if there is some problem created by the harvester.
                        */

                        /*TODO:
                        *Harvesters wishing to harvest with finer than day granularity must examine the &lt;granularity&gt;
                        *element in the Identify response. Repositories will issue a badGranularity error if from and until
                        *parameters are issued with finer granularity than is supported.

                        *Items in a repository may change or be added during a harvest, or after a harvest within the same
                        *datestamp (i.e. the same day if the datestamp is YYYY-MM-DD). This means that to incrementally harvest
                        *from a repository, a harvester should overlap successive incremental harvests by one datestamp increment
                        *(i.e. one day if the granularity is YYYY-MM-DD). Furthermore, since it is repository implementation dependent
                        *whether changes that occur during the harvest will be reflected in the response, the from argument of the next
                        *incremental harvest should be based on the the responseDate returned in the first partial-list response of a sequence.
                        *When harvesting from repositories which use a datestamp granularity of one second, it is advisable to overlap by a small
                        *additional amount to account for any discrepancy between the reported responseDate and the time at the repository when any
                        *search necessary to answer the request was performed.
                        */

                        /*TODO: steps
                        *Issue an Identify request to find the finest datestamp granularity supported.
                        *Issue a ListMetadataFormats request to obtain a list of all metadataPrefixes supported.
                        *Harvest using ListRecords requests for each metadataPrefix supported. Knowledge of the
                        datestamp granularity allows for less overlap if granularities finer than a day are supported.
                        *Set structure can be inferred from the setSpec elements in the header blocks of each record
                        returned (consistency checks are possible).
                        *Items may be reconstructed from the constituent records. Local datestamps must be assigned
                        to harvested items.
                        *Provenance and other information in &lt;about&gt; blocks may be re-assembled at the item level if
                        it is the same for all metadata formats harvested. However, this information may be supplied
                        differently for different metadata formats and may thus need to be store separately for each
                        metadata format.
                        */
                        //sending events to the consumer of the harvester, this.contentHandler
                        oaiHarv.sendPastHarvestsSummary();
                        oaiHarv.sendStoredHarvestingRequests();
                    }
                }

            } catch (SDXException e){
                e.toSAX(this.contentHandler);
            } catch (Exception e){
                String[] args = {"An exception terminated the process"};
                new SDXException(null, SDXExceptionCode.ERROR_GENERIC, args , e).toSAX(this.contentHandler);
            }
        </xsp:logic>
    </oai:harvest>
</xsp:page>
