Refactor TED Repository and Introduce TED Mapping Functionality
- Updated the tender repository to create a unique index on tender_id, enhancing database query performance. - Introduced new TED mapping functions to convert parsed TED documents into tender entities, improving integration with the tender management system. - Added error handling for contract notice processing in the TED scraper, ensuring robust logging and error management. - Removed deprecated eform structures to streamline the codebase and focus on essential components.
This commit is contained in:
+42
-43
@@ -5,26 +5,13 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"tm/pkg/xmlparser"
|
||||
"tm/ted/eform/business_registration_information"
|
||||
"tm/ted/eform/contract"
|
||||
"tm/ted/eform/contract_award"
|
||||
"tm/ted/eform/prior_information"
|
||||
)
|
||||
|
||||
type ParserType string
|
||||
|
||||
const (
|
||||
ParserTypeContractNotice ParserType = "ContractNotice"
|
||||
ParserTypeContractAwardNotice ParserType = "ContractAwardNotice"
|
||||
ParserTypePriorInformationNotice ParserType = "PriorInformationNotice"
|
||||
ParserTypeBusinessRegistrationInformationNotice ParserType = "BusinessRegistrationInformationNotice"
|
||||
)
|
||||
|
||||
type TEDParser struct {
|
||||
ContractNotice xmlparser.Parser[contract.ContractNotice]
|
||||
ContractAwardNotice xmlparser.Parser[contract_award.ContractAwardNotice]
|
||||
PriorInformationNotice xmlparser.Parser[prior_information.PriorInformationNotice]
|
||||
BusinessRegistrationInformationNotice xmlparser.Parser[business_registration_information.BusinessRegistrationInformationNotice]
|
||||
ContractNotice xmlparser.Parser[ContractNotice]
|
||||
ContractAwardNotice xmlparser.Parser[ContractAwardNotice]
|
||||
PriorInformationNotice xmlparser.Parser[PriorInformationNotice]
|
||||
// BusinessRegistrationInformationNotice xmlparser.Parser[BusinessRegistrationInformationNotice]
|
||||
}
|
||||
|
||||
// NewTEDParser creates a new TED parser
|
||||
@@ -33,43 +20,55 @@ func NewTEDParser() *TEDParser {
|
||||
options.MaxSize = 50 * 1024 * 1024 // 50MB limit for TED XML files
|
||||
options.IgnoreUnknownElements = true // TED XML has many optional fields
|
||||
|
||||
contractNoticeParser := xmlparser.NewParser[contract.ContractNotice](options)
|
||||
contractAwardNoticeParser := xmlparser.NewParser[contract_award.ContractAwardNotice](options)
|
||||
priorInformationNoticeParser := xmlparser.NewParser[prior_information.PriorInformationNotice](options)
|
||||
businessRegistrationInformationNoticeParser := xmlparser.NewParser[business_registration_information.BusinessRegistrationInformationNotice](options)
|
||||
contractNoticeParser := xmlparser.NewParser[ContractNotice](options)
|
||||
contractAwardNoticeParser := xmlparser.NewParser[ContractAwardNotice](options)
|
||||
priorInformationNoticeParser := xmlparser.NewParser[PriorInformationNotice](options)
|
||||
// businessRegistrationInformationNoticeParser := xmlparser.NewParser[BusinessRegistrationInformationNotice](options)
|
||||
|
||||
return &TEDParser{
|
||||
ContractNotice: contractNoticeParser,
|
||||
ContractAwardNotice: contractAwardNoticeParser,
|
||||
PriorInformationNotice: priorInformationNoticeParser,
|
||||
BusinessRegistrationInformationNotice: businessRegistrationInformationNoticeParser,
|
||||
ContractNotice: contractNoticeParser,
|
||||
ContractAwardNotice: contractAwardNoticeParser,
|
||||
PriorInformationNotice: priorInformationNoticeParser,
|
||||
// BusinessRegistrationInformationNotice: businessRegistrationInformationNoticeParser,
|
||||
}
|
||||
}
|
||||
|
||||
// ParseAnyNotice parses XML content and detects the notice type automatically
|
||||
func (tp *TEDParser) Parse(content []byte) (interface{}, string, ParserType, error) {
|
||||
// ParseXML automatically detects and parses any supported TED XML document type
|
||||
func (p *TEDParser) ParseXML(content []byte) (*ParsedDocument, string, DocumentType, error) {
|
||||
// First, detect the root element type
|
||||
rootElement, err := detectRootElement(content)
|
||||
if err != nil {
|
||||
return nil, "", ParserType(""), fmt.Errorf("failed to detect root element: %w", err)
|
||||
return nil, "", DocumentType(""), fmt.Errorf("failed to detect root element: %w", err)
|
||||
}
|
||||
|
||||
// Parse based on the detected type
|
||||
switch ParserType(rootElement) {
|
||||
case ParserTypeContractNotice:
|
||||
notice, err := tp.ContractNotice.ParseBytes(content)
|
||||
return notice, notice.GetID(), ParserTypeContractNotice, err
|
||||
case ParserTypeContractAwardNotice:
|
||||
notice, err := tp.ContractAwardNotice.ParseBytes(content)
|
||||
return notice, notice.GetID(), ParserTypeContractAwardNotice, err
|
||||
case ParserTypePriorInformationNotice:
|
||||
notice, err := tp.PriorInformationNotice.ParseBytes(content)
|
||||
return notice, notice.GetID(), ParserTypePriorInformationNotice, err
|
||||
case ParserTypeBusinessRegistrationInformationNotice:
|
||||
notice, err := tp.BusinessRegistrationInformationNotice.ParseBytes(content)
|
||||
return notice, notice.GetID(), ParserTypeBusinessRegistrationInformationNotice, err
|
||||
parsedDoc := &ParsedDocument{Type: DocumentType(rootElement)}
|
||||
|
||||
switch parsedDoc.Type {
|
||||
case DocumentTypeContractNotice:
|
||||
notice, err := p.ContractNotice.ParseBytes(content)
|
||||
if err != nil {
|
||||
return nil, "", DocumentType(""), fmt.Errorf("failed to parse contract notice: %w", err)
|
||||
}
|
||||
parsedDoc.ContractNotice = notice
|
||||
return parsedDoc, notice.GetID(), parsedDoc.Type, nil
|
||||
|
||||
case DocumentTypeContractAwardNotice:
|
||||
notice, err := p.ContractAwardNotice.ParseBytes(content)
|
||||
if err != nil {
|
||||
return nil, "", DocumentType(""), fmt.Errorf("failed to parse contract award notice: %w", err)
|
||||
}
|
||||
parsedDoc.ContractAwardNotice = notice
|
||||
return parsedDoc, notice.GetID(), parsedDoc.Type, nil
|
||||
|
||||
case DocumentTypePriorInformationNotice:
|
||||
notice, err := p.PriorInformationNotice.ParseBytes(content)
|
||||
if err != nil {
|
||||
return nil, "", DocumentType(""), fmt.Errorf("failed to parse prior information notice: %w", err)
|
||||
}
|
||||
parsedDoc.PriorInformationNotice = notice
|
||||
return parsedDoc, notice.GetID(), parsedDoc.Type, nil
|
||||
default:
|
||||
return nil, "", ParserType(""), fmt.Errorf("unsupported notice type: %s", rootElement)
|
||||
return nil, "", DocumentType(""), fmt.Errorf("unsupported document type: %s", parsedDoc.Type)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user