Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecUser. /* * 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.*; /* * CFAccRamSecUserTable in-memory RAM DbIO implementation * for SecUser. */ public class CFAccRamSecUserTable implements ICFAccSecUserTable { private CFAccRamSchema schema; private Map<CFAccSecUserPKey, CFAccSecUserBuff> dictByPKey = new HashMap<CFAccSecUserPKey, CFAccSecUserBuff>(); private SortedMap<CFAccSecUserByUEMailIdxKey, CFAccSecUserBuff> dictByUEMailIdx = new TreeMap<CFAccSecUserByUEMailIdxKey, CFAccSecUserBuff>(); public CFAccRamSecUserTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createSecUser(CFAccAuthorization Authorization, CFAccSecUserBuff Buff) { CFAccSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(schema.nextSecUserIdGen()); Buff.setRequiredSecUserId(pkey.getRequiredSecUserId()); CFAccSecUserByUEMailIdxKey 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 CFAccSecUserBuff readDerived(CFAccAuthorization Authorization, CFAccSecUserPKey PKey) { final String S_ProcName = "CFAccRamSecUser.readDerived() "; CFAccSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFAccSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecUserBuff lockDerived(CFAccAuthorization Authorization, CFAccSecUserPKey PKey) { final String S_ProcName = "CFAccRamSecUser.readDerived() "; CFAccSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFAccSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecUserBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecUser.readAllDerived() "; CFAccSecUserBuff[] retList = new CFAccSecUserBuff[dictByPKey.values().size()]; Iterator<CFAccSecUserBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccSecUserBuff readDerivedByUEMailIdx(CFAccAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFAccRamSecUser.readDerivedByUEMailIdx() "; CFAccSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(EMailAddress); CFAccSecUserBuff buff; if (dictByUEMailIdx.containsKey(key)) { buff = dictByUEMailIdx.get(key); } else { buff = null; } return (buff); } public CFAccSecUserBuff readDerivedByIdIdx(CFAccAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFAccRamSecUser.readDerivedByIdIdx() "; CFAccSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(SecUserId); CFAccSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecUserBuff readBuff(CFAccAuthorization Authorization, CFAccSecUserPKey PKey) { final String S_ProcName = "CFAccRamSecUser.readBuff() "; CFAccSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFAccSecUserBuff lockBuff(CFAccAuthorization Authorization, CFAccSecUserPKey PKey) { final String S_ProcName = "CFAccRamSecUser.readBuff() "; CFAccSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFAccSecUserBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecUser.readAllBuff() "; CFAccSecUserBuff buff; ArrayList<CFAccSecUserBuff> filteredList = new ArrayList<CFAccSecUserBuff>(); CFAccSecUserBuff[] 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 CFAccSecUserBuff[0])); } public CFAccSecUserBuff readBuffByIdIdx(CFAccAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFAccRamSecUser.readBuffByIdIdx() "; CFAccSecUserBuff buff = readDerivedByIdIdx(Authorization, SecUserId); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFAccSecUserBuff) buff); } else { return (null); } } public CFAccSecUserBuff readBuffByUEMailIdx(CFAccAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFAccRamSecUser.readBuffByUEMailIdx() "; CFAccSecUserBuff buff = readDerivedByUEMailIdx(Authorization, EMailAddress); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFAccSecUserBuff) buff); } else { return (null); } } public void updateSecUser(CFAccAuthorization Authorization, CFAccSecUserBuff Buff) { CFAccSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFAccSecUserBuff 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); CFAccSecUserByUEMailIdxKey existingKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); existingKeyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress()); CFAccSecUserByUEMailIdxKey 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<CFAccSecUserPKey, CFAccSecUserBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUEMailIdx.remove(existingKeyUEMailIdx); dictByUEMailIdx.put(newKeyUEMailIdx, Buff); } public void deleteSecUser(CFAccAuthorization Authorization, CFAccSecUserBuff Buff) { final String S_ProcName = "CFAccRamSecUserTable.deleteSecUser() "; CFAccSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFAccSecUserBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecUser", pkey); } CFAccSecUserByUEMailIdxKey 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<CFAccSecUserPKey, CFAccSecUserBuff> subdict; dictByPKey.remove(pkey); dictByUEMailIdx.remove(keyUEMailIdx); } public void deleteSecUserByIdIdx(CFAccAuthorization Authorization, UUID argSecUserId) { CFAccSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(argSecUserId); deleteSecUserByIdIdx(Authorization, key); } public void deleteSecUserByIdIdx(CFAccAuthorization Authorization, CFAccSecUserPKey argKey) { CFAccSecUserBuff cur; LinkedList<CFAccSecUserBuff> matchSet = new LinkedList<CFAccSecUserBuff>(); Iterator<CFAccSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public void deleteSecUserByUEMailIdx(CFAccAuthorization Authorization, String argEMailAddress) { CFAccSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(argEMailAddress); deleteSecUserByUEMailIdx(Authorization, key); } public void deleteSecUserByUEMailIdx(CFAccAuthorization Authorization, CFAccSecUserByUEMailIdxKey argKey) { CFAccSecUserBuff cur; LinkedList<CFAccSecUserBuff> matchSet = new LinkedList<CFAccSecUserBuff>(); Iterator<CFAccSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public CFAccCursor openSecUserCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamSecUserCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeSecUserCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccSecUserBuff nextSecUserCursor(CFAccCursor Cursor) { CFAccRamSecUserCursor cursor = (CFAccRamSecUserCursor) Cursor; CFAccSecUserBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccSecUserBuff prevSecUserCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFAccSecUserBuff firstSecUserCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccSecUserBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFAccSecUserBuff lastSecUserCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecUserCursor"); } public CFAccSecUserBuff nthSecUserCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }