use of org.killbill.billing.util.api.ColumnInfo in project killbill by killbill.
the class DatabaseExportDao method exportDataForAccount.
public void exportDataForAccount(final DatabaseExportOutputStream out, final InternalTenantContext context) {
if (context.getAccountRecordId() == null || context.getTenantRecordId() == null) {
return;
}
final List<DefaultColumnInfo> columns = databaseSchemaDao.getColumnInfoList();
if (columns.size() == 0) {
return;
}
final List<ColumnInfo> columnsForTable = new ArrayList<ColumnInfo>();
// The list of columns is ordered by table name first
String lastSeenTableName = columns.get(0).getTableName();
for (final ColumnInfo column : columns) {
if (!column.getTableName().equals(lastSeenTableName)) {
exportDataForAccountAndTable(out, columnsForTable, context);
lastSeenTableName = column.getTableName();
columnsForTable.clear();
}
columnsForTable.add(column);
}
exportDataForAccountAndTable(out, columnsForTable, context);
}
use of org.killbill.billing.util.api.ColumnInfo in project killbill by killbill.
the class DatabaseExportDao method exportDataForAccountAndTable.
private void exportDataForAccountAndTable(final DatabaseExportOutputStream out, final List<ColumnInfo> columnsForTable, final InternalTenantContext context) {
TableType tableType = TableType.OTHER;
final String tableName = columnsForTable.get(0).getTableName();
if (TableName.ACCOUNT.getTableName().equals(tableName)) {
tableType = TableType.KB_ACCOUNT;
} else if (TableName.ACCOUNT_HISTORY.getTableName().equals(tableName)) {
tableType = TableType.KB_ACCOUNT_HISTORY;
}
boolean firstColumn = true;
final StringBuilder queryBuilder = new StringBuilder("select ");
for (final ColumnInfo column : columnsForTable) {
if (!firstColumn) {
queryBuilder.append(", ");
} else {
firstColumn = false;
}
queryBuilder.append(column.getColumnName());
if (tableType == TableType.OTHER) {
if (column.getColumnName().equals(TableType.KB_PER_ACCOUNT.getAccountRecordIdColumnName())) {
tableType = TableType.KB_PER_ACCOUNT;
} else if (column.getColumnName().equals(TableType.NOTIFICATION.getAccountRecordIdColumnName())) {
tableType = TableType.NOTIFICATION;
}
}
}
// Don't export non-account specific tables
if (tableType == TableType.OTHER) {
return;
}
// Build the query - make sure to filter by account and tenant!
queryBuilder.append(" from ").append(tableName).append(" where ").append(tableType.getAccountRecordIdColumnName()).append(" = :accountRecordId and ").append(tableType.getTenantRecordIdColumnName()).append(" = :tenantRecordId");
// Notify the stream that we're about to write data for a different table
out.newTable(tableName, columnsForTable);
dbi.withHandle(new HandleCallback<Void>() {
@Override
public Void withHandle(final Handle handle) throws Exception {
final ResultIterator<Map<String, Object>> iterator = handle.createQuery(queryBuilder.toString()).bind("accountRecordId", context.getAccountRecordId()).bind("tenantRecordId", context.getTenantRecordId()).iterator();
try {
while (iterator.hasNext()) {
final Map<String, Object> row = iterator.next();
out.write(row);
}
} finally {
iterator.close();
}
return null;
}
});
}
use of org.killbill.billing.util.api.ColumnInfo in project killbill by killbill.
the class CSVExportOutputStream method newTable.
@Override
public void newTable(final String tableName, final List<ColumnInfo> columnsForTable) {
currentTableName = tableName;
final CsvSchema.Builder builder = CsvSchema.builder();
// Remove quoting of character which applies (somewhat arbitrarily, Tatu???) for string whose length is greater than MAX_QUOTE_CHECK = 24 -- See CVSWriter#_mayNeedQuotes
builder.disableQuoteChar();
builder.setColumnSeparator('|');
for (final ColumnInfo columnInfo : columnsForTable) {
builder.addColumn(columnInfo.getColumnName(), getColumnTypeFromSqlType(columnInfo.getDataType()));
}
currentCSVSchema = builder.build();
writer = mapper.writer(currentCSVSchema);
shouldWriteHeader = true;
}
Aggregations