Search in sources :

Example 1 with CIIToUBL23Converter

use of com.helger.en16931.cii2ubl.CIIToUBL23Converter in project en16931-cii2ubl by phax.

the class CIIToUBLConverter method call.

// doing the business
public Integer call() throws Exception {
    m_sOutputDir = _normalizeOutputDirectory(m_sOutputDir);
    m_aSourceFiles = _normalizeInputFiles(m_aSourceFiles);
    final AbstractCIIToUBLConverter<?> aConverter;
    if ("2.1".equals(m_sUBLVersion))
        aConverter = new CIIToUBL21Converter();
    else if ("2.2".equals(m_sUBLVersion))
        aConverter = new CIIToUBL22Converter();
    else if ("2.3".equals(m_sUBLVersion))
        aConverter = new CIIToUBL23Converter();
    else
        throw new IllegalStateException("Unsupported UBL version '" + m_sUBLVersion + "' provided.");
    aConverter.setUBLCreationMode(m_eMode).setVATScheme(m_sVATScheme).setCustomizationID(m_sCustomizationID).setProfileID(m_sProfileID).setCardAccountNetworkID(m_sCardAccountNetworkID);
    final Locale aErrorLocale = Locale.US;
    for (final File f : m_aSourceFiles) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Converting file=" + f.getAbsolutePath());
        final File aDestFile = new File(m_sOutputDir, FilenameHelper.getBaseName(f) + "-ubl.xml");
        // TODO switch between versions
        final ErrorList aErrorList = new ErrorList();
        final Serializable aUBL = aConverter.convertCIItoUBL(f, aErrorList);
        if (aErrorList.containsAtLeastOneError() || aUBL == null) {
            LOGGER.error("Failed to convert CII file '" + f.getAbsolutePath() + "' to UBL:");
            for (final IError aError : aErrorList) LOGGER.error(aError.getAsString(aErrorLocale));
        } else {
            final boolean bFormattedOutput = true;
            final ESuccess eSuccess;
            if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.invoice_21.InvoiceType) {
                eSuccess = UBL21Writer.invoice().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.invoice_21.InvoiceType) aUBL, aDestFile);
            } else if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.creditnote_21.CreditNoteType) {
                eSuccess = UBL21Writer.creditNote().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.creditnote_21.CreditNoteType) aUBL, aDestFile);
            } else if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.invoice_22.InvoiceType) {
                eSuccess = UBL22Writer.invoice().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.invoice_22.InvoiceType) aUBL, aDestFile);
            } else if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.creditnote_22.CreditNoteType) {
                eSuccess = UBL22Writer.creditNote().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.creditnote_22.CreditNoteType) aUBL, aDestFile);
            } else if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.invoice_23.InvoiceType) {
                eSuccess = UBL23Writer.invoice().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.invoice_23.InvoiceType) aUBL, aDestFile);
            } else if (aUBL instanceof oasis.names.specification.ubl.schema.xsd.creditnote_23.CreditNoteType) {
                eSuccess = UBL23Writer.creditNote().setFormattedOutput(bFormattedOutput).write((oasis.names.specification.ubl.schema.xsd.creditnote_23.CreditNoteType) aUBL, aDestFile);
            } else
                throw new IllegalStateException("Unsupported UBL version '" + m_sUBLVersion + "'");
            if (eSuccess.isSuccess())
                LOGGER.info("Successfully wrote UBL file " + aDestFile.getAbsolutePath());
            else
                LOGGER.error("Failed to write UBL file " + aDestFile.getAbsolutePath());
        }
    }
    return Integer.valueOf(0);
}
Also used : Locale(java.util.Locale) Serializable(java.io.Serializable) CIIToUBL22Converter(com.helger.en16931.cii2ubl.CIIToUBL22Converter) ESuccess(com.helger.commons.state.ESuccess) IError(com.helger.commons.error.IError) CIIToUBL23Converter(com.helger.en16931.cii2ubl.CIIToUBL23Converter) ErrorList(com.helger.commons.error.list.ErrorList) CIIToUBL21Converter(com.helger.en16931.cii2ubl.CIIToUBL21Converter) File(java.io.File)

Aggregations

IError (com.helger.commons.error.IError)1 ErrorList (com.helger.commons.error.list.ErrorList)1 ESuccess (com.helger.commons.state.ESuccess)1 CIIToUBL21Converter (com.helger.en16931.cii2ubl.CIIToUBL21Converter)1 CIIToUBL22Converter (com.helger.en16931.cii2ubl.CIIToUBL22Converter)1 CIIToUBL23Converter (com.helger.en16931.cii2ubl.CIIToUBL23Converter)1 File (java.io.File)1 Serializable (java.io.Serializable)1 Locale (java.util.Locale)1