package com.sailthru.client.params.job;

import com.google.gson.reflect.TypeToken;

import com.sailthru.client.SailthruUtil;
import com.sailthru.client.params.ApiFileParams;
import com.sailthru.client.params.query.Query;

import java.io.File;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Prajwal Tuladhar <praj@sailthru.com>
 */
public class UpdateJob extends Job implements ApiFileParams {
    
    private static final String JOB = "update";
    
    protected String emails;
    protected String url;
    protected transient File file;
    protected Map<String, Object> update;
    protected Map<String, Object> query;
    
    public UpdateJob() {
        this.job = JOB;
        this.update = new HashMap<String, Object>();
    }
    
    public UpdateJob setEmails(List<String> emails) {
        this.emails = SailthruUtil.arrayListToCSV(emails);
        return this;
    }
    
    public UpdateJob setUrl(String url) {
        this.url = url;
        return this;
    }
    
    public UpdateJob setUrl(URI url) {
        this.url = url.toString();
        return this;
    }
    
    public UpdateJob setFile(File file) {
        this.file = file;
        return this;
    }
    
    public UpdateJob setFile(String file) {
        this.file = new File(file);
        return this;
    }
    
    public UpdateJob setUpdate(Map<String, Object> update) {
        this.update = update;
        return this;
    }
    
    public UpdateJob setUpdateVars(Map<String, Object> vars) {
        this.update.put("vars", vars);
        return this;
    }
    
    public UpdateJob setUpdateLists(Map<String, Integer> lists) {
        this.update.put("lists", lists);
        return this;
    }
    
    public UpdateJob setUpdateOptout(String optout) {
        this.update.put("optout", optout);
        return this;
    }
    
    public UpdateJob setQuery(Query query) {
        this.query = query.toHashMap();
        return this;
    }
    
    @Override
    public Type getType() {
        return new TypeToken<UpdateJob>() {}.getType();
    }

    public Map<String, File> getFileParams() {
        Map<String, File> files = new HashMap<String, File>();
        if (this.file != null) {
            files.put("file", this.file);
        }
        return files;
    }
}

@@ -218,28 +218,155 @@
 ry;%0A
-%0Aimport java.io.File
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException
 ;%0Aim
@@ -491,67 +491,8 @@
 p;%0A%0A
-/**%0A *%0A * @author Prajwal Tuladhar %3Cpraj@sailthru.com%3E%0A */%0A
 publ
@@ -549,24 +549,98 @@
 arams %7B%0A    
+protected static Logger logger = LoggerFactory.getLogger(UpdateJob.class);
 %0A    private
@@ -676,20 +676,16 @@
 pdate%22;%0A
-    
 %0A    pro
@@ -756,24 +756,35 @@
 ansient File
+InputStream
  file;%0A    p
@@ -861,20 +861,16 @@
  query;%0A
-    
 %0A    pub
@@ -1379,25 +1379,160 @@
    t
-his.file = 
+ry %7B%0A            this.file = new FileInputStream(
 file
+)
 ;%0A
+        %7D catch (FileNotFoundException e) %7B%0A            logger.error(e.getMessage());%0A        %7D%0A
     
@@ -1595,32 +1595,50 @@
 (String file) %7B%0A
+        try %7B%0A    
         this.fil
@@ -1649,23 +1649,130 @@
 new File
-(file);
+InputStream(file);%0A        %7D catch (FileNotFoundException e) %7B%0A            logger.error(e.getMessage());%0A        %7D
 %0A       
@@ -2570,16 +2570,27 @@
 ng, File
+InputStream
 %3E getFil
@@ -2625,16 +2625,27 @@
 ng, File
+InputStream
 %3E files 
@@ -2670,16 +2670,27 @@
 ng, File
+InputStream
 %3E();%0A   

Change implementation from File to FileInputStream for Update job