net.sourceforge.msscodefactory.cfcrm.v2_0.CFCrmXMsgRspnHandler.CFCrmXMsgRspnDomainBaseRecHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfcrm.v2_0.CFCrmXMsgRspnHandler.CFCrmXMsgRspnDomainBaseRecHandler.java

Source

// Description: Java 7 XML Message SAX Response Record Element Handler for DomainBase

/*
 *   CF Customer Relations Management model
 *
 *   Copyright (c) 2010-2014 Mark Sobkow
 *   
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *   
 *          http://www.apache.org/licenses/LICENSE-2.0
 *   
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfcrm.v2_0.CFCrmXMsgRspnHandler;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import org.xml.sax.*;
import net.sourceforge.msscodefactory.cfcrm.v2_0.CFCrm.*;
import net.sourceforge.msscodefactory.cfcrm.v2_0.CFCrmObj.*;

/*
 *   CFCrmXMsgRspnDomainBaseRecHandler XML SAX Element Handler implementation
 *   for DomainBase.
 */
public class CFCrmXMsgRspnDomainBaseRecHandler extends CFLibXmlCoreElementHandler {
    public CFCrmXMsgRspnDomainBaseRecHandler(CFCrmXMsgRspnHandler xmsgRspnHandler) {
        super(xmsgRspnHandler);
    }

    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        try {
            // Common XML Attributes
            String attrId = null;
            String attrRevision = null;
            // DomainBase Attributes
            String attrTenantId = null;
            String attrDescription = null;
            String attrCreatedAt = null;
            String attrCreatedBy = null;
            String attrUpdatedAt = null;
            String attrUpdatedBy = null;
            // Attribute Extraction
            String attrLocalName;
            int numAttrs;
            int idxAttr;
            final String S_ProcName = "startElement";
            final String S_LocalName = "LocalName";

            assert qName.equals("DomainBase");

            CFCrmXMsgRspnHandler xmsgRspnHandler = (CFCrmXMsgRspnHandler) getParser();
            if (xmsgRspnHandler == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "getParser()");
            }

            ICFCrmSchemaObj schemaObj = xmsgRspnHandler.getSchemaObj();
            if (schemaObj == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "getParser().getSchemaObj()");
            }

            // Extract Attributes
            numAttrs = attrs.getLength();
            for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) {
                attrLocalName = attrs.getLocalName(idxAttr);
                if (attrLocalName.equals("Id")) {
                    if (attrId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrId = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("Revision")) {
                    if (attrRevision != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrRevision = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("CreatedAt")) {
                    if (attrCreatedAt != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrCreatedAt = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("CreatedBy")) {
                    if (attrCreatedBy != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrCreatedBy = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("UpdatedAt")) {
                    if (attrUpdatedAt != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrUpdatedAt = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("UpdatedBy")) {
                    if (attrUpdatedBy != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrUpdatedBy = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("TenantId")) {
                    if (attrTenantId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrTenantId = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("Description")) {
                    if (attrDescription != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrDescription = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("schemaLocation")) {
                    // ignored
                } else {
                    throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(),
                            S_ProcName, getParser().getLocationInfo(), attrLocalName);
                }
            }

            // Ensure that required attributes have values
            if ((attrTenantId == null) || (attrTenantId.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "TenantId");
            }
            if ((attrId == null) || (attrId.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Id");
            }

            // Save named attributes to context
            CFLibXmlCoreContext curContext = xmsgRspnHandler.getCurContext();

            // Convert string attributes to native Java types

            long natTenantId = Long.parseLong(attrTenantId);

            long natId = Long.parseLong(attrId);

            String natDescription = attrDescription;

            int natRevision = Integer.parseInt(attrRevision);
            UUID createdBy = null;
            if (attrCreatedBy != null) {
                createdBy = UUID.fromString(attrCreatedBy);
            }
            Calendar createdAt = null;
            if (attrCreatedAt != null) {
                createdAt = CFLibXmlUtil.parseTimestamp(attrCreatedAt);
            }
            UUID updatedBy = null;
            if (attrUpdatedBy != null) {
                updatedBy = UUID.fromString(attrUpdatedBy);
            }
            Calendar updatedAt = null;
            if (attrUpdatedAt != null) {
                updatedAt = CFLibXmlUtil.parseTimestamp(attrUpdatedAt);
            }
            // Get the parent context
            CFLibXmlCoreContext parentContext = curContext.getPrevContext();
            // Instantiate a buffer for the parsed information
            ICFCrmDomainBaseObj obj = schemaObj.getDomainBaseTableObj().newInstance();
            CFCrmDomainBaseBuff dataBuff = obj.getDomainBaseBuff();
            dataBuff.setRequiredTenantId(natTenantId);
            dataBuff.setRequiredId(natId);
            dataBuff.setOptionalDescription(natDescription);
            dataBuff.setRequiredRevision(natRevision);
            if (createdBy != null) {
                dataBuff.setCreatedByUserId(createdBy);
            }
            if (createdAt != null) {
                dataBuff.setCreatedAt(createdAt);
            }
            if (updatedBy != null) {
                dataBuff.setUpdatedByUserId(updatedBy);
            }
            if (updatedAt != null) {
                dataBuff.setUpdatedAt(updatedAt);
            }
            obj.copyBuffToPKey();
            SortedMap<CFCrmDomainBasePKey, ICFCrmDomainBaseObj> sortedMap = (SortedMap<CFCrmDomainBasePKey, ICFCrmDomainBaseObj>) xmsgRspnHandler
                    .getSortedMapOfObjects();
            ICFCrmDomainBaseObj realized = (ICFCrmDomainBaseObj) obj.realize();
            xmsgRspnHandler.setLastObjectProcessed(realized);
            if (sortedMap != null) {
                sortedMap.put(realized.getPKey(), realized);
            }
        } catch (RuntimeException e) {
            throw new RuntimeException("Near " + getParser().getLocationInfo() + ": Caught and rethrew "
                    + e.getClass().getName() + " - " + e.getMessage(), e);
        } catch (Error e) {
            throw new Error("Near " + getParser().getLocationInfo() + ": Caught and rethrew "
                    + e.getClass().getName() + " - " + e.getMessage(), e);
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
    }
}