Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ISOCurrency. /* * CFBam * * Copyright (c) 2014-2016 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.cfbam.v2_7.CFBamRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_7.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternet.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBam.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternetObj.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; /* * CFBamRamISOCurrencyTable in-memory RAM DbIO implementation * for ISOCurrency. */ public class CFBamRamISOCurrencyTable implements ICFBamISOCurrencyTable { private ICFBamSchema 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 CFBamRamISOCurrencyTable(ICFBamSchema argSchema) { schema = argSchema; } public void createISOCurrency(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyBuff Buff) { final String S_ProcName = "createISOCurrency"; CFSecurityISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey(); pkey.setRequiredISOCurrencyId(schema.nextISOCurrencyIdGen()); Buff.setRequiredISOCurrencyId(pkey.getRequiredISOCurrencyId()); 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 = "CFBamRamISOCurrency.readDerived"; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredISOCurrencyId(PKey.getRequiredISOCurrencyId()); 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 = "CFBamRamISOCurrency.readDerived"; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredISOCurrencyId(PKey.getRequiredISOCurrencyId()); CFSecurityISOCurrencyBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCurrencyBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamISOCurrency.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 = "CFBamRamISOCurrency.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 = "CFBamRamISOCurrency.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 ISOCurrencyId) { final String S_ProcName = "CFBamRamISOCurrency.readDerivedByIdIdx() "; CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredISOCurrencyId(ISOCurrencyId); 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 = "CFBamRamISOCurrency.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 = "CFBamRamISOCurrency.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 ISOCurrencyId) { final String S_ProcName = "CFBamRamISOCurrency.readBuffByIdIdx() "; CFSecurityISOCurrencyBuff buff = readDerivedByIdIdx(Authorization, ISOCurrencyId); if ((buff != null) && buff.getClassCode().equals("ISCY")) { return ((CFSecurityISOCurrencyBuff) buff); } else { return (null); } } public CFSecurityISOCurrencyBuff readBuffByCcyCdIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFBamRamISOCurrency.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 = "CFBamRamISOCurrency.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.setRequiredISOCurrencyId(Buff.getRequiredISOCurrencyId()); 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 = "CFBamRamISOCurrencyTable.deleteISOCurrency() "; String classCode; CFSecurityISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey(); pkey.setRequiredISOCurrencyId(Buff.getRequiredISOCurrencyId()); CFSecurityISOCurrencyBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCurrency", pkey); } // Short circuit self-referential code to prevent stack overflows Object arrCheckISOCurrencyCountries[] = schema.getTableISOCountryCurrency() .readDerivedByCurrencyIdx(Authorization, existing.getRequiredISOCurrencyId()); if (arrCheckISOCurrencyCountries.length > 0) { schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCurrencyIdx(Authorization, existing.getRequiredISOCurrencyId()); } 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 Map<CFSecurityISOCurrencyPKey, CFSecurityISOCurrencyBuff> subdict; dictByPKey.remove(pkey); dictByCcyCdIdx.remove(keyCcyCdIdx); dictByCcyNmIdx.remove(keyCcyNmIdx); } public void deleteISOCurrencyByIdIdx(CFSecurityAuthorization Authorization, short argISOCurrencyId) { CFSecurityISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey(); key.setRequiredISOCurrencyId(argISOCurrencyId); deleteISOCurrencyByIdIdx(Authorization, key); } public void deleteISOCurrencyByIdIdx(CFSecurityAuthorization Authorization, CFSecurityISOCurrencyPKey argKey) { boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } 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(); cur = schema.getTableISOCurrency().readDerivedByIdIdx(Authorization, cur.getRequiredISOCurrencyId()); 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; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } 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(); cur = schema.getTableISOCurrency().readDerivedByIdIdx(Authorization, cur.getRequiredISOCurrencyId()); 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; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } 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(); cur = schema.getTableISOCurrency().readDerivedByIdIdx(Authorization, cur.getRequiredISOCurrencyId()); deleteISOCurrency(Authorization, cur); } } public CFSecurityCursor openISOCurrencyCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamISOCurrencyCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCurrencyCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityISOCurrencyBuff nextISOCurrencyCursor(CFSecurityCursor Cursor) { CFBamRamISOCurrencyCursor cursor = (CFBamRamISOCurrencyCursor) 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() { } }