Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for ISOCountry. /* * MSS Code Factory Accounting Business Application Model * * Copyright (c) 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.cfacc.v2_0.CFAccRam; 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.cfacc.v2_0.CFAcc.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*; /* * CFAccRamISOCountryTable in-memory RAM DbIO implementation * for ISOCountry. */ public class CFAccRamISOCountryTable implements ICFAccISOCountryTable { private CFAccRamSchema schema; private Map<CFAccISOCountryPKey, CFAccISOCountryBuff> dictByPKey = new HashMap<CFAccISOCountryPKey, CFAccISOCountryBuff>(); private SortedMap<CFAccISOCountryByISOCodeIdxKey, CFAccISOCountryBuff> dictByISOCodeIdx = new TreeMap<CFAccISOCountryByISOCodeIdxKey, CFAccISOCountryBuff>(); private SortedMap<CFAccISOCountryByNameIdxKey, CFAccISOCountryBuff> dictByNameIdx = new TreeMap<CFAccISOCountryByNameIdxKey, CFAccISOCountryBuff>(); public CFAccRamISOCountryTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createISOCountry(CFAccAuthorization Authorization, CFAccISOCountryBuff Buff) { CFAccISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); Buff.setRequiredId(pkey.getRequiredId()); CFAccISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFAccISOCountryByNameIdxKey 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 CFAccISOCountryBuff readDerived(CFAccAuthorization Authorization, CFAccISOCountryPKey PKey) { final String S_ProcName = "CFAccRamISOCountry.readDerived() "; CFAccISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAccISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccISOCountryBuff lockDerived(CFAccAuthorization Authorization, CFAccISOCountryPKey PKey) { final String S_ProcName = "CFAccRamISOCountry.readDerived() "; CFAccISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAccISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccISOCountryBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamISOCountry.readAllDerived() "; CFAccISOCountryBuff[] retList = new CFAccISOCountryBuff[dictByPKey.values().size()]; Iterator<CFAccISOCountryBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccISOCountryBuff readDerivedByISOCodeIdx(CFAccAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAccRamISOCountry.readDerivedByISOCodeIdx() "; CFAccISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(ISOCode); CFAccISOCountryBuff buff; if (dictByISOCodeIdx.containsKey(key)) { buff = dictByISOCodeIdx.get(key); } else { buff = null; } return (buff); } public CFAccISOCountryBuff readDerivedByNameIdx(CFAccAuthorization Authorization, String Name) { final String S_ProcName = "CFAccRamISOCountry.readDerivedByNameIdx() "; CFAccISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(Name); CFAccISOCountryBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFAccISOCountryBuff readDerivedByIdIdx(CFAccAuthorization Authorization, short Id) { final String S_ProcName = "CFAccRamISOCountry.readDerivedByIdIdx() "; CFAccISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(Id); CFAccISOCountryBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccISOCountryBuff readBuff(CFAccAuthorization Authorization, CFAccISOCountryPKey PKey) { final String S_ProcName = "CFAccRamISOCountry.readBuff() "; CFAccISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFAccISOCountryBuff lockBuff(CFAccAuthorization Authorization, CFAccISOCountryPKey PKey) { final String S_ProcName = "CFAccRamISOCountry.readBuff() "; CFAccISOCountryBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) { buff = null; } return (buff); } public CFAccISOCountryBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamISOCountry.readAllBuff() "; CFAccISOCountryBuff buff; ArrayList<CFAccISOCountryBuff> filteredList = new ArrayList<CFAccISOCountryBuff>(); CFAccISOCountryBuff[] 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 CFAccISOCountryBuff[0])); } public CFAccISOCountryBuff readBuffByIdIdx(CFAccAuthorization Authorization, short Id) { final String S_ProcName = "CFAccRamISOCountry.readBuffByIdIdx() "; CFAccISOCountryBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAccISOCountryBuff) buff); } else { return (null); } } public CFAccISOCountryBuff readBuffByISOCodeIdx(CFAccAuthorization Authorization, String ISOCode) { final String S_ProcName = "CFAccRamISOCountry.readBuffByISOCodeIdx() "; CFAccISOCountryBuff buff = readDerivedByISOCodeIdx(Authorization, ISOCode); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAccISOCountryBuff) buff); } else { return (null); } } public CFAccISOCountryBuff readBuffByNameIdx(CFAccAuthorization Authorization, String Name) { final String S_ProcName = "CFAccRamISOCountry.readBuffByNameIdx() "; CFAccISOCountryBuff buff = readDerivedByNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("ISOC")) { return ((CFAccISOCountryBuff) buff); } else { return (null); } } public void updateISOCountry(CFAccAuthorization Authorization, CFAccISOCountryBuff Buff) { CFAccISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAccISOCountryBuff 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); CFAccISOCountryByISOCodeIdxKey existingKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); existingKeyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFAccISOCountryByISOCodeIdxKey newKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); newKeyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode()); CFAccISOCountryByNameIdxKey existingKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey(); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFAccISOCountryByNameIdxKey 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<CFAccISOCountryPKey, CFAccISOCountryBuff> 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(CFAccAuthorization Authorization, CFAccISOCountryBuff Buff) { final String S_ProcName = "CFAccRamISOCountryTable.deleteISOCountry() "; CFAccISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAccISOCountryBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCountry", pkey); } CFAccISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey(); keyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode()); CFAccISOCountryByNameIdxKey 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<CFAccISOCountryPKey, CFAccISOCountryBuff> subdict; dictByPKey.remove(pkey); dictByISOCodeIdx.remove(keyISOCodeIdx); dictByNameIdx.remove(keyNameIdx); } public void deleteISOCountryByIdIdx(CFAccAuthorization Authorization, short argId) { CFAccISOCountryPKey key = schema.getFactoryISOCountry().newPKey(); key.setRequiredId(argId); deleteISOCountryByIdIdx(Authorization, key); } public void deleteISOCountryByIdIdx(CFAccAuthorization Authorization, CFAccISOCountryPKey argKey) { CFAccISOCountryBuff cur; LinkedList<CFAccISOCountryBuff> matchSet = new LinkedList<CFAccISOCountryBuff>(); Iterator<CFAccISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByISOCodeIdx(CFAccAuthorization Authorization, String argISOCode) { CFAccISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey(); key.setRequiredISOCode(argISOCode); deleteISOCountryByISOCodeIdx(Authorization, key); } public void deleteISOCountryByISOCodeIdx(CFAccAuthorization Authorization, CFAccISOCountryByISOCodeIdxKey argKey) { CFAccISOCountryBuff cur; LinkedList<CFAccISOCountryBuff> matchSet = new LinkedList<CFAccISOCountryBuff>(); Iterator<CFAccISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public void deleteISOCountryByNameIdx(CFAccAuthorization Authorization, String argName) { CFAccISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey(); key.setRequiredName(argName); deleteISOCountryByNameIdx(Authorization, key); } public void deleteISOCountryByNameIdx(CFAccAuthorization Authorization, CFAccISOCountryByNameIdxKey argKey) { CFAccISOCountryBuff cur; LinkedList<CFAccISOCountryBuff> matchSet = new LinkedList<CFAccISOCountryBuff>(); Iterator<CFAccISOCountryBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccISOCountryBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteISOCountry(Authorization, cur); } } public CFAccCursor openISOCountryCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamISOCountryCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeISOCountryCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccISOCountryBuff nextISOCountryCursor(CFAccCursor Cursor) { CFAccRamISOCountryCursor cursor = (CFAccRamISOCountryCursor) Cursor; CFAccISOCountryBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccISOCountryBuff prevISOCountryCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFAccISOCountryBuff firstISOCountryCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccISOCountryBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public CFAccISOCountryBuff lastISOCountryCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOCountryCursor"); } public CFAccISOCountryBuff nthISOCountryCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccISOCountryBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextISOCountryCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }