Java tutorial
// Description: Java 7 XML Message SAX Response Record Element Handler for AccountEntry /* * 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.CFAccXMsgRqstHandler; 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.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccXMsg.*; /* * CFAccXMsgRqstAccountEntryUpdateHandler XML SAX Element Handler implementation * for AccountEntry. */ public class CFAccXMsgRqstAccountEntryUpdateHandler extends CFLibXmlCoreElementHandler { public CFAccXMsgRqstAccountEntryUpdateHandler(CFAccXMsgRqstHandler 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; // AccountEntry Attributes String attrTenantId = null; String attrAccountId = null; String attrEntryId = null; String attrSplitEntryId = null; String attrEntryStamp = null; String attrDescription = null; String attrTransferTenantId = null; String attrTransferAccountId = null; String attrDebitCurrencyId = null; String attrDebit = null; String attrCreditCurrencyId = null; String attrCredit = null; String attrConvertedCurrencyId = null; String attrConvertedAmount = null; String attrBalanceCurrencyId = null; String attrBalance = 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("RqstAccountEntryUpdate"); CFAccXMsgRqstHandler xmsgRqstHandler = (CFAccXMsgRqstHandler) getParser(); if (xmsgRqstHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFAccSchemaObj 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("TenantId")) { if (attrTenantId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrTenantId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("AccountId")) { if (attrAccountId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrAccountId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("EntryId")) { if (attrEntryId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrEntryId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("SplitEntryId")) { if (attrSplitEntryId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrSplitEntryId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("EntryStamp")) { if (attrEntryStamp != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrEntryStamp = 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("TransferTenantId")) { if (attrTransferTenantId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrTransferTenantId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("TransferAccountId")) { if (attrTransferAccountId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrTransferAccountId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("DebitCurrencyId")) { if (attrDebitCurrencyId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDebitCurrencyId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Debit")) { if (attrDebit != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDebit = attrs.getValue(idxAttr); } else if (attrLocalName.equals("CreditCurrencyId")) { if (attrCreditCurrencyId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrCreditCurrencyId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Credit")) { if (attrCredit != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrCredit = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ConvertedCurrencyId")) { if (attrConvertedCurrencyId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrConvertedCurrencyId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ConvertedAmount")) { if (attrConvertedAmount != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrConvertedAmount = attrs.getValue(idxAttr); } else if (attrLocalName.equals("BalanceCurrencyId")) { if (attrBalanceCurrencyId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrBalanceCurrencyId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Balance")) { if (attrBalance != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrBalance = 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 ((attrAccountId == null) || (attrAccountId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "AccountId"); } if ((attrEntryId == null) || (attrEntryId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "EntryId"); } if ((attrEntryStamp == null) || (attrEntryStamp.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "EntryStamp"); } if (attrDescription == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Description"); } if ((attrConvertedCurrencyId == null) || (attrConvertedCurrencyId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ConvertedCurrencyId"); } if ((attrConvertedAmount == null) || (attrConvertedAmount.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ConvertedAmount"); } if ((attrBalanceCurrencyId == null) || (attrBalanceCurrencyId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "BalanceCurrencyId"); } if ((attrBalance == null) || (attrBalance.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "Balance"); } 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 CFAccAccountEntryPKey pkey = schemaObj.getBackingStore().getFactoryAccountEntry().newPKey(); long natTenantId; natTenantId = Long.parseLong(attrTenantId); pkey.setRequiredTenantId(natTenantId); long natAccountId; natAccountId = Long.parseLong(attrAccountId); pkey.setRequiredAccountId(natAccountId); UUID natEntryId; natEntryId = UUID.fromString(attrEntryId); pkey.setRequiredEntryId(natEntryId); // Read the instance ICFAccAccountEntryObj origBuff = schemaObj.getAccountEntryTableObj().readAccountEntry(pkey); if (origBuff == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getAccountEntryTableObj().readAccountEntry()"); } else { // Edit the instance ICFAccAccountEntryEditObj editBuff = (ICFAccAccountEntryEditObj) origBuff.beginEdit(); CFAccAccountEntryBuff dataBuff = editBuff.getAccountEntryBuff(); // Convert string attributes to native Java types // and apply the converted attributes to the editBuff. UUID natSplitEntryId; if ((attrSplitEntryId == null) || (attrSplitEntryId.length() <= 0)) { natSplitEntryId = null; } else { natSplitEntryId = UUID.fromString(attrSplitEntryId); } dataBuff.setOptionalSplitEntryId(natSplitEntryId); Calendar natEntryStamp; try { natEntryStamp = CFLibXmlUtil.parseTimestamp(attrEntryStamp); } catch (RuntimeException e) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, 0, "EntryStamp", attrEntryStamp, e); } dataBuff.setRequiredEntryStamp(natEntryStamp); String natDescription = attrDescription; dataBuff.setRequiredDescription(natDescription); Long natTransferTenantId; if ((attrTransferTenantId == null) || (attrTransferTenantId.length() <= 0)) { natTransferTenantId = null; } else { natTransferTenantId = new Long(Long.parseLong(attrTransferTenantId)); } dataBuff.setOptionalTransferTenantId(natTransferTenantId); Long natTransferAccountId; if ((attrTransferAccountId == null) || (attrTransferAccountId.length() <= 0)) { natTransferAccountId = null; } else { natTransferAccountId = new Long(Long.parseLong(attrTransferAccountId)); } dataBuff.setOptionalTransferAccountId(natTransferAccountId); Short natDebitCurrencyId; if ((attrDebitCurrencyId == null) || (attrDebitCurrencyId.length() <= 0)) { natDebitCurrencyId = null; } else { natDebitCurrencyId = new Short(Short.parseShort(attrDebitCurrencyId)); } dataBuff.setOptionalDebitCurrencyId(natDebitCurrencyId); BigDecimal natDebit; if ((attrDebit == null) || (attrDebit.length() <= 0)) { natDebit = null; } else { natDebit = new BigDecimal(attrDebit); } dataBuff.setOptionalDebit(natDebit); Short natCreditCurrencyId; if ((attrCreditCurrencyId == null) || (attrCreditCurrencyId.length() <= 0)) { natCreditCurrencyId = null; } else { natCreditCurrencyId = new Short(Short.parseShort(attrCreditCurrencyId)); } dataBuff.setOptionalCreditCurrencyId(natCreditCurrencyId); BigDecimal natCredit; if ((attrCredit == null) || (attrCredit.length() <= 0)) { natCredit = null; } else { natCredit = new BigDecimal(attrCredit); } dataBuff.setOptionalCredit(natCredit); short natConvertedCurrencyId = Short.parseShort(attrConvertedCurrencyId); dataBuff.setRequiredConvertedCurrencyId(natConvertedCurrencyId); BigDecimal natConvertedAmount = new BigDecimal(attrConvertedAmount); dataBuff.setRequiredConvertedAmount(natConvertedAmount); short natBalanceCurrencyId = Short.parseShort(attrBalanceCurrencyId); dataBuff.setRequiredBalanceCurrencyId(natBalanceCurrencyId); BigDecimal natBalance = new BigDecimal(attrBalance); dataBuff.setRequiredBalance(natBalance); 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 = CFAccXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAccXMsgAccountEntryMessageFormatter.formatAccountEntryRspnUpdated("\n\t\t\t", origBuff.getAccountEntryBuff()) + "\n" + CFAccXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); ((CFAccXMsgRqstHandler) getParser()).appendResponse(response); } } catch (RuntimeException e) { String response = CFAccXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAccXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFAccXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFAccXMsgRqstHandler xmsgRqstHandler = ((CFAccXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (Error e) { String response = CFAccXMsgSchemaMessageFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAccXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + CFAccXMsgSchemaMessageFormatter.formatRspnXmlPostamble(); CFAccXMsgRqstHandler xmsgRqstHandler = ((CFAccXMsgRqstHandler) getParser()); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } } public void endElement(String uri, String localName, String qName) throws SAXException { } }