Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ISOCountry. /* * 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.*; /* * CFAsteriskRamISOCountryTable in-memory RAM DbIO implementation * for ISOCountry. */ public class CFAsteriskRamISOCountryTable implements ICFAsteriskISOCountryTable { private ICFAsteriskSchema 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 CFAsteriskRamISOCountryTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createISOCountry(CFSecurityAuthorization Authorization, CFSecurityISOCountryBuff Buff) { final String S_ProcName = "createISOCountry"; CFSecurityISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); Buff.setRequiredId(pkey.getRequiredId()); 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 = "CFAsteriskRamISOCountry.readDerived"; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); 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 = "CFAsteriskRamISOCountry.readDerived"; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityISOCountryBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamISOCountry.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 = "CFAsteriskRamISOCountry.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 = "CFAsteriskRamISOCountry.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 Id) { final String S_ProcName = "CFAsteriskRamISOCountry.readDerivedByIdIdx() "; CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(Id); 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 = "CFAsteriskRamISOCountry.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 = "CFAsteriskRamISOCountry.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 Id) { final String S_ProcName = "CFAsteriskRamISOCountry.readBuffByIdIdx() "; CFSecurityISOCountryBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFSecurityISOCountryBuff) buff); } else { return (null); } } public CFSecurityISOCountryBuff readBuffByISOCodeIdx(CFSecurityAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAsteriskRamISOCountry.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 = "CFAsteriskRamISOCountry.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.setRequiredId(Buff.getRequiredId()); 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 = "CFAsteriskRamISOCountryTable.deleteISOCountry() "; CFSecurityISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityISOCountryBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCountry", pkey); } 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 schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCountryIdx(Authorization, Buff.getRequiredId()); schema.getTableISOCountryLanguage().deleteISOCountryLanguageByCountryIdx(Authorization, Buff.getRequiredId()); Map<CFSecurityISOCountryPKey, CFSecurityISOCountryBuff> subdict; dictByPKey.remove(pkey); dictByISOCodeIdx.remove(keyISOCodeIdx); dictByNameIdx.remove(keyNameIdx); } public void deleteISOCountryByIdIdx(CFSecurityAuthorization Authorization, short argId) { CFSecurityISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(argId); deleteISOCountryByIdIdx(Authorization, key); } public void deleteISOCountryByIdIdx(CFSecurityAuthorization Authorization, CFSecurityISOCountryPKey argKey) { 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(); 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; 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(); 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; 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(); deleteISOCountry(Authorization, cur); } } public CFSecurityCursor openISOCountryCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamISOCountryCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCountryCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityISOCountryBuff nextISOCountryCursor(CFSecurityCursor Cursor) { CFAsteriskRamISOCountryCursor cursor = (CFAsteriskRamISOCountryCursor) 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() { } }