net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.CFAstRamRealProjectTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.CFAstRamRealProjectTable.java

Source

// Description: Java7 in-memory RAM DbIO implementation for RealProject.

/*
 *   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.*;

/*
 *   CFAstRamRealProjectTable in-memory RAM DbIO implementation
 *   for RealProject.
 */
public class CFAstRamRealProjectTable implements ICFAstRealProjectTable {
    private CFAstRamSchema schema;
    private Map<CFAstDomainBasePKey, CFAstRealProjectBuff> dictByPKey = new HashMap<CFAstDomainBasePKey, CFAstRealProjectBuff>();

    public CFAstRamRealProjectTable(CFAstRamSchema argSchema) {
        schema = argSchema;
    }

    public void createRealProject(CFAstAuthorization Authorization, CFAstRealProjectBuff Buff) {
        schema.getTableProjectBase().createProjectBase(Authorization, Buff);
        CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey();
        pkey.setClassCode(Buff.getClassCode());
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(Buff.getRequiredId());
        // Validate unique indexes

        if (dictByPKey.containsKey(pkey)) {
            throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createRealProject",
                    pkey);
        }

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableProjectBase().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredTenantId(), Buff.getRequiredId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createRealProject", "Superclass", "SuperClass", "ProjectBase", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

    }

    public CFAstRealProjectBuff readDerived(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) {
        final String S_ProcName = "CFAstRamRealProject.readDerived() ";
        CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredId(PKey.getRequiredId());
        CFAstRealProjectBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstRealProjectBuff lockDerived(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) {
        final String S_ProcName = "CFAstRamRealProject.readDerived() ";
        CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredId(PKey.getRequiredId());
        CFAstRealProjectBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstRealProjectBuff[] readAllDerived(CFAstAuthorization Authorization) {
        final String S_ProcName = "CFAstRamRealProject.readAllDerived() ";
        CFAstRealProjectBuff[] retList = new CFAstRealProjectBuff[dictByPKey.values().size()];
        Iterator<CFAstRealProjectBuff> iter = dictByPKey.values().iterator();
        int idx = 0;
        while (iter.hasNext()) {
            retList[idx++] = iter.next();
        }
        return (retList);
    }

    public CFAstRealProjectBuff[] readDerivedByTenantIdx(CFAstAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAstRamDomainBase.readDerivedByTenantIdx() ";
        CFAstDomainBaseBuff buffList[] = schema.getTableDomainBase().readDerivedByTenantIdx(Authorization,
                TenantId);
        if (buffList == null) {
            return (null);
        } else {
            CFAstDomainBaseBuff buff;
            ArrayList<CFAstRealProjectBuff> filteredList = new ArrayList<CFAstRealProjectBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof CFAstRealProjectBuff)) {
                    filteredList.add((CFAstRealProjectBuff) buff);
                }
            }
            return (filteredList.toArray(new CFAstRealProjectBuff[0]));
        }
    }

    public CFAstRealProjectBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long TenantId, long Id) {
        final String S_ProcName = "CFAstRamDomainBase.readDerivedByIdIdx() ";
        CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredId(Id);

        CFAstRealProjectBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstRealProjectBuff readBuff(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) {
        final String S_ProcName = "CFAstRamRealProject.readBuff() ";
        CFAstRealProjectBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("RPRJ"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstRealProjectBuff lockBuff(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) {
        final String S_ProcName = "CFAstRamRealProject.readBuff() ";
        CFAstRealProjectBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("RPRJ"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstRealProjectBuff[] readAllBuff(CFAstAuthorization Authorization) {
        final String S_ProcName = "CFAstRamRealProject.readAllBuff() ";
        CFAstRealProjectBuff buff;
        ArrayList<CFAstRealProjectBuff> filteredList = new ArrayList<CFAstRealProjectBuff>();
        CFAstRealProjectBuff[] buffList = readAllDerived(Authorization);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("RPRJ")) {
                filteredList.add(buff);
            }
        }
        return (filteredList.toArray(new CFAstRealProjectBuff[0]));
    }

    public CFAstRealProjectBuff readBuffByIdIdx(CFAstAuthorization Authorization, long TenantId, long Id) {
        final String S_ProcName = "CFAstRamDomainBase.readBuffByIdIdx() ";
        CFAstDomainBaseBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id);
        if ((buff != null) && buff.getClassCode().equals("BDOM")) {
            return ((CFAstRealProjectBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstRealProjectBuff[] readBuffByTenantIdx(CFAstAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAstRamDomainBase.readBuffByTenantIdx() ";
        CFAstDomainBaseBuff buff;
        ArrayList<CFAstRealProjectBuff> filteredList = new ArrayList<CFAstRealProjectBuff>();
        CFAstDomainBaseBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("BDOM")) {
                filteredList.add((CFAstRealProjectBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAstRealProjectBuff[0]));
    }

    public void updateRealProject(CFAstAuthorization Authorization, CFAstRealProjectBuff Buff) {
        schema.getTableProjectBase().updateProjectBase(Authorization, Buff);
        CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstRealProjectBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateRealProject",
                    "Existing record not found", "RealProject", pkey);
        }
        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableProjectBase().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredTenantId(), Buff.getRequiredId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateRealProject", "Superclass", "SuperClass", "ProjectBase", null);
                }
            }
        }

        // Update is valid

        SortedMap<CFAstDomainBasePKey, CFAstRealProjectBuff> subdict;

        dictByPKey.remove(pkey);
        dictByPKey.put(pkey, Buff);

    }

    public void deleteRealProject(CFAstAuthorization Authorization, CFAstRealProjectBuff Buff) {
        final String S_ProcName = "CFAstRamRealProjectTable.deleteRealProject() ";
        CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstRealProjectBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteRealProject",
                    pkey);
        }

        // Validate reverse foreign keys

        if (schema.getTableTopProject().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(),
                existing.getRequiredId()) != null) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteRealProject",
                    "Superclass", "SuperClass", "TopProject", pkey);
        }

        if (schema.getTableSubProject().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(),
                existing.getRequiredId()) != null) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteRealProject",
                    "Superclass", "SuperClass", "SubProject", pkey);
        }

        // Delete is valid

        schema.getTableMajorVersion().deleteMajorVersionByPPrjIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredId());
        SortedMap<CFAstDomainBasePKey, CFAstRealProjectBuff> subdict;

        dictByPKey.remove(pkey);

        schema.getTableProjectBase().deleteProjectBase(Authorization, Buff);
    }

    public void deleteRealProjectByIdIdx(CFAstAuthorization Authorization, long argTenantId, long argId) {
        CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredId(argId);
        deleteRealProjectByIdIdx(Authorization, key);
    }

    public void deleteRealProjectByIdIdx(CFAstAuthorization Authorization, CFAstDomainBasePKey argKey) {
        final String S_ProcName = "deleteRealProjectByIdIdx";
        CFAstRealProjectBuff cur;
        LinkedList<CFAstRealProjectBuff> matchSet = new LinkedList<CFAstRealProjectBuff>();
        Iterator<CFAstRealProjectBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstRealProjectBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            String subClassCode = cur.getClassCode();
            if ("RPRJ".equals(subClassCode)) {
                schema.getTableRealProject().deleteRealProject(Authorization, cur);
            } else if ("TPRJ".equals(subClassCode)) {
                schema.getTableTopProject().deleteTopProject(Authorization, (CFAstTopProjectBuff) cur);
            } else if ("SPRJ".equals(subClassCode)) {
                schema.getTableSubProject().deleteSubProject(Authorization, (CFAstSubProjectBuff) cur);
            } else {
                throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                        "subClassCode", cur,
                        "Instance of or subclass of RealProject must not be \"" + subClassCode + "\"");
            }
        }
    }

    public void deleteRealProjectByTenantIdx(CFAstAuthorization Authorization, long argTenantId) {
        CFAstDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey();
        key.setRequiredTenantId(argTenantId);
        deleteRealProjectByTenantIdx(Authorization, key);
    }

    public void deleteRealProjectByTenantIdx(CFAstAuthorization Authorization,
            CFAstDomainBaseByTenantIdxKey argKey) {
        final String S_ProcName = "deleteRealProjectByTenantIdx";
        CFAstRealProjectBuff cur;
        LinkedList<CFAstRealProjectBuff> matchSet = new LinkedList<CFAstRealProjectBuff>();
        Iterator<CFAstRealProjectBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstRealProjectBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            String subClassCode = cur.getClassCode();
            if ("RPRJ".equals(subClassCode)) {
                schema.getTableRealProject().deleteRealProject(Authorization, cur);
            } else if ("TPRJ".equals(subClassCode)) {
                schema.getTableTopProject().deleteTopProject(Authorization, (CFAstTopProjectBuff) cur);
            } else if ("SPRJ".equals(subClassCode)) {
                schema.getTableSubProject().deleteSubProject(Authorization, (CFAstSubProjectBuff) cur);
            } else {
                throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                        "subClassCode", cur,
                        "Instance of or subclass of RealProject must not be \"" + subClassCode + "\"");
            }
        }
    }

    public CFAstCursor openRealProjectCursorAll(CFAstAuthorization Authorization) {
        CFAstCursor cursor = new CFAstRamRealProjectCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public void closeRealProjectCursor(CFAstCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public CFAstRealProjectBuff nextRealProjectCursor(CFAstCursor Cursor) {
        CFAstRamRealProjectCursor cursor = (CFAstRamRealProjectCursor) Cursor;
        CFAstRealProjectBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public CFAstRealProjectBuff prevRealProjectCursor(CFAstCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        CFAstRealProjectBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextRealProjectCursor(Cursor);
        }
        return (rec);
    }

    public CFAstRealProjectBuff firstRealProjectCursor(CFAstCursor Cursor) {
        int targetRowIdx = 1;
        CFAstRealProjectBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextRealProjectCursor(Cursor);
        }
        return (rec);
    }

    public CFAstRealProjectBuff lastRealProjectCursor(CFAstCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastRealProjectCursor");
    }

    public CFAstRealProjectBuff nthRealProjectCursor(CFAstCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAstRealProjectBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextRealProjectCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}