Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for Tld. /* * 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.*; /* * CFAsteriskRamTldTable in-memory RAM DbIO implementation * for Tld. */ public class CFAsteriskRamTldTable implements ICFAsteriskTldTable { private ICFAsteriskSchema schema; private Map<CFInternetTldPKey, CFInternetTldBuff> dictByPKey = new HashMap<CFInternetTldPKey, CFInternetTldBuff>(); private Map<CFInternetTldByTenantIdxKey, Map<CFInternetTldPKey, CFInternetTldBuff>> dictByTenantIdx = new HashMap<CFInternetTldByTenantIdxKey, Map<CFInternetTldPKey, CFInternetTldBuff>>(); private Map<CFInternetTldByNameIdxKey, CFInternetTldBuff> dictByNameIdx = new HashMap<CFInternetTldByNameIdxKey, CFInternetTldBuff>(); public CFAsteriskRamTldTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createTld(CFSecurityAuthorization Authorization, CFInternetTldBuff Buff) { final String S_ProcName = "createTld"; CFInternetTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTldId(((CFAsteriskRamTenantTable) schema.getTableTenant()).nextTldIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTldId(pkey.getRequiredTldId()); CFInternetTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFInternetTldByNameIdxKey keyNameIdx = schema.getFactoryTld().newNameIdxKey(); keyNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByNameIdx.containsKey(keyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "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(), S_ProcName, "Container", "Tenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFInternetTldPKey, CFInternetTldBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new HashMap<CFInternetTldPKey, CFInternetTldBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByNameIdx.put(keyNameIdx, Buff); } public CFInternetTldBuff readDerived(CFSecurityAuthorization Authorization, CFInternetTldPKey PKey) { final String S_ProcName = "CFAsteriskRamTld.readDerived"; CFInternetTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTldId(PKey.getRequiredTldId()); CFInternetTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetTldBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetTldPKey PKey) { final String S_ProcName = "CFAsteriskRamTld.readDerived"; CFInternetTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTldId(PKey.getRequiredTldId()); CFInternetTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetTldBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamTld.readAllDerived"; CFInternetTldBuff[] retList = new CFInternetTldBuff[dictByPKey.values().size()]; Iterator<CFInternetTldBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetTldBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAsteriskRamTld.readDerivedByTenantIdx"; CFInternetTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFInternetTldBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { Map<CFInternetTldPKey, CFInternetTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFInternetTldBuff[subdictTenantIdx.size()]; Iterator<CFInternetTldBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFInternetTldPKey, CFInternetTldBuff> subdictTenantIdx = new HashMap<CFInternetTldPKey, CFInternetTldBuff>(); dictByTenantIdx.put(key, subdictTenantIdx); recArray = new CFInternetTldBuff[0]; } return (recArray); } public CFInternetTldBuff readDerivedByNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAsteriskRamTld.readDerivedByNameIdx"; CFInternetTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFInternetTldBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFInternetTldBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long TldId) { final String S_ProcName = "CFAsteriskRamTld.readDerivedByIdIdx() "; CFInternetTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTldId(TldId); CFInternetTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetTldBuff readBuff(CFSecurityAuthorization Authorization, CFInternetTldPKey PKey) { final String S_ProcName = "CFAsteriskRamTld.readBuff"; CFInternetTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTld"))) { buff = null; } return (buff); } public CFInternetTldBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetTldPKey PKey) { final String S_ProcName = "lockBuff"; CFInternetTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTld"))) { buff = null; } return (buff); } public CFInternetTldBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamTld.readAllBuff"; CFInternetTldBuff buff; ArrayList<CFInternetTldBuff> filteredList = new ArrayList<CFInternetTldBuff>(); CFInternetTldBuff[] 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 CFInternetTldBuff[0])); } public CFInternetTldBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long TldId) { final String S_ProcName = "CFAsteriskRamTld.readBuffByIdIdx() "; CFInternetTldBuff buff = readDerivedByIdIdx(Authorization, TenantId, TldId); if ((buff != null) && buff.getClassCode().equals("GTld")) { return ((CFInternetTldBuff) buff); } else { return (null); } } public CFInternetTldBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAsteriskRamTld.readBuffByTenantIdx() "; CFInternetTldBuff buff; ArrayList<CFInternetTldBuff> filteredList = new ArrayList<CFInternetTldBuff>(); CFInternetTldBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("GTld")) { filteredList.add((CFInternetTldBuff) buff); } } return (filteredList.toArray(new CFInternetTldBuff[0])); } public CFInternetTldBuff readBuffByNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAsteriskRamTld.readBuffByNameIdx() "; CFInternetTldBuff buff = readDerivedByNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("GTld")) { return ((CFInternetTldBuff) buff); } else { return (null); } } public void updateTld(CFSecurityAuthorization Authorization, CFInternetTldBuff Buff) { CFInternetTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTldId(Buff.getRequiredTldId()); CFInternetTldBuff 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); CFInternetTldByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFInternetTldByTenantIdxKey newKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFInternetTldByNameIdxKey existingKeyNameIdx = schema.getFactoryTld().newNameIdxKey(); existingKeyNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFInternetTldByNameIdxKey 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 Map<CFInternetTldPKey, CFInternetTldBuff> 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 HashMap<CFInternetTldPKey, CFInternetTldBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByNameIdx.remove(existingKeyNameIdx); dictByNameIdx.put(newKeyNameIdx, Buff); } public void deleteTld(CFSecurityAuthorization Authorization, CFInternetTldBuff Buff) { final String S_ProcName = "CFAsteriskRamTldTable.deleteTld() "; CFInternetTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTldId(Buff.getRequiredTldId()); CFInternetTldBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTld", pkey); } CFInternetTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFInternetTldByNameIdxKey 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()); Map<CFInternetTldPKey, CFInternetTldBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByNameIdx.remove(keyNameIdx); } public void deleteTldByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argTldId) { CFInternetTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTldId(argTldId); deleteTldByIdIdx(Authorization, key); } public void deleteTldByIdIdx(CFSecurityAuthorization Authorization, CFInternetTldPKey argKey) { CFInternetTldBuff cur; LinkedList<CFInternetTldBuff> matchSet = new LinkedList<CFInternetTldBuff>(); Iterator<CFInternetTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFInternetTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTldByTenantIdx(Authorization, key); } public void deleteTldByTenantIdx(CFSecurityAuthorization Authorization, CFInternetTldByTenantIdxKey argKey) { CFInternetTldBuff cur; LinkedList<CFInternetTldBuff> matchSet = new LinkedList<CFInternetTldBuff>(); Iterator<CFInternetTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByNameIdx(CFSecurityAuthorization Authorization, long argTenantId, String argName) { CFInternetTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTldByNameIdx(Authorization, key); } public void deleteTldByNameIdx(CFSecurityAuthorization Authorization, CFInternetTldByNameIdxKey argKey) { CFInternetTldBuff cur; LinkedList<CFInternetTldBuff> matchSet = new LinkedList<CFInternetTldBuff>(); Iterator<CFInternetTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public CFSecurityCursor openTldCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamTldCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openTldCursorByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { CFSecurityCursor cursor; CFInternetTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { Map<CFInternetTldPKey, CFInternetTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAsteriskRamTldCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAsteriskRamTldCursor(Authorization, schema, new ArrayList<CFInternetTldBuff>()); } return (cursor); } public void closeTldCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetTldBuff nextTldCursor(CFSecurityCursor Cursor) { CFAsteriskRamTldCursor cursor = (CFAsteriskRamTldCursor) Cursor; CFInternetTldBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetTldBuff prevTldCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFInternetTldBuff firstTldCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetTldBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFInternetTldBuff lastTldCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTldCursor"); } public CFInternetTldBuff nthTldCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }