com.hin.hl7messaging.InvoiceSvgService.java Source code

Java tutorial

Introduction

Here is the source code for com.hin.hl7messaging.InvoiceSvgService.java

Source

/**
 * 
 */
package com.hin.hl7messaging;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.commons.io.IOUtils;
import org.apache.fop.svg.PDFTranscoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hin.domain.Concept;
import com.hin.domain.ConceptAttribute;
import com.hin.domain.vo.MessageVO;
import com.hin.hl7messaging.api.IInvoiceSvgService;
import com.hin.hl7messaging.cassandra.ICassandraConnector;
import com.hin.hl7messaging.utils.XMLHelper;
import com.hin.service.impl.ConceptService;

/**
 * @author shilpa.rao
 * 
 */
@Service(value = "invoiceSvgService")
public class InvoiceSvgService implements IInvoiceSvgService {

    @Autowired
    private ICassandraConnector cassandraConnector;

    @Autowired
    ConceptService conceptService;

    @Value("${messageConfig.organizationXpath}")
    private String organizationXpath;

    @Value("${messageConfig.participantXpath}")
    private String participantXpath;

    @Value("${FileAttachment.ATTACHMENT_DIR}")
    private String ATTACHMENT_DIR;

    @Value("${messageConfig.LicenseeXPath}")
    private String LicenseeXPath;

    int serviceYAxis = 0;

    @Override
    public String createSvgDocument(Document doc, List<Concept> invoiceServiceArray, String organizationName,
            String telecom, String address, String exchangeRate, String currencyCode) {

        String stringData = "", invoiceId = "", totalAmount = "", doctorName = "", patientName = "", discount = "",
                interest = "";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        HashMap<String, String> serviceMap = new HashMap<String, String>();
        HashMap<String, String> xCoordinatesMap = new HashMap<String, String>();

        String doctorPrefix = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/consultant/employmentStaff/employeePerson/name/prefix",
                XPathConstants.STRING);
        String doctorGiven = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/consultant/employmentStaff/employeePerson/name/given",
                XPathConstants.STRING);
        String doctorFamily = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/consultant/employmentStaff/employeePerson/name/family",
                XPathConstants.STRING);
        String doctorSuffix = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/consultant/employmentStaff/employeePerson/name/suffix",
                XPathConstants.STRING);

        if (doctorPrefix != null && doctorPrefix != "")
            doctorName = doctorName + " " + doctorPrefix;
        if (doctorGiven != null && doctorGiven != "")
            doctorName = doctorName + " " + doctorGiven;
        if (doctorFamily != null && doctorFamily != "")
            doctorName = doctorName + " " + doctorFamily;
        if (doctorSuffix != null && doctorSuffix != "")
            doctorName = doctorName + " " + doctorSuffix;

        NodeList idFields = (NodeList) XMLHelper.read(doc, "message/FIAB_MT020000HT02/id", XPathConstants.NODESET);
        int idSize = idFields.getLength();

        for (int i = 1; i <= idSize; i++) {

            String rootNode = "message/FIAB_MT020000HT02/id[" + i + "]/root";
            String root = (String) XMLHelper.read(doc, rootNode, XPathConstants.STRING);
            root = root.replaceAll("\\n", "").trim();
            if (root.equals("INVOICE_ID")) {
                invoiceId = (String) XMLHelper.read(doc, "message/FIAB_MT020000HT02/id[" + i + "]/extension",
                        XPathConstants.STRING);
            }
            if (root.equals("INVOICE_AMOUNT")) {
                totalAmount = (String) XMLHelper.read(doc, "message/FIAB_MT020000HT02/id[" + i + "]/extension",
                        XPathConstants.STRING);
            }
            if (root.equals("DISCOUNT")) {
                discount = (String) XMLHelper.read(doc, "message/FIAB_MT020000HT02/id[" + i + "]/extension",
                        XPathConstants.STRING);
            }
            if (root.equals("INTEREST")) {
                interest = (String) XMLHelper.read(doc, "message/FIAB_MT020000HT02/id[" + i + "]/extension",
                        XPathConstants.STRING);
            }

        }

        String date = (String) XMLHelper.read(doc, "message/FIAB_MT020000HT02/effectiveTime/value",
                XPathConstants.STRING);
        String[] myStringArray = date.split(" ");
        date = myStringArray[0];

        String prefix = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/subject/patient/patientPerson/name/prefix",
                XPathConstants.STRING);
        String given = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/subject/patient/patientPerson/name/given",
                XPathConstants.STRING);
        String family = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/subject/patient/patientPerson/name/family",
                XPathConstants.STRING);
        String suffix = (String) XMLHelper.read(doc,
                "message/FIAB_MT020000HT02/postingTo/patientAccount/pertinentInformation/encounterEvent/subject/patient/patientPerson/name/suffix",
                XPathConstants.STRING);

        if (prefix != null && prefix != "")
            patientName = patientName + " " + prefix;
        if (given != null && given != "")
            patientName = patientName + " " + given;
        if (family != null && family != "")
            patientName = patientName + " " + family;
        if (suffix != null && suffix != "")
            patientName = patientName + " " + suffix;

        //System.out.println("Patient Name: " + patientName);

        NodeList serviceFields = (NodeList) XMLHelper.read(doc, "message/FIAB_MT020000HT02/pertinentInformation",
                XPathConstants.NODESET);
        int serviceSize = serviceFields.getLength();

        for (int i = 1; i <= serviceSize; i++) {

            String serviceName = (String) XMLHelper.read(doc,
                    "message/FIAB_MT020000HT02/pertinentInformation[" + i + "]/observationOrder/code/displayName",
                    XPathConstants.STRING);
            serviceName = serviceName.replaceAll("\\n", "").trim();
            String cost = (String) XMLHelper.read(doc,
                    "message/FIAB_MT020000HT02/component[" + i
                            + "]/financialTransactionChargeDetail/unitPriceAmt/numerator/value",
                    XPathConstants.STRING);
            cost = cost.replaceAll("\\n", "").trim();

            String serviceCode = (String) XMLHelper.read(doc,
                    "message/FIAB_MT020000HT02/pertinentInformation[" + i + "]/observationOrder/code/code",
                    XPathConstants.STRING);
            serviceCode = serviceCode.replaceAll("\\n", "").trim();

            serviceMap.put(serviceName, cost);

            if (invoiceServiceArray != null && !invoiceServiceArray.isEmpty()) {
                for (Concept service : invoiceServiceArray) {
                    if (!service.getDescription().equals(serviceCode)) {
                        serviceMap.put("---" + service.getDescription(), "");
                    }
                }
            }
        }

        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        Document document = db.newDocument();
        Element rootElement = createRootElement(document);
        Element g = document.createElement("g");
        Element subG = document.createElement("g");

        Element imageElement = createImage(document, subG);

        String style = "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#500e4a;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light";

        Element organizationElement = createTestTitleElement(document, subG, organizationName, "500", "113", style);
        Element addressElement = createTestTitleElement(document, subG, address, "500", "133", style);
        Element telecomElement = createTestTitleElement(document, subG, telecom, "500", "153", style);

        style = "font-size:25.52000046px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold";
        Element headerElement = createTestTitleElement(document, subG, "INVOICE", "1050", "113", style);

        style = "font-size:18.44000006px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold";
        Element customerElement = createTestTitleElement(document, subG, "To: " + patientName, "100", "290", style);

        style = "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light";
        Element invoiceNumElement = createTestTitleElement(document, subG, "Invoice Number : " + invoiceId, "930",
                "290", style);
        Element dateElement = createTestTitleElement(document, subG, "Date : " + date, "930", "310", style);
        Element doctorElement = createTestTitleElement(document, subG, "Doctor : " + doctorName, "930", "330",
                style);

        Element horizontalElement = createHorizontalLine(document, subG, "100", "380");
        Element verticalElement = createVerticleLines(document, subG, "100", "380", serviceMap.size());
        Element horizontalHeaderEndElement = createHorizontalLine(document, subG, "100", "450");

        style = "font-size:18.44000006px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold";
        Element descriptionElement = createTestTitleElement(document, subG, "Description", "400", "410", style);
        Element amtHeaderElement = createTestTitleElement(document, subG, "Amount in USD", "980", "410", style);

        style = "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light";

        xCoordinatesMap.put("115", "1150");

        Element closeElement = createEndComponent(document, subG, "100", String.valueOf(serviceYAxis + 100),
                totalAmount, exchangeRate, currencyCode, discount, interest);
        Element serviceElement = createServiceElement(document, subG, serviceMap, xCoordinatesMap, "480", style);

        rootElement.appendChild(g);
        g.appendChild(imageElement);
        g.appendChild(organizationElement);
        g.appendChild(addressElement);
        g.appendChild(telecomElement);
        g.appendChild(headerElement);
        g.appendChild(customerElement);
        g.appendChild(invoiceNumElement);
        g.appendChild(dateElement);
        g.appendChild(doctorElement);
        g.appendChild(horizontalElement);
        g.appendChild(verticalElement);
        g.appendChild(horizontalHeaderEndElement);
        g.appendChild(descriptionElement);
        g.appendChild(amtHeaderElement);
        g.appendChild(closeElement);
        g.appendChild(serviceElement);
        g.appendChild(serviceElement);

        document.appendChild(rootElement);
        stringData = XMLHelper.getXMLDocumentAsString(document);
        //System.out.println(stringData);
        return stringData;
    }

    // creating RootElement
    public Element createRootElement(Document document) {
        Element rootElement = document.createElement("svg");
        rootElement.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        rootElement.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
        rootElement.setAttribute("width", "1500");
        rootElement.setAttribute("height", "1500");
        return rootElement;
    }

    //create horizontal line
    public Element createHorizontalLine(Document document, Element subG, String x, String y) {
        Element path = document.createElement("path");
        path.setAttribute("id", "horizontalLine");
        String dString = "m " + x + "," + y + " " + 1150 + ",0";
        path.setAttribute("d", dString);
        path.setAttribute("style",
                "fill:none;stroke:#0c0c0c;stroke-width:2.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");
        subG.appendChild(path);
        return subG;
    }

    // creatingVerticalLine
    public Element createVerticleLines(Document document, Element subG, String x, String y, int size) {

        int yMax = Integer.parseInt(y) + size * 30;

        String dString = "M " + x + "," + y + " " + x + "," + (yMax + 100) + "";
        Element path = document.createElement("path");
        path.setAttribute("id", "verticalLine");
        path.setAttribute("d", dString);
        path.setAttribute("style",
                "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

        subG.appendChild(path);

        //createVerticleEndLine
        x = String.valueOf(1250);

        String dString1 = "M " + x + "," + y + " " + x + "," + (yMax + 200) + "";
        Element path1 = document.createElement("path");
        path1.setAttribute("id", "verticalLine");
        path1.setAttribute("d", dString1);
        path1.setAttribute("style",
                "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

        subG.appendChild(path1);

        //verticalDividerElement
        x = String.valueOf(900);

        String dString2 = "M " + x + "," + y + " " + x + "," + (yMax + 200) + "";
        Element path2 = document.createElement("path");
        path2.setAttribute("id", "verticalLine");
        path2.setAttribute("d", dString2);
        path2.setAttribute("style",
                "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

        subG.appendChild(path2);
        serviceYAxis = yMax;

        return subG;

    }

    //create Image
    public Element createImage(Document document, Element subG) {
        Element path = document.createElement("image");
        path.setAttribute("xlink:href",
                "data:image/png;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f/8AAEQgAeQCWAwERAAIRAQMRAf/EAJoAAQABBQEBAAAAAAAAAAAAAAAHAwQFBggCAQEBAAMBAQEAAAAAAAAAAAAAAAEDBAIFBhAAAQMDAwIEBAMDCgcAAAAAAQIDBAARBSESBhMHMUFRImFxMhQjFQiBQheRobFSgjOzJDQWYnJDhCVFGBEAAgIBBAECBQQDAQAAAAAAAAERAgMhMRIEE0EiUWGBMgVxoUIzYiMUQ//aAAwDAQACEQMRAD8A6poBQCgFAKAUAoBQCgMTC5bxmdmZGFh5SNIy0QEyYLbqVOo2myrpB/dJ19POphnTo0pjQ07I5XupF7rIR0G/4epj73ntrRAs0bkr/vur1rAJHt2/y10ko+Zaq0dP8jesXl4mSbWuPuBbNlpWLEX8PC9ctQVWq0U4+bjP5R3HJQsONA3WbbSU2v8A00jQl00kyNQcCgFAKAUAoB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WpJKpLZQna4VAAlYtZRIAGtAe1R2FPIfU2kvNpUht0pG5KV23BKvEA7Rf5UBRaxWMZnPZBqGw3PkJCX5aG0JecSPALcA3KAt5mgPSsdj1sPsLitKYlFSpLRQkodKxZRcTaytwGt6Axc7g3DJ8NmFMwcB6JHKjHYVHa2NlZuvYAn27j428alNo6rdrZl1heNcfwTKmcNjY2ObXqtMZpDW63hu2gX/bRtsWs3uzJVByKAUAoBUAUAoBQCgFSBQCgFAKAUAoBQCgFAKAUAoBQCoAoBQCgFAKkCgFAKAUAoBQCgFAKAUAoBQH/9k=");
        path.setAttribute("x", "104");
        path.setAttribute("y", "75");
        path.setAttribute("height", "115px");
        path.setAttribute("width", "137px");
        subG.appendChild(path);
        return subG;
    }

    public Element createTestTitleElement(Document document, Element subG, String content, String x, String y,
            String style) {
        Element text = document.createElement("text");
        text.setAttribute("x", "223.8");
        text.setAttribute("y", "781.76");
        text.setAttribute("id", "legendTitle");
        Element tspan = document.createElement("tspan");
        tspan.setAttribute("id", "optimumTitle");
        tspan.setAttribute("x", x);
        tspan.setAttribute("y", y);
        tspan.setAttribute("style", style);
        tspan.setTextContent(content);
        text.appendChild(tspan);
        subG.appendChild(text);
        return subG;
    }

    // For services
    public Element createServiceElement(Document document, Element subG, HashMap<String, String> serviceMap,
            HashMap<String, String> xCoordinatesMap, String y, String style) {
        List<String> sortedKeys = new ArrayList<String>(serviceMap.size());
        sortedKeys.addAll(serviceMap.keySet());
        Collections.sort(sortedKeys, Collections.reverseOrder());

        int xService = 115;
        int xCost = 1150;

        for (String key : sortedKeys) {
            Element text = document.createElement("text");
            text.setAttribute("x", "223.8");
            text.setAttribute("y", "781.76");
            text.setAttribute("id", "legendTitle");
            Element tspan = document.createElement("tspan");
            tspan.setAttribute("id", "optimumTitle");
            tspan.setAttribute("x", String.valueOf(xService));
            tspan.setAttribute("y", y);
            tspan.setAttribute("style", style);
            tspan.setTextContent((String) key);
            text.appendChild(tspan);
            subG.appendChild(text);

            Element text1 = document.createElement("text");
            text1.setAttribute("x", "223.8");
            text1.setAttribute("y", "781.76");
            text1.setAttribute("id", "legendTitle");
            Element tspan1 = document.createElement("tspan");
            tspan1.setAttribute("id", "optimumTitle");
            tspan1.setAttribute("x", String.valueOf(xCost));
            tspan1.setAttribute("y", y);
            tspan1.setAttribute("style", style);
            tspan1.setTextContent((String) serviceMap.get(key));
            text1.appendChild(tspan1);
            subG.appendChild(text1);

            int yAxis = Integer.parseInt(y);
            y = String.valueOf(yAxis + 30);
        }

        return subG;
    }

    //create End Component line
    public Element createEndComponent(Document document, Element subG, String x, String y, String cost,
            String exchangeRate, String currencyCode, String discount, String interest) {
        int respY = Integer.parseInt(y);
        int respX = Integer.parseInt(x);

        Element path = document.createElement("path");
        path.setAttribute("id", "horizontalLine");
        String dString = "m " + x + "," + y + " " + 1150 + ",0";

        path.setAttribute("d", dString);
        path.setAttribute("style",
                "fill:none;stroke:#0c0c0c;stroke-width:2.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

        Element path1 = document.createElement("path");
        path1.setAttribute("id", "horizontalLine");
        String d2String = "m " + (respX + 800) + "," + (respY + 100) + " " + 350 + ",0";
        path1.setAttribute("d", d2String);
        path1.setAttribute("style",
                "fill:none;stroke:#0c0c0c;stroke-width:2.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

        if (!discount.isEmpty() && Float.parseFloat(discount) > 0) {
            Element discountText = document.createElement("text");
            discountText.setAttribute("x", "223.8");
            discountText.setAttribute("y", "781.76");
            discountText.setAttribute("id", "legendTitle");
            Element tspan = document.createElement("tspan");
            tspan.setAttribute("id", "optimumTitle");
            tspan.setAttribute("x", "820");
            tspan.setAttribute("y", String.valueOf(respY + 60));
            tspan.setAttribute("style",
                    "font-size:18.44000006px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold");
            tspan.setTextContent("Discount");
            discountText.appendChild(tspan);
            subG.appendChild(discountText);

            Element discountTotalText = document.createElement("text");
            discountTotalText.setAttribute("x", "223.8");
            discountTotalText.setAttribute("y", "781.76");
            discountTotalText.setAttribute("id", "legendTitle");
            Element totalTspan = document.createElement("tspan");
            totalTspan.setAttribute("id", "optimumTitle");
            totalTspan.setAttribute("x", "1150");
            totalTspan.setAttribute("y", String.valueOf(respY + 60));
            totalTspan.setAttribute("style",
                    "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light");
            totalTspan.setTextContent(discount);
            discountTotalText.appendChild(totalTspan);
            subG.appendChild(discountTotalText);

            respY += 100;

            Element discountPath = document.createElement("path");
            discountPath.setAttribute("id", "horizontalLine");
            String discount2String = "m " + (respX + 800) + "," + (respY + 100) + " " + 350 + ",0";
            discountPath.setAttribute("d", discount2String);
            discountPath.setAttribute("style",
                    "fill:none;stroke:#0c0c0c;stroke-width:2.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            x = String.valueOf(1250);

            String dString1 = "M " + x + "," + y + " " + x + "," + (respY + 100) + "";
            Element discountVerticalPath = document.createElement("path");
            discountVerticalPath.setAttribute("id", "verticalLine");
            discountVerticalPath.setAttribute("d", dString1);
            discountVerticalPath.setAttribute("style",
                    "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            subG.appendChild(discountVerticalPath);

            //verticalDividerElement
            x = String.valueOf(900);

            String dString2 = "M " + x + "," + y + " " + x + "," + (respY + 100) + "";
            Element discountVerticalPath2 = document.createElement("path");
            discountVerticalPath2.setAttribute("id", "verticalLine");
            discountVerticalPath2.setAttribute("d", dString2);
            discountVerticalPath2.setAttribute("style",
                    "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            subG.appendChild(discountVerticalPath2);
            subG.appendChild(discountPath);
        }

        if (!interest.isEmpty() && Float.parseFloat(interest) > 0) {
            Element interestText = document.createElement("text");
            interestText.setAttribute("x", "223.8");
            interestText.setAttribute("y", "781.76");
            interestText.setAttribute("id", "legendTitle");
            Element tspan = document.createElement("tspan");
            tspan.setAttribute("id", "optimumTitle");
            tspan.setAttribute("x", "820");
            tspan.setAttribute("y", String.valueOf(respY + 60));
            tspan.setAttribute("style",
                    "font-size:18.44000006px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold");
            tspan.setTextContent("Interest");
            interestText.appendChild(tspan);
            subG.appendChild(interestText);

            Element interestTotalText = document.createElement("text");
            interestTotalText.setAttribute("x", "223.8");
            interestTotalText.setAttribute("y", "781.76");
            interestTotalText.setAttribute("id", "legendTitle");
            Element totalTspan = document.createElement("tspan");
            totalTspan.setAttribute("id", "optimumTitle");
            totalTspan.setAttribute("x", "1150");
            totalTspan.setAttribute("y", String.valueOf(respY + 60));
            totalTspan.setAttribute("style",
                    "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light");
            totalTspan.setTextContent(interest);
            interestTotalText.appendChild(totalTspan);
            subG.appendChild(interestTotalText);

            respY += 100;

            Element interestPath = document.createElement("path");
            interestPath.setAttribute("id", "horizontalLine");
            String interest2String = "m " + (respX + 800) + "," + (respY + 100) + " " + 350 + ",0";
            interestPath.setAttribute("d", interest2String);
            interestPath.setAttribute("style",
                    "fill:none;stroke:#0c0c0c;stroke-width:2.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            x = String.valueOf(1250);

            String dString1 = "M " + x + "," + y + " " + x + "," + (respY + 100) + "";
            Element interestVerticalPath = document.createElement("path");
            interestVerticalPath.setAttribute("id", "verticalLine");
            interestVerticalPath.setAttribute("d", dString1);
            interestVerticalPath.setAttribute("style",
                    "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            subG.appendChild(interestVerticalPath);

            //verticalDividerElement
            x = String.valueOf(900);

            String dString2 = "M " + x + "," + y + " " + x + "," + (respY + 100) + "";
            Element interestVerticalPath2 = document.createElement("path");
            interestVerticalPath2.setAttribute("id", "verticalLine");
            interestVerticalPath2.setAttribute("d", dString2);
            interestVerticalPath2.setAttribute("style",
                    "#fill:none;stroke:#0c0c0c;stroke-width:1.75;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-opacity:1;stroke-dasharray:none");

            subG.appendChild(interestVerticalPath2);
            subG.appendChild(interestPath);
        }

        Element text = document.createElement("text");
        text.setAttribute("x", "223.8");
        text.setAttribute("y", "781.76");
        text.setAttribute("id", "legendTitle");
        Element tspan = document.createElement("tspan");
        tspan.setAttribute("id", "optimumTitle");
        tspan.setAttribute("x", "850");
        tspan.setAttribute("y", String.valueOf(respY + 60));
        tspan.setAttribute("style",
                "font-size:18.44000006px;font-variant:normal;font-weight:bold;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light-Bold");
        tspan.setTextContent("Total");
        text.appendChild(tspan);
        subG.appendChild(text);

        Element totalText = document.createElement("text");
        totalText.setAttribute("x", "223.8");
        totalText.setAttribute("y", "781.76");
        totalText.setAttribute("id", "legendTitle");
        Element totalTspan = document.createElement("tspan");
        totalTspan.setAttribute("id", "optimumTitle");
        totalTspan.setAttribute("x", "1150");
        totalTspan.setAttribute("y", String.valueOf(respY + 60));
        totalTspan.setAttribute("style",
                "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light");
        totalTspan.setTextContent(cost);
        totalText.appendChild(totalTspan);
        subG.appendChild(totalText);

        Element convertedText = document.createElement("text");
        convertedText.setAttribute("x", "223.8");
        convertedText.setAttribute("y", "781.76");
        convertedText.setAttribute("id", "legendTitle");
        Element convertedTspan = document.createElement("tspan");
        convertedTspan.setAttribute("id", "optimumTitle");
        convertedTspan.setAttribute("x", "1107");
        convertedTspan.setAttribute("y", String.valueOf(respY + 80));
        convertedTspan.setAttribute("style",
                "font-size:18.44000006px;font-variant:normal;font-weight:normal;writing-mode:lr-tb;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;font-family:Myriad Pro Light;-inkscape-font-specification:Myriad Pro Light");
        convertedTspan.setTextContent("(" + String.valueOf(Float.parseFloat(exchangeRate) * Float.parseFloat(cost))
                + " " + currencyCode + ")");
        convertedText.appendChild(convertedTspan);
        subG.appendChild(convertedText);

        subG.appendChild(path);
        subG.appendChild(path1);

        return subG;
    }

    @Override
    public void generatePdf(MessageVO messageVO) {
        List<Concept> serviceArray = new ArrayList<Concept>();
        String svgContent = "", address = "", exchangeRate = "";
        NodeList serviceFields = (NodeList) XMLHelper.read(messageVO.getMessageDocument(),
                "message/FIAB_MT020000HT02/pertinentInformation", XPathConstants.NODESET);
        int serviceSize = serviceFields.getLength();
        String patientId = (String) XMLHelper.read(messageVO.getMessageDocument(), participantXpath,
                XPathConstants.STRING);

        String organizationId = (String) XMLHelper.read(messageVO.getMessageDocument(), organizationXpath,
                XPathConstants.STRING);
        String organizationVersion = cassandraConnector.getColumnValue("COCT_MT150000HT04_ST", organizationId,
                "VERSION", organizationId);
        String organizationMessage = cassandraConnector.getColumnValue("COCT_MT150000HT04_ST", organizationId,
                organizationVersion, organizationId);

        Document organizationDocument = XMLHelper.getXMLDocument(organizationMessage);

        String organizationName = (String) XMLHelper.read(organizationDocument,
                "message/COCT_MT150000HT04/name/prefix", XPathConstants.STRING);
        String organizationTelecom = (String) XMLHelper.read(organizationDocument,
                "message/COCT_MT150000HT04/telecom/value", XPathConstants.STRING);

        String houseNumber = (String) XMLHelper.read(organizationDocument,
                "message/COCT_MT150000HT04/addr/houseNumber", XPathConstants.STRING);
        String city = (String) XMLHelper.read(organizationDocument, "message/COCT_MT150000HT04/addr/city",
                XPathConstants.STRING);
        String state = (String) XMLHelper.read(organizationDocument, "message/COCT_MT150000HT04/addr/state",
                XPathConstants.STRING);
        String country = (String) XMLHelper.read(organizationDocument, "message/COCT_MT150000HT04/addr/country",
                XPathConstants.STRING);

        if (houseNumber != null && !houseNumber.isEmpty()) {
            address = address + houseNumber + ", ";
        }
        if (city != null && !city.isEmpty()) {
            address = address + city + ", ";
        }
        if (state != null && !state.isEmpty()) {
            address = address + state + ", ";
        }
        if (country != null && !country.isEmpty()) {
            address = address + country;
        }

        String licenseeId = getLicenseeId(organizationDocument);
        String licenseeVersion = cassandraConnector.getColumnValue("LICENSEE_ST", licenseeId, "VERSION",
                organizationId);
        String licenseeMessage = cassandraConnector.getColumnValue("LICENSEE_ST", licenseeId, licenseeVersion,
                organizationId);

        Document licenseeDocument = XMLHelper.getXMLDocument(licenseeMessage);
        String currenceyCode = (String) XMLHelper.read(licenseeDocument, "message/LICENSEE/currency/code/code",
                XPathConstants.STRING);

        Concept concept = new Concept();
        concept.setName(currenceyCode);
        try {
            Concept currenyConcept = conceptService.findByProperty("name", currenceyCode, Concept.class);
            List<ConceptAttribute> conceptAttributes = currenyConcept.getConceptAttributes();

            for (ConceptAttribute attribute : conceptAttributes) {
                if (attribute.getKey().equals("ExchangeRate")) {
                    exchangeRate = attribute.getValue();
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }

        File dir = new File(ATTACHMENT_DIR + "/" + patientId);
        dir.mkdirs();
        File file = new File(dir + "/" + messageVO.getId() + ".pdf");
        OutputStream out = null;

        for (int i = 1; i <= serviceSize; i++) {
            String serviceCode = (String) XMLHelper.read(messageVO.getMessageDocument(),
                    "message/FIAB_MT020000HT02/pertinentInformation[" + i + "]/observationOrder/code/code",
                    XPathConstants.STRING);
            serviceCode = serviceCode.replaceAll("\\n", "").trim();
            if (serviceCode != null && !serviceCode.isEmpty()) {
                serviceArray = conceptService.findAllConceptsByProperty("conceptClasses.name", serviceCode);
            }
            svgContent = createSvgDocument(messageVO.getMessageDocument(), serviceArray, organizationName,
                    organizationTelecom, address, exchangeRate, currenceyCode);
        }

        Transcoder transcoder = new PDFTranscoder();
        java.io.InputStream in = null;

        try {
            in = IOUtils.toInputStream(svgContent, "UTF-8");
            TranscoderInput input = new TranscoderInput(in);
            // Setup output
            out = new FileOutputStream(file);
            out = new BufferedOutputStream(out);
            TranscoderOutput output = new TranscoderOutput(out);
            transcoder.transcode(input, output);
            System.out.println("File created");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TranscoderException e) {
            e.printStackTrace();
        }

    }

    private String getLicenseeId(Document messageDocument) {
        String value = (String) XMLHelper.read(messageDocument, LicenseeXPath, XPathConstants.STRING);
        return value;
    }
}