Search in sources :

Example 51 with ObjectNotFoundException

use of org.vcell.util.ObjectNotFoundException in project vcell by virtualcell.

the class GeomDbDriver method getPixelClass.

/**
 * This method was created in VisualAge.
 * @param vcImage cbit.image.VCImage
 */
private VCPixelClass getPixelClass(QueryHashtable dbc, Connection con, KeyValue imageRegionKey) throws SQLException, DataAccessException {
    // 
    // check object cache first
    // 
    VCPixelClass vcPixelClass = (VCPixelClass) dbc.get(imageRegionKey);
    if (vcPixelClass != null) {
        return vcPixelClass;
    }
    String sql;
    sql = " SELECT *" + " FROM  " + imageRegionTable.getTableName() + " WHERE " + imageRegionTable.id + " = " + imageRegionKey;
    // 
    // System.out.println(sql);
    // 
    // Connection con = conFact.getConnection();
    Statement stmt = con.createStatement();
    try {
        ResultSet rset = stmt.executeQuery(sql);
        // 
        if (rset.next()) {
            KeyValue vcpcKey = new KeyValue(rset.getBigDecimal(imageRegionTable.id.toString()));
            String rName = rset.getString(imageRegionTable.regionName.toString()).trim();
            int pixValue = rset.getInt(imageRegionTable.pixelValue.toString());
            vcPixelClass = new VCPixelClass(vcpcKey, rName, pixValue);
            dbc.put(vcpcKey, vcPixelClass);
        } else {
            throw new ObjectNotFoundException("ImageRegion(" + imageRegionKey + ") not found");
        }
        return vcPixelClass;
    } finally {
        // Release resources include resultset
        stmt.close();
    }
}
Also used : VCPixelClass(cbit.image.VCPixelClass) KeyValue(org.vcell.util.document.KeyValue) Statement(java.sql.Statement) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResultSet(java.sql.ResultSet)

Example 52 with ObjectNotFoundException

use of org.vcell.util.ObjectNotFoundException in project vcell by virtualcell.

the class GeomDbDriver method getGeometry.

/**
 * getModel method comment.
 */
private Geometry getGeometry(QueryHashtable dbc, Connection con, User user, KeyValue geomKey, boolean bCheckPermission) throws SQLException, DataAccessException, ObjectNotFoundException {
    if (user == null || geomKey == null) {
        throw new IllegalArgumentException("Improper parameters for getGeometry");
    }
    // log.print("GeomDbDriver.getGeometry(user="+user+", id="+geomKey+")");
    String sql;
    Field[] f = { new StarField(geomTable), userTable.userid, extentTable.extentX, extentTable.extentY, extentTable.extentZ };
    Table[] t = { geomTable, userTable, extentTable };
    String condition = geomTable.id.getQualifiedColName() + " = " + geomKey + " AND " + userTable.id.getQualifiedColName() + " = " + geomTable.ownerRef.getQualifiedColName() + " AND " + extentTable.id.getQualifiedColName() + " = " + geomTable.extentRef.getQualifiedColName();
    sql = DatabasePolicySQL.enforceOwnershipSelect(user, f, t, (OuterJoin) null, condition, null, dbSyntax, bCheckPermission);
    // System.out.println(sql);
    Geometry geom = null;
    // Connection con = conFact.getConnection();
    Statement stmt = con.createStatement();
    try {
        ResultSet rset = stmt.executeQuery(sql);
        if (rset.next()) {
            // This geometry privacy flag gives this user permission to access
            geom = getGeometry(dbc, con, user, rset);
        } else {
            // see if at least 1 geometry parents (Mathmodels and/or BioModels) are shared with this user
            if (bCheckPermission) {
                rset.close();
                String parentSQL = GeometryTable.getParentsPermissionSQL(geomKey, user);
                rset = stmt.executeQuery(parentSQL);
                if (rset.next()) {
                    // At least 1 parent of the geometry exists that's shared to this user so give them the geometry
                    rset.close();
                    // Get the geometry without checking the geometry permission
                    sql = DatabasePolicySQL.enforceOwnershipSelect(user, f, t, (OuterJoin) null, condition, null, dbSyntax, false);
                    rset = stmt.executeQuery(sql);
                    if (rset.next()) {
                        geom = getGeometry(dbc, con, user, rset);
                    }
                }
            }
            if (geom == null) {
                throw new ObjectNotFoundException("Geometry id=" + geomKey + " not found for user '" + user + "'");
            }
        }
    } finally {
        // Release resources include resultset
        stmt.close();
    }
    return geom;
}
Also used : StarField(cbit.sql.StarField) Geometry(cbit.vcell.geometry.Geometry) StarField(cbit.sql.StarField) Field(cbit.sql.Field) Table(cbit.sql.Table) Statement(java.sql.Statement) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) OuterJoin(cbit.vcell.modeldb.DatabasePolicySQL.OuterJoin) ResultSet(java.sql.ResultSet)

Example 53 with ObjectNotFoundException

use of org.vcell.util.ObjectNotFoundException in project vcell by virtualcell.

the class ServerDocumentManager method saveSimulation.

/**
 * Insert the method's description here.
 * Creation date: (10/28/00 12:08:30 AM)
 * @deprecated for testing purposes only.
 */
