Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ISOCurrency. /* * Code Factory Asterisk 11 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.cfasterisk.v2_4.CFAsteriskRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; 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.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; /* * CFAsteriskRamISOCurrencyTable in-memory RAM DbIO implementation * for ISOCurrency. */ public class CFAsteriskRamISOCurrencyTable implements ICFAsteriskISOCurrencyTable { private ICFAsteriskSchema schema; private Map<CFSecurityISOCurrencyPKey, CFSecurityISOCurrencyBuff> dictByPKey = new HashMap<CFSecurityISOCurrencyPKey, CFSecurityISOCurrencyBuff>(); private Map<CFSecurityISOCurrencyByCcyCdIdxKey, CFSecurityISOCurrencyBuff> dictByCcyCdIdx = new HashMap<CFSecurityISOCurrencyByCcyCdIdxKey, CFSecurityISOCurrencyBuff>(); private Map<CFSecurityISOCurrencyByCcyNmIdxKey, CFSecurityISOCurrencyBuff> dictByCcyNmIdx = new HashMap<CFSecurityISOCurrencyByCcyNmIdxKey, CFSecurityISOCurrencyBuff>(); public CFAsteriskRamISOCurrencyTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createISOCurrency(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyBuff Buff) { final String S_ProcName = "createISOCurrency"; CFSecurityISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); Buff.setRequiredId(pkey.getRequiredId()); CFSecurityISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey(); keyCcyCdIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFSecurityISOCurrencyByCcyNmIdxKey keyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey(); keyCcyNmIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByCcyCdIdx.containsKey(keyCcyCdIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ISOCurrencyCodeIdx", keyCcyCdIdx); } if (dictByCcyNmIdx.containsKey(keyCcyNmIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ISOCurrencyNameIdx", keyCcyNmIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByCcyCdIdx.put(keyCcyCdIdx, Buff); dictByCcyNmIdx.put(keyCcyNmIdx, Buff); } public CFSecurityISOCurrencyBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey PKey) { final String S_ProcName = "CFAsteriskRamISOCurrency.readDerived"; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityISOCurrencyBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey PKey) { final String S_ProcName = "CFAsteriskRamISOCurrency.readDerived"; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityISOCurrencyBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamISOCurrency.readAllDerived"; CFSecurityISOCurrencyBuff[] retList = new CFSecurityISOCurrencyBuff[dictByPKey.values().size()]; Iterator<CFSecurityISOCurrencyBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityISOCurrencyBuff readDerivedByCcyCdIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAsteriskRamISOCurrency.readDerivedByCcyCdIdx"; CFSecurityISOCurrencyByCcyCdIdxKey key = schema.getFactoryISOCurrency().newCcyCdIdxKey(); key.setRequiredISOCode(ISOCode); CFSecurityISOCurrencyBuff buff; if (dictByCcyCdIdx.containsKey(key)) { buff = dictByCcyCdIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff readDerivedByCcyNmIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFAsteriskRamISOCurrency.readDerivedByCcyNmIdx"; CFSecurityISOCurrencyByCcyNmIdxKey key = schema.getFactoryISOCurrency().newCcyNmIdxKey(); key.setRequiredName(Name); CFSecurityISOCurrencyBuff buff; if (dictByCcyNmIdx.containsKey(key)) { buff = dictByCcyNmIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, short Id) { final String S_ProcName = "CFAsteriskRamISOCurrency.readDerivedByIdIdx() "; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredId(Id); CFSecurityISOCurrencyBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey PKey) { final String S_ProcName = "CFAsteriskRamISOCurrency.readBuff"; CFSecurityISOCurrencyBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey PKey) { final String S_ProcName = "lockBuff"; CFSecurityISOCurrencyBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamISOCurrency.readAllBuff"; CFSecurityISOCurrencyBuff buff; ArrayList<CFSecurityISOCurrencyBuff> filteredList = new ArrayList<CFSecurityISOCurrencyBuff>(); CFSecurityISOCurrencyBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ISCY")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityISOCurrencyBuff[0])); } public CFSecurityISOCurrencyBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, short Id) { final String S_ProcName = "CFAsteriskRamISOCurrency.readBuffByIdIdx() "; CFSecurityISOCurrencyBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ISCY")) { return ((CFSecurityISOCurrencyBuff) buff); } else { return (null); } } public CFSecurityISOCurrencyBuff readBuffByCcyCdIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAsteriskRamISOCurrency.readBuffByCcyCdIdx() "; CFSecurityISOCurrencyBuff buff = readDerivedByCcyCdIdx(Authorization, ISOCode); if ((buff != null) && buff.getClassCode().equals("ISCY")) { return ((CFSecurityISOCurrencyBuff) buff); } else { return (null); } } public CFSecurityISOCurrencyBuff readBuffByCcyNmIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFAsteriskRamISOCurrency.readBuffByCcyNmIdx() "; CFSecurityISOCurrencyBuff buff = readDerivedByCcyNmIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("ISCY")) { return ((CFSecurityISOCurrencyBuff) buff); } else { return (null); } } public void updateISOCurrency(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyBuff Buff) { CFSecurityISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityISOCurrencyBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateISOCurrency", "Existing record not found", "ISOCurrency", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateISOCurrency", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityISOCurrencyByCcyCdIdxKey existingKeyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey(); existingKeyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFSecurityISOCurrencyByCcyCdIdxKey newKeyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey(); newKeyCcyCdIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFSecurityISOCurrencyByCcyNmIdxKey existingKeyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey(); existingKeyCcyNmIdx.setRequiredName(existing.getRequiredName()); CFSecurityISOCurrencyByCcyNmIdxKey newKeyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey(); newKeyCcyNmIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyCcyCdIdx.equals(newKeyCcyCdIdx)) { if (dictByCcyCdIdx.containsKey(newKeyCcyCdIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateISOCurrency", "ISOCurrencyCodeIdx", newKeyCcyCdIdx); } } if (!existingKeyCcyNmIdx.equals(newKeyCcyNmIdx)) { if (dictByCcyNmIdx.containsKey(newKeyCcyNmIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateISOCurrency", "ISOCurrencyNameIdx", newKeyCcyNmIdx); } } // Validate foreign keys // Update is valid Map<CFSecurityISOCurrencyPKey, CFSecurityISOCurrencyBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByCcyCdIdx.remove(existingKeyCcyCdIdx); dictByCcyCdIdx.put(newKeyCcyCdIdx, Buff); dictByCcyNmIdx.remove(existingKeyCcyNmIdx); dictByCcyNmIdx.put(newKeyCcyNmIdx, Buff); } public void deleteISOCurrency(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyBuff Buff) { final String S_ProcName = "CFAsteriskRamISOCurrencyTable.deleteISOCurrency() "; CFSecurityISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityISOCurrencyBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCurrency", pkey); } CFSecurityISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey(); keyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFSecurityISOCurrencyByCcyNmIdxKey keyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey(); keyCcyNmIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCurrencyIdx(Authorization, Buff.getRequiredId()); Map<CFSecurityISOCurrencyPKey, CFSecurityISOCurrencyBuff> subdict; dictByPKey.remove(pkey); dictByCcyCdIdx.remove(keyCcyCdIdx); dictByCcyNmIdx.remove(keyCcyNmIdx); } public void deleteISOCurrencyByIdIdx(CFSecurityAuthorization Authorization, short argId) { CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredId(argId); deleteISOCurrencyByIdIdx(Authorization, key); } public void deleteISOCurrencyByIdIdx(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey argKey) { CFSecurityISOCurrencyBuff cur; LinkedList<CFSecurityISOCurrencyBuff> matchSet = new LinkedList<CFSecurityISOCurrencyBuff>(); Iterator<CFSecurityISOCurrencyBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCurrencyBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCurrency(Authorization, cur); } } public void deleteISOCurrencyByCcyCdIdx(CFSecurityAuthorization Authorization, String argISOCode) { CFSecurityISOCurrencyByCcyCdIdxKey key = schema.getFactoryISOCurrency().newCcyCdIdxKey(); key.setRequiredISOCode(argISOCode); deleteISOCurrencyByCcyCdIdx(Authorization, key); } public void deleteISOCurrencyByCcyCdIdx(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyByCcyCdIdxKey argKey) { CFSecurityISOCurrencyBuff cur; LinkedList<CFSecurityISOCurrencyBuff> matchSet = new LinkedList<CFSecurityISOCurrencyBuff>(); Iterator<CFSecurityISOCurrencyBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCurrencyBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCurrency(Authorization, cur); } } public void deleteISOCurrencyByCcyNmIdx(CFSecurityAuthorization Authorization, String argName) { CFSecurityISOCurrencyByCcyNmIdxKey key = schema.getFactoryISOCurrency().newCcyNmIdxKey(); key.setRequiredName(argName); deleteISOCurrencyByCcyNmIdx(Authorization, key); } public void deleteISOCurrencyByCcyNmIdx(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyByCcyNmIdxKey argKey) { CFSecurityISOCurrencyBuff cur; LinkedList<CFSecurityISOCurrencyBuff> matchSet = new LinkedList<CFSecurityISOCurrencyBuff>(); Iterator<CFSecurityISOCurrencyBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCurrencyBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCurrency(Authorization, cur); } } public CFSecurityCursor openISOCurrencyCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamISOCurrencyCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCurrencyCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityISOCurrencyBuff nextISOCurrencyCursor(CFSecurityCursor Cursor) { CFAsteriskRamISOCurrencyCursor cursor = (CFAsteriskRamISOCurrencyCursor) Cursor; CFSecurityISOCurrencyBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityISOCurrencyBuff prevISOCurrencyCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityISOCurrencyBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCurrencyCursor(Cursor); } return (rec); } public CFSecurityISOCurrencyBuff firstISOCurrencyCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityISOCurrencyBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCurrencyCursor(Cursor); } return (rec); } public CFSecurityISOCurrencyBuff lastISOCurrencyCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOCurrencyCursor"); } public CFSecurityISOCurrencyBuff nthISOCurrencyCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityISOCurrencyBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCurrencyCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }