use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class PreparationUtils method prettyPrint.
private static void prettyPrint(PreparationActions blob, OutputStream out) {
if (blob == null) {
return;
}
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.append("\t\t\tContent: ").append("\n");
writer.append("======").append("\n");
writer.append(blob.toString()).append("\n");
writer.append("======").append("\n");
writer.flush();
} catch (IOException e) {
throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class ActionParser method parse.
/**
* Return the parsed actions ready to be run.
*
* @param actions the actions to be parsed as string.
* @return the parsed actions.
* @throws IllegalArgumentException if <code>actions</code> is null.
*/
public List<RunnableAction> parse(String actions) {
if (actions == null) {
// Actions cannot be null (but can be empty string for no op actions).
throw new IllegalArgumentException("Actions parameter can not be null.");
}
if (StringUtils.isEmpty(actions)) {
return Collections.emptyList();
}
try {
// Parse action JSON
final Actions parsedActions = mapper.reader(Actions.class).readValue(actions);
final List<Action> allActions = parsedActions.getActions();
// Create closures from parsed actions
final List<RunnableAction> builtActions = new ArrayList<>(allActions.size() + 1);
//
allActions.stream().filter(//
parsedAction -> parsedAction != null && parsedAction.getName() != null).forEach(parsedAction -> {
String actionNameLowerCase = parsedAction.getName().toLowerCase();
final ActionDefinition metadata = actionRegistry.get(actionNameLowerCase);
builtActions.add(factory.create(metadata, parsedAction.getParameters()));
});
// all set: wraps everything and return to caller
return builtActions;
} catch (TalendRuntimeException tpe) {
// leave TDPException as is
throw tpe;
} catch (Exception e) {
throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_JSON, e);
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class TypeDetectionNode method store.
// Store row in temporary file
private void store(DataSetRow row, List<ColumnMetadata> columns) {
try {
generator.writeStartObject();
columns.forEach(column -> {
try {
generator.writeStringField(column.getId(), row.get(column.getId()));
} catch (IOException e) {
throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
}
});
if (row.isDeleted()) {
generator.writeBooleanField("_deleted", true);
}
final Optional<Long> tdpId = Optional.ofNullable(row.getTdpId());
if (tdpId.isPresent()) {
generator.writeNumberField(FlagNames.TDP_ID, tdpId.get());
}
for (Map.Entry<String, String> entry : row.getInternalValues().entrySet()) {
generator.writeStringField(entry.getKey(), entry.getValue());
}
generator.writeEndObject();
} catch (IOException e) {
throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class ActionMetadataValidationTest method checkScopeConsistency_should_throw_exception_on_missing_scope.
@Test
public void checkScopeConsistency_should_throw_exception_on_missing_scope() throws Exception {
// given
final Map<String, String> parameters = new HashMap<>();
parameters.put("column_id", "0001");
ActionDefinition actionMock = new ActionMetadataExtendingColumn();
// when
try {
validator.checkScopeConsistency(actionMock, parameters);
fail("should have thrown TDP exception because param scope is missing");
}// then
catch (final TalendRuntimeException e) {
assertThat(e.getCode(), is(MISSING_ACTION_SCOPE));
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class ReorderColumn method compile.
@Override
public void compile(ActionContext actionContext) {
super.compile(actionContext);
if (ActionsUtils.doesCreateNewColumn(actionContext.getParameters(), CREATE_NEW_COLUMN_DEFAULT)) {
ActionsUtils.createNewColumn(actionContext, singletonList(ActionsUtils.additionalColumn()));
}
Map<String, String> parameters = actionContext.getParameters();
RowMetadata rowMetadata = actionContext.getRowMetadata();
String targetColumnId = parameters.get(SELECTED_COLUMN_PARAMETER);
ColumnMetadata targetColumn = rowMetadata.getById(targetColumnId);
if (targetColumn == null) {
return;
}
String originColumnId = parameters.get(COLUMN_ID.getKey());
// column id may be different from index in the list
// we cannot rely on id as index
// so we have to find first the origin and target index
int index = 0, originIndex = 0, targetIndex = 0;
for (ColumnMetadata columnMetadata : rowMetadata.getColumns()) {
if (StringUtils.equals(columnMetadata.getId(), originColumnId)) {
originIndex = index;
}
if (StringUtils.equals(columnMetadata.getId(), targetColumnId)) {
targetIndex = index;
}
index++;
}
// now we have both index so we can iterate again and swap few columns
// we have different case as target can he lower than origin or the opposite
boolean forwardMove = targetIndex > originIndex;
try {
if (forwardMove) {
for (index = originIndex; index < targetIndex; index++) {
swapColumnMetadata(rowMetadata.getColumns().get(index), rowMetadata.getColumns().get(index + 1));
}
} else {
for (index = originIndex; index > targetIndex; index--) {
swapColumnMetadata(rowMetadata.getColumns().get(index), rowMetadata.getColumns().get(index - 1));
}
}
} catch (Exception e) {
LOGGER.debug("cannot swap columns: {}", e.getMessage());
throw new TalendRuntimeException(UNEXPECTED_EXCEPTION, build().put("message", e.getMessage()));
}
}
Aggregations