// ============================================================================
// Copyright (C) 2006-2016 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// https://github.com/Talend/data-prep/blob/master/LICENSE
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================

package org.talend.dataprep.command.preparation;

import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
import static org.talend.dataprep.command.Defaults.asString;
import static org.talend.dataprep.exception.error.CommonErrorCodes.UNEXPECTED_EXCEPTION;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.talend.dataprep.api.preparation.Step;
import org.talend.dataprep.command.GenericCommand;
import org.talend.dataprep.exception.TDPException;

import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * Hystrix command used to update a step row etadata.
 */
@Component
@Scope("request")
public class UpdateStepRowMetadata extends GenericCommand<String> {

    /**
     * Private constructor to ensure the IoC.
     *
     * @param preparationId the preparation id to update the step from.
     * @param steps the steps to update.
     */
    private UpdateStepRowMetadata(String preparationId, List<Step> steps) {
        super(PREPARATION_GROUP);
        execute(() -> onExecute(preparationId, steps));
        on(HttpStatus.OK).then(asString());
    }

    private HttpRequestBase onExecute(String preparationId, List<Step> steps) {
        try {
            final String stepsAsJson = objectMapper.writeValueAsString(steps);
            final HttpPut updater = new HttpPut(preparationServiceUrl + "/preparations/" + preparationId + "/steps");
            updater.setHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE);
            updater.setEntity(new StringEntity(stepsAsJson));
            updater.setHeader(ACCEPT, TEXT_PLAIN_VALUE);
            return updater;
        } catch (UnsupportedEncodingException | JsonProcessingException e) {
            throw new TDPException(UNEXPECTED_EXCEPTION, e);
        }
    }
}

@@ -557,47 +557,55 @@
 org.
-springframework.http.HttpHeaders.ACCEPT
+apache.http.entity.ContentType.APPLICATION_JSON
 ;%0Aim
@@ -744,75 +744,8 @@
 UE;%0A
-import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;%0A
 impo
@@ -895,53 +895,8 @@
 N;%0A%0A
-import java.io.UnsupportedEncodingException;%0A
 impo
@@ -2400,66 +2400,27 @@
 Json
-));%0A            updater.setHeader(ACCEPT, TEXT_PLAIN_VALUE
+, APPLICATION_JSON)
 );%0A 
@@ -2467,39 +2467,8 @@
 ch (
-UnsupportedEncodingException %7C 
 Json

set the string entity charset to JSON