/*
 * Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
 */
package com.gooddata.dataset;

import com.gooddata.AbstractService;
import com.gooddata.gdc.DataStoreService;
import com.gooddata.project.Project;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.HttpMessageConverterExtractor;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

/**
 */
public class DatasetService extends AbstractService {

    private static final String MANIFEST_FILE_NAME = "upload_info.json";
    private final DataStoreService dataStoreService;
    private final static ObjectMapper mapper = new ObjectMapper();

    public DatasetService(RestTemplate restTemplate, DataStoreService dataStoreService) {
        super(restTemplate);
        this.dataStoreService = dataStoreService;
    }

    public DatasetManifest getDatasetManifest(Project project, String datasetId) {
        return restTemplate.getForObject(DatasetManifest.URI, DatasetManifest.class, project.getId(), datasetId);
    }

    public void loadDataset(Project project, InputStream dataset, DatasetManifest manifest) {
        final String dirPath = "/" + project.getId() + "_" + RandomStringUtils.randomAlphabetic(3) + "/";
        try {
            dataStoreService.upload(dirPath + manifest.getFile(), dataset);
            final String manifestJson = mapper.writeValueAsString(manifest);
            dataStoreService.upload(dirPath + MANIFEST_FILE_NAME, IOUtils.toInputStream(manifestJson));

            final PullTask pullTask = restTemplate.postForObject(Pull.URI, new Pull(dirPath), PullTask.class, project.getId());
            final PullTaskStatus taskStatus = poll(URI.create(pullTask.getUri()), new ConditionCallback() {
                @Override
                public boolean finished(ClientHttpResponse response) throws IOException {
                    final PullTaskStatus status = extractData(response, PullTaskStatus.class);
                    return status.isFinished();
                }
            }, PullTaskStatus.class);
            if (!taskStatus.isSuccess()) {
                throw new DatasetException("ETL pull finished with status " + taskStatus.getStatus());
            }
        } catch (IOException e) {
            throw new DatasetException("Unable to serialize manifest", e);
        } finally {
            dataStoreService.delete(dirPath);
        }

    }

    public void loadDataset(Project project, String datasetId, InputStream dataset) {
        loadDataset(project, dataset, getDatasetManifest(project, datasetId));
    }
}

@@ -418,77 +418,8 @@
 se;%0A
-import org.springframework.web.client.HttpMessageConverterExtractor;%0A
 impo
@@ -1310,81 +1310,27 @@
 h = 
-%22/%22 + project.getId() + %22_%22 + RandomStringUtils.randomAlphabetic(3) + %22/%22
+getDirPath(project)
 ;%0A  
@@ -2495,32 +2495,317 @@
       %7D%0A%0A    %7D%0A%0A
+    private String getDirPath(Project project) %7B%0A        return new StringBuilder(%22/%22)%0A                .append(project.getId())%0A                .append(%22_%22)%0A                .append(RandomStringUtils.randomAlphabetic(3))%0A                .append(%22/%22)%0A                .toString();%0A    %7D%0A%0A
     public void 

fix string concatenation