Search in sources :

Example 11 with VersionInfo

use of org.vcell.util.document.VersionInfo in project vcell by virtualcell.

the class DbDriver method groupAddUser.

/**
 * This method was created in VisualAge.
 * @return cbit.sql.Versionable
 * @param user cbit.vcell.server.User
 * @param versionable cbit.sql.Versionable
 */
public static void groupAddUser(Connection con, KeyFactory keyFactory, User owner, VersionableType vType, KeyValue vKey, String userAddToGroupString, boolean isHiddenFromOwner, DatabaseSyntax dbSyntax) throws SQLException, ObjectNotFoundException, DataAccessException {
    User userAddToGroup = getUserFromUserid(con, userAddToGroupString);
    if (userAddToGroup == null) {
        throw new IllegalArgumentException("User name " + userAddToGroupString + " not found");
    }
    // 
    if ((con == null) || (vType == null) || (owner == null) || (vKey == null) || (userAddToGroup == null)) {
        throw new IllegalArgumentException("Improper parameters for groupAddUser userAddToGroupString=" + (userAddToGroupString == null ? "NULL" : userAddToGroupString));
    }
    // 
    Version currentVersion = permissionInit(con, vType, vKey, owner);
    // If userAddToGroup is already in group it is an error
    // ----- Also can't add members to GroupAccessAll(Public) (CHANGED!!!) -----
    boolean bExists = false;
    if (currentVersion.getGroupAccess() instanceof GroupAccessSome) {
        bExists = (((GroupAccessSome) currentVersion.getGroupAccess()).isNormalMember(userAddToGroup) && !isHiddenFromOwner) || (((GroupAccessSome) currentVersion.getGroupAccess()).isHiddenMember(userAddToGroup) && isHiddenFromOwner);
    }
    // }
    if (currentVersion.getOwner().compareEqual(userAddToGroup) || bExists) {
        throw new DataAccessException(userAddToGroup + " Already a member of group");
    }
    if (lg.isTraceEnabled())
        lg.trace("DbDriver.groupAddUser(user=" + owner + ", type =" + vType + ", key=" + vKey + ")");
    VersionTable vTable = VersionTable.getVersionTable(vType);
    // 
    // 
    BigDecimal newHash = null;
    // 
    if (currentVersion.getGroupAccess() instanceof GroupAccessSome) {
        // Calculate the hash of the currentVersion's group plus a new user
        GroupAccessSome currentGroup = (GroupAccessSome) currentVersion.getGroupAccess();
        newHash = currentGroup.calculateHashWithNewMember(userAddToGroup, isHiddenFromOwner);
    } else if (currentVersion.getGroupAccess() instanceof GroupAccessNone || currentVersion.getGroupAccess() instanceof GroupAccessAll) {
        // Calculate hash for a new group with only userAddToGroup in it
        KeyValue[] kvArr = new KeyValue[1];
        boolean[] hiddenArr = new boolean[1];
        kvArr[0] = userAddToGroup.getID();
        hiddenArr[0] = isHiddenFromOwner;
        newHash = GroupAccess.calculateHash(kvArr, hiddenArr);
    }
    // 
    BigDecimal updatedGroupID = null;
    // 
    // See if the newly calculated hash is present in the database GroupTable
    // indicating a group we can reuse by reference
    // 
    String sql = "SELECT groupid FROM " + GroupTable.table.getTableName() + " WHERE groupMemberHash = " + newHash.toString();
    java.sql.Statement stmt = con.createStatement();
    try {
        java.sql.ResultSet rset = stmt.executeQuery(sql);
        if (rset.next()) {
            // There may be more than one, just get the first, all have the same groupid and hash
            // Group already exists,Re-Use the groupid, we don't have to make a new group
            updatedGroupID = rset.getBigDecimal(GroupTable.table.groupid.toString());
        }
    } finally {
        stmt.close();
    }
    // 
    if (updatedGroupID == null) {
        // Create new Group id
        updatedGroupID = getNewGroupID(con, keyFactory);
        int groupMemberCount = 1;
        // Get all the members of the currentVersion Group or skip if currentVersion group is GroupAccessNone
        // Don't worry about GroupAccessAll, we couldn't have gotten this far
        // 
        // Add new User
        // 
        sql = "INSERT INTO " + GroupTable.table.getTableName() + " VALUES ( " + keyFactory.getNewKey(con).toString() + "," + updatedGroupID + "," + userAddToGroup.getID().toString() + "," + (isHiddenFromOwner ? "1" : "0") + "," + newHash + " )";
        updateCleanSQL(con, sql);
        if (currentVersion.getGroupAccess() instanceof GroupAccessSome) {
            // Add all the old Normal Users
            User[] normalUsers = ((GroupAccessSome) currentVersion.getGroupAccess()).getNormalGroupMembers();
            for (int i = 0; normalUsers != null && i < normalUsers.length; i += 1) {
                String userRef = normalUsers[i].getID().toString();
                sql = "INSERT INTO " + GroupTable.table.getTableName() + " VALUES ( " + keyFactory.getNewKey(con).toString() + "," + updatedGroupID + "," + userRef + "," + (false ? "1" : "0") + "," + newHash + " )";
                updateCleanSQL(con, sql);
            }
            // Add all the old Hidden Users
            User[] hiddenUsers = ((GroupAccessSome) currentVersion.getGroupAccess()).getHiddenGroupMembers();
            for (int i = 0; hiddenUsers != null && i < hiddenUsers.length; i += 1) {
                String userRef = hiddenUsers[i].getID().toString();
                sql = "INSERT INTO " + GroupTable.table.getTableName() + " VALUES ( " + keyFactory.getNewKey(con).toString() + "," + updatedGroupID + "," + userRef + "," + (true ? "1" : "0") + "," + newHash + " )";
                updateCleanSQL(con, sql);
            }
        }
    }
    // Update the vTable to point to the new Group
    String set = vTable.privacy.getQualifiedColName() + " = " + updatedGroupID;
    String cond = vTable.id.getQualifiedColName() + " = " + vKey;
    // " AND " + vTable.ownerRef.getQualifiedColName() + " = " + owner.getID();
    sql = DatabasePolicySQL.enforceOwnershipUpdate(owner, vTable, set, cond);
    // System.out.println(sql);
    int numRowsProcessed = updateCleanSQL(con, sql);
    if (numRowsProcessed != 1) {
        // 
        // check if update failed
        // 
        Vector<VersionInfo> versionInfoList = getVersionableInfos(con, owner, vType, false, vKey, true, dbSyntax);
        if (versionInfoList.size() == 0) {
            throw new DataAccessException("Add User " + userAddToGroup + " Permission to access failed, " + vType.getTypeName() + "(" + vKey + ") record not found");
        } else {
            throw new DataAccessException("Add User " + userAddToGroup + " Permission to access failed " + vType.getTypeName() + "(" + vKey + ")");
        }
    }
}
Also used : User(org.vcell.util.document.User) BigDecimal(java.math.BigDecimal) GroupAccessNone(org.vcell.util.document.GroupAccessNone) VersionInfo(org.vcell.util.document.VersionInfo) GroupAccessAll(org.vcell.util.document.GroupAccessAll) Version(org.vcell.util.document.Version) VersionableTypeVersion(org.vcell.util.document.VersionableTypeVersion) ResultSet(java.sql.ResultSet) Statement(java.sql.Statement) GroupAccessSome(org.vcell.util.document.GroupAccessSome) DataAccessException(org.vcell.util.DataAccessException)

Example 12 with VersionInfo

use of org.vcell.util.document.VersionInfo in project vcell by virtualcell.

the class DbDriver method getVersionableInfos.

/**
 * This method was created in VisualAge.
 * @return java.lang.Object
 * @param user cbit.vcell.server.User
 * @param vType int
 */
public static Vector<VersionInfo> getVersionableInfos(Connection con, User user, VersionableType vType, boolean bAll, KeyValue versionKey, boolean bCheckPermission, DatabaseSyntax dbSyntax) throws ObjectNotFoundException, SQLException, DataAccessException {
    if (user == null) {
        throw new IllegalArgumentException("Improper parameters for getVersionables");
    }
    // gvilog.print("DbDriver.getVersionableInfo(all=" + bAll + ",user=" + user + ", type="+vType+")");
    String sql;
    StringBuffer conditions = new StringBuffer();
    String special = null;
    VersionTable vTable = VersionTable.getVersionTable(vType);
    boolean bFirstClause = true;
    if (!bAll) {
        conditions.append(vTable.ownerRef.getQualifiedColName() + " = " + user.getID());
        bFirstClause = false;
    }
    if (versionKey != null) {
        if (!bFirstClause) {
            conditions.append(" AND ");
        }
        conditions.append(vTable.id.getQualifiedColName() + " = " + versionKey);
    }
    special = " ORDER BY " + vTable.name.getQualifiedColName() + "," + vTable.versionBranchID.getQualifiedColName() + "," + vTable.versionDate.getQualifiedColName();
    if (vType.equals(VersionableType.BioModelMetaData)) {
        sql = ((BioModelTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else if (vType.equals(VersionableType.MathModelMetaData)) {
        sql = ((MathModelTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else if (vType.equals(VersionableType.Simulation)) {
        sql = ((SimulationTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else if (vType.equals(VersionableType.Geometry)) {
        sql = ((GeometryTable) vTable).getInfoSQL(user, conditions.toString(), special, bCheckPermission, dbSyntax);
    } else if (vType.equals(VersionableType.VCImage)) {
        sql = ((ImageTable) vTable).getInfoSQL(user, conditions.toString(), special, bCheckPermission, dbSyntax);
    } else if (vType.equals(VersionableType.Model)) {
        sql = ((ModelTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else if (vType.equals(VersionableType.SimulationContext)) {
        sql = ((SimContextTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else if (vType.equals(VersionableType.MathDescription)) {
        sql = ((MathDescTable) vTable).getInfoSQL(user, conditions.toString(), special, dbSyntax);
    } else {
        throw new RuntimeException("VersionInfo not availlable for type '" + vType.getTypeName() + "'");
    }
    // 
    StringBuffer optimizedSQL = new StringBuffer(sql);
    optimizedSQL.insert(7, Table.SQL_GLOBAL_HINT);
    sql = optimizedSQL.toString();
    // 
    // System.out.println("getVersionableInfo--->"+sql);
    VersionInfo vInfo;
    Vector<VersionInfo> vInfoList = new Vector<VersionInfo>();
    // Connection con = conFact.getConnection();
    Statement stmt = con.createStatement();
    try {
        ResultSet rset = stmt.executeQuery(sql);
        while (rset.next()) {
            if (vType.equals(VersionableType.BioModelMetaData)) {
                vInfo = ((BioModelTable) vTable).getInfo(rset, con, dbSyntax);
            } else if (vType.equals(VersionableType.MathModelMetaData)) {
                vInfo = ((MathModelTable) vTable).getInfo(rset, con, dbSyntax);
            } else if (vType.equals(VersionableType.Simulation)) {
                vInfo = ((SimulationTable) vTable).getInfo(rset, con);
            } else if (vType.equals(VersionableType.Geometry)) {
                vInfo = ((GeometryTable) vTable).getInfo(rset, con);
            } else if (vType.equals(VersionableType.VCImage)) {
                vInfo = ((ImageTable) vTable).getInfo(rset, con, dbSyntax);
            } else if (vType.equals(VersionableType.Model)) {
                vInfo = ((ModelTable) vTable).getInfo(rset, con);
            } else if (vType.equals(VersionableType.SimulationContext)) {
                vInfo = ((SimContextTable) vTable).getInfo(rset, con);
            } else if (vType.equals(VersionableType.MathDescription)) {
                vInfo = ((MathDescTable) vTable).getInfo(rset, con);
            } else {
                throw new RuntimeException("VersionInfo not availlable for type '" + vType.getTypeName() + "'");
            }
            // 
            // only add version info record to list if not a duplicate,
            // this occurs because the DatabasePolicySQL.enforceOwnershipSelect() can get the same record several ways.
            // 
            VersionInfo previousVInfo = (vInfoList.size() > 0) ? vInfoList.lastElement() : null;
            if (previousVInfo == null || !previousVInfo.getVersion().getVersionKey().compareEqual(vInfo.getVersion().getVersionKey())) {
                vInfoList.addElement(vInfo);
            }
        }
    } finally {
        stmt.close();
    }
    // return oInfoArray;
    return vInfoList;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) VersionInfo(org.vcell.util.document.VersionInfo) ResultSet(java.sql.ResultSet) Vector(java.util.Vector)

Example 13 with VersionInfo

use of org.vcell.util.document.VersionInfo in project vcell by virtualcell.

the class DbDriver method groupSetPublic.

/**
 * This method was created in VisualAge.
 * @return cbit.sql.Versionable
 * @param owner cbit.vcell.server.User
 * @param versionable cbit.sql.Versionable
 */
public static void groupSetPublic(Connection con, User owner, VersionableType vType, KeyValue vKey, DatabaseSyntax dbSyntax) throws SQLException, ObjectNotFoundException, DataAccessException {
    if ((con == null) || (vType == null) || (owner == null) || (vKey == null)) {
        throw new IllegalArgumentException("Improper parameters for groupAccessSetPublic");
    }
    // 
    Version currentVersion = permissionInit(con, vType, vKey, owner);
    if (lg.isTraceEnabled())
        lg.trace("DbDriver.groupSetPublic(owner=" + owner + ", type =" + vType + ", key=" + vKey + ")");
    BigDecimal updatedGroupID = GroupAccess.GROUPACCESS_ALL;
    VersionTable vTable = VersionTable.getVersionTable(vType);
    String set = vTable.privacy.getQualifiedColName() + " = " + updatedGroupID;
    String cond = vTable.id.getQualifiedColName() + " = " + vKey;
    // " AND " + vTable.ownerRef.getQualifiedColName() + " = " + owner.getID();
    String sql = DatabasePolicySQL.enforceOwnershipUpdate(owner, vTable, set, cond);
    // System.out.println(sql);
    int numRowsProcessed = updateCleanSQL(con, sql);
    if (numRowsProcessed != 1) {
        // 
        // check if update failed, or just already updated
        // 
        Vector<VersionInfo> versionInfoList = getVersionableInfos(con, owner, vType, false, vKey, true, dbSyntax);
        if (versionInfoList.size() == 0) {
            throw new DataAccessException("groupSetPublic failed " + vType.getTypeName() + "(" + vKey + ") record not found");
        } else {
            throw new DataAccessException("groupSetPublic failed " + vType.getTypeName() + "(" + vKey + ")");
        }
    }
}
Also used : VersionInfo(org.vcell.util.document.VersionInfo) Version(org.vcell.util.document.Version) VersionableTypeVersion(org.vcell.util.document.VersionableTypeVersion) BigDecimal(java.math.BigDecimal) DataAccessException(org.vcell.util.DataAccessException)

Example 14 with VersionInfo

use of org.vcell.util.document.VersionInfo in project vcell by virtualcell.

the class DBTopLevel method getBioModelXML.

/**
 * This method was created in VisualAge.
 * @return cbit.sql.Versionable
 * @param object cbit.sql.Versionable
 * @param name java.lang.String
 * @param bVersion boolean
 * @exception org.vcell.util.DataAccessException The exception description.
 * @exception java.sql.SQLException The exception description.
 * @exception cbit.sql.RecordChangedException The exception description.
 */
String getBioModelXML(User user, KeyValue key, boolean bEnableRetry) throws DataAccessException, java.sql.SQLException, ObjectNotFoundException {
    Object lock = new Object();
    Connection con = conFactory.getConnection(lock);
    DbDriver driver = getDbDriver(VersionableType.BioModelMetaData);
    try {
        // 
        // Getting the corresponding VersionInfo will fail if you don't have permission to the object.
        // This is needed because the DbDriver-level services can return objects directly from the
        // cache without checking for permissions first.
        // 
        // This check is placed in DbTopLevel because this is the client API entry point.
        // Child objects (of the requested object) are given permission by reachablity anyway,
        // so if the user is allowed permission to the parent, no further checks are necessary.
        // 
        Vector<VersionInfo> vInfos = getVersionableInfos(user, key, VersionableType.BioModelMetaData, true, true, false);
        if (vInfos.size() == 0) {
            throw new ObjectNotFoundException(VersionableType.BioModelMetaData.getTypeName() + " not found");
        }
        return driver.getVersionableXML(con, VersionableType.BioModelMetaData, key);
    } catch (Throwable e) {
        lg.error(e.getMessage(), e);
        if (bEnableRetry && isBadConnection(con)) {
            conFactory.failed(con, lock);
            return getBioModelXML(user, key, false);
        } else {
            handle_DataAccessException_SQLException(e);
            // never gets here;
            return null;
        }
    } finally {
        conFactory.release(con, lock);
    }
}
Also used : VersionInfo(org.vcell.util.document.VersionInfo) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) Connection(java.sql.Connection)

Example 15 with VersionInfo

use of org.vcell.util.document.VersionInfo in project vcell by virtualcell.

the class ClientRequestManager method createSelectDocTask.

private AsynchClientTask createSelectDocTask(final TopLevelWindowManager requester) {
    AsynchClientTask selectDocumentTypeTask = new AsynchClientTask("Select/Load geometry", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            String[][] docTypeoptions = new String[][] { { "BioModel names" }, { "MathModel names" } };
            VCDocumentType[] sourceDocumentTypes = new VCDocumentType[] { VCDocumentType.BIOMODEL_DOC, VCDocumentType.MATHMODEL_DOC };
            VCAssert.assertTrue(docTypeoptions.length == sourceDocumentTypes.length, "Label and types mismatch");
            int[] geomType = DialogUtils.showComponentOKCancelTableList(JOptionPane.getFrameForComponent(requester.getComponent()), "Select different Geometry", new String[] { "Search by" }, docTypeoptions, ListSelectionModel.SINGLE_SELECTION);
            final int selectedType = geomType[0];
            VCDocumentType sourceDocumentType = sourceDocumentTypes[selectedType];
            VersionInfo vcVersionInfo = null;
            if (geomType[0] == 3) {
                ImageDbTreePanel imageDbTreePanel = new ImageDbTreePanel();
                imageDbTreePanel.setDocumentManager(getDocumentManager());
                imageDbTreePanel.setPreferredSize(new java.awt.Dimension(200, 400));
                vcVersionInfo = DialogUtils.getDBTreePanelSelection(requester.getComponent(), imageDbTreePanel, "OK", "Select Image:");
            } else {
                vcVersionInfo = selectDocumentFromType(sourceDocumentType, requester);
            }
            hashTable.put(VERSIONINFO_KEY, vcVersionInfo);
        }
    };
    return selectDocumentTypeTask;
}
Also used : VersionInfo(org.vcell.util.document.VersionInfo) AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Dimension(java.awt.Dimension) Hashtable(java.util.Hashtable) VCDocumentType(org.vcell.util.document.VCDocument.VCDocumentType) ImageDbTreePanel(cbit.vcell.desktop.ImageDbTreePanel)

Aggregations

VersionInfo (org.vcell.util.document.VersionInfo)28 DataAccessException (org.vcell.util.DataAccessException)18 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)10 SQLException (java.sql.SQLException)6 BigDecimal (java.math.BigDecimal)5 VersionableTypeVersion (org.vcell.util.document.VersionableTypeVersion)5 ResultSet (java.sql.ResultSet)4 Statement (java.sql.Statement)4 Hashtable (java.util.Hashtable)4 MathModelInfo (org.vcell.util.document.MathModelInfo)4 Version (org.vcell.util.document.Version)4 VCImageInfo (cbit.image.VCImageInfo)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)3 GeometryInfo (cbit.vcell.geometry.GeometryInfo)3 Connection (java.sql.Connection)3 Vector (java.util.Vector)3 TreePath (javax.swing.tree.TreePath)3 BioModelInfo (org.vcell.util.document.BioModelInfo)3 VCDocumentInfoNode (cbit.vcell.desktop.VCellBasicCellRenderer.VCDocumentInfoNode)2 SimulationInfo (cbit.vcell.solver.SimulationInfo)2