0

This is the service class.I am creating a XML file by reading value from database. Code is using three more pojo classes. Mt700, Header and Swift details. MT700 is main class for Header and swift details. Problem is I am able to store everything one time. Doesn't matter how many rows of data I have when the file get generated with one record it has only one header and one swift details. How can I make this work in loop? I think I have to use list but I am not sure how to use it to make it work.

    public void generateEliteExtracts(int trdCustomerKy, Date lastRunDate, Date currentDate) throws TradeException {
            FileOutputStream fout = null;
            try {

                            MT700 mt700 = getMT700(trdCustomerKy,lastRunDate,currentDate);
                            if (null != mt700){
                            StringBuffer fileName = new StringBuffer(1024);
                            fileName.append(mConfiguration.getOutDirectory()).append(MT700_MSGTYPE)
                                    .append(DOT).append(mConfiguration.getOutputFileExtn());
                            smLog.debug("Generated Extract for BankRef" + fileName.toString());
                            mTracer.log("Generated Extract for BankRef" + fileName.toString());
                            File xmlFile = new File(fileName.toString());
                            fout = new FileOutputStream(xmlFile);
                            fout.write(MT700_XMLHEADER.getBytes());
                            JAXBContext jaxbContext = JAXBContext.newInstance(MT700.class);
                            Marshaller marshaller = jaxbContext.createMarshaller();
                            marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING_ASCII);
                            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
                            marshaller.setProperty("com.sun.xml.internal.bind.xmlDeclaration", Boolean.FALSE);
                            marshaller.marshal(mt700, fout);
                            IOUtils.closeQuietly(fout);
                            }
        }catch(

        Exception ex)

        {
            smLog.error("Caught unexpected error while creating extracts. ", ex);
            throw new TradeException("Caught unexpected error while creating extracts.", ex);
        } finally

        {
            IOUtils.closeQuietly(fout);
        }

        }

        private  MT700 getMT700(int trdCustomerKy, Date lastRunDate, Date currentDate) throws TradeException {
            MT700 mt700 = new MT700();

            AbInBevEliteExtractDAO dao = new AbInBevEliteExtractDAO(mConnection);
            CompanyCodesHelper ccHelper = new CompanyCodesHelper(mConnection);
            String cifCodes = ccHelper.getDescription(trdCustomerKy, "CIF Codes", "CIF Codes");
            if (false == TradeUtil.isStringNull(cifCodes)) {
                mTracer.log("Fetching records for CIFs: " + StringUtils.replace(cifCodes, PIPE, COMMA));
                String[] codes = StringUtils.split(cifCodes, PIPE);
                List<ExportAdvicesData> exportList = dao.getExportAdvices(trdCustomerKy, lastRunDate, currentDate, codes);
                for (int i = 0; i < exportList.size(); i++) {

                    ExportAdvicesData exportData = exportList.get(i);

                    if ("XXLC".equalsIgnoreCase(exportData.getDocAcronym())) {
                        Header header = new Header();
                        header.setMessageType("N");
                        header.setVersionNo("1.0");
                        header.setRevisionNo("00");
                        header.setDocumentDate(DateUtil.formatDate(new Date(), DATE_FORMAT_YYYY_MM_DD_HHMMSS));
                        header.setBankId("BOA" + StringUtils.substring(exportData.getCustRef(), 0, 4));
                        header.setCustId("XOM");
                        SwiftDetails swiftTest = new SwiftDetails();
                        header.setDocumentType(MT700_MSGTYPE);

                        SwiftParserBankDocs parser = new SwiftParserBankDocs(exportData.getDocumentContent());

                        String bankRef = parser.getTagValue("21");
                        String custRef = parser.getTagValue("20");

                        if (TradeUtil.isStringNull(bankRef)) {
                            header.setCustRefNo("NONREF");
                            header.setBankRefNo(custRef);
                        } else {
                            header.setCustRefNo(custRef);
                            header.setBankRefNo(bankRef);
                        }

                        swiftTest.setTAG_27("1/1");
                        swiftTest.setTAG_20(custRef);
                        swiftTest.setTAG_23(EMPTY_STRING);
                        String issueDate = parser.getTagValue("31C");
                        swiftTest.setTAG_31C(getDateInYYMMDD(issueDate));
                        swiftTest.setTAG_40E("UCP LATEST VERSION");
                        String datePlaceOfExpiry = parser.getTagValue("31D");
                        swiftTest.setTAG_31D(getFormattedDatePlaceOfExpiry(datePlaceOfExpiry));
                        swiftTest.setTAG_50(parser.getTagValue("50"));
                        swiftTest.setTAG_59(parser.getTagValue("59"));
                        swiftTest.setTAG_32B(getCurrencyCdAmount(parser.getTagValue("32B")));
                        if (false == TradeUtil.isStringNull(exportData.getPositiveTolerance())) {
                            swiftTest.setTAG_39A(
                                    exportData.getPositiveTolerance() + "/" + exportData.getPositiveTolerance());
                        } else {
                            swiftTest.setTAG_39A(EMPTY_STRING);
                        }
                        swiftTest.setTAG_39B(EMPTY_STRING);
                        swiftTest.setTAG_39C(EMPTY_STRING);
                        swiftTest.setTAG_41A(parser.getTagValue("41D"));
                        String tag42A = parser.getTagValue("42A");
                        swiftTest.setTAG_42A(tag42A);
                        if (TradeUtil.isStringNull(tag42A)) {
                            swiftTest.setTAG_42A(parser.getTagValue("42D"));
                        }

                        swiftTest.setTAG_42C(parser.getTagValue("42C"));
                        swiftTest.setTAG_42M(parser.getTagValue("42M"));
                        swiftTest.setTAG_42P(parser.getTagValue("42P"));
                        swiftTest.setTAG_43P(parser.getTagValue("43P"));
                        swiftTest.setTAG_43T(parser.getTagValue("43T"));

                        if (!(TradeUtil.isStringNull(parser.getTagValue("44A")))) {
                            swiftTest.setTAG_44A(parser.getTagValue("44A"));
                        }

                        if (!(TradeUtil.isStringNull(parser.getTagValue("44B")))) {
                            swiftTest.setTAG_44B(parser.getTagValue("44B"));
                        }

                        if (!(TradeUtil.isStringNull(parser.getTagValue("44E")))) {
                            swiftTest.setTAG_44E(parser.getTagValue("44E"));
                        }

                        if (!(TradeUtil.isStringNull(parser.getTagValue("44F")))) {
                            swiftTest.setTAG_44F(parser.getTagValue("44F"));
                        }

                        Date latestShipDate = exportData.getLatestShipDate();
                        if (null != latestShipDate) {
                            swiftTest.setTAG_44C(DateUtil.formatDate(latestShipDate, DATE_FORMAT_YYMMDD));
                        } else {
                            swiftTest.setTAG_44C(EMPTY_STRING);
                        }
                        swiftTest.setTAG_44D(parser.getTagValue("44D"));

                        swiftTest.setTAG_45A(parser.getTagValue("45") + BLANK_STRING + parser.getTagValue("45A")
                                + BLANK_STRING + parser.getTagValue("45B"));
                        swiftTest.setTAG_46A(parser.getTagValue("46") + BLANK_STRING + parser.getTagValue("46A")
                                + BLANK_STRING + parser.getTagValue("46B"));
                        swiftTest.setTAG_47A(parser.getTagValue("47") + BLANK_STRING + parser.getTagValue("47A")
                                + BLANK_STRING + parser.getTagValue("47B"));
                        swiftTest.setTAG_71B(parser.getTagValue("71B"));
                        swiftTest.setTAG_48(parser.getTagValue("48"));
                        swiftTest.setTAG_49(parser.getTagValue("49"));
                        swiftTest.setTAG_50B(EMPTY_STRING);
                        swiftTest.setTAG_51A(EMPTY_STRING);

                        String issuingBank = parser.getAddress(SwiftParserBankDocs.ISSUING_BANK);
                        if (TradeUtil.isStringNull(issuingBank)) {
                            String errorMsg = "Issuing Bank address not found in bankdoc text, SWIFT content is possibly invalid, skipped processed record: "
                                    + exportData.getCustRef();
                            smLog.error(errorMsg);
                            mTracer.log("ERROR: " + errorMsg);

                        }
                        issuingBank = StringUtils.replace(issuingBank, CRLF, BLANK_STRING + CRLF);
                        swiftTest.setTAG_52A(issuingBank);
                        swiftTest.setTAG_53A(parser.getTagValue("53A"));
                        swiftTest.setTAG_78(parser.getTagValue("78"));
                        swiftTest.setTAG_57A(parser.getAddress("TO:"));
                        swiftTest.setTAG_72(parser.getTagValue("72"));
                        swiftTest.setTAG_40A(parser.getTagValue("40B"));
                        if (parser.is710Advice()) {
                            swiftTest.setTAG_20(parser.getTagValue("21"));

                        }
                        mt700.setSwift700(swiftTest);
                        mt700.setHeader(header);
                    } else if ("XAMD".equalsIgnoreCase(exportData.getDocAcronym())) {
                        Header header = new Header();
                        header.setMessageType("N");
                        header.setVersionNo("1.0");
                        header.setRevisionNo("00");
                        header.setDocumentDate(DateUtil.formatDate(new Date(), DATE_FORMAT_YYYY_MM_DD_HHMMSS));
                        header.setBankId("BOA" + StringUtils.substring(exportData.getCustRef(), 0, 4));
                        header.setCustId("XOM");
                        SwiftDetails swift = new SwiftDetails();
                        header.setDocumentType(MT707_MSGTYPE);
                        SwiftParserBankDocs parser = new SwiftParserBankDocs(exportData.getDocumentContent());

                        String custRef = parser.getTagValue("20");
                        String bankRef = parser.getTagValue("23");

                        if (TradeUtil.isStringNull(bankRef)) {
                            header.setBankRefNo("NONREF");
                        } else {
                            header.setBankRefNo(bankRef);
                        }
                        header.setCustRefNo(custRef);
                        swift.setTAG_20(custRef);
                        swift.setTAG_21(parser.getTagValue("21"));
                        swift.setTAG_23(EMPTY_STRING);
                        String issuingBank = parser.getAddress(SwiftParserBankDocs.ISSUING_BANK);
                        if (TradeUtil.isStringNull(issuingBank)) {
                            String errorMsg = "Issuing Bank address not found in bankdoc text, SWIFT content is possibly invalid, skipped processed record: "
                                    + exportData.getCustRef();
                            smLog.error(errorMsg);
                            mTracer.log("ERROR: " + errorMsg);
                            swift.setTAG_52A(EMPTY_STRING);
                        } else {
                            issuingBank = StringUtils.replace(issuingBank, CRLF, BLANK_STRING + CRLF);
                            swift.setTAG_52A(issuingBank);
                        }
                        swift.setTAG_31C(getDateInYYMMDD(parser.getTagValue("31C")));
                        swift.setTAG_30(getDateInYYMMDD(parser.getTagValue("30")));
                        swift.setTAG_26E(parser.getTagValue("26E"));
                        swift.setTAG_59(parser.getTagValue("59"));
                        swift.setTAG_31E(getDateInYYMMDD(parser.getTagValue("31E")));
                        swift.setTAG_79(parser.getTagValue("79"));
                        swift.setTAG_72(parser.getTagValue("72"));
                        swift.setTAG_32B(getCurrencyCdAmount(parser.getTagValue("32B")));
                        swift.setTAG_33B(getCurrencyCdAmount(parser.getTagValue("33B")));
                        swift.setTAG_34B(getCurrencyCdAmount(parser.getTagValue("34B")));
                        swift.setTAG_39A(parser.getTagValue("39A"));
                        swift.setTAG_39B(parser.getTagValue("39B"));
                        swift.setTAG_39C(parser.getTagValue("39C"));
                        swift.setTAG_44A(parser.getTagValue("44A"));
                        swift.setTAG_44B(parser.getTagValue("44B"));
                        swift.setTAG_44C(parser.getTagValue("44C"));
                        swift.setTAG_44D(parser.getTagValue("44D"));
                        swift.setTAG_44E(parser.getTagValue("44E"));
                        swift.setTAG_44F(parser.getTagValue("44F"));
                        mt700.setHeader(header);
                        mt700.setSwift700(swift);
                    }


                }
            }
            return mt700;
        }

