use of org.jumpmind.exception.ParseException in project symmetric-ds by JumpMind.
the class FormatUtils method parseDate.
public static Date parseDate(String str, String[] parsePatterns, TimeZone timeZone) {
if (str == null || parsePatterns == null) {
throw new IllegalArgumentException("Date and Patterns must not be null");
}
SimpleDateFormat parser = null;
ParsePosition pos = new ParsePosition(0);
for (int i = 0; i < parsePatterns.length; i++) {
if (i == 0) {
parser = new SimpleDateFormat(parsePatterns[0]);
if (timeZone != null) {
parser.setTimeZone(timeZone);
}
} else {
parser.applyPattern(parsePatterns[i]);
}
pos.setIndex(0);
Date date = parser.parse(str, pos);
if (date != null && pos.getIndex() == str.length()) {
return date;
}
}
try {
Date date = new Date(Long.parseLong(str));
return date;
} catch (NumberFormatException e) {
}
throw new ParseException("Unable to parse the date: " + str);
}
use of org.jumpmind.exception.ParseException in project symmetric-ds by JumpMind.
the class DefaultDatabaseWriterConflictResolver method isTimestampNewer.
protected boolean isTimestampNewer(Conflict conflict, AbstractDatabaseWriter writer, CsvData data) {
DefaultDatabaseWriter databaseWriter = (DefaultDatabaseWriter) writer;
IDatabasePlatform platform = databaseWriter.getPlatform();
String columnName = conflict.getDetectExpression();
Table targetTable = writer.getTargetTable();
Table sourceTable = writer.getSourceTable();
String[] pkData = data.getPkData(targetTable);
Object[] objectValues = databaseWriter.getPlatform().getObjectValues(writer.getBatch().getBinaryEncoding(), pkData, targetTable.getPrimaryKeyColumns());
DmlStatement stmt = databaseWriter.getPlatform().createDmlStatement(DmlType.FROM, targetTable, writer.getWriterSettings().getTextColumnExpression());
Column column = targetTable.getColumnWithName(columnName);
if (column == null) {
throw new RuntimeException(String.format("Could not find a timestamp column with a name of %s on the table %s. " + "Please check your conflict resolution configuration", columnName, targetTable.getQualifiedTableName()));
}
String sql = stmt.getColumnsSql(new Column[] { column });
Map<String, String> newData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.ROW_DATA);
String loadingStr = newData.get(columnName);
Date loadingTs = null;
Date existingTs = null;
if (column.isTimestampWithTimezone()) {
// Get the existingTs with timezone
String existingStr = databaseWriter.getTransaction().queryForObject(sql, String.class, objectValues);
// because the row doesn't exist, then existing simply needs to be null
if (existingStr != null) {
int split = existingStr.lastIndexOf(" ");
existingTs = FormatUtils.parseDate(existingStr.substring(0, split).trim(), FormatUtils.TIMESTAMP_PATTERNS, TimeZone.getTimeZone(existingStr.substring(split).trim()));
}
// Get the loadingTs with timezone
int split = loadingStr.lastIndexOf(" ");
loadingTs = FormatUtils.parseDate(loadingStr.substring(0, split).trim(), FormatUtils.TIMESTAMP_PATTERNS, TimeZone.getTimeZone(loadingStr.substring(split).trim()));
} else {
// Get the existingTs
existingTs = databaseWriter.getTransaction().queryForObject(sql, Timestamp.class, objectValues);
// Get the loadingTs
Object[] values = platform.getObjectValues(writer.getBatch().getBinaryEncoding(), new String[] { loadingStr }, new Column[] { column });
if (values[0] instanceof Date) {
loadingTs = (Date) values[0];
} else if (values[0] instanceof String && column.getJdbcTypeName().equalsIgnoreCase(TypeMap.DATETIME2)) {
// SQL Server DateTime2 type is treated as a string internally.
loadingTs = databaseWriter.getPlatform().parseDate(Types.VARCHAR, (String) values[0], false);
} else {
throw new ParseException("Could not parse " + columnName + " with a value of " + loadingStr + " for purposes of conflict detection");
}
}
return existingTs == null || loadingTs.compareTo(existingTs) > 0;
}
Aggregations