public String saveSimulation(QueryHashtable dbc, User user, String simulationXML, boolean bForceIndependent) throws DataAccessException, java.sql.SQLException, java.beans.PropertyVetoException, cbit.vcell.xml.XmlParseException {
    // 
    // this invokes "update" on the database layer
    // 
    Simulation simulation = XmlHelper.XMLToSim(simulationXML);
    forceDeepDirtyIfForeign(user, simulation);
    Version oldVersion = simulation.getVersion();
    Simulation origSimulation = null;
    if (oldVersion != null) {
        try {
            origSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, oldVersion.getVersionKey());
        } catch (ObjectNotFoundException e) {
        }
    }
    boolean bSomethingChanged = false;
    // 
    // UPDATE AND SUBSTITUTE FROM BOTTOM UP
    // 
    // Image->Geometry
    // Geometry->MathDescription
    // MathDescription->Simulation
    // 
    // 
    // if this simulation has an image:
    // save if necessary (only once) and store saved instance in hashTable
    // 
    Hashtable<Versionable, Versionable> memoryToDatabaseHash = new Hashtable<Versionable, Versionable>();
    {
        VCImage memoryImage = simulation.getMathDescription().getGeometry().getGeometrySpec().getImage();
        if (memoryImage != null) {
            // defaults to unchanged
            memoryToDatabaseHash.put(memoryImage, memoryImage);
            if (memoryImage.getKey() != null && memoryImage.getVersion().getName().equals(memoryImage.getName())) {
                // 
                // if image had previously been saved, not been forced 'dirty', and name not changed
                // compare with original image to see if "update" is required.
                // 
                VCImage databaseImage = null;
                if (origSimulation != null) {
                    VCImage origImage = origSimulation.getMathDescription().getGeometry().getGeometrySpec().getImage();
                    if (origImage != null && origImage.getKey().equals(memoryImage.getKey())) {
                        databaseImage = origImage;
                    }
                }
                if (databaseImage == null) {
                    // 
                    // saved image not found in origMathModel (too bad), get from database.
                    // 
                    databaseImage = dbServer.getDBTopLevel().getVCImage(dbc, user, memoryImage.getKey(), false);
                }
                if (databaseImage != null && !databaseImage.compareEqual(memoryImage)) {
                    KeyValue updatedImageKey = dbServer.getDBTopLevel().updateVersionable(user, memoryImage, false, true);
                    VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
                    memoryToDatabaseHash.put(memoryImage, updatedImage);
                    bSomethingChanged = true;
                }
            } else {
                // 
                // Image hasn't been saved, has been renamed, or has been forced 'dirty'
                // insert it with a unique name
                // 
                int count = 0;
                fixNullImageName(memoryImage);
                while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.VCImage, memoryImage.getName(), true)) {
                    try {
                        memoryImage.setName(TokenMangler.getNextRandomToken(memoryImage.getName()));
                    } catch (java.beans.PropertyVetoException e) {
                        e.printStackTrace(System.out);
                    }
                    if (count++ > 5) {
                        throw new DataAccessException("failed to find unique image name '" + memoryImage.getName() + "' is last name tried");
                    }
                }
                KeyValue updatedImageKey = dbServer.getDBTopLevel().insertVersionable(user, memoryImage, memoryImage.getName(), false, true);
                VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
                memoryToDatabaseHash.put(memoryImage, updatedImage);
                bSomethingChanged = true;
            }
        }
    }
    // 
    // for the Geometry:
    // substitute saved Image into Geometry and
    // save Geometry if necessary (only once) and store saved instance in hashtable.
    // 
    {
        Geometry memoryGeometry = simulation.getMathDescription().getGeometry();
        // defaults to unchanged
        memoryToDatabaseHash.put(memoryGeometry, memoryGeometry);
        boolean bMustSaveGeometry = false;
        VCImage geometryImage = memoryGeometry.getGeometrySpec().getImage();
        if (geometryImage != null && memoryToDatabaseHash.get(geometryImage) != geometryImage) {
            // 
            // image had changed and was saved, load saved image into geometry and force a save of this geometry.
            // 
            memoryGeometry.getGeometrySpec().setImage((VCImage) memoryToDatabaseHash.get(geometryImage));
            geometryImage = (VCImage) memoryToDatabaseHash.get(geometryImage);
            bMustSaveGeometry = true;
        }
        if (memoryGeometry.getKey() != null && memoryGeometry.getVersion().getName().equals(memoryGeometry.getName())) {
            if (!bMustSaveGeometry) {
                // 
                // if geometry had previously been saved, not been forced 'dirty', and name not changed
                // compare with original geometry to see if "update" is required.
                // 
                Geometry databaseGeometry = null;
                if (origSimulation != null) {
                    Geometry origGeometry = origSimulation.getMathDescription().getGeometry();
                    if (origGeometry.getKey().equals(memoryGeometry.getKey())) {
                        databaseGeometry = origGeometry;
                    }
                }
                if (databaseGeometry == null) {
                    // 
                    // saved geometry not found in origMathModel (too bad), get from database.
                    // 
                    databaseGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, memoryGeometry.getKey(), false);
                }
                if (databaseGeometry != null && !databaseGeometry.compareEqual(memoryGeometry)) {
                    bMustSaveGeometry = true;
                }
            }
            if (bMustSaveGeometry) {
                KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
                KeyValue updatedGeometryKey = dbServer.getDBTopLevel().updateVersionable(dbc, user, memoryGeometry, updatedImageKey, false, true);
                Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
                memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
                bSomethingChanged = true;
            }
        } else {
            // 
            // Geometry hasn't been saved, has been renamed, or has been forced 'dirty'
            // insert it with a unique name
            // 
            int count = 0;
            while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.Geometry, memoryGeometry.getName(), true)) {
                try {
                    memoryGeometry.setName(TokenMangler.getNextRandomToken(memoryGeometry.getName()));
                } catch (java.beans.PropertyVetoException e) {
                    e.printStackTrace(System.out);
                }
                if (count++ > 5) {
                    throw new DataAccessException("failed to find unique geometry name '" + memoryGeometry.getName() + "' is last name tried");
                }
            }
            KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
            KeyValue updatedGeometryKey = dbServer.getDBTopLevel().insertVersionable(dbc, user, memoryGeometry, updatedImageKey, memoryGeometry.getName(), false, true);
            Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
            memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
            bSomethingChanged = true;
        }
    }
    // 
    // for the MathDescription:
    // substitute saved geometry into MathDescription
    // save MathDescription if necessary (only once) and store saved instance in hashtable.
    // 
    MathCompareResults mathCompareResults = null;
    {
        MathDescription memoryMathDescription = simulation.getMathDescription();
        // defaults to unchanged
        memoryToDatabaseHash.put(memoryMathDescription, memoryMathDescription);
        boolean bMustSaveMathDescription = false;
        Geometry scGeometry = memoryMathDescription.getGeometry();
        if (scGeometry != null && memoryToDatabaseHash.get(scGeometry) != scGeometry) {
            // 
            // geometry had changed and was saved, load saved geometry into SimulationContext (and it's MathDescription) and force a save of this SimulationContext.
            // 
            memoryMathDescription.setGeometry((Geometry) memoryToDatabaseHash.get(scGeometry));
            bMustSaveMathDescription = true;
        }
        if (memoryMathDescription.getKey() != null && memoryMathDescription.getVersion().getName().equals(memoryMathDescription.getName())) {
            if (!bMustSaveMathDescription) {
                // 
                // if MathDescription had previously been saved, not been forced 'dirty', and name not changed
                // compare with original MathDescription to see if "update" is required.
                // 
                MathDescription databaseMathDescription = null;
                if (origSimulation != null) {
                    MathDescription origMathDescription = origSimulation.getMathDescription();
                    if (origMathDescription.getKey().equals(memoryMathDescription.getKey())) {
                        databaseMathDescription = origMathDescription;
                    }
                }
                if (databaseMathDescription == null) {
                    // 
                    // saved mathDescription not found in origMathModel (too bad), get from database.
                    // 
                    databaseMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, memoryMathDescription.getKey());
                }
                if (databaseMathDescription != null) {
                    mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), memoryMathDescription, databaseMathDescription);
                    if (!mathCompareResults.decision.equals(Decision.MathEquivalent_SAME_MATHDESC_AS_IN_DB)) {
                        bMustSaveMathDescription = true;
                    }
                }
            }
            if (bMustSaveMathDescription) {
                KeyValue updatedGeometryKey = memoryMathDescription.getGeometry().getKey();
                KeyValue updatedMathDescriptionKey = dbServer.getDBTopLevel().updateVersionable(user, memoryMathDescription, updatedGeometryKey, false, true);
                MathDescription updatedMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, updatedMathDescriptionKey);
                memoryToDatabaseHash.put(memoryMathDescription, updatedMathDescription);
                bSomethingChanged = true;
            }
        } else {
            // 
            // MathDescription hasn't been saved, has been renamed, or has been forced 'dirty'
            // insert it with a any name (doens't have to be unique ... mathDescription is not a top-level versionable).
            // 
            KeyValue updatedGeometryKey = memoryMathDescription.getGeometry().getKey();
            KeyValue updatedMathDescriptionKey = dbServer.getDBTopLevel().insertVersionable(user, memoryMathDescription, updatedGeometryKey, memoryMathDescription.getName(), false, true);
            MathDescription updatedMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, updatedMathDescriptionKey);
            memoryToDatabaseHash.put(memoryMathDescription, updatedMathDescription);
            bSomethingChanged = true;
        }
    }
    // 
    // for each Simulation in document:
    // substitute saved MathDescriptions into Simulation and
    // save Simulation if necessary (only once) and store saved instance in hashtable.
    // 
    Simulation memorySimulation = simulation;
    // 
    // didn't evaluate this Simulation yet.
    // 
    // defaults to unchanged
    memoryToDatabaseHash.put(memorySimulation, memorySimulation);
    boolean bMustSaveSimulation = false;
    MathDescription simMathDescription = memorySimulation.getMathDescription();
    if (simMathDescription != null && memoryToDatabaseHash.get(simMathDescription) != simMathDescription) {
        if (memoryToDatabaseHash.get(simMathDescription) != null) {
            // make sure mathDescription hasn't already propagated (newer math won't be in hashtable)
            // 
            // mathDescription had changed and was saved, load saved mathDescription into SimulationContext (and force a save)
            // 
            memorySimulation.setMathDescription((MathDescription) memoryToDatabaseHash.get(simMathDescription));
            bMustSaveSimulation = true;
        }
    }
    Simulation databaseSimulation = null;
    // 
    if (memorySimulation.getKey() != null) {
        databaseSimulation = origSimulation;
        if (databaseSimulation != null && !databaseSimulation.compareEqual(memorySimulation)) {
            bMustSaveSimulation = true;
        }
        if (!memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
            // name was changed.
            bMustSaveSimulation = true;
        }
    } else {
        // never been saved.
        bMustSaveSimulation = true;
    }
    if (bMustSaveSimulation) {
        KeyValue updatedMathDescriptionKey = memorySimulation.getMathDescription().getKey();
        KeyValue updatedSimulationKey = null;
        boolean bMathEquivalent = false;
        if (origSimulation != null) {
            bMathEquivalent = !bForceIndependent && Simulation.testEquivalency(memorySimulation, origSimulation, mathCompareResults);
        }
        if (memorySimulation.getKey() != null && memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
            // name not changed, update simulation (but pass in database Simulation to check for parent-equivalence)
            updatedSimulationKey = dbServer.getDBTopLevel().updateVersionable(user, memorySimulation, updatedMathDescriptionKey, false, bMathEquivalent, true);
        } else {
            // name changed, insert simulation (but pass in database Simulation to check for parent-equivalence)
            updatedSimulationKey = dbServer.getDBTopLevel().insertVersionable(user, memorySimulation, updatedMathDescriptionKey, memorySimulation.getName(), false, bMathEquivalent, true);
        }
        Simulation updatedSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, updatedSimulationKey);
        memoryToDatabaseHash.put(memorySimulation, updatedSimulation);
        bSomethingChanged = true;
        simulationXML = XmlHelper.simToXML(updatedSimulation);
        return simulationXML;
    } else {
        return simulationXML;
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) MathDescription(cbit.vcell.math.MathDescription) Hashtable(java.util.Hashtable) QueryHashtable(cbit.sql.QueryHashtable) VCImage(cbit.image.VCImage) Versionable(org.vcell.util.document.Versionable) PropertyVetoException(java.beans.PropertyVetoException) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) Version(org.vcell.util.document.Version) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) MathCompareResults(cbit.vcell.math.MathCompareResults) DataAccessException(org.vcell.util.DataAccessException)

Example 54 with ObjectNotFoundException

use of org.vcell.util.ObjectNotFoundException in project vcell by virtualcell.

the class ServerDocumentManager method saveMathModel.

/**
 * Insert the method's description here.
 * Creation date: (10/28/00 12:08:30 AM)
 */
public String saveMathModel(QueryHashtable dbc, User user, String mathModelXML, String newName, String[] independentSims) throws DataAccessException, java.sql.SQLException, java.beans.PropertyVetoException, cbit.vcell.xml.XmlParseException {
    // 
    // this invokes "update" on the database layer
    // 
    MathModel mathModel = XmlHelper.XMLToMathModel(new XMLSource(mathModelXML));
    forceDeepDirtyIfForeign(user, mathModel);
    boolean isSaveAsNew = true;
    // 
    if (newName != null) {
        try {
            mathModel.setName(newName);
        } catch (java.beans.PropertyVetoException e) {
            e.printStackTrace(System.out);
            throw new DataAccessException("couldn't set new name for MathModel: " + e.getMessage());
        }
    } else {
        isSaveAsNew = false;
    }
    Version oldVersion = mathModel.getVersion();
    MathModel origMathModel = null;
    if (oldVersion != null) {
        try {
            String origMathModelXML = getMathModelXML(dbc, user, oldVersion.getVersionKey(), false);
            origMathModel = XmlHelper.XMLToMathModel(new XMLSource(origMathModelXML));
        } catch (ObjectNotFoundException nfe) {
            if (isSaveAsNew) {
                User foceClearVersionUser = new User("foceClearVersionUser", new KeyValue("0"));
                forceDeepDirtyIfForeign(foceClearVersionUser, mathModel);
            } else {
                throw new DataAccessException("Stored model has been changed or removed, please use 'Save As..'");
            }
        }
    }
    boolean bSomethingChanged = false;
    // 
    // UPDATE AND SUBSTITUTE FROM BOTTOM UP
    // 
    // Image->Geometry
    // Geometry->MathDescription
    // MathDescription->Simulation,MathModel
    // Simulation->MathModel
    // 
    Simulation[] simArray = mathModel.getSimulations();
    // 
    // if this mathModel has an image:
    // save if necessary (only once) and store saved instance in hashTable
    // 
    Hashtable<Versionable, Versionable> memoryToDatabaseHash = new Hashtable<Versionable, Versionable>();
    {
        VCImage memoryImage = mathModel.getMathDescription().getGeometry().getGeometrySpec().getImage();
        if (memoryImage != null) {
            // defaults to unchanged
            memoryToDatabaseHash.put(memoryImage, memoryImage);
            if (memoryImage.getKey() != null && memoryImage.getVersion().getName().equals(memoryImage.getName())) {
                // 
                // if image had previously been saved, not been forced 'dirty', and name not changed
                // compare with original image to see if "update" is required.
                // 
                VCImage databaseImage = null;
                if (origMathModel != null) {
                    VCImage origImage = origMathModel.getMathDescription().getGeometry().getGeometrySpec().getImage();
                    if (origImage != null && origImage.getKey().equals(memoryImage.getKey())) {
                        databaseImage = origImage;
                    }
                }
                if (databaseImage == null) {
                    // 
                    // saved image not found in origMathModel (too bad), get from database.
                    // 
                    databaseImage = dbServer.getDBTopLevel().getVCImage(dbc, user, memoryImage.getKey(), false);
                }
                if (databaseImage != null && !databaseImage.compareEqual(memoryImage)) {
                    KeyValue updatedImageKey = dbServer.getDBTopLevel().updateVersionable(user, memoryImage, false, true);
                    VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
                    memoryToDatabaseHash.put(memoryImage, updatedImage);
                    bSomethingChanged = true;
                }
            } else {
                // 
                // Image hasn't been saved, has been renamed, or has been forced 'dirty'
                // insert it with a unique name
                // 
                int count = 0;
                fixNullImageName(memoryImage);
                while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.VCImage, memoryImage.getName(), true)) {
                    try {
                        memoryImage.setName(TokenMangler.getNextRandomToken(memoryImage.getName()));
                    } catch (java.beans.PropertyVetoException e) {
                        e.printStackTrace(System.out);
                    }
                    if (count++ > 5) {
                        throw new DataAccessException("failed to find unique image name '" + memoryImage.getName() + "' is last name tried");
                    }
                }
                KeyValue updatedImageKey = dbServer.getDBTopLevel().insertVersionable(user, memoryImage, memoryImage.getName(), false, true);
                VCImage updatedImage = dbServer.getDBTopLevel().getVCImage(dbc, user, updatedImageKey, false);
                memoryToDatabaseHash.put(memoryImage, updatedImage);
                bSomethingChanged = true;
            }
        }
    }
    // 
    // for the Geometry:
    // substitute saved Image into Geometry and
    // save Geometry if necessary (only once) and store saved instance in hashtable.
    // 
    {
        Geometry memoryGeometry = mathModel.getMathDescription().getGeometry();
        // defaults to unchanged
        memoryToDatabaseHash.put(memoryGeometry, memoryGeometry);
        boolean bMustSaveGeometry = false;
        VCImage geometryImage = memoryGeometry.getGeometrySpec().getImage();
        if (geometryImage != null && memoryToDatabaseHash.get(geometryImage) != geometryImage) {
            // 
            // image had changed and was saved, load saved image into geometry and force a save of this geometry.
            // 
            memoryGeometry.getGeometrySpec().setImage((VCImage) memoryToDatabaseHash.get(geometryImage));
            geometryImage = (VCImage) memoryToDatabaseHash.get(geometryImage);
            bMustSaveGeometry = true;
        }
        if (memoryGeometry.getKey() != null && memoryGeometry.getVersion().getName().equals(memoryGeometry.getName())) {
            if (!bMustSaveGeometry) {
                // 
                // if geometry had previously been saved, not been forced 'dirty', and name not changed
                // compare with original geometry to see if "update" is required.
                // 
                Geometry databaseGeometry = null;
                if (origMathModel != null) {
                    Geometry origGeometry = origMathModel.getMathDescription().getGeometry();
                    if (origGeometry.getKey().equals(memoryGeometry.getKey())) {
                        databaseGeometry = origGeometry;
                    }
                }
                if (databaseGeometry == null) {
                    // 
                    // saved geometry not found in origMathModel (too bad), get from database.
                    // 
                    databaseGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, memoryGeometry.getKey(), false);
                }
                if (databaseGeometry != null && !databaseGeometry.compareEqual(memoryGeometry)) {
                    bMustSaveGeometry = true;
                }
            }
            if (bMustSaveGeometry) {
                KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
                KeyValue updatedGeometryKey = dbServer.getDBTopLevel().updateVersionable(dbc, user, memoryGeometry, updatedImageKey, false, true);
                Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
                memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
                bSomethingChanged = true;
            }
        } else {
            // 
            // Geometry hasn't been saved, has been renamed, or has been forced 'dirty'
            // insert it with a unique name
            // 
            int count = 0;
            while (dbServer.getDBTopLevel().isNameUsed(user, VersionableType.Geometry, memoryGeometry.getName(), true)) {
                try {
                    memoryGeometry.setName(TokenMangler.getNextRandomToken(memoryGeometry.getName()));
                } catch (java.beans.PropertyVetoException e) {
                    e.printStackTrace(System.out);
                }
                if (count++ > 5) {
                    throw new DataAccessException("failed to find unique geometry name '" + memoryGeometry.getName() + "' is last name tried");
                }
            }
            KeyValue updatedImageKey = (geometryImage != null) ? (geometryImage.getKey()) : (null);
            KeyValue updatedGeometryKey = dbServer.getDBTopLevel().insertVersionable(dbc, user, memoryGeometry, updatedImageKey, memoryGeometry.getName(), false, true);
            Geometry updatedGeometry = dbServer.getDBTopLevel().getGeometry(dbc, user, updatedGeometryKey, false);
            memoryToDatabaseHash.put(memoryGeometry, updatedGeometry);
            bSomethingChanged = true;
        }
    }
    // 
    // for the MathDescription:
    // substitute saved geometry into MathDescription
    // save MathDescription if necessary (only once) and store saved instance in hashtable.
    // 
    MathCompareResults mathCompareResults = null;
    {
        MathDescription memoryMathDescription = mathModel.getMathDescription();
        // defaults to unchanged
        memoryToDatabaseHash.put(memoryMathDescription, memoryMathDescription);
        boolean bMustSaveMathDescription = false;
        Geometry scGeometry = memoryMathDescription.getGeometry();
        if (scGeometry != null && memoryToDatabaseHash.get(scGeometry) != scGeometry) {
            // 
            // geometry had changed and was saved, load saved geometry into SimulationContext (and it's MathDescription) and force a save of this SimulationContext.
            // 
            memoryMathDescription.setGeometry((Geometry) memoryToDatabaseHash.get(scGeometry));
            bMustSaveMathDescription = true;
        }
        MathDescription databaseMathDescription = null;
        if (memoryMathDescription.getKey() != null) {
            // 
            if (origMathModel != null) {
                MathDescription origMathDescription = origMathModel.getMathDescription();
                if (origMathDescription.getKey().equals(memoryMathDescription.getKey())) {
                    databaseMathDescription = origMathDescription;
                }
            }
            if (databaseMathDescription == null) {
                // 
                // saved mathDescription not found in origMathModel (too bad), get from database.
                // 
                databaseMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, memoryMathDescription.getKey());
            }
            if (databaseMathDescription != null) {
                if (!memoryMathDescription.compareEqual(databaseMathDescription)) {
                    bMustSaveMathDescription = true;
                }
            }
        } else {
            bMustSaveMathDescription = true;
        }
        if (bMustSaveMathDescription) {
            // 
            if (databaseMathDescription != null) {
                try {
                    mathCompareResults = MathDescription.testEquivalency(SimulationSymbolTable.createMathSymbolTableFactory(), memoryMathDescription, databaseMathDescription);
                } catch (Exception e) {
                    e.printStackTrace(System.out);
                    mathCompareResults = new MathCompareResults(Decision.MathDifferent_FAILURE_UNKNOWN, "Exception: '" + e.getMessage() + "'");
                    System.out.println("FAILED TO COMPARE THE FOLLOWING MATH DESCRIPTIONS");
                    try {
                        System.out.println("MemoryMathDescription:\n" + ((memoryMathDescription != null) ? (memoryMathDescription.getVCML_database()) : ("null")));
                        System.out.println("DatabaseMathDescription:\n" + ((databaseMathDescription != null) ? (databaseMathDescription.getVCML_database()) : ("null")));
                    } catch (Exception e2) {
                        System.out.println("couldn't print math descriptions");
                    }
                }
            } else {
                mathCompareResults = new MathCompareResults(Decision.MathDifferent_NOT_SAVED);
            }
            KeyValue updatedGeometryKey = memoryMathDescription.getGeometry().getKey();
            KeyValue updatedMathDescriptionKey = null;
            if (memoryMathDescription.getVersion() != null && memoryMathDescription.getVersion().getName().equals(memoryMathDescription.getName())) {
                updatedMathDescriptionKey = dbServer.getDBTopLevel().updateVersionable(user, memoryMathDescription, updatedGeometryKey, false, true);
            } else {
                updatedMathDescriptionKey = dbServer.getDBTopLevel().insertVersionable(user, memoryMathDescription, updatedGeometryKey, memoryMathDescription.getName(), false, true);
            }
            MathDescription updatedMathDescription = dbServer.getDBTopLevel().getMathDescription(dbc, user, updatedMathDescriptionKey);
            memoryToDatabaseHash.put(memoryMathDescription, updatedMathDescription);
            bSomethingChanged = true;
        } else {
            mathCompareResults = new MathCompareResults(Decision.MathEquivalent_SAME_MATHDESC_AS_IN_DB);
        }
    }
    // 
    for (int i = 0; simArray != null && i < simArray.length; i++) {
        Simulation memorySimulation = simArray[i];
        if (!memoryToDatabaseHash.containsKey(memorySimulation)) {
            // 
            // didn't evaluate this Simulation yet.
            // 
            // defaults to unchanged
            memoryToDatabaseHash.put(memorySimulation, memorySimulation);
            boolean bMustSaveSimulation = false;
            MathDescription simMathDescription = memorySimulation.getMathDescription();
            if (simMathDescription != null && memoryToDatabaseHash.get(simMathDescription) != simMathDescription) {
                if (memoryToDatabaseHash.get(simMathDescription) != null) {
                    // make sure mathDescription hasn't already propagated (newer math won't be in hashtable)
                    // 
                    // mathDescription had changed and was saved, load saved mathDescription into SimulationContext (and force a save)
                    // 
                    memorySimulation.setMathDescription((MathDescription) memoryToDatabaseHash.get(simMathDescription));
                    bMustSaveSimulation = true;
                }
            }
            Simulation databaseSimulation = null;
            // 
            if (memorySimulation.getKey() != null) {
                if (origMathModel != null) {
                    for (int j = 0; j < origMathModel.getNumSimulations(); j++) {
                        if (origMathModel.getSimulations(j).getKey().equals(memorySimulation.getKey())) {
                            databaseSimulation = origMathModel.getSimulations(j);
                            break;
                        }
                    }
                }
                if (databaseSimulation == null) {
                    // 
                    // saved simulation not found in origBioModel (too bad), get from database.
                    // 
                    databaseSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, memorySimulation.getKey());
                }
                if (databaseSimulation != null && !databaseSimulation.compareEqual(memorySimulation)) {
                    bMustSaveSimulation = true;
                }
                if (!memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
                    // name was changed.
                    bMustSaveSimulation = true;
                }
            } else {
                // never been saved.
                bMustSaveSimulation = true;
            }
            if (bMustSaveSimulation) {
                KeyValue updatedMathDescriptionKey = memorySimulation.getMathDescription().getKey();
                KeyValue updatedSimulationKey = null;
                boolean bSimMathematicallyEquivalent = false;
                if (databaseSimulation != null) {
                    // 
                    // if to be forced "independent", then set equivalent to false
                    // 
                    boolean bForceIndependent = false;
                    for (int j = 0; independentSims != null && j < independentSims.length; j++) {
                        if (independentSims[j].equals(memorySimulation.getName())) {
                            bForceIndependent = true;
                        }
                    }
                    // check for math equivalency
                    try {
                        bSimMathematicallyEquivalent = !bForceIndependent && Simulation.testEquivalency(memorySimulation, databaseSimulation, mathCompareResults);
                    } catch (Exception e) {
                        e.printStackTrace(System.out);
                        throw new DataAccessException(e.getMessage());
                    }
                    // 
                    if (bSimMathematicallyEquivalent) {
                        VCSimulationIdentifier vcSimulationIdentifier = databaseSimulation.getSimulationInfo().getAuthoritativeVCSimulationIdentifier();
                        SimulationStatusPersistent simStatus = dbServer.getSimulationStatus(vcSimulationIdentifier.getSimulationKey());
                        if (simStatus == null || !simStatus.getHasData()) {
                            bSimMathematicallyEquivalent = false;
                        }
                    }
                }
                if (memorySimulation.getKey() != null && memorySimulation.getVersion().getName().equals(memorySimulation.getName())) {
                    // name not changed, update simulation (but pass in database Simulation to check for parent-equivalence)
                    updatedSimulationKey = dbServer.getDBTopLevel().updateVersionable(user, memorySimulation, updatedMathDescriptionKey, false, bSimMathematicallyEquivalent, true);
                } else {
                    // name changed, insert simulation (but pass in database Simulation to check for parent-equivalence)
                    updatedSimulationKey = dbServer.getDBTopLevel().insertVersionable(user, memorySimulation, updatedMathDescriptionKey, memorySimulation.getName(), false, bSimMathematicallyEquivalent, true);
                }
                Simulation updatedSimulation = dbServer.getDBTopLevel().getSimulation(dbc, user, updatedSimulationKey);
                memoryToDatabaseHash.put(memorySimulation, updatedSimulation);
                bSomethingChanged = true;
            }
        }
    }
    if (bSomethingChanged || origMathModel == null || !mathModel.compareEqual(origMathModel)) {
        // 
        // create new MathModelMetaData and save to server
        // 
        KeyValue mathDescriptionKey = ((MathDescription) memoryToDatabaseHash.get(mathModel.getMathDescription())).getKey();
        KeyValue[] simKeys = new KeyValue[mathModel.getNumSimulations()];
        for (int i = 0; i < mathModel.getNumSimulations(); i++) {
            simKeys[i] = ((Simulation) memoryToDatabaseHash.get(mathModel.getSimulations(i))).getKey();
        }
        MathModelMetaData mathModelMetaData = null;
        if (oldVersion == null) {
            mathModelMetaData = new MathModelMetaData(mathDescriptionKey, simKeys, mathModel.getName(), mathModel.getDescription(), mathModel.getOutputFunctionContext().getOutputFunctionsList());
        } else {
            mathModelMetaData = new MathModelMetaData(oldVersion, mathDescriptionKey, simKeys, mathModel.getOutputFunctionContext().getOutputFunctionsList());
            if (!mathModel.getDescription().equals(oldVersion.getAnnot())) {
                try {
                    mathModelMetaData.setDescription(mathModel.getDescription());
                } catch (java.beans.PropertyVetoException e) {
                    e.printStackTrace(System.out);
                }
            }
        }
        MathModelMetaData updatedMathModelMetaData = null;
        if (mathModel.getVersion() == null || !mathModel.getVersion().getName().equals(mathModel.getName())) {
            KeyValue updatedMathModelKey = dbServer.getDBTopLevel().insertVersionable(user, mathModelMetaData, null, /*hack*/
            mathModel.getName(), false, true);
            updatedMathModelMetaData = dbServer.getDBTopLevel().getMathModelMetaData(dbc, user, updatedMathModelKey);
        } else {
            KeyValue updatedMathModelKey = dbServer.getDBTopLevel().updateVersionable(user, mathModelMetaData, null, /*hack*/
            false, true);
            updatedMathModelMetaData = dbServer.getDBTopLevel().getMathModelMetaData(dbc, user, updatedMathModelKey);
        }
        // 
        // (THIS IS THE REALLY SCARY PART...NOT GETTING A FRESH VIEW OF EVERYTING FROM THE DATABASE FOR CREATING THE XML)
        // 
        // mathModelXML = getMathModelXML(user,updatedMathModelMetaData.getVersion().getVersionKey());
        MathModel updatedMathModel = new MathModel(updatedMathModelMetaData.getVersion());
        updatedMathModel.setMathDescription((MathDescription) memoryToDatabaseHash.get(mathModel.getMathDescription()));
        for (int i = 0; i < mathModel.getNumSimulations(); i++) {
            updatedMathModel.addSimulation((Simulation) memoryToDatabaseHash.get(mathModel.getSimulations(i)));
        }
        updatedMathModel.getOutputFunctionContext().setOutputFunctions(mathModel.getOutputFunctionContext().getOutputFunctionsList());
        mathModelXML = cbit.vcell.xml.XmlHelper.mathModelToXML(updatedMathModel);
        dbServer.insertVersionableChildSummary(user, VersionableType.MathModelMetaData, updatedMathModel.getVersion().getVersionKey(), updatedMathModel.createMathModelChildSummary().toDatabaseSerialization());
        dbServer.insertVersionableXML(user, VersionableType.MathModelMetaData, updatedMathModel.getVersion().getVersionKey(), mathModelXML);
        return mathModelXML;
    } else {
        return mathModelXML;
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) VCSimulationIdentifier(cbit.vcell.solver.VCSimulationIdentifier) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) MathDescription(cbit.vcell.math.MathDescription) VCImage(cbit.image.VCImage) PropertyVetoException(java.beans.PropertyVetoException) Versionable(org.vcell.util.document.Versionable) Version(org.vcell.util.document.Version) MathCompareResults(cbit.vcell.math.MathCompareResults) DataAccessException(org.vcell.util.DataAccessException) Hashtable(java.util.Hashtable) QueryHashtable(cbit.sql.QueryHashtable) SimulationStatusPersistent(cbit.vcell.server.SimulationStatusPersistent) MathModelMetaData(cbit.vcell.mathmodel.MathModelMetaData) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) PropertyVetoException(java.beans.PropertyVetoException) XmlParseException(cbit.vcell.xml.XmlParseException) DataAccessException(org.vcell.util.DataAccessException) MappingException(cbit.vcell.mapping.MappingException) PropertyVetoException(java.beans.PropertyVetoException) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) XMLSource(cbit.vcell.xml.XMLSource)