This is MT700 POJO class. In this class I am calling header and swift details pojo classes.

    @XmlRootElement(name = "MT700")
    public class MT700 implements Serializable
    {

        /**
         * serialVersionUID
         */
        private static final long serialVersionUID = 1L;

        private Header header;
        private SwiftDetails swift700;

        private String version = "1.0";

        public Header getHeader()
        {
            return header;
        }

        @XmlElement(name = "Header")
        public void setHeader(Header header)
        {
            this.header = header;
        }

        /**
         * @return the swift700
         */
        public SwiftDetails getSwift700()
        {
            return swift700;
        }

        @XmlElement(name = "Swift_Details_700")
        public void setSwift700(SwiftDetails swift700)
        {
            this.swift700 = swift700;
        }

        public String getVersion()
        {
            return version;
        }


        @XmlAttribute(name = "Version")
        public void setVersion(String version)
        {
            this.version = version;
        }

    }

This is Header class. I class similar to like this which has tags and that is swift details

    @XmlRootElement(name = "Header")
    @XmlType(propOrder = { "documentType", "messageType", "versionNo",
            "revisionNo", "documentDate", "bankId", "custId", "custRefNo",
            "bankRefNo" })
    public class Header implements Serializable
    {

        private static final long serialVersionUID = 1L;

        private String documentType;
        private String messageType;
        private String versionNo;
        private String revisionNo;
        private String documentDate;
        private String bankId;
        private String custId;
        private String custRefNo;
        private String bankRefNo;

I am not adding getter and setter for this class to make the post look simple

1 Answer 1

0

You are creating one MT700 instance and then in this loop, you are reassigning the header and swift fields each time through the loop:

MT700 mt700 = new MT700();
for (int i = 0; i < exportList.size(); i++) {
    ...
    mt700.setHeader(header);
    mt700.setSwift700(swift);
}

This means that the document you are outputting contains just the last header/swift returned from the database query.

You need to make one or more of these three into a list of some sort. Either your MT700 contains a list of headers and swifts, or more likely you want to have a list of MT700s each with one header and one swift.

In other words, you want to have a fourth type that will be the actual root of your XML document. That element will contain one MT700 element for each row found by the query. Each MT700 element will have a header element and a swift element.

So, more specifically, here is what you want to do:

@XmlRootElement
class MT700s {
    @XmlElement(name = "MT700")
    private List<MT700> mt700s = new ArrayList<>();
    public List<MT700> getMT700s() { return mt700s; }
    // Etc.
}

MT700s mt700s = new MT700s();
for (int i = 0; i < exportList.size(); i++) {
    MT700 mt700 = new MT700();
    ...
    mt700.setHeader(header);
    mt700.setSwift700(swift);
    mt700s.getMT700s().add(mt700);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I did that now the file is getting generate. I has data but it is not XML. I created the LIst. However at JAXB I am getting error. Seems JAXB is not accepting the LIST....
I am exactly in this situation. What should I do? stackoverflow.com/questions/12139996/…
The answer on that question tells you exactly what you want to do. I will edit my answer to provide some more specific guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.