net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSaxLoader.CFAccSaxLoaderISOLanguageHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSaxLoader.CFAccSaxLoaderISOLanguageHandler.java

Source

// Description: Java 7 XML SAX Element Handler for ISOLanguage

/*
 *   MSS Code Factory Accounting Business Application Model
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *   This program is available as free software under the GNU GPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *   
 *   Under the terms of the GPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *     
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *     
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *      This source code incorporates modified modules originally licensed
 *      under the Apache 2.0 license by MSS Code Factory including CFSecurity
 *      (net-sourceforge-msscodefactory-2.0-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and
 *      CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 1.11
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 1.11 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfacc.v2_0.CFAccSaxLoader;

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.cfacc.v2_0.CFAcc.*;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccObj.*;

/*
 *   CFAccSaxLoaderISOLanguageParse XML SAX Element Handler implementation
 *   for ISOLanguage.
 */
public class CFAccSaxLoaderISOLanguageHandler extends CFLibXmlCoreElementHandler {
    public CFAccSaxLoaderISOLanguageHandler(CFAccSaxLoader saxLoader) {
        super(saxLoader);
    }

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

            assert qName.equals("ISOLanguage");

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

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

            // Instantiate an edit buffer for the parsed information
            ICFAccISOLanguageEditObj editBuff = (ICFAccISOLanguageEditObj) schemaObj.getISOLanguageTableObj()
                    .newInstance().beginEdit();

            // 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("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");
            }

            // Save named attributes to context
            CFLibXmlCoreContext curContext = getParser().getCurContext();
            curContext.putNamedValue("Id", attrId);
            curContext.putNamedValue("ISOCode", attrISOCode);
            curContext.putNamedValue("BaseLanguageCode", attrBaseLanguageCode);
            curContext.putNamedValue("ISOCountryId", attrISOCountryId);

            // Convert string attributes to native Java types
            // and apply the converted attributes to the editBuff.

            short natId;
            natId = Short.parseShort(attrId);
            editBuff.getPKey().setRequiredId(natId);
            editBuff.getISOLanguageBuff().setRequiredId(natId);

            String natISOCode = attrISOCode;
            editBuff.setRequiredISOCode(natISOCode);

            String natBaseLanguageCode = attrBaseLanguageCode;
            editBuff.setRequiredBaseLanguageCode(natBaseLanguageCode);

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

            // Get the scope/container object

            CFLibXmlCoreContext parentContext = curContext.getPrevContext();
            Object scopeObj;
            if (parentContext != null) {
                scopeObj = parentContext.getNamedValue("Object");
            } else {
                scopeObj = null;
            }

            CFAccSaxLoader.LoaderBehaviourEnum loaderBehaviour = saxLoader.getISOLanguageLoaderBehaviour();
            ICFAccISOLanguageEditObj editISOLanguage = null;
            ICFAccISOLanguageObj origISOLanguage = schemaObj.getISOLanguageTableObj()
                    .readISOLanguageByCodeIdx(editBuff.getRequiredISOCode());
            editBuff.getPKey().setRequiredId(natId);
            editBuff.getISOLanguageBuff().setRequiredId(natId);
            if (origISOLanguage == null) {
                editISOLanguage = editBuff;
            } else {
                switch (loaderBehaviour) {
                case Insert:
                    break;
                case Update:
                    editISOLanguage = (ICFAccISOLanguageEditObj) origISOLanguage.beginEdit();
                    editISOLanguage.setRequiredISOCode(editBuff.getRequiredISOCode());
                    editISOLanguage.setRequiredBaseLanguageCode(editBuff.getRequiredBaseLanguageCode());
                    editISOLanguage.setOptionalISOCountryId(editBuff.getOptionalISOCountryId());
                    break;
                case Replace:
                    editISOLanguage = (ICFAccISOLanguageEditObj) origISOLanguage.beginEdit();
                    editISOLanguage.delete();
                    editISOLanguage.endEdit();
                    origISOLanguage = null;
                    editISOLanguage = editBuff;
                    break;
                }
            }

            if (editISOLanguage != null) {
                if (origISOLanguage != null) {
                    editISOLanguage.update();
                } else {
                    origISOLanguage = (ICFAccISOLanguageObj) editISOLanguage.create();
                }
                editISOLanguage.endEdit();
            }

            curContext.putNamedValue("Object", origISOLanguage);
        } 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 {
    }
}