Example 55 with ObjectNotFoundException

use of org.vcell.util.ObjectNotFoundException in project vcell by virtualcell.

the class ServerDocumentManager method isChanged.

/**
 * Insert the method's description here.
 * Creation date: (10/28/00 12:08:30 AM)
 */
boolean isChanged(QueryHashtable dbc, User user, Geometry geometry) throws DataAccessException {
    // 
    // identify versionable as it was last loaded from the database
    // 
    KeyValue key = (geometry.getVersion() != null) ? (geometry.getVersion().getVersionKey()) : null;
    if (key == null) {
        return true;
    }
    Versionable savedVersionable = null;
    // 
    try {
        savedVersionable = dbServer.getDBTopLevel().getGeometry(dbc, user, key, true);
    } catch (ObjectNotFoundException e) {
        // 
        return true;
    } catch (Throwable e) {
        // 
        // loaded version has been deleted
        // 
        e.printStackTrace(System.out);
        return true;
    }
    return isChanged0(user, geometry, savedVersionable);
}
Also used : Versionable(org.vcell.util.document.Versionable) KeyValue(org.vcell.util.document.KeyValue) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException)

Aggregations

ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)62 DataAccessException (org.vcell.util.DataAccessException)26 KeyValue (org.vcell.util.document.KeyValue)21 ResultSet (java.sql.ResultSet)18 Statement (java.sql.Statement)17 PermissionException (org.vcell.util.PermissionException)17 SQLException (java.sql.SQLException)13 User (org.vcell.util.document.User)12 BigString (org.vcell.util.BigString)11 XmlParseException (cbit.vcell.xml.XmlParseException)10 VersionInfo (org.vcell.util.document.VersionInfo)10 Field (cbit.sql.Field)9 SimulationRep (cbit.vcell.modeldb.SimulationRep)9 XMLSource (cbit.vcell.xml.XMLSource)9 Table (cbit.sql.Table)8 OuterJoin (cbit.vcell.modeldb.DatabasePolicySQL.OuterJoin)8 PropertyVetoException (java.beans.PropertyVetoException)8 ResourceException (org.restlet.resource.ResourceException)8 VCellApiApplication (org.vcell.rest.VCellApiApplication)8 BioModel (cbit.vcell.biomodel.BioModel)7