use of org.talend.dataprep.api.preparation.AppendStep in project data-prep by Talend.
the class ReorderStepsUtils method renameCreatedColumns.
/**
* Renames the created columns according to their order of creation starting from the minimum created column.
*
* @param appendSteps the specified list of append steps
*/
void renameCreatedColumns(List<AppendStep> appendSteps) {
final List<String> createdColumns = appendSteps.stream().flatMap(step -> step.getDiff().getCreatedColumns().stream()).collect(Collectors.toList());
if (createdColumns.isEmpty()) {
return;
}
// retrieve the minimum index
final int firstIndex = Integer.parseInt(createdColumns.stream().min(String::compareTo).get());
// map old created column names to the new ones
final DecimalFormat format = new DecimalFormat("0000");
Map<String, String> rename = new HashMap<>();
IntStream.range(0, createdColumns.size()).forEach(i -> {
rename.put(createdColumns.get(i), format.format(i + firstIndex));
});
// walk over the list of append steps and change names (id) of created columns
appendSteps.stream().forEach(step -> {
// first for created columns
List<String> renamedCreatedColumns = step.getDiff().getCreatedColumns().stream().map(s -> {
if (rename.containsKey(s)) {
return rename.get(s);
} else {
return s;
}
}).collect(Collectors.toList());
// then within actions
step.getDiff().setCreatedColumns(renamedCreatedColumns);
for (Action action : step.getActions()) {
final Map<String, String> parameters = action.getParameters();
final String columnId = parameters.get(ImplicitParameters.COLUMN_ID.getKey());
if (rename.containsKey(columnId)) {
parameters.put(ImplicitParameters.COLUMN_ID.getKey(), rename.get(columnId));
}
}
});
}
use of org.talend.dataprep.api.preparation.AppendStep in project data-prep by Talend.
the class ReorderStepsUtilsTest method renameCreatedColumns_reorderCorrectlyColumnIdsAndUpdateRefs.
@Test
public void renameCreatedColumns_reorderCorrectlyColumnIdsAndUpdateRefs() throws Exception {
List<AppendStep> steps = new ArrayList<>();
AppendStep firstStep = createAppendStep("0002", singletonList("0004"));
steps.add(firstStep);
AppendStep secondStep = createAppendStep("0002", singletonList("0003"));
steps.add(secondStep);
AppendStep thirdStep = createAppendStep("0003", emptyList());
steps.add(thirdStep);
reorderStepsUtils.renameCreatedColumns(steps);
assertEquals("0003", firstStep.getDiff().getCreatedColumns().iterator().next());
assertEquals("0004", secondStep.getDiff().getCreatedColumns().iterator().next());
assertEquals("0004", thirdStep.getActions().iterator().next().getParameters().get(ImplicitParameters.COLUMN_ID.getKey()));
}
use of org.talend.dataprep.api.preparation.AppendStep in project data-prep by Talend.
the class ReorderStepsUtilsTest method createAppendStep.
private static AppendStep createAppendStep(String usedColumnId, List<String> createdColumns) {
AppendStep firstStep = new AppendStep();
StepDiff firstStepDiff = new StepDiff();
firstStepDiff.setCreatedColumns(createdColumns);
firstStep.setDiff(firstStepDiff);
Action firstStepAction = new Action();
MixedContentMap parameters = new MixedContentMap();
parameters.put(ImplicitParameters.COLUMN_ID.getKey(), usedColumnId);
firstStepAction.setParameters(parameters);
firstStep.setActions(asList(firstStepAction));
return firstStep;
}
use of org.talend.dataprep.api.preparation.AppendStep in project data-prep by Talend.
the class PreparationAPITest method testMoveStep.
@Test
public void testMoveStep() throws Exception {
final String datasetId = testClient.createDataset("preview/preview_dataset.csv", "testPreview");
String testPrepId = testClient.createPreparationFromDataset(datasetId, "testPrep", home.getId());
AppendStep appendStep = new AppendStep();
appendStep.setActions(Arrays.asList(buildAction("uppercase", buildParametersMap("column_id", "0002", "column_name", "lastname", "scope", "column")), buildAction("uppercase", buildParametersMap("column_id", "0001", "column_name", "firstname", "scope", "column"))));
appendStepsToPrep(testPrepId, appendStep);
// Adding steps
EnrichedPreparation testPrepDetails = getPreparationDetails(testPrepId);
List<String> stepsCreated = testPrepDetails.getSteps();
String rootStep = stepsCreated.get(0);
String secondStep = stepsCreated.get(2);
// changing steps order
changePreparationStepsOrder(testPrepId, rootStep, secondStep);
EnrichedPreparation testPrepDetailsAfter = getPreparationDetails(testPrepId);
assertEquals(testPrepDetailsAfter.getActions().get(0), testPrepDetails.getActions().get(1));
assertEquals(testPrepDetailsAfter.getActions().get(1), testPrepDetails.getActions().get(0));
}
use of org.talend.dataprep.api.preparation.AppendStep in project data-prep by Talend.
the class EntityBuilder method step.
public static AppendStep step(StepDiff diff, Action... actions) {
AppendStep appendStep = new AppendStep();
appendStep.setDiff(diff);
appendStep.setActions(Arrays.asList(actions));
return appendStep;
}
Aggregations