List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:com.funambol.foundation.items.dao.PIMContactDAO.java
/** * Attaches extra information to a contact on the basis of a ResultSet. * * @param cw the contact still lacking extra information * @param rs the result of the execution of a proper SQL SELECT statement on * the fnbl_pim_contact_item table, with the cursor before its * first row/*w ww . j av a 2s.c om*/ * @return the ContactWrapper object with extra information attached * @throws java.sql.SQLException */ private static ContactWrapper addPIMContactItems(ContactWrapper cw, ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); Contact c = cw.getContact(); int type = 0; String value = null; int columnCount = 0; String column = null; Phone phone; Email email; WebPage webPage; while (rs.next()) { columnCount = rsmd.getColumnCount(); for (int i = 1; i <= columnCount; ++i) { column = rsmd.getColumnName(i); if (SQL_FIELD_TYPE.equalsIgnoreCase(column)) { type = rs.getInt(i); } else if (SQL_FIELD_VALUE.equalsIgnoreCase(column)) { value = rs.getString(i); } else { throw new SQLException("Unhandled column: " + column); } } switch (type) { case TYPE_HOME_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_HOME_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_HOME_FAX_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_HOME_FAX_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_MOBILE_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_MOBILE_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_EMAIL_1_ADDRESS: email = new Email(); email.setEmailType(FIELD_EMAIL_1_ADDRESS); email.setPropertyValue(value); c.getPersonalDetail().addEmail(email); break; case TYPE_WEB_PAGE: webPage = new WebPage(); webPage.setWebPageType(FIELD_WEB_PAGE); webPage.setPropertyValue(value); c.getPersonalDetail().addWebPage(webPage); break; case TYPE_BUSINESS_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_BUSINESS_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_BUSINESS_FAX_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_BUSINESS_FAX_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_COMPANY_MAIN_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_COMPANY_MAIN_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_ASSISTANT_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_ASSISTANT_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_PAGER_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_PAGER_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_CALLBACK_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_CALLBACK_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_EMAIL_2_ADDRESS: email = new Email(); email.setEmailType(FIELD_EMAIL_2_ADDRESS); email.setPropertyValue(value); c.getPersonalDetail().addEmail(email); break; case TYPE_EMAIL_3_ADDRESS: email = new Email(); email.setEmailType(FIELD_EMAIL_3_ADDRESS); email.setPropertyValue(value); c.getBusinessDetail().addEmail(email); break; case TYPE_HOME_WEB_PAGE: webPage = new WebPage(); webPage.setWebPageType(FIELD_HOME_WEB_PAGE); webPage.setPropertyValue(value); c.getPersonalDetail().addWebPage(webPage); break; case TYPE_CAR_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_CAR_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_PRIMARY_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_PRIMARY_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_HOME_2_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_HOME_2_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_BUSINESS_2_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_BUSINESS_2_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_BUSINESS_WEB_PAGE: webPage = new WebPage(); webPage.setWebPageType(FIELD_BUSINESS_WEB_PAGE); webPage.setPropertyValue(value); c.getBusinessDetail().addWebPage(webPage); break; case TYPE_INSTANT_MESSENGER: email = new Email(); email.setEmailType(FIELD_INSTANT_MESSENGER); email.setPropertyValue(value); c.getPersonalDetail().addEmail(email); break; case TYPE_BUSINESS_LABEL: c.getBusinessDetail().getAddress().getLabel().setPropertyValue(value); break; case TYPE_OTHER_LABEL: c.getPersonalDetail().getOtherAddress().getLabel().setPropertyValue(value); break; case TYPE_HOME_LABEL: c.getPersonalDetail().getAddress().getLabel().setPropertyValue(value); break; case TYPE_OTHER_FAX_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_OTHER_FAX_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_OTHER_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_OTHER_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; case TYPE_TELEX_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_TELEX_NUMBER); phone.setPropertyValue(value); c.getBusinessDetail().addPhone(phone); break; case TYPE_RADIO_TELEPHONE_NUMBER: phone = new Phone(); phone.setPhoneType(FIELD_RADIO_TELEPHONE_NUMBER); phone.setPropertyValue(value); c.getPersonalDetail().addPhone(phone); break; default: break; } } return cw; }
From source file:com.funambol.foundation.items.dao.PIMContactDAO.java
/** * Attaches the address(es) to a contact on the basis of a ResultSet. * * @param cw the contact (as a ContactWrapper) still lacking address * information/* w ww. ja v a2 s . c o m*/ * @param rs the result of the execution of a proper SQL SELECT statement on * the fnbl_pim_address table, with the cursor before its first * row * @return the ContactWrapper object with address information attached * @throws java.sql.SQLException */ private static ContactWrapper addPIMAddresses(ContactWrapper cw, ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); Contact c = cw.getContact(); int type = 0; String street = null; String city = null; String state = null; String postalCode = null; String country = null; String poBox = null; String extendedAddress = null; String column = null; int columnCount = 0; while (rs.next()) { columnCount = rsmd.getColumnCount(); for (int i = 1; i <= columnCount; ++i) { column = rsmd.getColumnName(i); if (SQL_FIELD_TYPE.equalsIgnoreCase(column)) { type = rs.getInt(i); } else if (SQL_FIELD_STREET.equalsIgnoreCase(column)) { street = rs.getString(i); } else if (SQL_FIELD_CITY.equalsIgnoreCase(column)) { city = rs.getString(i); } else if (SQL_FIELD_STATE.equalsIgnoreCase(column)) { state = rs.getString(i); } else if (SQL_FIELD_POSTAL_CODE.equalsIgnoreCase(column)) { postalCode = rs.getString(i); } else if (SQL_FIELD_COUNTRY.equalsIgnoreCase(column)) { country = rs.getString(i); } else if (SQL_FIELD_PO_BOX.equalsIgnoreCase(column)) { poBox = rs.getString(i); } else if (SQL_FIELD_EXTENDED_ADDRESS.equalsIgnoreCase(column)) { extendedAddress = rs.getString(i); } else { throw new SQLException("Unhandled column: " + column); } } switch (type) { case ADDRESS_TYPE_HOME: c.getPersonalDetail().getAddress().getStreet().setPropertyValue(street); c.getPersonalDetail().getAddress().getCity().setPropertyValue(city); c.getPersonalDetail().getAddress().getState().setPropertyValue(state); c.getPersonalDetail().getAddress().getPostalCode().setPropertyValue(postalCode); c.getPersonalDetail().getAddress().getCountry().setPropertyValue(country); c.getPersonalDetail().getAddress().getPostOfficeAddress().setPropertyValue(poBox); c.getPersonalDetail().getAddress().getExtendedAddress().setPropertyValue(extendedAddress); break; case ADDRESS_TYPE_OTHER: c.getPersonalDetail().getOtherAddress().getStreet().setPropertyValue(street); c.getPersonalDetail().getOtherAddress().getCity().setPropertyValue(city); c.getPersonalDetail().getOtherAddress().getState().setPropertyValue(state); c.getPersonalDetail().getOtherAddress().getPostalCode().setPropertyValue(postalCode); c.getPersonalDetail().getOtherAddress().getCountry().setPropertyValue(country); c.getPersonalDetail().getOtherAddress().getPostOfficeAddress().setPropertyValue(poBox); c.getPersonalDetail().getOtherAddress().getExtendedAddress().setPropertyValue(extendedAddress); break; case ADDRESS_TYPE_WORK: c.getBusinessDetail().getAddress().getStreet().setPropertyValue(street); c.getBusinessDetail().getAddress().getCity().setPropertyValue(city); c.getBusinessDetail().getAddress().getState().setPropertyValue(state); c.getBusinessDetail().getAddress().getPostalCode().setPropertyValue(postalCode); c.getBusinessDetail().getAddress().getCountry().setPropertyValue(country); c.getBusinessDetail().getAddress().getPostOfficeAddress().setPropertyValue(poBox); c.getBusinessDetail().getAddress().getExtendedAddress().setPropertyValue(extendedAddress); break; default: break; } } return cw; }
From source file:edu.ku.brc.specify.conversion.ConvertVerifier.java
/** * /*from w w w . j av a 2 s . c o m*/ */ private void verifyShipments() { newSQL = "SELECT s.ShipmentNumber,s.ShipmentDate, s.ShipmentMethod, s.NumberOfPackages, s.Weight, s.InsuredForAmount, ato.FirstName, ato.LastName, aby.FirstName, aby.LastName " + "FROM shipment AS s " + "Inner Join agent AS ato ON s.ShippedToID = ato.AgentID " + "Inner Join agent AS aby ON s.ShippedByID = aby.AgentID " + "ORDER BY s.ShipmentNumber ASC"; oldSQL = "SELECT s.ShipmentNumber,s.ShipmentDate, s.ShipmentMethod, s.NumberOfPackages, s.Weight, s.InsuredForAmount, ato.FirstName, ato.LastName, ato.Name, aby.FirstName, aby.LastName, aby.Name " + "FROM shipment AS s " + "Inner Join agentaddress aato ON s.ShippedToID = aato.AgentAddressID inner join agent ato on ato.AgentID = aato.AgentID " + "Inner Join agentaddress AS aaby ON s.ShippedByID = aaby.AgentAddressID inner join agent aby on aby.AgentID = aaby.AgentID " + "ORDER BY s.ShipmentNumber ASC"; log.info(newSQL); log.info(oldSQL); //int prevOldId = Integer.MAX_VALUE; //int prevNewId = Integer.MAX_VALUE; try { getResultSets(oldSQL, newSQL); ResultSetMetaData rmd = newDBRS.getMetaData(); while (true) { boolean hasOldRec = oldDBRS.next(); boolean hasNewRec = newDBRS.next(); if (!hasOldRec || !hasNewRec) { break; } Integer newId = newDBRS.getInt(1); Integer oldId = oldDBRS.getInt(1); if (newId == null || oldId == null) { System.err.println("Id is NULL! newId " + newId + " oldId " + oldId); } String oldNewIdStr = oldId + " / " + newId; /*if (newId == Integer.MAX_VALUE) { prevNewId = newId; } if (oldId == Integer.MAX_VALUE) { prevOldId = oldId; }*/ compareStrings(oldNewIdStr, rmd.getColumnName(1), 1, 1); compareStrings(oldNewIdStr, rmd.getColumnName(2), 2, 2); compareDates(oldNewIdStr, 3, 3); compareStrings(oldNewIdStr, rmd.getColumnName(4), 4, 4); compareStrings(oldNewIdStr, rmd.getColumnName(5), 5, 5); compareStrings(oldNewIdStr, rmd.getColumnName(6), 6, 6); compareNames(oldNewIdStr, 7, 7); compareNames(oldNewIdStr, 9, 10); } oldDBRS.close(); newDBRS.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.mirth.connect.donkey.server.data.jdbc.JdbcDao.java
private Map<String, Object> getMetaDataMap(String channelId, long messageId, int metaDataId) { PreparedStatement statement = null; ResultSet resultSet = null;//from www.j a v a2 s .c o m try { Map<String, Object> values = new HashMap<String, Object>(); values.put("localChannelId", getLocalChannelId(channelId)); // do not cache this statement since metadata columns may be added/removed statement = connection.prepareStatement(querySource.getQuery("getMetaDataMap", values)); statement.setLong(1, messageId); statement.setInt(2, metaDataId); Map<String, Object> metaDataMap = new HashMap<String, Object>(); resultSet = statement.executeQuery(); if (resultSet.next()) { ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); int columnCount = resultSetMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { MetaDataColumnType metaDataColumnType = MetaDataColumnType .fromSqlType(resultSetMetaData.getColumnType(i)); Object value = null; switch (metaDataColumnType) {//@formatter:off case STRING: value = resultSet.getString(i); break; case NUMBER: value = resultSet.getBigDecimal(i); break; case BOOLEAN: value = resultSet.getBoolean(i); break; case TIMESTAMP: Timestamp timestamp = resultSet.getTimestamp(i); if (timestamp != null) { value = Calendar.getInstance(); ((Calendar) value).setTimeInMillis(timestamp.getTime()); } break; default: throw new Exception("Unrecognized MetaDataColumnType"); } //@formatter:on metaDataMap.put(resultSetMetaData.getColumnName(i).toUpperCase(), value); } } return metaDataMap; } catch (Exception e) { throw new DonkeyDaoException(e); } finally { close(resultSet); close(statement); } }
From source file:com.mirth.connect.donkey.server.data.jdbc.JdbcDao.java
private Map<Long, Map<Integer, Map<String, Object>>> getMetaDataMaps(String channelId, List<Long> messageIds) { if (messageIds.size() > 1000) { throw new DonkeyDaoException("Only up to 1000 message Ids at a time are supported."); }//from w ww .j a v a 2 s . c om Map<Long, Map<Integer, Map<String, Object>>> metaDataMaps = new HashMap<Long, Map<Integer, Map<String, Object>>>(); PreparedStatement statement = null; ResultSet resultSet = null; try { Map<String, Object> values = new HashMap<String, Object>(); values.put("localChannelId", getLocalChannelId(channelId)); values.put("messageIds", StringUtils.join(messageIds, ",")); // do not cache this statement since metadata columns may be added/removed statement = connection.prepareStatement(querySource.getQuery("getMetaDataMapByMessageId", values)); resultSet = statement.executeQuery(); while (resultSet.next()) { Long messageId = resultSet.getLong("message_id"); Integer metaDataId = resultSet.getInt("metadata_id"); Map<Integer, Map<String, Object>> connectorMetaDataMap = metaDataMaps.get(messageId); if (connectorMetaDataMap == null) { connectorMetaDataMap = new HashMap<Integer, Map<String, Object>>(); metaDataMaps.put(messageId, connectorMetaDataMap); } Map<String, Object> metaDataMap = connectorMetaDataMap.get(metaDataId); if (metaDataMap == null) { metaDataMap = new HashMap<String, Object>(); connectorMetaDataMap.put(metaDataId, metaDataMap); } ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); int columnCount = resultSetMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { MetaDataColumnType metaDataColumnType = MetaDataColumnType .fromSqlType(resultSetMetaData.getColumnType(i)); Object value = null; switch (metaDataColumnType) {//@formatter:off case STRING: value = resultSet.getString(i); break; case NUMBER: value = resultSet.getBigDecimal(i); break; case BOOLEAN: value = resultSet.getBoolean(i); break; case TIMESTAMP: Timestamp timestamp = resultSet.getTimestamp(i); if (timestamp != null) { value = Calendar.getInstance(); ((Calendar) value).setTimeInMillis(timestamp.getTime()); } break; default: throw new Exception("Unrecognized MetaDataColumnType"); } //@formatter:on metaDataMap.put(resultSetMetaData.getColumnName(i).toUpperCase(), value); } } return metaDataMaps; } catch (Exception e) { throw new DonkeyDaoException(e); } finally { close(resultSet); close(statement); } }
From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider.java
private int getSqlColumnType(String fieldId) { try {// w w w. j a v a 2 s. c o m ResultSetMetaData rsmd = getCachedRowSet().getMetaData(); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { if (rsmd.getColumnName(i).equalsIgnoreCase(fieldId)) { return rsmd.getColumnType(i); } } } catch (Exception ex) { TLC.getBitacora().fatal(ex); } return java.sql.Types.OTHER; }
From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider.java
public boolean isColumnaDisponible(String fieldId) { try {/*from w w w .j a v a 2s . c om*/ ResultSetMetaData rsmd = getCachedRowSet().getMetaData(); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { if (rsmd.getColumnName(i).equalsIgnoreCase(fieldId)) { return true; } } } catch (Exception ex) { TLC.getBitacora().fatal(ex); } return false; }
From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider.java
private boolean isNullableColumn(String fieldId) { try {/* w w w .j a v a 2 s .co m*/ ResultSetMetaData rsmd = getCachedRowSet().getMetaData(); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { if (rsmd.getColumnName(i).equalsIgnoreCase(fieldId)) { if (rsmd.isNullable(i) == ResultSetMetaData.columnNullable) { return true; } else { return false; } } } } catch (Exception ex) { TLC.getBitacora().fatal(ex); } return false; }
From source file:edu.ku.brc.specify.conversion.ConvertVerifier.java
/** * @param oldSQL//from w w w .j a va2 s . c o m * @param newSQL * @return * @throws SQLException */ private StatusType compareRecords(final String desc, final String oldCatNumArg, final String newCatNumArg, final String oldSQLArg, final String newSQLArg, final boolean nullsAreOK, final boolean notRetarded) throws SQLException { boolean dbg = false; if (dbg) { System.out.println(oldSQLArg); System.out.println(newSQLArg); } if (dbg) { System.out.println("\n" + desc); dump(desc, oldDBConn, compareTo6DBs ? newSQLArg : oldSQLArg); dump(desc, newDBConn, newSQLArg); } String oldCatNum = oldCatNumArg; String newCatNum = newCatNumArg; if (compareTo6DBs) { oldCatNum = newCatNumArg; } if (notRetarded) { getResultSetsNotRetarded(oldSQLArg, newSQLArg, oldCatNum, newCatNum); } else { getResultSets(oldSQLArg, newSQLArg); } try { boolean hasOldRec = oldDBRS.next(); boolean hasNewRec = newDBRS.next(); if (!hasOldRec && !hasNewRec) { return StatusType.COMPARE_OK; } if (!hasOldRec) { if (nullsAreOK) { log.error(desc + " - No Old Record for [" + oldCatNum + "]"); tblWriter.logErrors(oldCatNum, "No Old Record"); return StatusType.NO_OLD_REC; } return StatusType.COMPARE_OK; } if (!hasNewRec) { log.error(desc + " - No New Record for [" + newCatNum + "]"); tblWriter.logErrors(newCatNum, "No New Record"); return StatusType.NO_NEW_REC; } //check number of rows, if not equal don't try to compare oldDBRS.last(); newDBRS.last(); if (oldDBRS.getRow() != newDBRS.getRow()) { String msg = desc + " Cat Num [" + oldCatNum + "]: Sp5 DB has " + oldDBRS.getRow() + " related records. Sp6 DB has " + newDBRS.getRow(); log.error(msg); tblWriter.logErrors(newCatNum, msg); return oldDBRS.getRow() < newDBRS.getRow() ? StatusType.NO_NEW_REC : StatusType.NO_OLD_REC; } oldDBRS.first(); newDBRS.first(); String oldNewIdStr = oldCatNum + " / " + newCatNum; boolean checkForAgent = newSQL.indexOf("a.LastName") > -1; ResultSetMetaData oldRsmd = oldDBRS.getMetaData(); ResultSetMetaData newRsmd = newDBRS.getMetaData(); PartialDateConv datePair = new PartialDateConv(); Calendar cal = Calendar.getInstance(); StringBuilder errSB = new StringBuilder(); while (hasNewRec && hasOldRec) { errSB.setLength(0); int oldColInx = 0; int newColInx = 0; String idMsgStr = ""; int numCols = newRsmd.getColumnCount(); for (int col = 0; col < numCols; col++) { newColInx++; oldColInx++; if (dbg) { System.out.println("\ncol " + col + " / " + oldRsmd.getColumnCount()); System.out.println("newColInx " + newColInx); System.out.println("oldColInx " + oldColInx); System.out.println(oldRsmd.getColumnName(oldColInx)); System.out.println(newRsmd.getColumnName(newColInx)); } Object newObj = newDBRS.getObject(newColInx); Object oldObj = oldDBRS.getObject(oldColInx); if (oldObj == null && newObj == null) { String colName = newRsmd.getColumnName(newColInx); if (StringUtils.contains(colName, "Date") && StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; if (compareTo6DBs) oldColInx++; } continue; } if (col == 0) { idMsgStr = String.format(" - Rec Ids[%s / %s] ", (oldObj != null ? oldObj : -1), (newObj != null ? newObj : -1)); continue; } String oldColName = oldRsmd.getColumnName(oldColInx); if (oldColName.equals("PreparationMethod") && newObj != null) { String newObjStr = newObj.toString(); if ((oldObj == null && !newObjStr.equalsIgnoreCase("Misc")) || (oldObj != null && !newObjStr.equalsIgnoreCase(oldObj.toString()))) { String msg = idMsgStr + "Old Value was null and shouldn't have been for Old CatNum [" + oldCatNum + "] Field [" + oldColName + "] oldObj[" + oldObj + "] newObj [" + newObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldCatNum, msg); return StatusType.OLD_VAL_NULL; } continue; } if (oldObj == null && !StringUtils.contains(oldColName, "LastName")) { if (!oldColName.equals("PreparationMethod") || !newObj.equals("Misc")) { String msg = idMsgStr + "Old Value was null and shouldn't have been for Old CatNum [" + oldCatNum + "] Field [" + oldColName + "] New Val[" + newObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldCatNum, msg); return StatusType.OLD_VAL_NULL; } } if (newObj == null) { String clsName = newRsmd.getColumnClassName(newColInx); String colName = newRsmd.getColumnName(newColInx); if (compareTo6DBs) { if (!clsName.equals("java.sql.Date") || oldObj != null) { String msg = "New Value was null and shouldn't have been for Key Value New CatNo[" + newCatNum + "] Field [" + colName + "] [" + oldObj + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(newCatNum, msg); return StatusType.NEW_VAL_NULL; } } else { if (!clsName.equals("java.sql.Date") || (!(oldObj instanceof String) && ((Number) oldObj).intValue() != 0)) { String msg = "New Value was null and shouldn't have been for Key Value New CatNo[" + newCatNum + "] Field [" + colName + "] [" + oldObj + "]"; log.error(desc + " - " + msg); if (tblWriter != null && newCatNum != null && msg != null) tblWriter.logErrors(newCatNum, msg); dbg = true; return StatusType.NEW_VAL_NULL; } } if (StringUtils.contains(colName, "Date") && StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; if (compareTo6DBs) oldColInx++; } continue; } //String colName = newRsmd.getColumnName(col); //System.out.println(newObj.getClass().getName()+" "+oldObj.getClass().getName()); if (newObj instanceof java.sql.Date) { boolean isPartialDate = false; Byte partialDateType = null; if (StringUtils.contains(newRsmd.getColumnName(newColInx + 1), "DatePrecision")) { newColInx++; numCols--; partialDateType = newDBRS.getByte(newColInx); isPartialDate = true; } if (compareTo6DBs) { Object dateObj = oldDBRS.getObject(oldColInx); boolean isPartialDate2 = false; Byte partialDateType2 = null; if (StringUtils.contains(oldRsmd.getColumnName(oldColInx + 1), "DatePrecision")) { oldColInx++; partialDateType2 = newDBRS.getByte(oldColInx); isPartialDate2 = true; } else { log.error("Next isn't DatePrecision and can't be!"); tblWriter.logErrors(oldNewIdStr, errSB.toString()); } if (!newObj.equals(dateObj) || (isPartialDate2 && !partialDateType2.equals(partialDateType))) { errSB.insert(0, oldColName + " "); errSB.append("["); errSB.append(datePair); errSB.append("]["); errSB.append(dateFormatter.format((Date) newObj)); errSB.append("] oldDate["); errSB.append(dateFormatter.format((Date) dateObj)); errSB.append("]"); log.error(errSB.toString()); tblWriter.logErrors(oldNewIdStr, errSB.toString()); return StatusType.BAD_DATE; } } else { int oldIntDate = oldDBRS.getInt(oldColInx); if (oldIntDate == 0) { continue; } BasicSQLUtils.getPartialDate(oldIntDate, datePair, false); if (partialDateType != null) { boolean ok = StringUtils.isNotEmpty(datePair.getPartial()) && StringUtils.isNumeric(datePair.getPartial()); if (!ok || (Byte.parseByte(datePair.getPartial()) != partialDateType.byteValue())) { errSB.append("Partial Dates Type do not match. Old[" + datePair.getPartial() + "] New [" + partialDateType.byteValue() + "]"); // error partial dates don't match } } cal.setTime((Date) newObj); if (StringUtils.isNotEmpty(datePair.getDateStr()) && !datePair.getDateStr().equalsIgnoreCase("null")) { int year = Integer.parseInt(datePair.getDateStr().substring(0, 4)); int mon = Integer.parseInt(datePair.getDateStr().substring(5, 7)); int day = Integer.parseInt(datePair.getDateStr().substring(8, 10)); if (mon > 0) mon--; boolean isYearOK = true; int yr = cal.get(Calendar.YEAR); if (year != yr) { errSB.append("Year mismatch Old[" + year + "] New [" + yr + "] "); isYearOK = false; } if (mon != cal.get(Calendar.MONTH)) { errSB.append("Month mismatch Old[" + mon + "] New [" + cal.get(Calendar.MONTH) + "] "); } if (day != cal.get(Calendar.DAY_OF_MONTH)) { errSB.append("Day mismatch Old[" + day + "] New [" + cal.get(Calendar.DAY_OF_MONTH) + "] "); } if (errSB.length() > 0 && (!isYearOK || !isPartialDate)) { errSB.insert(0, oldColName + " "); errSB.append("["); errSB.append(datePair); errSB.append("]["); errSB.append(dateFormatter.format((Date) newObj)); errSB.append("]"); log.error(errSB.toString()); tblWriter.logErrors(oldNewIdStr, errSB.toString()); return StatusType.BAD_DATE; } } else { //String msg = "Date contains the string 'NULL'"; //log.error(msg); //tblWriter.logErrors(oldNewIdStr, msg); //return StatusType.BAD_DATE; } } } else if (newObj instanceof Float || newObj instanceof Double) { String s1 = String.format("%10.5f", newObj instanceof Float ? (Float) newObj : (Double) newObj); String s2 = String.format("%10.5f", oldObj instanceof Float ? (Float) oldObj : (Double) oldObj); if (!s1.equals(s2)) { String msg = idMsgStr + "Columns don't compare[" + s1 + "][" + s2 + "] [" + newRsmd.getColumnName(col) + "][" + oldRsmd.getColumnName(oldColInx) + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; } } else { String newColName = newRsmd.getColumnName(newColInx); if (checkForAgent && StringUtils.contains(newColName, "LastName")) { String lastName = oldDBRS.getString(oldColInx); String agentName = oldDBRS.getString(oldColInx + 1); // The 'Name' Column String newLastName = newDBRS.getString(newColInx); if (!newLastName.equals(lastName) && !newLastName.equals(agentName)) { String msg = idMsgStr + "Name Columns don't compare[" + newObj + "][" + oldObj + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); log.error(oldSQLArg + "\n" + newSQLArg); return StatusType.NO_COMPARE; } } else if (StringUtils.contains(newColName, "YesNo")) { boolean yesNoNew = newDBRS.getBoolean(newColInx); boolean yesNoOld = oldDBRS.getInt(oldColInx) != 0; if (yesNoNew != yesNoOld) { String msg = idMsgStr + "Columns don't Cat Num[" + oldCatNum + "] compare[" + yesNoNew + "][" + yesNoOld + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; } } else if (!newObj.equals(oldObj)) { String msg = idMsgStr + "Columns don't Cat Num[" + oldCatNum + "] compare[" + newObj + "][" + oldObj + "] [" + newColName + "][" + oldColName + "]"; log.error(desc + " - " + msg); tblWriter.logErrors(oldNewIdStr, msg); return StatusType.NO_COMPARE; /*boolean isOK = false; if (oldObj instanceof String) { String oldStr = (String)oldObj; String newStr = (String)newObj; String lof = "\\r\\n"; int inx = newStr.indexOf(lof); if (inx > -1) { String tok = oldStr.substring(0, inx); if (newStr.equals(tok)) { isOK = true; } } } if (!isOK) { log.error(desc+ " - Columns don't compare["+newObj+"]["+oldObj+"] ["+newRsmd.getColumnName(newColInx)+"]["+oldRsmd.getColumnName(oldColInx)+"]"); return false; }*/ } } } hasOldRec = oldDBRS.next(); hasNewRec = newDBRS.next(); if (!hasOldRec && !hasNewRec) { return StatusType.COMPARE_OK; } if (!hasOldRec) { log.error(desc + idMsgStr + " - No Old Record for [" + oldCatNum + "]"); tblWriter.logErrors(oldNewIdStr, "No Old Record for [" + oldCatNum + "]"); return StatusType.NO_OLD_REC; } if (!hasNewRec) { log.error(desc + idMsgStr + " No New Record for [" + newCatNum + "]"); tblWriter.logErrors(oldNewIdStr, "No New Record for [" + newCatNum + "]"); return StatusType.NO_NEW_REC; } } } finally { doneWithRS(); } return StatusType.COMPARE_OK; }
From source file:rems.Global.java
public static void exprtToHTMLTblr(ResultSet dtst, String fileNm, String rptTitle, String[] colsToGrp, String[] colsToCnt, String[] colsToSum, String[] colsToAvrg, String[] colsToFrmt, boolean isfirst, boolean islast, boolean shdAppnd) { try {/*from w w w .ja v a 2s . c om*/ System.out.println(fileNm); DecimalFormat myFormatter = new DecimalFormat("###,##0.00"); DecimalFormat myFormatter2 = new DecimalFormat("###,##0"); dtst.last(); int totlRows = dtst.getRow(); dtst.beforeFirst(); ResultSetMetaData dtstmd = dtst.getMetaData(); int colCnt = dtstmd.getColumnCount(); long totlLen = 0; for (int d = 0; d < colCnt; d++) { totlLen += dtstmd.getColumnName(d + 1).length(); } long[] colcntVals = new long[colCnt]; double[] colsumVals = new double[colCnt]; double[] colavrgVals = new double[colCnt]; String cption = ""; if (isfirst) { cption = "<caption align=\"top\">" + rptTitle + "</caption>"; Global.strSB.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"[]><html xmlns=\"http://www.w3.org/1999/xhtml\" dir=\"ltr\" lang=\"en-US\" xml:lang=\"en\"><head><meta http-equiv=\"Content-Type\" " + "content=\"text/html; charset=utf-8\">" + System.getProperty("line.separator") + "<title>" + rptTitle + "</title>" + System.getProperty("line.separator") + "<link rel=\"stylesheet\" href=\"../amcharts/rpt.css\" type=\"text/css\"></head><body>"); Files.copy( new File(Global.getOrgImgsDrctry() + "/" + String.valueOf(Global.UsrsOrg_ID) + ".png") .toPath(), new File(Global.getRptDrctry() + "/amcharts_2100/images/" + String.valueOf(Global.UsrsOrg_ID) + ".png").toPath(), StandardCopyOption.REPLACE_EXISTING); if (Global.callngAppType.equals("DESKTOP")) { Global.upldImgsFTP(9, Global.getRptDrctry(), "/amcharts_2100/images/" + String.valueOf(Global.UsrsOrg_ID) + ".png"); } //Org Name String orgNm = Global.getOrgName(Global.UsrsOrg_ID); String pstl = Global.getOrgPstlAddrs(Global.UsrsOrg_ID); //Contacts Nos String cntcts = Global.getOrgContactNos(Global.UsrsOrg_ID); //Email Address String email = Global.getOrgEmailAddrs(Global.UsrsOrg_ID); Global.strSB .append("<p><img src=\"../images/" + String.valueOf(Global.UsrsOrg_ID) + ".png\">" + orgNm + "<br/>" + pstl + "<br/>" + cntcts + "<br/>" + email + "<br/>" + "</p>") .append(System.getProperty("line.separator")); } Global.strSB.append("<table style=\"margin-top:5px;\">" + cption + "<thead>") .append(System.getProperty("line.separator")); int wdth = 0; String finalStr = " "; for (int d = 0; d < colCnt; d++) { String algn = "left"; int colLen = dtstmd.getColumnName(d + 1).length(); wdth = (int) Math.round(((double) colLen / (double) totlLen) * 100); if (colLen >= 3) { if (Global.mustColBeFrmtd(String.valueOf(d), colsToFrmt) == true) { algn = "right"; finalStr = StringUtils.leftPad(dtstmd.getColumnName(d + 1).trim(), colLen, ' '); } else { finalStr = dtstmd.getColumnName(d + 1).trim() + " "; } Global.strSB .append("<th align=\"" + algn + "\" width=\"" + wdth + "%\">" + finalStr.replace(" ", " ") + "</th>") .append(System.getProperty("line.separator")); } } Global.strSB.append("</thead><tbody>").append(System.getProperty("line.separator")); String[][] prevRowVal = new String[totlRows][colCnt]; dtst.beforeFirst(); System.out.println(Global.strSB.toString()); for (int a = 0; a < totlRows; a++) { dtst.next(); Global.strSB.append("<tr>").append(System.getProperty("line.separator")); for (int d = 0; d < colCnt; d++) { String algn = "left"; double nwval = 0; boolean mstgrp = Global.mustColBeGrpd(String.valueOf(d), colsToGrp); if (Global.mustColBeCntd(String.valueOf(d), colsToCnt) == true) { if ((a > 0) && (mstgrp == true)) { if ((prevRowVal[a - 1][d].equals(dtst.getString(d + 1)))) { } else { colcntVals[d] += 1; } } else { colcntVals[d] += 1; } } else if (Global.mustColBeSumd(String.valueOf(d), colsToSum) == true) { nwval = Double.parseDouble(dtst.getString(d + 1)); if ((a > 0) && (mstgrp == true)) { if ((prevRowVal[a - 1][d].equals(dtst.getString(d + 1)))) { } else { colsumVals[d] += nwval; } } else { colsumVals[d] += nwval; } } else if (Global.mustColBeAvrgd(String.valueOf(d), colsToAvrg) == true) { nwval = Double.parseDouble(dtst.getString(d + 1)); if ((a > 0) && (mstgrp == true)) { if (prevRowVal[a - 1][d].equals(dtst.getString(d + 1))) { } else { colcntVals[d] += 1; colsumVals[d] += nwval; } } else { colcntVals[d] += 1; colsumVals[d] += nwval; } } int colLen = dtstmd.getColumnName(d + 1).length(); if (colLen >= 3) { if ((a > 0) && (Global.mustColBeGrpd(String.valueOf(d), colsToGrp) == true)) { if (prevRowVal[a - 1][d].equals(dtst.getString(d + 1))) { wdth = (int) Math.round(((double) colLen / (double) totlLen) * 100); Global.strSB .append("<td align=\"" + algn + "\" width=\"" + wdth + "%\">" + " ".replace(" ", " ") + "</td>") .append(System.getProperty("line.separator")); } else { wdth = (int) Math.round(((double) colLen / (double) totlLen) * 100); String frsh = " "; if (Global.mustColBeFrmtd(String.valueOf(d), colsToFrmt) == true) { algn = "right"; double num = Double.parseDouble(dtst.getString(d + 1).trim()); if (!dtst.getString(d + 1).equals("")) { frsh = myFormatter.format(num);//.Trim().PadRight(60, ' ') } else { frsh = dtst.getString(d + 1) + " "; } } else { frsh = dtst.getString(d + 1) + " "; } Global.strSB.append("<td align=\"" + algn + "\" width=\"" + wdth + "%\">" + Global.breakTxtDownHTML(frsh, dtstmd.getColumnName(d + 1).length()) .replace(" ", " ") + "</td>").append(System.getProperty("line.separator"));//.replace(" ", " ") } } else { wdth = (int) Math.round(((double) colLen / (double) totlLen) * 100); String frsh = " "; if (Global.mustColBeFrmtd(String.valueOf(d), colsToFrmt) == true) { algn = "right"; double num = Double.parseDouble(dtst.getString(d + 1).trim()); if (!dtst.getString(d + 1).equals("")) { frsh = myFormatter.format(num);//.Trim().PadRight(60, ' ') } else { frsh = dtst.getString(d + 1) + " "; } } else { frsh = dtst.getString(d + 1) + " "; } Global.strSB .append("<td align=\"" + algn + "\" width=\"" + wdth + "%\">" + Global.breakTxtDownHTML(frsh, dtstmd.getColumnName(d + 1).length()) .replace(" ", " ") + "</td>") .append(System.getProperty("line.separator"));//.replace(" ", " ") } } } Global.strSB.append("</tr>").append(System.getProperty("line.separator")); } //Populate Counts/Sums/Averages Global.strSB.append("<tr>").append(System.getProperty("line.separator")); for (int f = 0; f < colCnt; f++) { String algn = "left"; int colLen = dtstmd.getColumnName(f + 1).length(); finalStr = " "; if (colLen >= 3) { if (Global.mustColBeCntd(String.valueOf(f), colsToCnt) == true) { if (Global.mustColBeFrmtd(String.valueOf(f), colsToFrmt) == true) { algn = "right"; finalStr = ("Count = " + myFormatter2.format(colcntVals[f])); } else { finalStr = ("Count = " + String.valueOf(colcntVals[f])); } } else if (Global.mustColBeSumd(String.valueOf(f), colsToSum) == true) { if (Global.mustColBeFrmtd(String.valueOf(f), colsToFrmt) == true) { algn = "right"; finalStr = ("Sum = " + myFormatter.format(colsumVals[f])); } else { finalStr = ("Sum = " + String.valueOf(colcntVals[f])); } } else if (Global.mustColBeAvrgd(String.valueOf(f), colsToAvrg) == true) { if (Global.mustColBeFrmtd(String.valueOf(f), colsToFrmt) == true) { algn = "right"; finalStr = ("Average = " + myFormatter.format(colsumVals[f] / colcntVals[f])); } else { finalStr = ("Average = " + String.valueOf(colsumVals[f] / colcntVals[f])); } } else { finalStr = " "; } Global.strSB .append("<td align=\"" + algn + "\" width=\"" + wdth + "%\">" + Global.breakTxtDownHTML(finalStr, dtstmd.getColumnName(f + 1).length()) .replace(" ", " ") + "</td>") .append(System.getProperty("line.separator"));//.replace(" ", " ") } } Global.strSB.append("</tr>").append(System.getProperty("line.separator")); Global.strSB.append("</tbody></table>").append(System.getProperty("line.separator")); if (islast) { Global.strSB.append("</body></html>"); File file = new File(fileNm); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write(Global.strSB.toString()); bw.close(); if (Global.callngAppType.equals("DESKTOP")) { Global.upldImgsFTP(9, Global.getRptDrctry(), "/amcharts_2100/samples/" + String.valueOf(Global.runID) + ".html"); } } } catch (Exception ex) { System.out.println(ex.getMessage() + "\r\n\r\n" + Arrays.toString(ex.getStackTrace()) + "\r\n\r\n"); Global.errorLog += ex.getMessage() + "\r\n\r\n" + Arrays.toString(ex.getStackTrace()) + "\r\n\r\n"; Global.writeToLog(); } }