Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ISOLanguage. /* * 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.*; /* * CFBamRamISOLanguageTable in-memory RAM DbIO implementation * for ISOLanguage. */ public class CFBamRamISOLanguageTable implements ICFBamISOLanguageTable { private ICFBamSchema schema; private Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> dictByPKey = new HashMap<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>(); private Map<CFSecurityISOLanguageByCode3IdxKey, CFSecurityISOLanguageBuff> dictByCode3Idx = new HashMap<CFSecurityISOLanguageByCode3IdxKey, CFSecurityISOLanguageBuff>(); private Map<CFSecurityISOLanguageByCode2IdxKey, Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>> dictByCode2Idx = new HashMap<CFSecurityISOLanguageByCode2IdxKey, Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>>(); public CFBamRamISOLanguageTable(ICFBamSchema argSchema) { schema = argSchema; } public void createISOLanguage(CFSecurityAuthorization Authorization, CFSecurityISOLanguageBuff Buff) { final String S_ProcName = "createISOLanguage"; CFSecurityISOLanguagePKey pkey = schema.getFactoryISOLanguage().newPKey(); pkey.setRequiredISOLanguageId(schema.nextISOLanguageIdGen()); Buff.setRequiredISOLanguageId(pkey.getRequiredISOLanguageId()); CFSecurityISOLanguageByCode3IdxKey keyCode3Idx = schema.getFactoryISOLanguage().newCode3IdxKey(); keyCode3Idx.setRequiredISO6392Code(Buff.getRequiredISO6392Code()); CFSecurityISOLanguageByCode2IdxKey keyCode2Idx = schema.getFactoryISOLanguage().newCode2IdxKey(); keyCode2Idx.setOptionalISO6391Code(Buff.getOptionalISO6391Code()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByCode3Idx.containsKey(keyCode3Idx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ISOLanguage6392Idx", keyCode3Idx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByCode3Idx.put(keyCode3Idx, Buff); Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdictCode2Idx; if (dictByCode2Idx.containsKey(keyCode2Idx)) { subdictCode2Idx = dictByCode2Idx.get(keyCode2Idx); } else { subdictCode2Idx = new HashMap<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>(); dictByCode2Idx.put(keyCode2Idx, subdictCode2Idx); } subdictCode2Idx.put(pkey, Buff); } public CFSecurityISOLanguageBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityISOLanguagePKey PKey) { final String S_ProcName = "CFBamRamISOLanguage.readDerived"; CFSecurityISOLanguagePKey key = schema.getFactoryISOLanguage().newPKey(); key.setRequiredISOLanguageId(PKey.getRequiredISOLanguageId()); CFSecurityISOLanguageBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOLanguageBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityISOLanguagePKey PKey) { final String S_ProcName = "CFBamRamISOLanguage.readDerived"; CFSecurityISOLanguagePKey key = schema.getFactoryISOLanguage().newPKey(); key.setRequiredISOLanguageId(PKey.getRequiredISOLanguageId()); CFSecurityISOLanguageBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOLanguageBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamISOLanguage.readAllDerived"; CFSecurityISOLanguageBuff[] retList = new CFSecurityISOLanguageBuff[dictByPKey.values().size()]; Iterator<CFSecurityISOLanguageBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityISOLanguageBuff readDerivedByCode3Idx(CFSecurityAuthorization Authorization, String ISO6392Code) { final String S_ProcName = "CFBamRamISOLanguage.readDerivedByCode3Idx"; CFSecurityISOLanguageByCode3IdxKey key = schema.getFactoryISOLanguage().newCode3IdxKey(); key.setRequiredISO6392Code(ISO6392Code); CFSecurityISOLanguageBuff buff; if (dictByCode3Idx.containsKey(key)) { buff = dictByCode3Idx.get(key); } else { buff = null; } return (buff); } public CFSecurityISOLanguageBuff[] readDerivedByCode2Idx(CFSecurityAuthorization Authorization, String ISO6391Code) { final String S_ProcName = "CFBamRamISOLanguage.readDerivedByCode2Idx"; CFSecurityISOLanguageByCode2IdxKey key = schema.getFactoryISOLanguage().newCode2IdxKey(); key.setOptionalISO6391Code(ISO6391Code); CFSecurityISOLanguageBuff[] recArray; if (dictByCode2Idx.containsKey(key)) { Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdictCode2Idx = dictByCode2Idx.get(key); recArray = new CFSecurityISOLanguageBuff[subdictCode2Idx.size()]; Iterator<CFSecurityISOLanguageBuff> iter = subdictCode2Idx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdictCode2Idx = new HashMap<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>(); dictByCode2Idx.put(key, subdictCode2Idx); recArray = new CFSecurityISOLanguageBuff[0]; } return (recArray); } public CFSecurityISOLanguageBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, short ISOLanguageId) { final String S_ProcName = "CFBamRamISOLanguage.readDerivedByIdIdx() "; CFSecurityISOLanguagePKey key = schema.getFactoryISOLanguage().newPKey(); key.setRequiredISOLanguageId(ISOLanguageId); CFSecurityISOLanguageBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOLanguageBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityISOLanguagePKey PKey) { final String S_ProcName = "CFBamRamISOLanguage.readBuff"; CFSecurityISOLanguageBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISLN"))) { buff = null; } return (buff); } public CFSecurityISOLanguageBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityISOLanguagePKey PKey) { final String S_ProcName = "lockBuff"; CFSecurityISOLanguageBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISLN"))) { buff = null; } return (buff); } public CFSecurityISOLanguageBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamISOLanguage.readAllBuff"; CFSecurityISOLanguageBuff buff; ArrayList<CFSecurityISOLanguageBuff> filteredList = new ArrayList<CFSecurityISOLanguageBuff>(); CFSecurityISOLanguageBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ISLN")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityISOLanguageBuff[0])); } public CFSecurityISOLanguageBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, short ISOLanguageId) { final String S_ProcName = "CFBamRamISOLanguage.readBuffByIdIdx() "; CFSecurityISOLanguageBuff buff = readDerivedByIdIdx(Authorization, ISOLanguageId); if ((buff != null) && buff.getClassCode().equals("ISLN")) { return ((CFSecurityISOLanguageBuff) buff); } else { return (null); } } public CFSecurityISOLanguageBuff readBuffByCode3Idx(CFSecurityAuthorization Authorization, String ISO6392Code) { final String S_ProcName = "CFBamRamISOLanguage.readBuffByCode3Idx() "; CFSecurityISOLanguageBuff buff = readDerivedByCode3Idx(Authorization, ISO6392Code); if ((buff != null) && buff.getClassCode().equals("ISLN")) { return ((CFSecurityISOLanguageBuff) buff); } else { return (null); } } public CFSecurityISOLanguageBuff[] readBuffByCode2Idx(CFSecurityAuthorization Authorization, String ISO6391Code) { final String S_ProcName = "CFBamRamISOLanguage.readBuffByCode2Idx() "; CFSecurityISOLanguageBuff buff; ArrayList<CFSecurityISOLanguageBuff> filteredList = new ArrayList<CFSecurityISOLanguageBuff>(); CFSecurityISOLanguageBuff[] buffList = readDerivedByCode2Idx(Authorization, ISO6391Code); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ISLN")) { filteredList.add((CFSecurityISOLanguageBuff) buff); } } return (filteredList.toArray(new CFSecurityISOLanguageBuff[0])); } public void updateISOLanguage(CFSecurityAuthorization Authorization, CFSecurityISOLanguageBuff Buff) { CFSecurityISOLanguagePKey pkey = schema.getFactoryISOLanguage().newPKey(); pkey.setRequiredISOLanguageId(Buff.getRequiredISOLanguageId()); CFSecurityISOLanguageBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateISOLanguage", "Existing record not found", "ISOLanguage", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateISOLanguage", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityISOLanguageByCode3IdxKey existingKeyCode3Idx = schema.getFactoryISOLanguage().newCode3IdxKey(); existingKeyCode3Idx.setRequiredISO6392Code(existing.getRequiredISO6392Code()); CFSecurityISOLanguageByCode3IdxKey newKeyCode3Idx = schema.getFactoryISOLanguage().newCode3IdxKey(); newKeyCode3Idx.setRequiredISO6392Code(Buff.getRequiredISO6392Code()); CFSecurityISOLanguageByCode2IdxKey existingKeyCode2Idx = schema.getFactoryISOLanguage().newCode2IdxKey(); existingKeyCode2Idx.setOptionalISO6391Code(existing.getOptionalISO6391Code()); CFSecurityISOLanguageByCode2IdxKey newKeyCode2Idx = schema.getFactoryISOLanguage().newCode2IdxKey(); newKeyCode2Idx.setOptionalISO6391Code(Buff.getOptionalISO6391Code()); // Check unique indexes if (!existingKeyCode3Idx.equals(newKeyCode3Idx)) { if (dictByCode3Idx.containsKey(newKeyCode3Idx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateISOLanguage", "ISOLanguage6392Idx", newKeyCode3Idx); } } // Validate foreign keys // Update is valid Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByCode3Idx.remove(existingKeyCode3Idx); dictByCode3Idx.put(newKeyCode3Idx, Buff); subdict = dictByCode2Idx.get(existingKeyCode2Idx); if (subdict != null) { subdict.remove(pkey); } if (dictByCode2Idx.containsKey(newKeyCode2Idx)) { subdict = dictByCode2Idx.get(newKeyCode2Idx); } else { subdict = new HashMap<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff>(); dictByCode2Idx.put(newKeyCode2Idx, subdict); } subdict.put(pkey, Buff); } public void deleteISOLanguage(CFSecurityAuthorization Authorization, CFSecurityISOLanguageBuff Buff) { final String S_ProcName = "CFBamRamISOLanguageTable.deleteISOLanguage() "; String classCode; CFSecurityISOLanguagePKey pkey = schema.getFactoryISOLanguage().newPKey(); pkey.setRequiredISOLanguageId(Buff.getRequiredISOLanguageId()); CFSecurityISOLanguageBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOLanguage", pkey); } // Short circuit self-referential code to prevent stack overflows Object arrCheckISOLanguageCountries[] = schema.getTableISOCountryLanguage() .readDerivedByLanguageIdx(Authorization, existing.getRequiredISOLanguageId()); if (arrCheckISOLanguageCountries.length > 0) { schema.getTableISOCountryLanguage().deleteISOCountryLanguageByLanguageIdx(Authorization, existing.getRequiredISOLanguageId()); } CFSecurityISOLanguageByCode3IdxKey keyCode3Idx = schema.getFactoryISOLanguage().newCode3IdxKey(); keyCode3Idx.setRequiredISO6392Code(existing.getRequiredISO6392Code()); CFSecurityISOLanguageByCode2IdxKey keyCode2Idx = schema.getFactoryISOLanguage().newCode2IdxKey(); keyCode2Idx.setOptionalISO6391Code(existing.getOptionalISO6391Code()); // Validate reverse foreign keys // Delete is valid Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdict; dictByPKey.remove(pkey); dictByCode3Idx.remove(keyCode3Idx); subdict = dictByCode2Idx.get(keyCode2Idx); subdict.remove(pkey); } public void deleteISOLanguageByIdIdx(CFSecurityAuthorization Authorization, short argISOLanguageId) { CFSecurityISOLanguagePKey key = schema.getFactoryISOLanguage().newPKey(); key.setRequiredISOLanguageId(argISOLanguageId); deleteISOLanguageByIdIdx(Authorization, key); } public void deleteISOLanguageByIdIdx(CFSecurityAuthorization Authorization, CFSecurityISOLanguagePKey argKey) { boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } CFSecurityISOLanguageBuff cur; LinkedList<CFSecurityISOLanguageBuff> matchSet = new LinkedList<CFSecurityISOLanguageBuff>(); Iterator<CFSecurityISOLanguageBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOLanguageBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOLanguage().readDerivedByIdIdx(Authorization, cur.getRequiredISOLanguageId()); deleteISOLanguage(Authorization, cur); } } public void deleteISOLanguageByCode3Idx(CFSecurityAuthorization Authorization, String argISO6392Code) { CFSecurityISOLanguageByCode3IdxKey key = schema.getFactoryISOLanguage().newCode3IdxKey(); key.setRequiredISO6392Code(argISO6392Code); deleteISOLanguageByCode3Idx(Authorization, key); } public void deleteISOLanguageByCode3Idx(CFSecurityAuthorization Authorization, CFSecurityISOLanguageByCode3IdxKey argKey) { CFSecurityISOLanguageBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFSecurityISOLanguageBuff> matchSet = new LinkedList<CFSecurityISOLanguageBuff>(); Iterator<CFSecurityISOLanguageBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOLanguageBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOLanguage().readDerivedByIdIdx(Authorization, cur.getRequiredISOLanguageId()); deleteISOLanguage(Authorization, cur); } } public void deleteISOLanguageByCode2Idx(CFSecurityAuthorization Authorization, String argISO6391Code) { CFSecurityISOLanguageByCode2IdxKey key = schema.getFactoryISOLanguage().newCode2IdxKey(); key.setOptionalISO6391Code(argISO6391Code); deleteISOLanguageByCode2Idx(Authorization, key); } public void deleteISOLanguageByCode2Idx(CFSecurityAuthorization Authorization, CFSecurityISOLanguageByCode2IdxKey argKey) { CFSecurityISOLanguageBuff cur; boolean anyNotNull = false; if (argKey.getOptionalISO6391Code() != null) { anyNotNull = true; } if (!anyNotNull) { return; } LinkedList<CFSecurityISOLanguageBuff> matchSet = new LinkedList<CFSecurityISOLanguageBuff>(); Iterator<CFSecurityISOLanguageBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOLanguageBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOLanguage().readDerivedByIdIdx(Authorization, cur.getRequiredISOLanguageId()); deleteISOLanguage(Authorization, cur); } } public CFSecurityCursor openISOLanguageCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamISOLanguageCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openISOLanguageCursorByCode2Idx(CFSecurityAuthorization Authorization, String ISO6391Code) { CFSecurityCursor cursor; CFSecurityISOLanguageByCode2IdxKey key = schema.getFactoryISOLanguage().newCode2IdxKey(); key.setOptionalISO6391Code(ISO6391Code); if (dictByCode2Idx.containsKey(key)) { Map<CFSecurityISOLanguagePKey, CFSecurityISOLanguageBuff> subdictCode2Idx = dictByCode2Idx.get(key); cursor = new CFBamRamISOLanguageCursor(Authorization, schema, subdictCode2Idx.values()); } else { cursor = new CFBamRamISOLanguageCursor(Authorization, schema, new ArrayList<CFSecurityISOLanguageBuff>()); } return (cursor); } public void closeISOLanguageCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityISOLanguageBuff nextISOLanguageCursor(CFSecurityCursor Cursor) { CFBamRamISOLanguageCursor cursor = (CFBamRamISOLanguageCursor) Cursor; CFSecurityISOLanguageBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityISOLanguageBuff prevISOLanguageCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityISOLanguageBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOLanguageCursor(Cursor); } return (rec); } public CFSecurityISOLanguageBuff firstISOLanguageCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityISOLanguageBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOLanguageCursor(Cursor); } return (rec); } public CFSecurityISOLanguageBuff lastISOLanguageCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOLanguageCursor"); } public CFSecurityISOLanguageBuff nthISOLanguageCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityISOLanguageBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOLanguageCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }