package org.talend.dataprep.api.service.command.dataset;

import static org.talend.dataprep.api.service.command.common.Defaults.asString;
import static org.talend.dataprep.api.service.command.common.Defaults.emptyString;

import java.io.InputStream;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.InputStreamEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.talend.dataprep.api.service.PreparationAPI;
import org.talend.dataprep.api.service.command.common.GenericCommand;
import org.talend.dataprep.cache.ContentCache;
import org.talend.dataprep.cache.ContentCacheKey;
import org.talend.dataprep.exception.TDPException;
import org.talend.dataprep.exception.error.APIErrorCodes;

/**
 * Command in charge of mostly updating a dataset content.
 */
@Component
@Scope("request")
public class CreateOrUpdateDataSet extends GenericCommand<String> {

    /** The content cache. */
    @Autowired
    private ContentCache contentCache;

    /**
     * Private constructor.
     *
     * @param client the http client.
     * @param id the dataset id.
     * @param name the dataset name.
     * @param dataSetContent the new dataset content.
     */
    private CreateOrUpdateDataSet(HttpClient client, String id, String name, InputStream dataSetContent) {
        super(PreparationAPI.DATASET_GROUP, client);
        execute(() -> {
            final HttpPut put = new HttpPut(datasetServiceUrl + "/datasets/" + id + "/raw/?name=" + name); //$NON-NLS-1$ //$NON-NLS-2$
            put.setEntity(new InputStreamEntity(dataSetContent));
            return put;
        });
        onError(e -> new TDPException(APIErrorCodes.UNABLE_TO_CREATE_OR_UPDATE_DATASET, e));
        on(HttpStatus.NO_CONTENT, HttpStatus.ACCEPTED).then(emptyString());
        on(HttpStatus.OK).then((req, res) -> {
            contentCache.evict(new ContentCacheKey(id)); // clear the cache (dataset and all its preparations)
            return asString().apply(req, res); // Return response as String
        });
    }
}

@@ -242,16 +242,52 @@
 tStream;
+%0Aimport java.net.URISyntaxException;
 %0A%0Aimport
@@ -369,16 +369,64 @@
 ttpPut;%0A
+import org.apache.http.client.utils.URIBuilder;%0A
 import o
@@ -1005,16 +1005,77 @@
 orCodes;
+%0Aimport org.talend.dataprep.exception.error.CommonErrorCodes;
 %0A%0A/**%0A *
@@ -1731,105 +1731,244 @@
     
-final HttpPut put = new HttpPut(datasetServiceUrl + %22/datasets/%22 + id + %22/raw/?name=%22 + name
+try %7B%0A                URIBuilder uriBuilder = new URIBuilder(datasetServiceUrl + %22/datasets/%22 + id + %22/raw/%22) //%0A                    .addParameter( %22name%22, name );%0A                final HttpPut put = new HttpPut(uriBuilder.build()
 ); //
+ 
 $NON
@@ -1989,16 +1989,20 @@
 -NLS-2$%0A
+    
         
@@ -2067,24 +2067,28 @@
             
+    
 return put;%0A
@@ -2087,16 +2087,157 @@
 rn put;%0A
+            %7D catch (URISyntaxException e) %7B%0A                throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);%0A            %7D%0A
         

fix issue when dataset name has spaces