Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecUser. /* * 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.*; /* * CFAstRamSecUserTable in-memory RAM DbIO implementation * for SecUser. */ public class CFAstRamSecUserTable implements ICFAstSecUserTable { private CFAstRamSchema schema; private Map<CFAstSecUserPKey, CFAstSecUserBuff> dictByPKey = new HashMap<CFAstSecUserPKey, CFAstSecUserBuff>(); private SortedMap<CFAstSecUserByUEMailIdxKey, CFAstSecUserBuff> dictByUEMailIdx = new TreeMap<CFAstSecUserByUEMailIdxKey, CFAstSecUserBuff>(); public CFAstRamSecUserTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) { CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(schema.nextSecUserIdGen()); Buff.setRequiredSecUserId(pkey.getRequiredSecUserId()); CFAstSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); keyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createSecUser", pkey); } if (dictByUEMailIdx.containsKey(keyUEMailIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecUser", "SecUserUEMailAddrIdx", keyUEMailIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByUEMailIdx.put(keyUEMailIdx, Buff); } public CFAstSecUserBuff readDerived(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) { final String S_ProcName = "CFAstRamSecUser.readDerived() "; CFAstSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFAstSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecUserBuff lockDerived(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) { final String S_ProcName = "CFAstRamSecUser.readDerived() "; CFAstSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFAstSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecUserBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecUser.readAllDerived() "; CFAstSecUserBuff[] retList = new CFAstSecUserBuff[dictByPKey.values().size()]; Iterator<CFAstSecUserBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstSecUserBuff readDerivedByUEMailIdx(CFAstAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFAstRamSecUser.readDerivedByUEMailIdx() "; CFAstSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(EMailAddress); CFAstSecUserBuff buff; if (dictByUEMailIdx.containsKey(key)) { buff = dictByUEMailIdx.get(key); } else { buff = null; } return (buff); } public CFAstSecUserBuff readDerivedByIdIdx(CFAstAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFAstRamSecUser.readDerivedByIdIdx() "; CFAstSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(SecUserId); CFAstSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecUserBuff readBuff(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) { final String S_ProcName = "CFAstRamSecUser.readBuff() "; CFAstSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFAstSecUserBuff lockBuff(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) { final String S_ProcName = "CFAstRamSecUser.readBuff() "; CFAstSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFAstSecUserBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecUser.readAllBuff() "; CFAstSecUserBuff buff; ArrayList<CFAstSecUserBuff> filteredList = new ArrayList<CFAstSecUserBuff>(); CFAstSecUserBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SUSR")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstSecUserBuff[0])); } public CFAstSecUserBuff readBuffByIdIdx(CFAstAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFAstRamSecUser.readBuffByIdIdx() "; CFAstSecUserBuff buff = readDerivedByIdIdx(Authorization, SecUserId); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFAstSecUserBuff) buff); } else { return (null); } } public CFAstSecUserBuff readBuffByUEMailIdx(CFAstAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFAstRamSecUser.readBuffByUEMailIdx() "; CFAstSecUserBuff buff = readDerivedByUEMailIdx(Authorization, EMailAddress); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFAstSecUserBuff) buff); } else { return (null); } } public void updateSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) { CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFAstSecUserBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecUser", "Existing record not found", "SecUser", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecUser", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstSecUserByUEMailIdxKey existingKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); existingKeyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress()); CFAstSecUserByUEMailIdxKey newKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); newKeyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress()); // Check unique indexes if (!existingKeyUEMailIdx.equals(newKeyUEMailIdx)) { if (dictByUEMailIdx.containsKey(newKeyUEMailIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateSecUser", "SecUserUEMailAddrIdx", newKeyUEMailIdx); } } // Validate foreign keys // Update is valid SortedMap<CFAstSecUserPKey, CFAstSecUserBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUEMailIdx.remove(existingKeyUEMailIdx); dictByUEMailIdx.put(newKeyUEMailIdx, Buff); } public void deleteSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) { final String S_ProcName = "CFAstRamSecUserTable.deleteSecUser() "; CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFAstSecUserBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecUser", pkey); } CFAstSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); keyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress()); // Validate reverse foreign keys // Delete is valid schema.getTableSecSession().deleteSecSessionBySecUserIdx(Authorization, Buff.getRequiredSecUserId()); schema.getTableSecGroupMember().deleteSecGroupMemberByUserIdx(Authorization, Buff.getRequiredSecUserId()); schema.getTableTSecGroupMember().deleteTSecGroupMemberByUserIdx(Authorization, Buff.getRequiredSecUserId()); SortedMap<CFAstSecUserPKey, CFAstSecUserBuff> subdict; dictByPKey.remove(pkey); dictByUEMailIdx.remove(keyUEMailIdx); } public void deleteSecUserByIdIdx(CFAstAuthorization Authorization, UUID argSecUserId) { CFAstSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(argSecUserId); deleteSecUserByIdIdx(Authorization, key); } public void deleteSecUserByIdIdx(CFAstAuthorization Authorization, CFAstSecUserPKey argKey) { CFAstSecUserBuff cur; LinkedList<CFAstSecUserBuff> matchSet = new LinkedList<CFAstSecUserBuff>(); Iterator<CFAstSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public void deleteSecUserByUEMailIdx(CFAstAuthorization Authorization, String argEMailAddress) { CFAstSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(argEMailAddress); deleteSecUserByUEMailIdx(Authorization, key); } public void deleteSecUserByUEMailIdx(CFAstAuthorization Authorization, CFAstSecUserByUEMailIdxKey argKey) { CFAstSecUserBuff cur; LinkedList<CFAstSecUserBuff> matchSet = new LinkedList<CFAstSecUserBuff>(); Iterator<CFAstSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public CFAstCursor openSecUserCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamSecUserCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeSecUserCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstSecUserBuff nextSecUserCursor(CFAstCursor Cursor) { CFAstRamSecUserCursor cursor = (CFAstRamSecUserCursor) Cursor; CFAstSecUserBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstSecUserBuff prevSecUserCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFAstSecUserBuff firstSecUserCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstSecUserBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFAstSecUserBuff lastSecUserCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecUserCursor"); } public CFAstSecUserBuff nthSecUserCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }