Example usage for java.sql ResultSet first

List of usage examples for java.sql ResultSet first

Introduction

In this page you can find the example usage for java.sql ResultSet first.

Prototype

boolean first() throws SQLException;

Source Link

Document

Moves the cursor to the first row in this ResultSet object.

Usage

From source file:edu.ku.brc.specify.conversion.GenericDBConversion.java

/**
 * Converts all the CollectionObject and CollectionObjectCatalog Records into the new schema
 * CollectionObject table. All "logical" records are moved to the CollectionObject table and all
 * "physical" records are moved to the Preparation table.
 * @return true if no errors// ww  w.  j  a va  2s .c  om
 */
@SuppressWarnings("cast")
public boolean convertCollectionObjects(final boolean useNumericCatNumbers, final boolean usePrefix) {
    final String ZEROES = "000000000";

    UIFieldFormatterIFace formatter0 = UIFieldFormatterMgr.getInstance().getFormatter("CatalogNumber");
    log.debug(formatter0);

    UIFieldFormatterIFace formatter = UIFieldFormatterMgr.getInstance().getFormatter("CatalogNumberNumeric");
    log.debug(formatter);

    DisciplineType dt;
    Discipline discipline = (Discipline) AppContextMgr.getInstance().getClassObject(Discipline.class);
    if (discipline != null) {
        System.out.println("discipline.getType()[" + discipline.getType() + "]");
        dt = DisciplineType.getDiscipline(discipline.getType());
    } else {
        Vector<Object[]> list = query(newDBConn, "SELECT Type FROM discipline");
        String typeStr = (String) list.get(0)[0];
        System.out.println("typeStr[" + typeStr + "]");
        dt = DisciplineType.getDiscipline(typeStr);
    }

    Pair<Integer, Boolean> objTypePair = dispToObjTypeHash.get(dt.getDisciplineType());
    if (objTypePair == null) {
        System.out.println("objTypePair is null dt[" + dt.getName() + "][" + dt.getTitle() + "]");

        for (STD_DISCIPLINES key : dispToObjTypeHash.keySet()) {
            Pair<Integer, Boolean> p = dispToObjTypeHash.get(key);
            System.out.println("[" + key + "] [" + p.first + "][" + p.second + "]");
        }

    } else if (objTypePair.first == null) {
        System.out.println("objTypePair.first is null dt[" + dt + "]");

        for (STD_DISCIPLINES key : dispToObjTypeHash.keySet()) {
            Pair<Integer, Boolean> p = dispToObjTypeHash.get(key);
            System.out.println("[" + key + "] [" + p.first + "][" + p.second + "]");
        }

    }
    //int objTypeId  = objTypePair.first;
    //boolean isEmbedded = objTypePair.second;

    idMapperMgr.dumpKeys();
    IdHashMapper colObjTaxonMapper = (IdHashMapper) idMapperMgr.get("ColObjCatToTaxonType".toLowerCase());
    IdHashMapper colObjAttrMapper = (IdHashMapper) idMapperMgr
            .get("biologicalobjectattributes_BiologicalObjectAttributesID");
    IdHashMapper colObjMapper = (IdHashMapper) idMapperMgr
            .get("collectionobjectcatalog_CollectionObjectCatalogID");

    colObjTaxonMapper.setShowLogErrors(false); // NOTE: TURN THIS ON FOR DEBUGGING or running new Databases through it
    colObjAttrMapper.setShowLogErrors(false);

    //IdHashMapper stratMapper    = (IdHashMapper)idMapperMgr.get("stratigraphy_StratigraphyID");
    //IdHashMapper stratGTPMapper = (IdHashMapper)idMapperMgr.get("stratigraphy_GeologicTimePeriodID");

    String[] fieldsToSkip = { "ContainerID", "ContainerItemID", "AltCatalogNumber", "GUID", "ContainerOwnerID",
            "RepositoryAgreementID", "GroupPermittedToView", // this may change when converting Specify 5.x
            "CollectionObjectID", "VisibilitySetBy", "ContainerOwnerID", "InventoryDate", "ObjectCondition",
            "Notifications", "ProjectNumber", "Restrictions", "YesNo3", "YesNo4", "YesNo5", "YesNo6",
            "FieldNotebookPageID", "ColObjAttributesID", "DNASequenceID", "AppraisalID", "TotalValue",
            "Description", "SGRStatus", "OCR", "ReservedText", "Text3" };

    HashSet<String> fieldsToSkipHash = new HashSet<String>();
    for (String fName : fieldsToSkip) {
        fieldsToSkipHash.add(fName);
    }

    TableWriter tblWriter = convLogger.getWriter("convertCollectionObjects.html", "Collection Objects");

    String msg = "colObjTaxonMapper: " + colObjTaxonMapper.size();
    log.info(msg);
    tblWriter.log(msg);

    setIdentityInsertONCommandForSQLServer(newDBConn, "collectionobject",
            BasicSQLUtils.myDestinationServerType);

    deleteAllRecordsFromTable(newDBConn, "collectionobject", BasicSQLUtils.myDestinationServerType); // automatically closes the connection

    TreeSet<String> badSubNumberCatNumsSet = new TreeSet<String>();

    TimeLogger timeLogger = new TimeLogger();

    try {
        Statement stmt = oldDBConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(Integer.MIN_VALUE);
        StringBuilder str = new StringBuilder();

        List<String> oldFieldNames = new ArrayList<String>();

        StringBuilder sql = new StringBuilder("select ");
        List<String> names = getFieldNamesFromSchema(oldDBConn, "collectionobject");

        sql.append(buildSelectFieldList(names, "collectionobject"));
        sql.append(", ");
        oldFieldNames.addAll(names);

        names = getFieldNamesFromSchema(oldDBConn, "collectionobjectcatalog");
        sql.append(buildSelectFieldList(names, "collectionobjectcatalog"));
        oldFieldNames.addAll(names);

        String fromClause = " FROM collectionobject Inner Join collectionobjectcatalog ON "
                + "collectionobject.CollectionObjectID = collectionobjectcatalog.CollectionObjectCatalogID "
                + "WHERE (collectionobject.DerivedFromID IS NULL) AND collectionobjectcatalog.CollectionObjectCatalogID = ";
        sql.append(fromClause);

        log.info(sql);
        String sqlStr = sql.toString();

        List<FieldMetaData> newFieldMetaData = getFieldMetaDataFromSchema(newDBConn, "collectionobject");

        log.info("Number of Fields in New CollectionObject " + newFieldMetaData.size());

        Map<String, Integer> oldNameIndex = new Hashtable<String, Integer>();
        int inx = 1;
        log.info("---- Old Names ----");
        for (String name : oldFieldNames) {
            log.info("[" + name + "][" + inx + "]");
            oldNameIndex.put(name, inx++);
        }

        log.info("---- New Names ----");
        for (FieldMetaData fmd : newFieldMetaData) {
            log.info("[" + fmd.getName() + "]");
        }
        String tableName = "collectionobject";

        Statement newStmt = oldDBConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        newStmt.setFetchSize(Integer.MIN_VALUE);
        ResultSet rsLooping = newStmt.executeQuery(
                "SELECT OldID, NewID FROM collectionobjectcatalog_CollectionObjectCatalogID ORDER BY OldID");

        if (hasFrame) {
            if (rsLooping.last()) {
                setProcess(0, rsLooping.getRow());
                rsLooping.first();

            } else {
                rsLooping.close();
                stmt.close();
                return true;
            }
        } else {
            if (!rsLooping.first()) {
                rsLooping.close();
                stmt.close();
                return true;
            }
        }

        int boaCnt = BasicSQLUtils.getCountAsInt(oldDBConn, "SELECT COUNT(*) FROM biologicalobjectattributes"); // ZZZ

        PartialDateConv partialDateConv = new PartialDateConv();

        Statement stmt2 = oldDBConn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        stmt2.setFetchSize(Integer.MIN_VALUE);

        int catNumInx = oldNameIndex.get("CatalogNumber");
        int catDateInx = oldNameIndex.get("CatalogedDate");
        int catSeriesIdInx = oldNameIndex.get("CatalogSeriesID");
        int lastEditedByInx = oldNameIndex.get("LastEditedBy");

        /*int     grpPrmtViewInx    = -1;
        Integer grpPrmtViewInxObj = oldNameIndex.get("GroupPermittedToView");
        if (grpPrmtViewInxObj != null)
        {
        grpPrmtViewInx = grpPrmtViewInxObj + 1;
        }*/

        Hashtable<Integer, CollectionInfo> oldCatSeriesIDToCollInfo = new Hashtable<Integer, CollectionInfo>();
        for (CollectionInfo ci : collectionInfoShortList) {
            if (ci.getCatSeriesId() != null) {
                oldCatSeriesIDToCollInfo.put(ci.getCatSeriesId(), ci);
            }
        }

        String insertStmtStr = null;

        /*String catIdTaxIdStrBase = "SELECT cc.CollectionObjectCatalogID, cc.CatalogSeriesID, ct.TaxonomyTypeID "
                                + "FROM collectionobjectcatalog AS cc "
                                + "Inner Join collectionobject AS co ON cc.CollectionObjectCatalogID = co.CollectionObjectID "
                                + "Inner Join collectiontaxonomytypes as ct ON co.CollectionObjectTypeID = ct.BiologicalObjectTypeID "
                                + "where cc.CollectionObjectCatalogID = ";*/

        int colObjAttrsNotMapped = 0;
        int count = 0;
        boolean skipRecord = false;
        do {
            String catSQL = sqlStr + rsLooping.getInt(1);
            //log.debug(catSQL);
            ResultSet rs = stmt.executeQuery(catSQL);
            if (!rs.next()) {
                log.error("Couldn't find CO with old  id[" + rsLooping.getInt(1) + "] " + catSQL);
                continue;
            }

            partialDateConv.nullAll();

            skipRecord = false;

            CollectionInfo collInfo = oldCatSeriesIDToCollInfo.get(rs.getInt(catSeriesIdInx));

            /*String catIdTaxIdStr = catIdTaxIdStrBase + rs.getInt(1);
            //log.info(catIdTaxIdStr);
                    
            ResultSet rs2 = stmt2.executeQuery(catIdTaxIdStr);
            if (!rs2.next())
            {
            log.info("QUERY failed to return results:\n"+catIdTaxIdStr+"\n");
            continue;
            }
            Integer catalogSeriesID = rs2.getInt(2);
            Integer taxonomyTypeID  = rs2.getInt(3);
            Integer newCatSeriesId  = collectionHash.get(catalogSeriesID + "_" + taxonomyTypeID);
            String  prefix          = prefixHash.get(catalogSeriesID + "_" + taxonomyTypeID);
            rs2.close();
                    
            if (newCatSeriesId == null)
            {
            msg = "Can't find " + catalogSeriesID + "_" + taxonomyTypeID;
            log.info(msg);
            tblWriter.logError(msg);
            continue;
            }*/

            /*if (false)
            {
            String stratGTPIdStr = "SELECT co.CollectionObjectID, ce.CollectingEventID, s.StratigraphyID, g.GeologicTimePeriodID FROM collectionobject co " +
                "LEFT JOIN collectingevent ce ON co.CollectingEventID = ce.CollectingEventID  " +
                "LEFT JOIN stratigraphy s ON ce.CollectingEventID = s.StratigraphyID  " +
                "LEFT JOIN geologictimeperiod g ON s.GeologicTimePeriodID = g.GeologicTimePeriodID  " +
                "WHERE co.CollectionObjectID  = " + rs.getInt(1);
            log.info(stratGTPIdStr);
            rs2 = stmt2.executeQuery(stratGTPIdStr);
                    
            Integer coId = null;
            Integer ceId = null;
            Integer stId = null;
            Integer gtpId = null;
            if (rs2.next())
            {
                coId = rs2.getInt(1);
                ceId = rs2.getInt(2);
                stId = rs2.getInt(3);
                gtpId = rs2.getInt(4);
            }
            rs2.close();
            }*/

            String catalogNumber = null;
            String colObjId = null;

            str.setLength(0);

            if (insertStmtStr == null) {
                StringBuffer fieldList = new StringBuffer();
                fieldList.append("( ");
                for (int i = 0; i < newFieldMetaData.size(); i++) {
                    if ((i > 0) && (i < newFieldMetaData.size())) {
                        fieldList.append(", ");
                    }
                    String newFieldName = newFieldMetaData.get(i).getName();
                    fieldList.append(newFieldName + " ");
                }
                fieldList.append(")");
                insertStmtStr = "INSERT INTO collectionobject " + fieldList + "  VALUES (";
            }
            str.append(insertStmtStr);

            for (int i = 0; i < newFieldMetaData.size(); i++) {
                if (i > 0) {
                    str.append(", ");
                }

                String newFieldName = newFieldMetaData.get(i).getName();

                if (i == 0) {
                    Integer oldColObjId = rs.getInt(1);
                    Integer newColObjId = colObjMapper.get(oldColObjId);

                    if (newColObjId == null) {
                        msg = "Couldn't find new ColObj Id for old [" + oldColObjId + "]";
                        tblWriter.logError(msg);
                        showError(msg);
                        throw new RuntimeException(msg);
                    }

                    colObjId = getStrValue(newColObjId);
                    if (contains(colObjId, '.')) {
                        String msgStr = String.format("CatalogNumber '%d' contains a decimal point.", colObjId);
                        log.debug(msgStr);
                        tblWriter.logError(msgStr);
                        skipRecord = true;
                        break;
                    }
                    str.append(colObjId);

                    if (useNumericCatNumbers) {
                        catalogNumber = rs.getString(catNumInx);

                        if (catalogNumber != null) {
                            int catNumInt = (int) Math.abs(rs.getDouble(catNumInx));
                            catalogNumber = Integer.toString(catNumInt);

                            if (catalogNumber.length() > 0 && catalogNumber.length() < ZEROES.length()) {
                                catalogNumber = "\"" + ZEROES.substring(catalogNumber.length()) + catalogNumber
                                        + "\"";

                            } else if (catalogNumber.length() > ZEROES.length()) {
                                showError(
                                        "Catalog Number[" + catalogNumber + "] is too long for formatter of 9");
                            }

                        } else {
                            String mssg = "Empty catalog number.";
                            log.debug(mssg);
                            //showError(msg);
                            tblWriter.logError(mssg);
                        }

                    } else {
                        String prefix = collInfo.getCatSeriesPrefix();

                        float catNum = rs.getFloat(catNumInx);
                        catalogNumber = "\"" + (usePrefix && isNotEmpty(prefix) ? (prefix + "-") : "")
                                + String.format("%9.0f", catNum).trim() + "\"";
                    }

                    int subNumber = rs.getInt(oldNameIndex.get("SubNumber"));
                    if (subNumber < 0 || rs.wasNull()) {
                        badSubNumberCatNumsSet.add(catalogNumber);

                        skipRecord = true;
                        //msg = "Collection Object is being skipped because SubNumber is less than zero CatalogNumber["+ catalogNumber + "]";
                        //log.error(msg);
                        //tblWriter.logError(msg);
                        //showError(msg);
                        break;
                    }

                } else if (fieldsToSkipHash.contains(newFieldName)) {
                    str.append("NULL");

                } else if (newFieldName.equals("CollectionID")) // User/Security changes
                {
                    str.append(collInfo.getCollectionId());

                } else if (newFieldName.equals("Version")) // User/Security changes
                {
                    str.append("0");

                } else if (newFieldName.equals("CreatedByAgentID")) // User/Security changes
                {
                    str.append(getCreatorAgentId(null));

                } else if (newFieldName.equals("ModifiedByAgentID")) // User/Security changes
                {
                    String lastEditedByStr = rs.getString(lastEditedByInx);
                    str.append(getModifiedByAgentId(lastEditedByStr));

                } else if (newFieldName.equals("CollectionMemberID")) // User/Security changes
                {
                    str.append(collInfo.getCollectionId());

                } else if (newFieldName.equals("PaleoContextID")) {
                    str.append("NULL");// newCatSeriesId);

                } else if (newFieldName.equals("CollectionObjectAttributeID")) // User/Security changes
                {
                    Object idObj = rs.getObject(1);
                    if (idObj != null) {
                        Integer coId = rs.getInt(1);
                        Integer newId = colObjAttrMapper.get(coId);
                        if (newId != null) {
                            str.append(getStrValue(newId));
                        } else {
                            if (boaCnt > 0)
                                colObjAttrsNotMapped++;
                            str.append("NULL");
                        }
                    } else {
                        str.append("NULL");
                    }

                } else if (newFieldName.equals("CatalogedDate")) {
                    if (partialDateConv.getDateStr() == null) {
                        getPartialDate(rs.getObject(catDateInx), partialDateConv);
                    }
                    str.append(partialDateConv.getDateStr());

                } else if (newFieldName.equals("CatalogedDatePrecision")) {
                    if (partialDateConv.getDateStr() == null) {
                        getPartialDate(rs.getObject(catDateInx), partialDateConv);
                    }
                    str.append(partialDateConv.getPartial());

                } else if (newFieldName.equals("CatalogedDateVerbatim")) {
                    if (partialDateConv.getDateStr() == null) {
                        getPartialDate(rs.getObject(catDateInx), partialDateConv);
                    }
                    str.append(partialDateConv.getVerbatim());

                } else if (newFieldName.equals("Availability")) {
                    str.append("NULL");

                } else if (newFieldName.equals("CatalogNumber")) {
                    str.append(catalogNumber);

                } else if (newFieldName.equals("Visibility")) // User/Security changes
                {
                    //str.append(grpPrmtViewInx > -1 ? rs.getObject(grpPrmtViewInx) : "NULL");
                    str.append("0");

                } else if (newFieldName.equals("VisibilitySetByID")) // User/Security changes
                {
                    str.append("NULL");

                } else if (newFieldName.equals("CountAmt")) {
                    Integer index = oldNameIndex.get("Count1");
                    if (index == null) {
                        index = oldNameIndex.get("Count");
                    }
                    Object countObj = rs.getObject(index);
                    if (countObj != null) {
                        str.append(getStrValue(countObj, newFieldMetaData.get(i).getType()));
                    } else {
                        str.append("NULL");
                    }

                } else {
                    Integer index = oldNameIndex.get(newFieldName);
                    Object data;
                    if (index == null) {
                        msg = "convertCollectionObjects - Couldn't find new field name[" + newFieldName
                                + "] in old field name in index Map";
                        log.warn(msg);
                        //                            tblWriter.logError(msg);
                        //                            showError(msg);
                        data = null;
                        // for (String key : oldNameIndex.keySet())
                        // {
                        // log.info("["+key+"]["+oldNameIndex.get(key)+"]");
                        // }
                        //stmt.close();
                        //throw new RuntimeException(msg);
                    } else {

                        data = rs.getObject(index);
                    }
                    if (data != null) {
                        int idInx = newFieldName.lastIndexOf("ID");
                        if (idMapperMgr != null && idInx > -1) {
                            IdMapperIFace idMapper = idMapperMgr.get(tableName, newFieldName);
                            if (idMapper != null) {
                                Integer origValue = rs.getInt(index);
                                data = idMapper.get(origValue);
                                if (data == null) {
                                    msg = "No value [" + origValue + "] in map  [" + tableName + "]["
                                            + newFieldName + "]";
                                    log.error(msg);
                                    tblWriter.logError(msg);
                                    //showError(msg);
                                }
                            } else {
                                msg = "No Map for [" + tableName + "][" + newFieldName + "]";
                                log.error(msg);
                                tblWriter.logError(msg);
                                //showError(msg);
                            }
                        }
                    }
                    str.append(getStrValue(data, newFieldMetaData.get(i).getType()));
                }
            }

            if (!skipRecord) {
                str.append(")");
                // log.info("\n"+str.toString());
                if (hasFrame) {
                    if (count % 500 == 0) {
                        setProcess(count);
                    }
                    if (count % 5000 == 0) {
                        log.info("CollectionObject Records: " + count);
                    }

                } else {
                    if (count % 2000 == 0) {
                        log.info("CollectionObject Records: " + count);
                    }
                }

                try {
                    Statement updateStatement = newDBConn.createStatement();
                    if (BasicSQLUtils.myDestinationServerType != BasicSQLUtils.SERVERTYPE.MS_SQLServer) {
                        removeForeignKeyConstraints(newDBConn, BasicSQLUtils.myDestinationServerType);
                    }
                    // updateStatement.executeUpdate("SET FOREIGN_KEY_CHECKS = 0");
                    //if (count < 50) System.err.println(str.toString());

                    updateStatement.executeUpdate(str.toString());
                    updateStatement.clearBatch();
                    updateStatement.close();
                    updateStatement = null;

                } catch (SQLException e) {
                    log.error("Count: " + count);
                    log.error("Key: [" + colObjId + "][" + catalogNumber + "]");
                    log.error("SQL: " + str.toString());
                    e.printStackTrace();
                    log.error(e);
                    showError(e.getMessage());
                    rs.close();
                    stmt.close();
                    throw new RuntimeException(e);
                }

                count++;
            } else {
                tblWriter.logError("Skipping - CatNo:" + catalogNumber);
            }
            // if (count > 10) break;

            rs.close();

        } while (rsLooping.next());

        /*if (boaCnt > 0)
        {
        msg = "CollectionObjectAttributes not mapped: " + colObjAttrsNotMapped + " out of "+boaCnt;
        log.info(msg);
        tblWriter.logError(msg);
        }*/

        stmt2.close();

        if (hasFrame) {
            setProcess(count);
        } else {
            log.info("Processed CollectionObject " + count + " records.");
        }

        tblWriter.log(String.format("Collection Objects Processing Time: %s", timeLogger.end()));

        tblWriter.log("Processed CollectionObject " + count + " records.");
        rsLooping.close();
        newStmt.close();
        stmt.close();

        tblWriter.append(
                "<br><br><b>Catalog Numbers rejected because the SubNumber was NULL or less than Zero</b><br>");
        tblWriter.startTable();
        tblWriter.logHdr("Catalog Number");
        for (String catNum : badSubNumberCatNumsSet) {
            tblWriter.log(catNum);
        }
        tblWriter.endTable();

    } catch (SQLException e) {
        setIdentityInsertOFFCommandForSQLServer(newDBConn, "collectionobject",
                BasicSQLUtils.myDestinationServerType);
        e.printStackTrace();
        log.error(e);
        tblWriter.logError(e.getMessage());
        showError(e.getMessage());
        throw new RuntimeException(e);

    } finally {
        tblWriter.close();
    }
    setIdentityInsertOFFCommandForSQLServer(newDBConn, "collectionobject",
            BasicSQLUtils.myDestinationServerType);

    return true;
}