Search in sources :

Example 1 with UNABLE_TO_READ_PREPARATION

use of org.talend.dataprep.exception.error.PreparationErrorCodes.UNABLE_TO_READ_PREPARATION in project data-prep by Talend.

the class DiffMetadata method onExecute.

private HttpRequestBase onExecute(final String dataSetId, final String preparationId, final List<Action> actionsToAdd) {
    // original actions (currently applied on the preparation)
    final List<Action> originalActions;
    try {
        originalActions = objectMapper.readerFor(new TypeReference<List<Action>>() {
        }).readValue(getInput());
    } catch (final IOException e) {
        throw new TDPException(UNABLE_TO_READ_PREPARATION, e, withBuilder().put("id", preparationId).build());
    }
    // prepare the preview parameters out of the preparation actions
    final List<PreviewParameters> previewParameters = IntStream.range(0, actionsToAdd.size()).mapToObj((index) -> {
        try {
            // base actions = original actions + actions to add from 0 to index
            final List<Action> previousActionsToAdd = actionsToAdd.subList(0, index);
            final List<Action> baseActions = new ArrayList<>(originalActions);
            baseActions.addAll(previousActionsToAdd);
            // diff actions actions = base actions + the action to add for diff
            final Action singleActionToAdd = actionsToAdd.get(index);
            final List<Action> diffActions = new ArrayList<>(baseActions);
            diffActions.add(singleActionToAdd);
            return new // 
            PreviewParameters(// 
            serializeActions(baseActions), // 
            serializeActions(diffActions), // 
            dataSetId, // 
            null, // 
            null, HEAD);
        } catch (IOException e) {
            throw new TDPException(UNABLE_TO_READ_PREPARATION, e, withBuilder().put("id", preparationId).build());
        }
    }).collect(toList());
    // create the http action to perform
    try {
        final String uri = transformationServiceUrl + "/transform/diff/metadata";
        final HttpPost transformationCall = new HttpPost(uri);
        transformationCall.setEntity(new StringEntity(objectMapper.writer().writeValueAsString(previewParameters), APPLICATION_JSON));
        return transformationCall;
    } catch (JsonProcessingException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : IntStream(java.util.stream.IntStream) HttpPost(org.apache.http.client.methods.HttpPost) GenericCommand(org.talend.dataprep.command.GenericCommand) TDPException(org.talend.dataprep.exception.TDPException) PreviewParameters(org.talend.dataprep.transformation.preview.api.PreviewParameters) SCOPE_PROTOTYPE(org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE) Scope(org.springframework.context.annotation.Scope) UNABLE_TO_READ_PREPARATION(org.talend.dataprep.exception.error.PreparationErrorCodes.UNABLE_TO_READ_PREPARATION) ArrayList(java.util.ArrayList) ChainedCommand(org.talend.dataprep.api.service.command.common.ChainedCommand) Action(org.talend.dataprep.api.preparation.Action) TypeReference(com.fasterxml.jackson.core.type.TypeReference) APPLICATION_JSON(org.apache.http.entity.ContentType.APPLICATION_JSON) HEAD(org.talend.dataprep.api.export.ExportParameters.SourceType.HEAD) Defaults.pipeStream(org.talend.dataprep.command.Defaults.pipeStream) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) StringEntity(org.apache.http.entity.StringEntity) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Component(org.springframework.stereotype.Component) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ExceptionContext.withBuilder(org.talend.daikon.exception.ExceptionContext.withBuilder) InputStream(java.io.InputStream) HttpPost(org.apache.http.client.methods.HttpPost) Action(org.talend.dataprep.api.preparation.Action) IOException(java.io.IOException) TDPException(org.talend.dataprep.exception.TDPException) PreviewParameters(org.talend.dataprep.transformation.preview.api.PreviewParameters) StringEntity(org.apache.http.entity.StringEntity) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 APPLICATION_JSON (org.apache.http.entity.ContentType.APPLICATION_JSON)1 StringEntity (org.apache.http.entity.StringEntity)1 SCOPE_PROTOTYPE (org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE)1 Scope (org.springframework.context.annotation.Scope)1 HttpStatus (org.springframework.http.HttpStatus)1 Component (org.springframework.stereotype.Component)1 ExceptionContext.withBuilder (org.talend.daikon.exception.ExceptionContext.withBuilder)1 HEAD (org.talend.dataprep.api.export.ExportParameters.SourceType.HEAD)1 Action (org.talend.dataprep.api.preparation.Action)1 ChainedCommand (org.talend.dataprep.api.service.command.common.ChainedCommand)1