Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecUser. /* * CF Customer Information Managerment template model * * Copyright (c) 2010-2014 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. * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfinternet.v2_0.CFInternetRam; 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.cfinternet.v2_0.CFInternet.*; import net.sourceforge.msscodefactory.cfinternet.v2_0.CFInternetRam.*; /* * CFInternetRamSecUserTable in-memory RAM DbIO implementation * for SecUser. */ public class CFInternetRamSecUserTable implements ICFInternetSecUserTable { private CFInternetRamSchema schema; private Map<CFInternetSecUserPKey, CFInternetSecUserBuff> dictByPKey = new HashMap<CFInternetSecUserPKey, CFInternetSecUserBuff>(); private SortedMap<CFInternetSecUserByUEMailIdxKey, CFInternetSecUserBuff> dictByUEMailIdx = new TreeMap<CFInternetSecUserByUEMailIdxKey, CFInternetSecUserBuff>(); public CFInternetRamSecUserTable(CFInternetRamSchema argSchema) { schema = argSchema; } public void createSecUser(CFInternetAuthorization Authorization, CFInternetSecUserBuff Buff) { CFInternetSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(schema.nextSecUserIdGen()); Buff.setRequiredSecUserId(pkey.getRequiredSecUserId()); CFInternetSecUserByUEMailIdxKey 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 CFInternetSecUserBuff readDerived(CFInternetAuthorization Authorization, CFInternetSecUserPKey PKey) { final String S_ProcName = "CFInternetRamSecUser.readDerived() "; CFInternetSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFInternetSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetSecUserBuff lockDerived(CFInternetAuthorization Authorization, CFInternetSecUserPKey PKey) { final String S_ProcName = "CFInternetRamSecUser.readDerived() "; CFInternetSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); CFInternetSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetSecUserBuff[] readAllDerived(CFInternetAuthorization Authorization) { final String S_ProcName = "CFInternetRamSecUser.readAllDerived() "; CFInternetSecUserBuff[] retList = new CFInternetSecUserBuff[dictByPKey.values().size()]; Iterator<CFInternetSecUserBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetSecUserBuff readDerivedByUEMailIdx(CFInternetAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFInternetRamSecUser.readDerivedByUEMailIdx() "; CFInternetSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(EMailAddress); CFInternetSecUserBuff buff; if (dictByUEMailIdx.containsKey(key)) { buff = dictByUEMailIdx.get(key); } else { buff = null; } return (buff); } public CFInternetSecUserBuff readDerivedByIdIdx(CFInternetAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFInternetRamSecUser.readDerivedByIdIdx() "; CFInternetSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(SecUserId); CFInternetSecUserBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetSecUserBuff readBuff(CFInternetAuthorization Authorization, CFInternetSecUserPKey PKey) { final String S_ProcName = "CFInternetRamSecUser.readBuff() "; CFInternetSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFInternetSecUserBuff lockBuff(CFInternetAuthorization Authorization, CFInternetSecUserPKey PKey) { final String S_ProcName = "CFInternetRamSecUser.readBuff() "; CFInternetSecUserBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) { buff = null; } return (buff); } public CFInternetSecUserBuff[] readAllBuff(CFInternetAuthorization Authorization) { final String S_ProcName = "CFInternetRamSecUser.readAllBuff() "; CFInternetSecUserBuff buff; ArrayList<CFInternetSecUserBuff> filteredList = new ArrayList<CFInternetSecUserBuff>(); CFInternetSecUserBuff[] 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 CFInternetSecUserBuff[0])); } public CFInternetSecUserBuff readBuffByIdIdx(CFInternetAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFInternetRamSecUser.readBuffByIdIdx() "; CFInternetSecUserBuff buff = readDerivedByIdIdx(Authorization, SecUserId); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFInternetSecUserBuff) buff); } else { return (null); } } public CFInternetSecUserBuff readBuffByUEMailIdx(CFInternetAuthorization Authorization, String EMailAddress) { final String S_ProcName = "CFInternetRamSecUser.readBuffByUEMailIdx() "; CFInternetSecUserBuff buff = readDerivedByUEMailIdx(Authorization, EMailAddress); if ((buff != null) && buff.getClassCode().equals("SUSR")) { return ((CFInternetSecUserBuff) buff); } else { return (null); } } public void updateSecUser(CFInternetAuthorization Authorization, CFInternetSecUserBuff Buff) { CFInternetSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFInternetSecUserBuff 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); CFInternetSecUserByUEMailIdxKey existingKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey(); existingKeyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress()); CFInternetSecUserByUEMailIdxKey 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<CFInternetSecUserPKey, CFInternetSecUserBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUEMailIdx.remove(existingKeyUEMailIdx); dictByUEMailIdx.put(newKeyUEMailIdx, Buff); } public void deleteSecUser(CFInternetAuthorization Authorization, CFInternetSecUserBuff Buff) { final String S_ProcName = "CFInternetRamSecUserTable.deleteSecUser() "; CFInternetSecUserPKey pkey = schema.getFactorySecUser().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); CFInternetSecUserBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecUser", pkey); } CFInternetSecUserByUEMailIdxKey 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<CFInternetSecUserPKey, CFInternetSecUserBuff> subdict; dictByPKey.remove(pkey); dictByUEMailIdx.remove(keyUEMailIdx); } public void deleteSecUserByIdIdx(CFInternetAuthorization Authorization, UUID argSecUserId) { CFInternetSecUserPKey key = schema.getFactorySecUser().newPKey(); key.setRequiredSecUserId(argSecUserId); deleteSecUserByIdIdx(Authorization, key); } public void deleteSecUserByIdIdx(CFInternetAuthorization Authorization, CFInternetSecUserPKey argKey) { CFInternetSecUserBuff cur; LinkedList<CFInternetSecUserBuff> matchSet = new LinkedList<CFInternetSecUserBuff>(); Iterator<CFInternetSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public void deleteSecUserByUEMailIdx(CFInternetAuthorization Authorization, String argEMailAddress) { CFInternetSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey(); key.setRequiredEMailAddress(argEMailAddress); deleteSecUserByUEMailIdx(Authorization, key); } public void deleteSecUserByUEMailIdx(CFInternetAuthorization Authorization, CFInternetSecUserByUEMailIdxKey argKey) { CFInternetSecUserBuff cur; LinkedList<CFInternetSecUserBuff> matchSet = new LinkedList<CFInternetSecUserBuff>(); Iterator<CFInternetSecUserBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetSecUserBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecUser(Authorization, cur); } } public CFInternetCursor openSecUserCursorAll(CFInternetAuthorization Authorization) { CFInternetCursor cursor = new CFInternetRamSecUserCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeSecUserCursor(CFInternetCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetSecUserBuff nextSecUserCursor(CFInternetCursor Cursor) { CFInternetRamSecUserCursor cursor = (CFInternetRamSecUserCursor) Cursor; CFInternetSecUserBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetSecUserBuff prevSecUserCursor(CFInternetCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFInternetSecUserBuff firstSecUserCursor(CFInternetCursor Cursor) { int targetRowIdx = 1; CFInternetSecUserBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public CFInternetSecUserBuff lastSecUserCursor(CFInternetCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecUserCursor"); } public CFInternetSecUserBuff nthSecUserCursor(CFInternetCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetSecUserBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecUserCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }