org.ojbc.bundles.adapters.staticmock.custody.CustodySearchResultBuilder.java Source code

Java tutorial

Introduction

Here is the source code for org.ojbc.bundles.adapters.staticmock.custody.CustodySearchResultBuilder.java

Source

/*
 * Unless explicitly acquired and licensed from Licensor under another license, the contents of
 * this file are subject to the Reciprocal Public License ("RPL") Version 1.5, or subsequent
 * versions as allowed by the RPL, and You may not copy or use this file in either source code
 * or executable form, except in compliance with the terms and conditions of the RPL
 *
 * All software distributed under the RPL is provided strictly on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND LICENSOR HEREBY DISCLAIMS ALL SUCH
 * WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific language
 * governing rights and limitations under the RPL.
 *
 * http://opensource.org/licenses/RPL-1.5
 *
 * Copyright 2012-2015 Open Justice Broker Consortium
 */
package org.ojbc.bundles.adapters.staticmock.custody;

import org.apache.commons.lang.StringUtils;
import org.ojbc.util.xml.OjbcNamespaceContext;
import org.ojbc.util.xml.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CustodySearchResultBuilder {

    public static Element buildCustodySearchResultElement(Document custodySearchResultsDoc,
            Document custodyQueryResult, String resultId) throws Exception {

        CustodyDetail custodyDetail = getCustodyDetail(custodyQueryResult);

        Element custodySearchResultElement = custodySearchResultsDoc
                .createElementNS(OjbcNamespaceContext.NS_CUSTODY_SEARCH_RESULTS, "CustodySearchResult");
        custodySearchResultElement.setPrefix(OjbcNamespaceContext.NS_PREFIX_CUSTODY_SEARCH_RESULTS);

        XmlUtils.addAttribute(custodySearchResultElement, OjbcNamespaceContext.NS_STRUCTURES_30, "id",
                "Result_" + resultId);

        String sDocCreationDate = custodyDetail.getDocCreationDate();

        if (StringUtils.isNotBlank(sDocCreationDate)) {

            Element docCreateDate = XmlUtils.appendElement(custodySearchResultElement,
                    OjbcNamespaceContext.NS_NC_30, "DocumentCreationDate");

            Element docCreateDateTime = XmlUtils.appendElement(docCreateDate, OjbcNamespaceContext.NS_NC_30,
                    "DateTime");

            sDocCreationDate = sDocCreationDate.trim();

            docCreateDateTime.setTextContent(sDocCreationDate);
        }

        String docId = custodyDetail.getDocId();

        String docIdCatDesc = custodyDetail.getDocumentIdCategoryDescription();

        boolean hasDocId = StringUtils.isNotBlank(docId);

        boolean hasDocIdCatDesc = StringUtils.isNotBlank(docIdCatDesc);

        if (hasDocId || hasDocIdCatDesc) {

            Element docIdElement = XmlUtils.appendElement(custodySearchResultElement, OjbcNamespaceContext.NS_NC_30,
                    "DocumentIdentification");

            if (hasDocId) {

                Element docIdValElement = XmlUtils.appendElement(docIdElement, OjbcNamespaceContext.NS_NC_30,
                        "IdentificationID");

                docId = docId.trim();

                docIdValElement.setTextContent(docId);
            }

            if (hasDocIdCatDesc) {

                Element docIdCatDescTxtElement = XmlUtils.appendElement(docIdElement, OjbcNamespaceContext.NS_NC_30,
                        "IdentificationCategoryDescriptionText");

                docIdCatDesc = docIdCatDesc.trim();

                docIdCatDescTxtElement.setTextContent(docIdCatDesc);
            }
        }

        Element personElement = XmlUtils.appendElement(custodySearchResultElement, OjbcNamespaceContext.NS_NC_30,
                "Person");

        XmlUtils.addAttribute(personElement, OjbcNamespaceContext.NS_STRUCTURES_30, "id", "Person_" + resultId);
        XmlUtils.addAttribute(personElement, OjbcNamespaceContext.NS_STRUCTURES_30, "metadata", "M" + resultId);

        String sPersonDob = custodyDetail.getPersonDob();

        if (StringUtils.isNotBlank(sPersonDob)) {

            Element personDob = XmlUtils.appendElement(personElement, OjbcNamespaceContext.NS_NC_30,
                    "PersonBirthDate");
            Element dobDateTimeElement = XmlUtils.appendElement(personDob, OjbcNamespaceContext.NS_NC_30,
                    "DateTime");

            sPersonDob = sPersonDob.trim();

            dobDateTimeElement.setTextContent(sPersonDob);
        }

        String sPersonGivenName = custodyDetail.getPersonGivenName();
        boolean hasPersonGivenName = StringUtils.isNotBlank(sPersonGivenName);

        String sPersonMiddleName = custodyDetail.getPersonMiddleName();
        boolean hasPersonMiddleName = StringUtils.isNotBlank(sPersonMiddleName);

        String sPersonSurName = custodyDetail.getPersonSurName();
        boolean hasPersonSurName = StringUtils.isNotBlank(sPersonSurName);

        if (hasPersonGivenName || hasPersonMiddleName || hasPersonSurName) {

            Element personNameElement = XmlUtils.appendElement(personElement, OjbcNamespaceContext.NS_NC_30,
                    "PersonName");

            if (hasPersonGivenName) {

                Element personGivenNameElement = XmlUtils.appendElement(personNameElement,
                        OjbcNamespaceContext.NS_NC_30, "PersonGivenName");

                sPersonGivenName = sPersonGivenName.trim();

                personGivenNameElement.setTextContent(sPersonGivenName);
            }

            if (hasPersonMiddleName) {

                Element middleNameElement = XmlUtils.appendElement(personNameElement, OjbcNamespaceContext.NS_NC_30,
                        "PersonMiddleName");

                sPersonMiddleName = sPersonMiddleName.trim();

                middleNameElement.setTextContent(sPersonMiddleName);
            }

            if (hasPersonSurName) {

                Element surNameElement = XmlUtils.appendElement(personNameElement, OjbcNamespaceContext.NS_NC_30,
                        "PersonSurName");

                sPersonSurName = sPersonSurName.trim();

                surNameElement.setTextContent(sPersonSurName);
            }

        }

        String sPersonSex = custodyDetail.getPersonSex();

        if (StringUtils.isNotBlank(sPersonSex)) {

            Element personSexTxt = XmlUtils.appendElement(personElement, OjbcNamespaceContext.NS_NC_30,
                    "PersonSexText");

            sPersonSex = sPersonSex.trim();

            personSexTxt.setTextContent(sPersonSex);
        }

        String sPersonSSNId = custodyDetail.getPersonSsn();

        if (StringUtils.isNotBlank(sPersonSSNId)) {

            Element personSSNIdentification = XmlUtils.appendElement(personElement, OjbcNamespaceContext.NS_NC_30,
                    "PersonSSNIdentification");

            Element ssnIdVal = XmlUtils.appendElement(personSSNIdentification, OjbcNamespaceContext.NS_NC_30,
                    "IdentificationID");

            sPersonSSNId = sPersonSSNId.trim();

            ssnIdVal.setTextContent(sPersonSSNId);
        }

        String personStateId = custodyDetail.getPersonStateId();

        if (StringUtils.isNotBlank(personStateId)) {

            Element personStateIdElement = XmlUtils.appendElement(personElement, OjbcNamespaceContext.NS_NC_30,
                    "PersonStateIdentification");

            Element personStateIdValElement = XmlUtils.appendElement(personStateIdElement,
                    OjbcNamespaceContext.NS_NC_30, "IdentificationID");

            personStateId = personStateId.trim();

            personStateIdValElement.setTextContent(personStateId);
        }

        Element booking = XmlUtils.appendElement(custodySearchResultElement,
                OjbcNamespaceContext.NS_CUSTODY_SEARCH_RES_EXT, "Booking");
        XmlUtils.addAttribute(booking, OjbcNamespaceContext.NS_STRUCTURES_30, "id", "Booking" + resultId);

        String sFingerprintDate = custodyDetail.getFingerprintDate();

        if (StringUtils.isNotBlank(sFingerprintDate)) {

            Element fingerprintDate = XmlUtils.appendElement(booking, OjbcNamespaceContext.NS_JXDM_51,
                    "FingerprintDate");

            Element fingerprintDateTime = XmlUtils.appendElement(fingerprintDate, OjbcNamespaceContext.NS_NC_30,
                    "DateTime");

            sFingerprintDate = sFingerprintDate.trim();

            fingerprintDateTime.setTextContent(sFingerprintDate);
        }

        String bookingSubjId = custodyDetail.getBookingSubjectId();

        if (StringUtils.isNotBlank(bookingSubjId)) {

            Element bookingSubjectElement = XmlUtils.appendElement(booking, OjbcNamespaceContext.NS_JXDM_51,
                    "BookingSubject");

            Element bookingSubjIdElement = XmlUtils.appendElement(bookingSubjectElement,
                    OjbcNamespaceContext.NS_JXDM_51, "SubjectIdentification");

            Element bookingSubjIdValElement = XmlUtils.appendElement(bookingSubjIdElement,
                    OjbcNamespaceContext.NS_NC_30, "IdentificationID");

            bookingSubjId = bookingSubjId.trim();

            bookingSubjIdValElement.setTextContent(bookingSubjId);
        }

        String sImgLoc = custodyDetail.getImageLocation();

        if (StringUtils.isNotBlank(sImgLoc)) {

            Element imageElement = XmlUtils.appendElement(booking, OjbcNamespaceContext.NS_NC_30, "Image");

            Element imgLocElement = XmlUtils.appendElement(imageElement, OjbcNamespaceContext.NS_NC_30,
                    "ImageLocation");

            Element imgLocDescTxtElement = XmlUtils.appendElement(imgLocElement, OjbcNamespaceContext.NS_NC_30,
                    "LocationDescriptionText");

            sImgLoc = sImgLoc.trim();

            imgLocDescTxtElement.setTextContent(sImgLoc);
        }

        Element chargeElement = XmlUtils.appendElement(custodySearchResultElement, OjbcNamespaceContext.NS_JXDM_51,
                "Charge");
        XmlUtils.addAttribute(chargeElement, OjbcNamespaceContext.NS_STRUCTURES_30, "id", "Charge_" + resultId);

        String sChargeCount = custodyDetail.getChargeCount();

        if (StringUtils.isNotBlank(sChargeCount)) {

            Element chargeCountQuantityElement = XmlUtils.appendElement(chargeElement,
                    OjbcNamespaceContext.NS_JXDM_51, "ChargeCountQuantity");

            sChargeCount = sChargeCount.trim();

            chargeCountQuantityElement.setTextContent(sChargeCount);
        }

        String sChargeDesc = custodyDetail.getChargeDescription();

        if (StringUtils.isNotBlank(sChargeDesc)) {

            Element chargeDescriptionTxtElement = XmlUtils.appendElement(chargeElement,
                    OjbcNamespaceContext.NS_JXDM_51, "ChargeDescriptionText");

            sChargeDesc = sChargeDesc.trim();

            chargeDescriptionTxtElement.setTextContent(sChargeDesc);
        }

        String sChargeStatuteCodeId = custodyDetail.getChargeStatuteCodeId();

        String sStatuteIdCatDescTxt = custodyDetail.getStatuteIdCategoryDescriptionTxt();

        boolean hasChargeStatuteCodeId = StringUtils.isNotBlank(sChargeStatuteCodeId);

        boolean hasStatuteIdCatDescTxt = StringUtils.isNotBlank(sStatuteIdCatDescTxt);

        if (hasChargeStatuteCodeId || hasStatuteIdCatDescTxt) {

            Element chargeStatuteElement = XmlUtils.appendElement(chargeElement, OjbcNamespaceContext.NS_JXDM_51,
                    "ChargeStatute");

            Element statuteCodeIdElement = XmlUtils.appendElement(chargeStatuteElement,
                    OjbcNamespaceContext.NS_JXDM_51, "StatuteCodeIdentification");

            if (hasChargeStatuteCodeId) {

                Element statCodeIdValElement = XmlUtils.appendElement(statuteCodeIdElement,
                        OjbcNamespaceContext.NS_NC_30, "IdentificationID");

                sChargeStatuteCodeId = sChargeStatuteCodeId.trim();

                statCodeIdValElement.setTextContent(sChargeStatuteCodeId);
            }

            if (hasStatuteIdCatDescTxt) {

                Element statCodeIdCatDescTxtElement = XmlUtils.appendElement(statuteCodeIdElement,
                        OjbcNamespaceContext.NS_NC_30, "IdentificationCategoryDescriptionText");

                sStatuteIdCatDescTxt = sStatuteIdCatDescTxt.trim();

                statCodeIdCatDescTxtElement.setTextContent(sStatuteIdCatDescTxt);
            }

        }

        Element personChargeAssociation = XmlUtils.appendElement(custodySearchResultElement,
                OjbcNamespaceContext.NS_JXDM_51, "PersonChargeAssociation");

        Element person = XmlUtils.appendElement(personChargeAssociation, OjbcNamespaceContext.NS_NC_30, "Person");
        XmlUtils.addAttribute(person, OjbcNamespaceContext.NS_STRUCTURES_30, "ref", "Person_" + resultId);

        Element personCharge = XmlUtils.appendElement(personChargeAssociation, OjbcNamespaceContext.NS_JXDM_51,
                "Charge");
        XmlUtils.addAttribute(personCharge, OjbcNamespaceContext.NS_STRUCTURES_30, "ref", "Charge_" + resultId);

        Element activityChargeAssociation = XmlUtils.appendElement(custodySearchResultElement,
                OjbcNamespaceContext.NS_JXDM_51, "ActivityChargeAssociation");

        Element activity = XmlUtils.appendElement(activityChargeAssociation, OjbcNamespaceContext.NS_NC_30,
                "Activity");
        XmlUtils.addAttribute(activity, OjbcNamespaceContext.NS_STRUCTURES_30, "ref", "Booking_" + resultId);

        Element activityCharge = XmlUtils.appendElement(activityChargeAssociation, OjbcNamespaceContext.NS_JXDM_51,
                "Charge");
        XmlUtils.addAttribute(activityCharge, OjbcNamespaceContext.NS_STRUCTURES_30, "ref", "Charge_" + resultId);

        String sSourceSystemNameText = custodyDetail.getSourceSystemNameText();

        if (StringUtils.isNotBlank(sSourceSystemNameText)) {

            Element sourceSystemNameTextElement = XmlUtils.appendElement(custodySearchResultElement,
                    OjbcNamespaceContext.NS_CUSTODY_SEARCH_RES_EXT, "SourceSystemNameText");

            sSourceSystemNameText = sSourceSystemNameText.trim();

            sourceSystemNameTextElement.setTextContent(sSourceSystemNameText);
        }

        String sSystemId = custodyDetail.getSystemId();

        String sSystemName = custodyDetail.getSystemName();

        boolean hasSystemId = StringUtils.isNotBlank(sSystemId);

        boolean hasSystemName = StringUtils.isNotBlank(sSystemName);

        if (hasSystemId || hasSystemName) {

            Element systemIdentification = XmlUtils.appendElement(custodySearchResultElement,
                    OjbcNamespaceContext.NS_INTEL_31, "SystemIdentification");

            if (hasSystemId) {

                Element systemIdVal = XmlUtils.appendElement(systemIdentification, OjbcNamespaceContext.NS_NC_30,
                        "IdentificationID");

                sSystemId = sSystemId.trim();

                systemIdVal.setTextContent(sSystemId);
            }

            if (hasSystemName) {

                Element systemName = XmlUtils.appendElement(systemIdentification, OjbcNamespaceContext.NS_NC_30,
                        "SystemName");

                sSystemName = sSystemName.trim();

                systemName.setTextContent(sSystemName);
            }
        }

        String sSearchResultCatTxt = custodyDetail.getSearchResultCategoryDescriptionText();

        if (StringUtils.isNotBlank(sSearchResultCatTxt)) {

            Element searchResultCategoryTxtElement = XmlUtils.appendElement(custodySearchResultElement,
                    OjbcNamespaceContext.NS_CUSTODY_SEARCH_RES_EXT, "SearchResultCategoryText");

            sSearchResultCatTxt = sSearchResultCatTxt.trim();

            searchResultCategoryTxtElement.setTextContent(sSearchResultCatTxt);
        }

        String sBranchName = custodyDetail.getOrganizationBranchName();

        String sOrgName = custodyDetail.getOrganizationName();

        boolean hasBranchName = StringUtils.isNotBlank(sBranchName);

        boolean hasOrgName = StringUtils.isNotBlank(sOrgName);

        if (hasBranchName || hasOrgName) {

            Element infoOwningOrgElement = XmlUtils.appendElement(custodySearchResultElement,
                    OjbcNamespaceContext.NS_CUSTODY_SEARCH_RES_EXT, "InformationOwningOrganization");

            if (hasBranchName) {

                Element organizationBranchNameElement = XmlUtils.appendElement(infoOwningOrgElement,
                        OjbcNamespaceContext.NS_NC_30, "OrganizationBranchName");

                sBranchName = sBranchName.trim();

                organizationBranchNameElement.setTextContent(sBranchName);
            }

            if (hasOrgName) {

                Element organizationName = XmlUtils.appendElement(infoOwningOrgElement,
                        OjbcNamespaceContext.NS_NC_30, "OrganizationName");

                sOrgName = sOrgName.trim();

                organizationName.setTextContent(sOrgName);
            }
        }

        String sLastUpdatedDate = custodyDetail.getLastUpdatedDate();

        if (StringUtils.isNotBlank(sLastUpdatedDate)) {

            Element metaData = XmlUtils.appendElement(custodySearchResultElement, OjbcNamespaceContext.NS_NC_30,
                    "Metadata");
            XmlUtils.addAttribute(metaData, OjbcNamespaceContext.NS_STRUCTURES_30, "id", "M" + resultId);

            Element lastUpdatedDate = XmlUtils.appendElement(metaData, OjbcNamespaceContext.NS_NC_30,
                    "LastUpdatedDate");

            Element lastUpdatedDateVal = XmlUtils.appendElement(lastUpdatedDate, OjbcNamespaceContext.NS_NC_30,
                    "Date");

            sLastUpdatedDate = sLastUpdatedDate.trim();

            lastUpdatedDateVal.setTextContent(sLastUpdatedDate);
        }

        return custodySearchResultElement;
    }

    static CustodyDetail getCustodyDetail(Document custodyDetailDoc) throws Exception {

        CustodyDetail rCustodyDetail = new CustodyDetail();

        String sDocCreationDate = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:DocumentCreationDate/nc30:DateTime");
        rCustodyDetail.setDocCreationDate(sDocCreationDate);

        String documentId = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "/cq-res-exch:CustodyQueryResults/nc30:DocumentIdentification/nc30:IdentificationID");
        rCustodyDetail.setDocId(documentId);

        String docIdCatDescription = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "/cq-res-exch:CustodyQueryResults/nc30:DocumentIdentification/nc30:IdentificationCategoryDescriptionText");
        rCustodyDetail.setDocumentIdCategoryDescription(docIdCatDescription);

        String systemId = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "/cq-res-exch:CustodyQueryResults/intel31:SystemIdentification/nc30:IdentificationID");
        rCustodyDetail.setSystemId(systemId);

        String systemName = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "/cq-res-exch:CustodyQueryResults/intel31:SystemIdentification/nc30:SystemName");
        rCustodyDetail.setSystemName(systemName);

        String dobVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//cq-res-ext:InmateCustody/nc30:PersonBirthDate/nc30:DateTime");
        rCustodyDetail.setPersonDob(dobVal);

        String personGivenName = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:PersonName/nc30:PersonGivenName");
        rCustodyDetail.setPersonGivenName(personGivenName);

        String middleNameVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:PersonName/nc30:PersonMiddleName");
        rCustodyDetail.setPersonMiddleName(middleNameVal);

        String surNameVal = XmlUtils.xPathStringSearch(custodyDetailDoc, "//nc30:PersonName/nc30:PersonSurName");
        rCustodyDetail.setPersonSurName(surNameVal);

        String personSexVal = XmlUtils.xPathStringSearch(custodyDetailDoc, "//nc30:PersonSexText");
        rCustodyDetail.setPersonSex(personSexVal);

        String ssnVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:PersonSSNIdentification/nc30:IdentificationID");
        rCustodyDetail.setPersonSsn(ssnVal);

        String personSidVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:PersonStateIdentification/nc30:IdentificationID");
        rCustodyDetail.setPersonStateId(personSidVal);

        String fingerprintDateTime = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//cq-res-ext:Booking/jxdm51:FingerprintDate/nc30:DateTime");
        rCustodyDetail.setFingerprintDate(fingerprintDateTime);

        String bookingSubjIdVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//jxdm51:BookingSubject/jxdm51:SubjectIdentification/nc30:IdentificationID");
        rCustodyDetail.setBookingSubjectId(bookingSubjIdVal);

        String imgLocDescTxt = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:Image/nc30:ImageLocation/nc30:LocationDescriptionText");
        rCustodyDetail.setImageLocation(imgLocDescTxt);

        String chargeCountQuantVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//jxdm51:Charge/jxdm51:ChargeCountQuantity");
        rCustodyDetail.setChargeCount(chargeCountQuantVal);

        String chargeDescTxt = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//jxdm51:Charge/jxdm51:ChargeDescriptionText");
        rCustodyDetail.setChargeDescription(chargeDescTxt);

        String chargeStatuteVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//jxdm51:ChargeStatute/jxdm51:StatuteCodeIdentification/nc30:IdentificationID");
        rCustodyDetail.setChargeStatuteCodeId(chargeStatuteVal);

        String idCatDescTxtVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//jxdm51:ChargeStatute/jxdm51:StatuteCodeIdentification/nc30:IdentificationCategoryDescriptionText");
        rCustodyDetail.setStatuteIdCategoryDescriptionTxt(idCatDescTxtVal);

        String sourceSystemNameText = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//cq-res-exch:CustodyQueryResults/cq-res-ext:SourceSystemNameText");
        rCustodyDetail.setSourceSystemNameText(sourceSystemNameText);

        String searchResultCatTxt = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//nc30:DocumentIdentification/nc30:IdentificationCategoryDescriptionText");
        rCustodyDetail.setSearchResultCategoryDescriptionText(searchResultCatTxt);

        String orgBranchName = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//cq-res-ext:InformationOwningOrganization/nc30:OrganizationBranchName");
        rCustodyDetail.setOrganizationBranchName(orgBranchName);

        String orgNameVal = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "//cq-res-ext:InformationOwningOrganization//nc30:OrganizationName");
        rCustodyDetail.setOrganizationName(orgNameVal);

        String lastUpdatedDate = XmlUtils.xPathStringSearch(custodyDetailDoc,
                "/cq-res-exch:CustodyQueryResults/nc30:Metadata/nc30:LastUpdatedDate/nc30:Date");

        rCustodyDetail.setLastUpdatedDate(lastUpdatedDate);

        return rCustodyDetail;
    }

}