Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for ISOCountry. /* * CF Asterisk 11 Configuration Model * * Copyright (c) 2013-2014 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * This source code incorporates modified modules originally licensed * under the Apache 2.0 license by MSS Code Factory including CFSecurity * (net-sourceforge-msscodefactory-2.0-cfsecurity.xml), * CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and * CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the * required models being available as part of the MSS Code Factory 1.11 * distribution source and install zips. * * You can download installations of MSS Code Factory 1.11 from * http://msscodefactory.sourceforge.net/ * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam; import java.sql.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAst.*; import net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.*; /* * CFAstRamISOCountryTable in-memory RAM DbIO implementation * for ISOCountry. */ public class CFAstRamISOCountryTable implements ICFAstISOCountryTable { private CFAstRamSchema schema; private Map<CFAstISOCountryPKey, CFAstISOCountryBuff> dictByPKey = new HashMap<CFAstISOCountryPKey, CFAstISOCountryBuff>(); private SortedMap<CFAstISOCountryByISOCodeIdxKey, CFAstISOCountryBuff> dictByISOCodeIdx = new TreeMap<CFAstISOCountryByISOCodeIdxKey, CFAstISOCountryBuff>(); private SortedMap<CFAstISOCountryByNameIdxKey, CFAstISOCountryBuff> dictByNameIdx = new TreeMap<CFAstISOCountryByNameIdxKey, CFAstISOCountryBuff>(); public CFAstRamISOCountryTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createISOCountry(CFAstAuthorization Authorization, CFAstISOCountryBuff Buff) { CFAstISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); Buff.setRequiredId(pkey.getRequiredId()); CFAstISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFAstISOCountryByNameIdxKey keyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); keyNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createISOCountry", pkey); } if (dictByISOCodeIdx.containsKey(keyISOCodeIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createISOCountry", "ISOCountryCodeIdx", keyISOCodeIdx); } if (dictByNameIdx.containsKey(keyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createISOCountry", "ISOCountryNameIdx", keyNameIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByISOCodeIdx.put(keyISOCodeIdx, Buff); dictByNameIdx.put(keyNameIdx, Buff); } public CFAstISOCountryBuff readDerived(CFAstAuthorization Authorization, CFAstISOCountryPKey PKey) { final String S_ProcName = "CFAstRamISOCountry.readDerived() "; CFAstISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAstISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstISOCountryBuff lockDerived(CFAstAuthorization Authorization, CFAstISOCountryPKey PKey) { final String S_ProcName = "CFAstRamISOCountry.readDerived() "; CFAstISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAstISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstISOCountryBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamISOCountry.readAllDerived() "; CFAstISOCountryBuff[] retList = new CFAstISOCountryBuff[dictByPKey.values().size()]; Iterator<CFAstISOCountryBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstISOCountryBuff readDerivedByISOCodeIdx(CFAstAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAstRamISOCountry.readDerivedByISOCodeIdx() "; CFAstISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(ISOCode); CFAstISOCountryBuff buff; if (dictByISOCodeIdx.containsKey(key)) { buff = dictByISOCodeIdx.get(key); } else { buff = null; } return (buff); } public CFAstISOCountryBuff readDerivedByNameIdx(CFAstAuthorization Authorization, String Name) { final String S_ProcName = "CFAstRamISOCountry.readDerivedByNameIdx() "; CFAstISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(Name); CFAstISOCountryBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstISOCountryBuff readDerivedByIdIdx(CFAstAuthorization Authorization, short Id) { final String S_ProcName = "CFAstRamISOCountry.readDerivedByIdIdx() "; CFAstISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(Id); CFAstISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstISOCountryBuff readBuff(CFAstAuthorization Authorization, CFAstISOCountryPKey PKey) { final String S_ProcName = "CFAstRamISOCountry.readBuff() "; CFAstISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFAstISOCountryBuff lockBuff(CFAstAuthorization Authorization, CFAstISOCountryPKey PKey) { final String S_ProcName = "CFAstRamISOCountry.readBuff() "; CFAstISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFAstISOCountryBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamISOCountry.readAllBuff() "; CFAstISOCountryBuff buff; ArrayList<CFAstISOCountryBuff> filteredList = new ArrayList<CFAstISOCountryBuff>(); CFAstISOCountryBuff[] 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 CFAstISOCountryBuff[0])); } public CFAstISOCountryBuff readBuffByIdIdx(CFAstAuthorization Authorization, short Id) { final String S_ProcName = "CFAstRamISOCountry.readBuffByIdIdx() "; CFAstISOCountryBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAstISOCountryBuff) buff); } else { return (null); } } public CFAstISOCountryBuff readBuffByISOCodeIdx(CFAstAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAstRamISOCountry.readBuffByISOCodeIdx() "; CFAstISOCountryBuff buff = readDerivedByISOCodeIdx(Authorization, ISOCode); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAstISOCountryBuff) buff); } else { return (null); } } public CFAstISOCountryBuff readBuffByNameIdx(CFAstAuthorization Authorization, String Name) { final String S_ProcName = "CFAstRamISOCountry.readBuffByNameIdx() "; CFAstISOCountryBuff buff = readDerivedByNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAstISOCountryBuff) buff); } else { return (null); } } public void updateISOCountry(CFAstAuthorization Authorization, CFAstISOCountryBuff Buff) { CFAstISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAstISOCountryBuff 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); CFAstISOCountryByISOCodeIdxKey existingKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); existingKeyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFAstISOCountryByISOCodeIdxKey newKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); newKeyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFAstISOCountryByNameIdxKey existingKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFAstISOCountryByNameIdxKey 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 SortedMap<CFAstISOCountryPKey, CFAstISOCountryBuff> 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(CFAstAuthorization Authorization, CFAstISOCountryBuff Buff) { final String S_ProcName = "CFAstRamISOCountryTable.deleteISOCountry() "; CFAstISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAstISOCountryBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCountry", pkey); } CFAstISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFAstISOCountryByNameIdxKey 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()); SortedMap<CFAstISOCountryPKey, CFAstISOCountryBuff> subdict; dictByPKey.remove(pkey); dictByISOCodeIdx.remove(keyISOCodeIdx); dictByNameIdx.remove(keyNameIdx); } public void deleteISOCountryByIdIdx(CFAstAuthorization Authorization, short argId) { CFAstISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(argId); deleteISOCountryByIdIdx(Authorization, key); } public void deleteISOCountryByIdIdx(CFAstAuthorization Authorization, CFAstISOCountryPKey argKey) { CFAstISOCountryBuff cur; LinkedList<CFAstISOCountryBuff> matchSet = new LinkedList<CFAstISOCountryBuff>(); Iterator<CFAstISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByISOCodeIdx(CFAstAuthorization Authorization, String argISOCode) { CFAstISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(argISOCode); deleteISOCountryByISOCodeIdx(Authorization, key); } public void deleteISOCountryByISOCodeIdx(CFAstAuthorization Authorization, CFAstISOCountryByISOCodeIdxKey argKey) { CFAstISOCountryBuff cur; LinkedList<CFAstISOCountryBuff> matchSet = new LinkedList<CFAstISOCountryBuff>(); Iterator<CFAstISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByNameIdx(CFAstAuthorization Authorization, String argName) { CFAstISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(argName); deleteISOCountryByNameIdx(Authorization, key); } public void deleteISOCountryByNameIdx(CFAstAuthorization Authorization, CFAstISOCountryByNameIdxKey argKey) { CFAstISOCountryBuff cur; LinkedList<CFAstISOCountryBuff> matchSet = new LinkedList<CFAstISOCountryBuff>(); Iterator<CFAstISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public CFAstCursor openISOCountryCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamISOCountryCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCountryCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstISOCountryBuff nextISOCountryCursor(CFAstCursor Cursor) { CFAstRamISOCountryCursor cursor = (CFAstRamISOCountryCursor) Cursor; CFAstISOCountryBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstISOCountryBuff prevISOCountryCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFAstISOCountryBuff firstISOCountryCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstISOCountryBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFAstISOCountryBuff lastISOCountryCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOCountryCursor"); } public CFAstISOCountryBuff nthISOCountryCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }