Search in sources :

Example 1 with AppendStep

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));
            }
        }
    });
}
Also used : IntStream(java.util.stream.IntStream) ImplicitParameters(org.talend.dataprep.transformation.actions.common.ImplicitParameters) StringUtils(org.apache.commons.lang.StringUtils) DecimalFormat(java.text.DecimalFormat) Set(java.util.Set) HashMap(java.util.HashMap) DeleteColumn(org.talend.dataprep.transformation.actions.column.DeleteColumn) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) AppendStep(org.talend.dataprep.api.preparation.AppendStep) Map(java.util.Map) Action(org.talend.dataprep.api.preparation.Action) Action(org.talend.dataprep.api.preparation.Action) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat)

Example 2 with AppendStep

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()));
}
Also used : ArrayList(java.util.ArrayList) AppendStep(org.talend.dataprep.api.preparation.AppendStep) Test(org.junit.Test)

Example 3 with AppendStep

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;
}
Also used : Action(org.talend.dataprep.api.preparation.Action) StepDiff(org.talend.dataprep.api.preparation.StepDiff) MixedContentMap(org.talend.dataprep.api.preparation.MixedContentMap) AppendStep(org.talend.dataprep.api.preparation.AppendStep)

Example 4 with AppendStep

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));
}
Also used : EnrichedPreparation(org.talend.dataprep.api.service.api.EnrichedPreparation) AppendStep(org.talend.dataprep.api.preparation.AppendStep) Test(org.junit.Test)

Example 5 with AppendStep

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;
}
Also used : AppendStep(org.talend.dataprep.api.preparation.AppendStep)

Aggregations

AppendStep (org.talend.dataprep.api.preparation.AppendStep)5 Test (org.junit.Test)2 Action (org.talend.dataprep.api.preparation.Action)2 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 StringUtils (org.apache.commons.lang.StringUtils)1 Component (org.springframework.stereotype.Component)1 MixedContentMap (org.talend.dataprep.api.preparation.MixedContentMap)1 StepDiff (org.talend.dataprep.api.preparation.StepDiff)1 EnrichedPreparation (org.talend.dataprep.api.service.api.EnrichedPreparation)1 DeleteColumn (org.talend.dataprep.transformation.actions.column.DeleteColumn)1 ImplicitParameters (org.talend.dataprep.transformation.actions.common.ImplicitParameters)1