Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for Tld. /* * 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.*; /* * CFAccRamTldTable in-memory RAM DbIO implementation * for Tld. */ public class CFAccRamTldTable implements ICFAccTldTable { private CFAccRamSchema schema; private Map<CFAccTldPKey, CFAccTldBuff> dictByPKey = new HashMap<CFAccTldPKey, CFAccTldBuff>(); private SortedMap<CFAccTldByTenantIdxKey, SortedMap<CFAccTldPKey, CFAccTldBuff>> dictByTenantIdx = new TreeMap<CFAccTldByTenantIdxKey, SortedMap<CFAccTldPKey, CFAccTldBuff>>(); private SortedMap<CFAccTldByNameIdxKey, CFAccTldBuff> dictByNameIdx = new TreeMap<CFAccTldByNameIdxKey, CFAccTldBuff>(); public CFAccRamTldTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createTld(CFAccAuthorization Authorization, CFAccTldBuff Buff) { CFAccTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(((CFAccRamTenantTable) schema.getTableTenant()).nextTLDIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTLDId(pkey.getRequiredTLDId()); CFAccTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAccTldByNameIdxKey keyNameIdx = schema.getFactoryTld().newNameIdxKey(); keyNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTld", pkey); } if (dictByNameIdx.containsKey(keyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTld", "TLDNameIdx", keyNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createTld", "Container", "Tenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAccTldPKey, CFAccTldBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFAccTldPKey, CFAccTldBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByNameIdx.put(keyNameIdx, Buff); } public CFAccTldBuff readDerived(CFAccAuthorization Authorization, CFAccTldPKey PKey) { final String S_ProcName = "CFAccRamTld.readDerived() "; CFAccTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTLDId(PKey.getRequiredTLDId()); CFAccTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTldBuff lockDerived(CFAccAuthorization Authorization, CFAccTldPKey PKey) { final String S_ProcName = "CFAccRamTld.readDerived() "; CFAccTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTLDId(PKey.getRequiredTLDId()); CFAccTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTldBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamTld.readAllDerived() "; CFAccTldBuff[] retList = new CFAccTldBuff[dictByPKey.values().size()]; Iterator<CFAccTldBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccTldBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamTld.readDerivedByTenantIdx() "; CFAccTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFAccTldBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAccTldPKey, CFAccTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFAccTldBuff[subdictTenantIdx.size()]; Iterator<CFAccTldBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAccTldBuff[0]; } return (recArray); } public CFAccTldBuff readDerivedByNameIdx(CFAccAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAccRamTld.readDerivedByNameIdx() "; CFAccTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFAccTldBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFAccTldBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long TLDId) { final String S_ProcName = "CFAccRamTld.readDerivedByIdIdx() "; CFAccTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTLDId(TLDId); CFAccTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTldBuff readBuff(CFAccAuthorization Authorization, CFAccTldPKey PKey) { final String S_ProcName = "CFAccRamTld.readBuff() "; CFAccTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTLD"))) { buff = null; } return (buff); } public CFAccTldBuff lockBuff(CFAccAuthorization Authorization, CFAccTldPKey PKey) { final String S_ProcName = "CFAccRamTld.readBuff() "; CFAccTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTLD"))) { buff = null; } return (buff); } public CFAccTldBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamTld.readAllBuff() "; CFAccTldBuff buff; ArrayList<CFAccTldBuff> filteredList = new ArrayList<CFAccTldBuff>(); CFAccTldBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("GTLD")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAccTldBuff[0])); } public CFAccTldBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long TLDId) { final String S_ProcName = "CFAccRamTld.readBuffByIdIdx() "; CFAccTldBuff buff = readDerivedByIdIdx(Authorization, TenantId, TLDId); if ((buff != null) && buff.getClassCode().equals("GTLD")) { return ((CFAccTldBuff) buff); } else { return (null); } } public CFAccTldBuff[] readBuffByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamTld.readBuffByTenantIdx() "; CFAccTldBuff buff; ArrayList<CFAccTldBuff> filteredList = new ArrayList<CFAccTldBuff>(); CFAccTldBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("GTLD")) { filteredList.add((CFAccTldBuff) buff); } } return (filteredList.toArray(new CFAccTldBuff[0])); } public CFAccTldBuff readBuffByNameIdx(CFAccAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAccRamTld.readBuffByNameIdx() "; CFAccTldBuff buff = readDerivedByNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("GTLD")) { return ((CFAccTldBuff) buff); } else { return (null); } } public void updateTld(CFAccAuthorization Authorization, CFAccTldBuff Buff) { CFAccTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(Buff.getRequiredTLDId()); CFAccTldBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTld", "Existing record not found", "Tld", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTld", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAccTldByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAccTldByTenantIdxKey newKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAccTldByNameIdxKey existingKeyNameIdx = schema.getFactoryTld().newNameIdxKey(); existingKeyNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFAccTldByNameIdxKey newKeyNameIdx = schema.getFactoryTld().newNameIdxKey(); newKeyNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); newKeyNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyNameIdx.equals(newKeyNameIdx)) { if (dictByNameIdx.containsKey(newKeyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTld", "TLDNameIdx", newKeyNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTld", "Container", "Tenant", "Tenant", null); } } } // Update is valid SortedMap<CFAccTldPKey, CFAccTldBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByTenantIdx.get(existingKeyTenantIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByTenantIdx.containsKey(newKeyTenantIdx)) { subdict = dictByTenantIdx.get(newKeyTenantIdx); } else { subdict = new TreeMap<CFAccTldPKey, CFAccTldBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByNameIdx.remove(existingKeyNameIdx); dictByNameIdx.put(newKeyNameIdx, Buff); } public void deleteTld(CFAccAuthorization Authorization, CFAccTldBuff Buff) { final String S_ProcName = "CFAccRamTldTable.deleteTld() "; CFAccTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(Buff.getRequiredTLDId()); CFAccTldBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTld", pkey); } CFAccTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAccTldByNameIdxKey keyNameIdx = schema.getFactoryTld().newNameIdxKey(); keyNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); keyNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableTopDomain().deleteTopDomainByTLDIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTLDId()); SortedMap<CFAccTldPKey, CFAccTldBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByNameIdx.remove(keyNameIdx); } public void deleteTldByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argTLDId) { CFAccTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTLDId(argTLDId); deleteTldByIdIdx(Authorization, key); } public void deleteTldByIdIdx(CFAccAuthorization Authorization, CFAccTldPKey argKey) { CFAccTldBuff cur; LinkedList<CFAccTldBuff> matchSet = new LinkedList<CFAccTldBuff>(); Iterator<CFAccTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByTenantIdx(CFAccAuthorization Authorization, long argTenantId) { CFAccTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTldByTenantIdx(Authorization, key); } public void deleteTldByTenantIdx(CFAccAuthorization Authorization, CFAccTldByTenantIdxKey argKey) { CFAccTldBuff cur; LinkedList<CFAccTldBuff> matchSet = new LinkedList<CFAccTldBuff>(); Iterator<CFAccTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByNameIdx(CFAccAuthorization Authorization, long argTenantId, String argName) { CFAccTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTldByNameIdx(Authorization, key); } public void deleteTldByNameIdx(CFAccAuthorization Authorization, CFAccTldByNameIdxKey argKey) { CFAccTldBuff cur; LinkedList<CFAccTldBuff> matchSet = new LinkedList<CFAccTldBuff>(); Iterator<CFAccTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public CFAccCursor openTldCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamTldCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAccCursor openTldCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) { CFAccCursor cursor; CFAccTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAccTldPKey, CFAccTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAccRamTldCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAccRamTldCursor(Authorization, schema, new ArrayList<CFAccTldBuff>()); } return (cursor); } public void closeTldCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccTldBuff nextTldCursor(CFAccCursor Cursor) { CFAccRamTldCursor cursor = (CFAccRamTldCursor) Cursor; CFAccTldBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccTldBuff prevTldCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFAccTldBuff firstTldCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccTldBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFAccTldBuff lastTldCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTldCursor"); } public CFAccTldBuff nthTldCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }