<commit_before>package us.kbase.narrativejobservice;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;

import us.kbase.auth.AuthToken;
import us.kbase.common.service.Tuple11;
import us.kbase.common.taskqueue2.TaskQueueConfig;
import us.kbase.common.taskqueue2.TaskRunner;
import us.kbase.workspace.ListObjectsParams;
import us.kbase.workspace.ObjectData;
import us.kbase.workspace.ObjectIdentity;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.workspace.SubObjectIdentity;
import us.kbase.workspace.WorkspaceClient;

public abstract class DefaultTaskBuilder<T> implements TaskRunner<T> {
	protected File tempDir;
	//protected File dataDir;
	protected ObjectStorage storage;
	protected String ujsUrl;
	protected String njsUrl;
	protected Map<String, String> config;

	@Override
	public void init(TaskQueueConfig queueCfg, Map<String, String> configParams) {
		init(getDirParam(configParams, NarrativeJobServiceServer.CFG_PROP_SCRATCH), 
				//getDirParam(configParams, "data.dir"),
				createDefaultObjectStorage(queueCfg.getWsUrl()), 
				configParams.get(NarrativeJobServiceServer.CFG_PROP_JOBSTATUS_SRV_URL),
				configParams.get(NarrativeJobServiceServer.CFG_PROP_NJS_SRV_URL),
				configParams);
	}

	public static ObjectStorage createDefaultObjectStorage(final WorkspaceClient client) {
		return new ObjectStorage() {
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> saveObjects(
					String authToken, SaveObjectsParams params) throws Exception {
				return client.saveObjects(params);
			}
			
			@Override
			public List<ObjectData> getObjects(String authToken,
					List<ObjectIdentity> objectIds) throws Exception {
				return client.getObjects(objectIds);
			}
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> listObjects(
					String authToken, ListObjectsParams params) throws Exception {
				return client.listObjects(params);
			}
			
			@Override
		    public List<ObjectData> getObjectSubset(String authToken, List<SubObjectIdentity> objectIds) throws Exception {
				return client.getObjectSubset(objectIds);
		    }
			
			@Override
			public String getUrl() {
				return client.getURL().toString();
			}
		};
	}
	
	public static ObjectStorage createDefaultObjectStorage(final String wsUrl) {
		return new ObjectStorage() {
			private WorkspaceClient client = null;
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> saveObjects(
					String authToken, SaveObjectsParams params) throws Exception {
				return client(authToken).saveObjects(params);
			}
			
			@Override
			public List<ObjectData> getObjects(String authToken,
					List<ObjectIdentity> objectIds) throws Exception {
				return client(authToken).getObjects(objectIds);
			}
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> listObjects(
					String authToken, ListObjectsParams params) throws Exception {
				return client(authToken).listObjects(params);
			}
			
			@Override
		    public List<ObjectData> getObjectSubset(String authToken, List<SubObjectIdentity> objectIds) throws Exception {
				return client(authToken).getObjectSubset(objectIds);
		    }
			
			@Override
			public String getUrl() {
				return wsUrl;
			}
			
			private WorkspaceClient client(String authToken) throws Exception {
				if (client != null)
					return client;
				WorkspaceClient client = new WorkspaceClient(new URL(wsUrl), new AuthToken(authToken));
				client.setAuthAllowedForHttp(true);
				return client;
			}
		};
	}

	public DefaultTaskBuilder<T> init(File tempDir, ObjectStorage ws, String ujsUrl, String njsUrl,
	        Map<String, String> allConfigParams) {
		this.tempDir = tempDir;
		if (!tempDir.exists())
			tempDir.mkdir();
		this.storage = ws;<commit_msg>Missed a dep method<commit_after>package us.kbase.narrativejobservice;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;

import us.kbase.auth.AuthToken;
import us.kbase.common.service.Tuple11;
import us.kbase.common.taskqueue2.TaskQueueConfig;
import us.kbase.common.taskqueue2.TaskRunner;
import us.kbase.workspace.ListObjectsParams;
import us.kbase.workspace.ObjectData;
import us.kbase.workspace.ObjectIdentity;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.workspace.SubObjectIdentity;
import us.kbase.workspace.WorkspaceClient;

public abstract class DefaultTaskBuilder<T> implements TaskRunner<T> {
	protected File tempDir;
	//protected File dataDir;
	protected ObjectStorage storage;
	protected String ujsUrl;
	protected String njsUrl;
	protected Map<String, String> config;

	@Override
	public void init(TaskQueueConfig queueCfg, Map<String, String> configParams) {
		init(getDirParam(configParams, NarrativeJobServiceServer.CFG_PROP_SCRATCH), 
				//getDirParam(configParams, "data.dir"),
				createDefaultObjectStorage(queueCfg.getWsUrl()), 
				configParams.get(NarrativeJobServiceServer.CFG_PROP_JOBSTATUS_SRV_URL),
				configParams.get(NarrativeJobServiceServer.CFG_PROP_NJS_SRV_URL),
				configParams);
	}

	public static ObjectStorage createDefaultObjectStorage(final WorkspaceClient client) {
		return new ObjectStorage() {
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> saveObjects(
					String authToken, SaveObjectsParams params) throws Exception {
				return client.saveObjects(params);
			}
			
			@Override
			public List<ObjectData> getObjects(String authToken,
					List<ObjectIdentity> objectIds) throws Exception {
				return client.getObjects(objectIds);
			}
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> listObjects(
					String authToken, ListObjectsParams params) throws Exception {
				return client.listObjects(params);
			}
			
			@Override
		    public List<ObjectData> getObjectSubset(String authToken, List<SubObjectIdentity> objectIds) throws Exception {
				return client.getObjectSubset(objectIds);
		    }
			
			@Override
			public String getUrl() {
				return client.getURL().toString();
			}
		};
	}
	
	public static ObjectStorage createDefaultObjectStorage(final String wsUrl) {
		return new ObjectStorage() {
			private WorkspaceClient client = null;
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> saveObjects(
					String authToken, SaveObjectsParams params) throws Exception {
				return client(authToken).saveObjects(params);
			}
			
			@Override
			public List<ObjectData> getObjects(String authToken,
					List<ObjectIdentity> objectIds) throws Exception {
				return client(authToken).getObjects(objectIds);
			}
			
			@Override
			public List<Tuple11<Long, String, String, String, Long, String, Long, String, String, Long, Map<String, String>>> listObjects(
					String authToken, ListObjectsParams params) throws Exception {
				return client(authToken).listObjects(params);
			}
			
			@Override
		    public List<ObjectData> getObjectSubset(String authToken, List<SubObjectIdentity> objectIds) throws Exception {
				return client(authToken).getObjectSubset(objectIds);
		    }
			
			@Override
			public String getUrl() {
				return wsUrl;
			}
			
			private WorkspaceClient client(String authToken) throws Exception {
				if (client != null)
					return client;
				WorkspaceClient client = new WorkspaceClient(new URL(wsUrl), new AuthToken(authToken));
				client.setIsInsecureHttpConnectionAllowed(true);
				return client;
			}
		};
	}

	public DefaultTaskBuilder<T> init(File tempDir, ObjectStorage ws, String ujsUrl, String njsUrl,
	        Map<String, String> allConfigParams) {
		this.tempDir = tempDir;
		if (!tempDir.exists())
			tempDir.mkdir();
		this.storage = ws;
{'fraction_non_alphanumeric': 0.09397798936564856, 'fraction_numerical': 0.0029677259799678495, 'mean_word_length': 4.703808180535966, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3831242', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2325, 'n_words': 566}