Imports System.IO
Imports System.Xml

Public Class ImporterPatents
    Private Shared dpmaPatents As DPMAConnectPatents
    Private Shared parserPatents As New ParserPatents()
    Private Shared patents As New List(Of Patent)

    Public Shared Function FromDPMA()
        Dim query = QueryBuilderPatents.Build(config, database)
        Dim mainDoc As XmlDocument
        Dim patentDoc As XmlDocument

        logger.Info("Sending request with query: \n\n {0}", query)
        logger.Info("Search Type: Patents")

        dpmaPatents = New DPMAConnectPatents(config.username, config.password, config.searchType)

        mainDoc = dpmaPatents.Search(query)
        mainDoc.Save(MainPathPatents)

        ' Ergebnis auslesen und Patent-Klassen erstellen
        patents = ParserPatents.ReadPatentXMLFile(MainPathPatents)

        logger.Info("Importing Patents..")

        For Each patent In patents
            Dim akz As String = patent.LeadingRegisteredNumber
            Dim file As String = Path.Combine(DataDir, $"Patent-Data-{akz}.xml")

            logger.Debug($"Getting Register Info for Patent {akz}")

            patentDoc = dpmaPatents.GetRegisterInfo(akz)
            patentDoc.Save(file)

            ParserPatents.ReadPatentRegisterInfoXMLFile(file, patent)
        Next

        logger.Info("{0} Patents imported", patents.Count)

        Return patents
    End Function
End Class
