use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class ModifyDate method applyOnColumn.
/**
* @see ColumnAction#applyOnColumn(DataSetRow, ActionContext)
*/
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
final String columnId = context.getColumnId();
final String originalValue = row.get(columnId);
if (StringUtils.isBlank(originalValue)) {
row.set(ActionsUtils.getTargetColumnId(context), originalValue);
return;
}
Map<String, String> parameters = context.getParameters();
String mode = parameters.get(MODE_PARAMETER);
long amount;
switch(mode) {
case CONSTANT_MODE:
amount = context.get(AMOUNT_CONTEXT_KEY);
break;
case OTHER_COLUMN_MODE:
String otherColId = parameters.get(SELECTED_COLUMN_PARAMETER);
if (!NumericHelper.isBigDecimal(row.get(otherColId))) {
// In this case, do not change the original value
return;
}
amount = computeAmount(row.get(otherColId));
break;
default:
throw new //
TalendRuntimeException(//
ActionErrorCodes.BAD_ACTION_PARAMETER, ExceptionContext.build().put("paramName", OtherColumnParameters.CONSTANT_MODE));
}
try {
final DatePattern outputPattern = new DatePattern(context.get(PATTERN_CONTEXT_KEY));
LocalDateTime date = Providers.get().parse(originalValue, context.getRowMetadata().getById(columnId));
date = date.plus(amount, context.get(UNIT_CONTEXT_KEY));
row.set(ActionsUtils.getTargetColumnId(context), outputPattern.getFormatter().format(date));
} catch (DateTimeException e) {
row.set(ActionsUtils.getTargetColumnId(context), originalValue);
// cannot parse the date, let's leave it as is
LOGGER.debug("Unable to parse date {}.", originalValue, e);
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.
the class PreparationUtils method prettyPrint.
private static void prettyPrint(PreparationRepository repository, String stepId, OutputStream out) {
if (stepId == null) {
return;
}
try {
Step step = repository.get(stepId, Step.class);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.append("\t\tStep (").append(step.id()).append(")").append("\n");
writer.flush();
PreparationActions blob = repository.get(step.getContent(), PreparationActions.class);
prettyPrint(blob, out);
if (step.getParent() != null) {
prettyPrint(repository, step.getParent(), out);
}
} 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 PreparationUtils method prettyPrint.
public static void prettyPrint(PreparationRepository repository, Preparation preparation, OutputStream out) {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.append("Preparation (").append(preparation.id()).append(")\n");
writer.append("\tData set: ").append(preparation.getDataSetId()).append("\n");
writer.append("\tAuthor: ").append(preparation.getAuthor()).append("\n");
writer.append("\tCreation date: ").append(String.valueOf(preparation.getCreationDate())).append("\n");
writer.append("\tSteps:").append("\n");
writer.flush();
prettyPrint(repository, preparation.getHeadId(), out);
} 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 NodeBuilder method to.
/**
* Append a new node at the end, with a custom link
*
* @param linkFunction A function that create the link from the previous nodes
* @param node The new node to append
*/
public NodeBuilder to(final Function<Node[], Link> linkFunction, final Node node) {
try {
state = state.next(linkFunction);
state = state.next(node);
} catch (IllegalStateException e) {
throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, new Exception("Each to() must be followed by node().", e));
}
return this;
}
use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class SimpleFileIOOutputErrorTest method testTryToOverwrite.
/**
* Basic unit test using all default values (except for the path) on an in-memory DFS cluster.
*/
@Test
public void testTryToOverwrite() throws IOException, URISyntaxException {
Path parent = new Path(mini.newFolder().toString());
Path dst = new Path(parent, "output");
String fileSpec = mini.getLocalFs().getUri().resolve(dst.toUri()).toString();
// Write something to the file before trying to run.
try (OutputStream out = mini.getLocalFs().create(new Path(dst, "part-00000"))) {
out.write(0);
}
// Trying to write to an existing destination throws an exception.
thrown.expect(TalendRuntimeException.class);
thrown.expect(hasProperty("code", is(SimpleFileIOErrorCode.OUTPUT_ALREADY_EXISTS)));
thrown.expectMessage("The path " + fileSpec + " already exists. Please remove it manually.");
// Now try using the component.
try {
// Configure the component.
SimpleFileIOOutputProperties props = SimpleFileIOOutputRuntimeTest.createOutputComponentProperties();
props.getDatasetProperties().path.setValue(fileSpec);
// Create the runtime.
SimpleFileIOOutputRuntime runtime = new SimpleFileIOOutputRuntime();
runtime.initialize(null, props);
// Use the runtime in a direct pipeline to test.
final Pipeline p = beam.createPipeline();
PCollection<IndexedRecord> input = //
p.apply(//
Create.of(//
ConvertToIndexedRecord.convertToAvro(new String[] { "1", "one" }), //
ConvertToIndexedRecord.convertToAvro(new String[] { "2", "two" })));
input.apply(runtime);
// And run the test.
p.run().waitUntilFinish();
} catch (Pipeline.PipelineExecutionException e) {
if (e.getCause() instanceof TalendRuntimeException)
throw (TalendRuntimeException) e.getCause();
throw e;
}
}
Aggregations