net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmXMsgRqstHandler.CFCrmXMsgRqstURLProtocolDeleteHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmXMsgRqstHandler.CFCrmXMsgRqstURLProtocolDeleteHandler.java

Source

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

/*
 *   CF Customer Relations Management model
 *
 *   Copyright (c) 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_1.CFCrmXMsgRqstHandler;

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

/*
 *   CFCrmXMsgRqstURLProtocolDeleteHandler XML SAX Element Handler implementation
 *   for URLProtocol.
 */
public class CFCrmXMsgRqstURLProtocolDeleteHandler extends CFLibXmlCoreElementHandler {
    public CFCrmXMsgRqstURLProtocolDeleteHandler(CFCrmXMsgRqstHandler xmsgRqstHandler) {
        super(xmsgRqstHandler);
    }

    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        try {
            // Common XML Attributes
            String attrId = null;
            String attrRevision = null;
            // Primary Key Attributes for Constant Enum support
            String attrURLProtocolId = null;
            // Attribute Extraction
            String attrLocalName;
            int numAttrs;
            int idxAttr;
            final String S_ProcName = "startElement";
            final String S_LocalName = "LocalName";

            assert qName.equals("RqstURLProtocolDelete");

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

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

            // Instantiate a PKey buffer for the parsed information
            CFCrmURLProtocolPKey pkey = schemaObj.getBackingStore().getFactoryURLProtocol().newPKey();

            // 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("URLProtocolId")) {
                    if (attrURLProtocolId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrURLProtocolId = 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 ((attrURLProtocolId == null) || (attrURLProtocolId.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "URLProtocolId");
            }
            if ((attrRevision == null) || (attrRevision.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "Revision");
            }

            // Get current context
            CFLibXmlCoreContext curContext = getParser().getCurContext();
            // Convert string attributes to native Java types
            // and apply the converted attributes to the editBuff.

            short natURLProtocolId;
            natURLProtocolId = Short.parseShort(attrURLProtocolId);
            pkey.setRequiredURLProtocolId(natURLProtocolId);

            pkey.setRequiredURLProtocolId(natURLProtocolId);
            int natRevision = Integer.parseInt(attrRevision);
            // Delete the object
            ICFCrmURLProtocolObj read = schemaObj.getURLProtocolTableObj().readURLProtocol(pkey);
            if (read != null) {
                if (read.getURLProtocolBuff().getRequiredRevision() > natRevision) {
                    throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                            "Collision detected preparing to delete URLProtocol");
                } else {
                    ICFCrmURLProtocolEditObj editBuff = (ICFCrmURLProtocolEditObj) read.beginEdit();
                    if (editBuff != null) {
                        editBuff.delete();
                        editBuff.endEdit();
                        String response = CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                                + CFCrmXMsgURLProtocolMessageFormatter.formatURLProtocolRspnDeleted() + "\n"
                                + CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPostamble();
                        ((CFCrmXMsgRqstHandler) getParser()).appendResponse(response);
                    } else {
                        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                                "read.beginEdit()");
                    }
                }
            } else {
                String response = CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                        + CFCrmXMsgSchemaMessageFormatter.formatRspnNoDataFound() + "\n"
                        + CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPostamble();
                ((CFCrmXMsgRqstHandler) getParser()).appendResponse(response);
            }
        } catch (RuntimeException e) {
            String response = CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFCrmXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPostamble();
            CFCrmXMsgRqstHandler xmsgRqstHandler = ((CFCrmXMsgRqstHandler) getParser());
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        } catch (Error e) {
            String response = CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFCrmXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + CFCrmXMsgSchemaMessageFormatter.formatRspnXmlPostamble();
            CFCrmXMsgRqstHandler xmsgRqstHandler = ((CFCrmXMsgRqstHandler) getParser());
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        }
    }

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