net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchXMsgRqstHandler.CFFreeSwitchXMsgRqstISOLanguageUpdateHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchXMsgRqstHandler.CFFreeSwitchXMsgRqstISOLanguageUpdateHandler.java

Source

// Description: Java 8 XML Message SAX Response Record Element Handler for ISOLanguage

/*
 *   CF FreeSwitch Configuration Model
 *
 *   Copyright (c) 2014-2015 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.
 *   
 */

package net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchXMsgRqstHandler;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import org.xml.sax.*;
import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*;
import net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitch.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*;
import net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchObj.*;
import net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchXMsg.*;

/*
 *   CFFreeSwitchXMsgRqstISOLanguageUpdateHandler XML SAX Element Handler implementation
 *   for ISOLanguage.
 */
public class CFFreeSwitchXMsgRqstISOLanguageUpdateHandler extends CFLibXmlCoreElementHandler {
    public CFFreeSwitchXMsgRqstISOLanguageUpdateHandler(CFFreeSwitchXMsgRqstHandler xmsgRqstHandler) {
        super(xmsgRqstHandler);
    }

    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        CFFreeSwitchXMsgSchemaMessageFormatter schemaFormatter = null;
        try {
            // Common XML Attributes
            String attrId = null;
            String attrRevision = null;
            // ISOLanguage Attributes
            String attrISOCode = null;
            String attrBaseLanguageCode = null;
            String attrISOCountryId = 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("RqstISOLanguageUpdate");

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

            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();

            ICFFreeSwitchSchemaObj schemaObj = xmsgRqstHandler.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("ISOCode")) {
                    if (attrISOCode != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrISOCode = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("BaseLanguageCode")) {
                    if (attrBaseLanguageCode != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrBaseLanguageCode = attrs.getValue(idxAttr);
                } else if (attrLocalName.equals("ISOCountryId")) {
                    if (attrISOCountryId != null) {
                        throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                                S_ProcName, S_LocalName, attrLocalName);
                    }
                    attrISOCountryId = 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 ((attrId == null) || (attrId.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Id");
            }
            if (attrISOCode == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "ISOCode");
            }
            if (attrBaseLanguageCode == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "BaseLanguageCode");
            }
            if ((attrRevision == null) || (attrRevision.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "Revision");
            }
            if ((attrCreatedAt == null) || (attrCreatedAt.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "CreatedAt");
            }
            if ((attrCreatedBy == null) || (attrCreatedBy.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "CreatedBy");
            }
            if ((attrUpdatedAt == null) || (attrUpdatedAt.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "UpdatedAt");
            }
            if ((attrUpdatedBy == null) || (attrUpdatedBy.length() <= 0)) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "UpdatedBy");
            }

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

            // Instantiate a PKey buffer for the parsed information
            CFSecurityISOLanguagePKey pkey = ((ICFFreeSwitchSchema) schemaObj.getBackingStore())
                    .getFactoryISOLanguage().newPKey();

            short natId;
            natId = Short.parseShort(attrId);
            pkey.setRequiredId(natId);
            // Read the instance
            ICFFreeSwitchISOLanguageObj origBuff = ((ICFFreeSwitchISOLanguageObj) schemaObj.getISOLanguageTableObj()
                    .readISOLanguage(pkey));
            if (origBuff == null) {
                throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                        "getISOLanguageTableObj().readISOLanguage()");
            } else {
                // Edit the instance
                ICFFreeSwitchISOLanguageEditObj editBuff = (ICFFreeSwitchISOLanguageEditObj) origBuff.beginEdit();
                CFSecurityISOLanguageBuff dataBuff = editBuff.getISOLanguageBuff();
                // Convert string attributes to native Java types
                // and apply the converted attributes to the editBuff.
                String natISOCode = attrISOCode;

                dataBuff.setRequiredISOCode(natISOCode);

                String natBaseLanguageCode = attrBaseLanguageCode;

                dataBuff.setRequiredBaseLanguageCode(natBaseLanguageCode);

                Short natISOCountryId;
                if ((attrISOCountryId == null) || (attrISOCountryId.length() <= 0)) {
                    natISOCountryId = null;
                } else {
                    natISOCountryId = new Short(Short.parseShort(attrISOCountryId));
                }

                dataBuff.setOptionalISOCountryId(natISOCountryId);

                int natRevision = Integer.parseInt(attrRevision);
                dataBuff.setRequiredRevision(natRevision);
                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);
                }
                if (createdBy != null) {
                    dataBuff.setCreatedByUserId(createdBy);
                }
                if (createdAt != null) {
                    dataBuff.setCreatedAt(createdAt);
                }
                if (updatedBy != null) {
                    dataBuff.setUpdatedByUserId(updatedBy);
                }
                if (updatedAt != null) {
                    dataBuff.setUpdatedAt(updatedAt);
                }
                //   Attempt the update
                editBuff.update();
                editBuff.endEdit();
                String response = schemaFormatter.formatRspnXmlPreamble()
                        + "\n" + "\t" + CFFreeSwitchXMsgISOLanguageMessageFormatter
                                .formatISOLanguageRspnUpdated("\n\t\t\t", origBuff.getISOLanguageBuff())
                        + "\n" + schemaFormatter.formatRspnXmlPostamble();
                ((CFFreeSwitchXMsgRqstHandler) getParser()).appendResponse(response);
            }
        } catch (RuntimeException e) {
            CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser());
            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();
            String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + schemaFormatter.formatRspnXmlPostamble();
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        } catch (Error e) {
            CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser());
            schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter();
            String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t"
                    + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n"
                    + schemaFormatter.formatRspnXmlPostamble();
            xmsgRqstHandler.resetResponse();
            xmsgRqstHandler.appendResponse(response);
            xmsgRqstHandler.setCaughtException(true);
        }
    }

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