Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ISOCountry. /* * 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.*; /* * CFBamRamISOCountryTable in-memory RAM DbIO implementation * for ISOCountry. */ public class CFBamRamISOCountryTable implements ICFBamISOCountryTable { private ICFBamSchema schema; private Map<CFSecurityISOCountryPKey, CFSecurityISOCountryBuff> dictByPKey = new HashMap<CFSecurityISOCountryPKey, CFSecurityISOCountryBuff>(); private Map<CFSecurityISOCountryByISOCodeIdxKey, CFSecurityISOCountryBuff> dictByISOCodeIdx = new HashMap<CFSecurityISOCountryByISOCodeIdxKey, CFSecurityISOCountryBuff>(); private Map<CFSecurityISOCountryByNameIdxKey, CFSecurityISOCountryBuff> dictByNameIdx = new HashMap<CFSecurityISOCountryByNameIdxKey, CFSecurityISOCountryBuff>(); public CFBamRamISOCountryTable(ICFBamSchema argSchema) { schema = argSchema; } public void createISOCountry(CFSecurityAuthorization Authorization, CFSecurityISOCountryBuff Buff) { final String S_ProcName = "createISOCountry"; CFSecurityISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredISOCountryId(schema.nextISOCountryIdGen()); Buff.setRequiredISOCountryId(pkey.getRequiredISOCountryId()); CFSecurityISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFSecurityISOCountryByNameIdxKey keyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); keyNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByISOCodeIdx.containsKey(keyISOCodeIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ISOCountryCodeIdx", keyISOCodeIdx); } if (dictByNameIdx.containsKey(keyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ISOCountryNameIdx", keyNameIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByISOCodeIdx.put(keyISOCodeIdx, Buff); dictByNameIdx.put(keyNameIdx, Buff); } public CFSecurityISOCountryBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey PKey) { final String S_ProcName = "CFBamRamISOCountry.readDerived"; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredISOCountryId(PKey.getRequiredISOCountryId()); CFSecurityISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey PKey) { final String S_ProcName = "CFBamRamISOCountry.readDerived"; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredISOCountryId(PKey.getRequiredISOCountryId()); CFSecurityISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamISOCountry.readAllDerived"; CFSecurityISOCountryBuff[] retList = new CFSecurityISOCountryBuff[dictByPKey.values().size()]; Iterator<CFSecurityISOCountryBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityISOCountryBuff readDerivedByISOCodeIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFBamRamISOCountry.readDerivedByISOCodeIdx"; CFSecurityISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(ISOCode); CFSecurityISOCountryBuff buff; if (dictByISOCodeIdx.containsKey(key)) { buff = dictByISOCodeIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff readDerivedByNameIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFBamRamISOCountry.readDerivedByNameIdx"; CFSecurityISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(Name); CFSecurityISOCountryBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, short ISOCountryId) { final String S_ProcName = "CFBamRamISOCountry.readDerivedByIdIdx() "; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredISOCountryId(ISOCountryId); CFSecurityISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey PKey) { final String S_ProcName = "CFBamRamISOCountry.readBuff"; CFSecurityISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFSecurityISOCountryBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey PKey) { final String S_ProcName = "lockBuff"; CFSecurityISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFSecurityISOCountryBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamISOCountry.readAllBuff"; CFSecurityISOCountryBuff buff; ArrayList<CFSecurityISOCountryBuff> filteredList = new ArrayList<CFSecurityISOCountryBuff>(); CFSecurityISOCountryBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ISOC")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityISOCountryBuff[0])); } public CFSecurityISOCountryBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, short ISOCountryId) { final String S_ProcName = "CFBamRamISOCountry.readBuffByIdIdx() "; CFSecurityISOCountryBuff buff = readDerivedByIdIdx(Authorization, ISOCountryId); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFSecurityISOCountryBuff) buff); } else { return (null); } } public CFSecurityISOCountryBuff readBuffByISOCodeIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFBamRamISOCountry.readBuffByISOCodeIdx() "; CFSecurityISOCountryBuff buff = readDerivedByISOCodeIdx(Authorization, ISOCode); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFSecurityISOCountryBuff) buff); } else { return (null); } } public CFSecurityISOCountryBuff readBuffByNameIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFBamRamISOCountry.readBuffByNameIdx() "; CFSecurityISOCountryBuff buff = readDerivedByNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFSecurityISOCountryBuff) buff); } else { return (null); } } public void updateISOCountry(CFSecurityAuthorization Authorization, CFSecurityISOCountryBuff Buff) { CFSecurityISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredISOCountryId(Buff.getRequiredISOCountryId()); CFSecurityISOCountryBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateISOCountry", "Existing record not found", "ISOCountry", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateISOCountry", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityISOCountryByISOCodeIdxKey existingKeyISOCodeIdx = schema.getFactoryISOCountry() .newISOCodeIdxKey(); existingKeyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFSecurityISOCountryByISOCodeIdxKey newKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); newKeyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFSecurityISOCountryByNameIdxKey existingKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFSecurityISOCountryByNameIdxKey newKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); newKeyNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyISOCodeIdx.equals(newKeyISOCodeIdx)) { if (dictByISOCodeIdx.containsKey(newKeyISOCodeIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateISOCountry", "ISOCountryCodeIdx", newKeyISOCodeIdx); } } if (!existingKeyNameIdx.equals(newKeyNameIdx)) { if (dictByNameIdx.containsKey(newKeyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateISOCountry", "ISOCountryNameIdx", newKeyNameIdx); } } // Validate foreign keys // Update is valid Map<CFSecurityISOCountryPKey, CFSecurityISOCountryBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByISOCodeIdx.remove(existingKeyISOCodeIdx); dictByISOCodeIdx.put(newKeyISOCodeIdx, Buff); dictByNameIdx.remove(existingKeyNameIdx); dictByNameIdx.put(newKeyNameIdx, Buff); } public void deleteISOCountry(CFSecurityAuthorization Authorization, CFSecurityISOCountryBuff Buff) { final String S_ProcName = "CFBamRamISOCountryTable.deleteISOCountry() "; String classCode; CFSecurityISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredISOCountryId(Buff.getRequiredISOCountryId()); CFSecurityISOCountryBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCountry", pkey); } schema.getTableISOCountryLanguage().deleteISOCountryLanguageByCountryIdx(Authorization, existing.getRequiredISOCountryId()); schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCountryIdx(Authorization, existing.getRequiredISOCountryId()); CFSecurityISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFSecurityISOCountryByNameIdxKey keyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); keyNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid Map<CFSecurityISOCountryPKey, CFSecurityISOCountryBuff> subdict; dictByPKey.remove(pkey); dictByISOCodeIdx.remove(keyISOCodeIdx); dictByNameIdx.remove(keyNameIdx); } public void deleteISOCountryByIdIdx(CFSecurityAuthorization Authorization, short argISOCountryId) { CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredISOCountryId(argISOCountryId); deleteISOCountryByIdIdx(Authorization, key); } public void deleteISOCountryByIdIdx(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey argKey) { boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } CFSecurityISOCountryBuff cur; LinkedList<CFSecurityISOCountryBuff> matchSet = new LinkedList<CFSecurityISOCountryBuff>(); Iterator<CFSecurityISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOCountry().readDerivedByIdIdx(Authorization, cur.getRequiredISOCountryId()); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByISOCodeIdx(CFSecurityAuthorization Authorization, String argISOCode) { CFSecurityISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(argISOCode); deleteISOCountryByISOCodeIdx(Authorization, key); } public void deleteISOCountryByISOCodeIdx(CFSecurityAuthorization Authorization, CFSecurityISOCountryByISOCodeIdxKey argKey) { CFSecurityISOCountryBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFSecurityISOCountryBuff> matchSet = new LinkedList<CFSecurityISOCountryBuff>(); Iterator<CFSecurityISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOCountry().readDerivedByIdIdx(Authorization, cur.getRequiredISOCountryId()); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByNameIdx(CFSecurityAuthorization Authorization, String argName) { CFSecurityISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(argName); deleteISOCountryByNameIdx(Authorization, key); } public void deleteISOCountryByNameIdx(CFSecurityAuthorization Authorization, CFSecurityISOCountryByNameIdxKey argKey) { CFSecurityISOCountryBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFSecurityISOCountryBuff> matchSet = new LinkedList<CFSecurityISOCountryBuff>(); Iterator<CFSecurityISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableISOCountry().readDerivedByIdIdx(Authorization, cur.getRequiredISOCountryId()); deleteISOCountry(Authorization, cur); } } public CFSecurityCursor openISOCountryCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamISOCountryCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCountryCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityISOCountryBuff nextISOCountryCursor(CFSecurityCursor Cursor) { CFBamRamISOCountryCursor cursor = (CFBamRamISOCountryCursor) Cursor; CFSecurityISOCountryBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityISOCountryBuff prevISOCountryCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFSecurityISOCountryBuff firstISOCountryCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityISOCountryBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFSecurityISOCountryBuff lastISOCountryCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOCountryCursor"); } public CFSecurityISOCountryBuff nthISOCountryCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }