Search in sources :

Example 1 with PreparationGetActions

use of org.talend.dataprep.command.preparation.PreparationGetActions in project data-prep by Talend.

the class PreparationAPI method updatePreparationAction.

// @formatter:off
@RequestMapping(value = "/api/preparations/{preparationId}/actions/{stepId}", method = PUT, produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Updates an action in the preparation.", notes = "Does not return any value, client may expect successful operation based on HTTP status code.")
@Timed
public void updatePreparationAction(@ApiParam(name = "preparationId", value = "Preparation id.") @PathVariable(value = "preparationId") final String preparationId, @ApiParam(name = "stepId", value = "Step id in the preparation.") @PathVariable(value = "stepId") final String stepId, @ApiParam("New content for the action.") @RequestBody final AppendStep step) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Updating preparation action at step #{} (pool: {} )...", stepId, getConnectionStats());
    }
    // get the preparation
    Preparation preparation = internalGetPreparation(preparationId);
    // get the preparation actions for up to the updated action
    final int stepIndex = preparation.getSteps().stream().map(Step::getId).collect(toList()).indexOf(stepId);
    final String parentStepId = preparation.getSteps().get(stepIndex - 1).id();
    final PreparationGetActions getActionsCommand = getCommand(PreparationGetActions.class, preparationId, parentStepId);
    // get the diff
    final DiffMetadata diffCommand = getCommand(DiffMetadata.class, preparation.getDataSetId(), preparationId, step.getActions(), getActionsCommand);
    // get the update action command and execute it
    final HystrixCommand<Void> command = getCommand(PreparationUpdateAction.class, preparationId, stepId, step, diffCommand);
    command.execute();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Updated preparation action at step #{} (pool: {} )...", stepId, getConnectionStats());
    }
}
Also used : PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) Timed(org.talend.dataprep.metrics.Timed) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with PreparationGetActions

use of org.talend.dataprep.command.preparation.PreparationGetActions in project data-prep by Talend.

the class BaseExportStrategy method getActions.

/**
 * Returns the actions for the preparation with <code>preparationId</code> between <code>startStepId</code> and
 * <code>endStepId</code>.
 *
 * @param preparationId The preparation id, if <code>null</code> or blank, returns <code>{actions: []}</code>
 * @param startStepId A step id that must exist in given preparation id.
 * @param endStepId A step id that must exist in given preparation id.
 * @return The actions that can be parsed by ActionParser.
 * @see org.talend.dataprep.transformation.api.action.ActionParser
 */
protected String getActions(String preparationId, String startStepId, String endStepId) {
    if (Step.ROOT_STEP.id().equals(startStepId)) {
        return getActions(preparationId, endStepId);
    }
    String actions;
    if (StringUtils.isBlank(preparationId)) {
        actions = "{\"actions\": []}";
    } else {
        try {
            final PreparationGetActions startStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, startStepId);
            final PreparationGetActions endStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, endStepId);
            final StringWriter actionsAsString = new StringWriter();
            final Action[] startActions = mapper.readValue(startStepActions.execute(), Action[].class);
            final Action[] endActions = mapper.readValue(endStepActions.execute(), Action[].class);
            if (endActions.length > startActions.length) {
                final Action[] filteredActions = (Action[]) ArrayUtils.subarray(endActions, startActions.length, endActions.length);
                LOGGER.debug("Reduced actions list from {} to {} action(s)", endActions.length, filteredActions.length);
                mapper.writeValue(actionsAsString, filteredActions);
            } else {
                LOGGER.debug("Unable to reduce list of actions (has {})", endActions.length);
                mapper.writeValue(actionsAsString, endActions);
            }
            return "{\"actions\": " + actionsAsString + '}';
        } catch (IOException e) {
            final ExceptionContext context = ExceptionContext.build().put("id", preparationId).put("version", endStepId);
            throw new TDPException(UNABLE_TO_READ_PREPARATION, e, context);
        }
    }
    return actions;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Action(org.talend.dataprep.api.preparation.Action) StringWriter(java.io.StringWriter) ExceptionContext(org.talend.daikon.exception.ExceptionContext) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) IOException(java.io.IOException)

Example 3 with PreparationGetActions

use of org.talend.dataprep.command.preparation.PreparationGetActions in project data-prep by Talend.

the class PreparationExportStrategyTest method setUp.

@Before
public void setUp() throws Exception {
    // Given
    mapper.registerModule(new Jdk8Module());
    strategy.setMapper(new ObjectMapper());
    when(formatRegistrationService.getByName(eq("JSON"))).thenReturn(new JsonFormat());
    final DataSetGetMetadata dataSetGetMetadata = mock(DataSetGetMetadata.class);
    when(applicationContext.getBean(eq(DataSetGetMetadata.class), anyVararg())).thenReturn(dataSetGetMetadata);
    DataSetGet dataSetGet = mock(DataSetGet.class);
    final StringWriter dataSetAsString = new StringWriter();
    DataSet dataSet = new DataSet();
    final DataSetMetadata dataSetMetadata = new DataSetMetadata("ds-1234", "", "", 0L, 0L, new RowMetadata(), "");
    final DataSetContent content = new DataSetContent();
    dataSetMetadata.setContent(content);
    dataSet.setMetadata(dataSetMetadata);
    dataSet.setRecords(Stream.empty());
    mapper.writerFor(DataSet.class).writeValue(dataSetAsString, dataSet);
    when(dataSetGet.execute()).thenReturn(new ByteArrayInputStream(dataSetAsString.toString().getBytes()));
    when(applicationContext.getBean(eq(DataSetGet.class), anyVararg())).thenReturn(dataSetGet);
    final PreparationGetActions preparationGetActions = mock(PreparationGetActions.class);
    when(preparationGetActions.execute()).thenReturn(new ByteArrayInputStream("{}".getBytes()));
    when(applicationContext.getBean(eq(PreparationGetActions.class), eq("prep-1234"), anyString())).thenReturn(preparationGetActions);
    final TransformationCacheKey cacheKey = mock(TransformationCacheKey.class);
    when(cacheKey.getKey()).thenReturn("cache-1234");
    when(cacheKeyGenerator.generateContentKey(anyString(), anyString(), anyString(), anyString(), any(), any(), anyString())).thenReturn(cacheKey);
    final ExecutableTransformer executableTransformer = mock(ExecutableTransformer.class);
    reset(transformer);
    when(transformer.buildExecutable(any(), any())).thenReturn(executableTransformer);
    when(factory.get(any())).thenReturn(transformer);
    when(contentCache.put(any(), any())).thenReturn(new NullOutputStream());
}
Also used : DataSetGet(org.talend.dataprep.command.dataset.DataSetGet) DataSet(org.talend.dataprep.api.dataset.DataSet) DataSetGetMetadata(org.talend.dataprep.command.dataset.DataSetGetMetadata) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JsonFormat(org.talend.dataprep.transformation.format.JsonFormat) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) ExecutableTransformer(org.talend.dataprep.transformation.api.transformer.ExecutableTransformer) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) DataSetContent(org.talend.dataprep.api.dataset.DataSetContent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Before(org.junit.Before)

Aggregations

PreparationGetActions (org.talend.dataprep.command.preparation.PreparationGetActions)3 StringWriter (java.io.StringWriter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1 Before (org.junit.Before)1 ExceptionContext (org.talend.daikon.exception.ExceptionContext)1 DataSet (org.talend.dataprep.api.dataset.DataSet)1 DataSetContent (org.talend.dataprep.api.dataset.DataSetContent)1 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)1 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)1 Action (org.talend.dataprep.api.preparation.Action)1 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)1 DataSetGet (org.talend.dataprep.command.dataset.DataSetGet)1 DataSetGetMetadata (org.talend.dataprep.command.dataset.DataSetGetMetadata)1 TDPException (org.talend.dataprep.exception.TDPException)1 Timed (org.talend.dataprep.metrics.Timed)1