/*
 * Copyright 2014 OrgSync
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.orgsync.api;

import com.orgsync.api.model.ApiError;
import com.orgsync.api.model.accounts.AccountFull;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * A task that handles the full life-cycle of getting an export.
 *
 * @author steffyj
 */
/* package */class DataExportTask<T> implements Callable<ApiResponse<T>> {

    private final ExportsResourceImpl exports;
    private final String exportType;

    public DataExportTask(ExportsResourceImpl exports, String exportType) {
        this.exports = exports;
        this.exportType = exportType;
    }

    @Override
    public ApiResponse<T> call() throws Exception {
        ApiResponse<ExportsResourceImpl.ExportRequest> response = exports.requestToken(exportType).get();

        if (!response.isSuccess()) {
            return ApiResponseFactory.error(response);
        } else if (response.getStatus() == 202) {
            return ApiResponseFactory.error(response.getStatus(), new ApiError("Export already in progress!"));
        }

        return ApiResponseFactory.error(response.getStatus(), new ApiError(response.getResult().getExportToken()));
    }

}

@@ -761,16 +761,64 @@
 llable;%0A
+import java.util.concurrent.ExecutionException;%0A
 import j
@@ -1391,16 +1391,8 @@
 e = 
-exports.
 requ
@@ -1404,27 +1404,66 @@
 ken(
-exportType).get();%0A
+);%0A%0A        // We have failed to get a good token...  bail
 %0A   
@@ -1564,53 +1564,73 @@
    %7D
- else if (response.getStatus() == 202) %7B%0A    
+%0A%0A        String token = response.getResult().getExportToken();%0A%0A
     
@@ -1704,49 +1704,263 @@
 ror(
-%22Export already in progress!%22)
+token));%0A    %7D%0A%0A    private ApiResponse%3CExportsResourceImpl.ExportRequest%3E requestToken() throws ExecutionException, InterruptedException %7B%0A        ApiResponse%3CExportsResourceImpl.ExportRequest%3E response = exports.requestToken(exportType).get(
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -1955,26 +1955,130 @@
         
-%7D%0A
+// 202 - Accepted means it is in progress...%0A        // we don't want to have two futures working on this.
 %0A       
  return 
@@ -2065,32 +2065,35 @@
 his.%0A       
+ //
  return 
 ApiResponseF
@@ -2084,114 +2084,273 @@
 urn 
-ApiResponseFactory.error(response.getStatus(), new ApiError(response.getResult().getExportToken()))
+it as a failure%0A        if (response.getStatus() == 202) %7B%0A            response = ApiResponseFactory.error(response.getStatus(), new ApiError(%22Export already in progress!%22));%0A        %7D%0A%0A        // normal success or fail, return as is.%0A        return response
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -2346,11 +2346,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

refactor the data export task