All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.runner.graphql.Operation Maven / Gradle / Ivy

The newest version!
package com.xlrit.gears.runner.graphql;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.xlrit.gears.base.choice.SearchParts;

public class Operation {
	public static final Operation clearCurrentDateTime = new Operation("Clear Current DateTime", "mutation { clearCurrentDateTime }");
	public static final Operation currentUser = new Operation("Current User", "query { currentUser { username } }");
	public static final Operation getTasks = new Operation("Get Tasks", "query { groupTasks { id } }");
	public static final Operation getDeployments = new Operation("Get Deployments", "query { deployments { id, name } }");

	private static final Map NO_VARIABLES = null;
	private static final List NO_FILES = null;

	public final String name;
	public final String query;
	public final Map variables;
	public final List files;

	private Operation(String name, String query) {
		this(name, query, NO_VARIABLES);
	}

	private Operation(String name, String query, Map variables) {
		this(name, query, variables, NO_FILES);
	}

	private Operation(String name, String query, Map variables, List files) {
		this.name = name;
		this.query = query;
		this.variables = variables;
		this.files = files;
	}

	@Override
	public String toString() {
		return "Operation[" + name + "]";
	}

	private static ArrayNode createNulls(int amount) {
		ArrayNode arrayNode = new ObjectMapper().createArrayNode();
		for (int i = 0; i < amount; i++) arrayNode.add(NullNode.instance);
		return arrayNode;
	}

	public static Operation loadData(List files) {
		String doc = "mutation LoadData($files: [Upload!]!) { loadData(files: $files) }";

		Map variables = Map.of("files", createNulls(files.size()));

		return new Operation(String.format("Load data from %d files", files.size()), doc, variables, files);
	}

	public static Operation importData(String target, String source, List files) {
		String doc = "mutation Import($target: String!, $source: String!, $files: [Upload!]!) " +
				"{ import(target: $target, source: $source, raw: $files) }";

		Map variables = Map.of(
				"source", source,
				"target", target,
				"files", createNulls(files.size())
		);

		return new Operation(String.format("Import data from %d files", files.size()), doc, variables, files);
	}

	public static Operation exportData(String target, String spec) {
		String doc = "mutation Export($target: String!, $spec: String!) " +
				"{ export(deploymentId: \"123\", target: $target, exportSpec: $spec) }";

		Map variables = Map.of(
				"target", target,
				"spec", spec
		);

		return new Operation(String.format("Export data to %s", target), doc, variables);
	}

	public static Operation evaluate(String snel) {
		String doc = "query Evaluate($snel: String!) { evaluate(deploymentId: \"123\", snel: $snel) }";

		Map variables = Map.of("snel", snel);

		return new Operation(String.format("Evaluate %s", snel), doc, variables);
	}

	public static Operation initAudit(String prefix) {
		String doc = "mutation InitAudit($prefix: String!) { initializeAuditing(prefix: $prefix) }";

		Map variables = Map.of("prefix", prefix);

		return new Operation("Initialize auditing for tables with prefix " + prefix, doc, variables);
	}

	public static Operation login(String username, String password) {
		String doc = """
			mutation Login($username: String!, $password: String!) {
				login(username: $username, password: $password)
			}
			""";

		Map variables = new HashMap<>();
		variables.put("username", username);
		variables.put("password", password);

		return new Operation(String.format("Login With %s / %s", username, password), doc, variables);
	}

	public static Operation processDefinitionByKey(String key) {
		String doc = """
			query ProcessDefinitionByKey($processKey: String!) {
				processDefinitionByKey(key: $processKey) { id, key }
			}
			""";

		Map variables = new HashMap<>();
		variables.put("processKey", key);

		return new Operation(String.format("Process Definition by Key %s", key), doc, variables);
	}

	public static Operation renderStartForm(String processDefinitionId) {
		String doc = """
			query RenderStartForm($processDefinitionId: ID!) {
				processDefinition(id: $processDefinitionId) { id, startForm }
			}
			""";

		Map variables = new HashMap<>();
		variables.put("processDefinitionId", processDefinitionId);

		return new Operation(String.format("Process start form for %s", processDefinitionId), doc, variables);
	}

	public static Operation choiceByValue(String id, String path, String value, boolean isTask) {
		return choiceByValue(id, path, value, isTask, null);
	}

	public static Operation choiceByValue(String id, String path, String value, boolean isTask, JsonNode values) {
		String doc = """
			query ChoiceByValue($taskId: ID, $processDefinitionId: ID, $path: String!, $value: String!, $values: JSON) {
				choice(
					taskId: $taskId,
					processDefinitionId: $processDefinitionId,
					path: $path,
					value: $value,
					values: $values
				) { value, label }
			}
			""";

		Map variables = new HashMap<>();
		variables.put(isTask ? "taskId" : "processDefinitionId", id);
		variables.put(isTask ? "processDefinitionId" : "taskId", null);
		variables.put("path", path);
		variables.put("value", value);
		variables.put("values", values);
		return new Operation(String.format("Choice by value %s for field %s on %s with %s",
				value, path, isTask ? "Task" : "ProcessInstance", id), doc, variables);
	}

	public static Operation choicesByLabel(String id, String path, String label, boolean isTask) {
		return choicesByLabel(id, path, label, isTask, null);
	}

	public static Operation choicesByLabel(String id, String path, String label, boolean isTask, JsonNode values) {
		String doc = """
			query ChoicesByLabel($taskId: ID, $processDefinitionId: ID, $path: String!, $filter: String!, $range: Range!, $values: JSON) {
				choices(
					taskId: $taskId,
					processDefinitionId: $processDefinitionId,
					path: $path,
					filter: $filter,
					range: $range,
					values: $values
				) { value, label }
			}
			""";

		Map variables = new HashMap<>();
		variables.put(isTask ? "taskId" : "processDefinitionId", id);
		variables.put(isTask ? "processDefinitionId" : "taskId", null);
		variables.put("path", path);
		variables.put("range", Map.of("start", 0, "count", 1));
		variables.put("values", values);

		SearchParts searchParts = SearchParts.extract(label, "contains");
		if (searchParts != null) {
			variables.put("filter", searchParts.toFilter());
		}

		return new Operation(String.format("Choices by label %s for field %s on %s with %s",
				label, path, isTask ? "Task" : "ProcessInstance", id), doc, variables);
	}

	public static Operation overrideCurrentDateTime(String value) {
		String doc = """
			mutation OverrideCurrentDateTime($value: DateTime!) {
				overrideCurrentDateTime(value: $value)
			}
			""";

		Map variables = Map.of("value", value);

		return new Operation(String.format("Override Current DateTime with %s", value), doc, variables);
	}

	public static Operation startProcessByKey(String key) {
		return startProcessByKey(key, null);
	}

	public static Operation startProcessByKey(String key, JsonNode valuesOpt) {
		String doc = """
			mutation StartProcessByKey($key: String!, $values: JSON) {
				startProcessByKey(key: $key, values: $values) {
				__typename
					... on ProcessInstance {
						id
						startTime
						processDefinition { id, key, name, deploymentId }
					}
					... on InputErrors {
						errors {
							path, name, type, label, value, status, message
						}
					}
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("key", key);
		variables.put("values", valuesOpt);

		return new Operation(String.format("Start Process with Key %s", key), doc, variables);
	}

	public static Operation startProcessByKey(String key, JsonNode values, List files) {
		String doc = """
			mutation StartProcessByKey($key: String!, $values: JSON, $files: [Upload!]) {
				startProcessByKey(key: $key, values: $values, files: $files) {
					__typename
					... on ProcessInstance {
						id
						startTime
						processDefinition { id, key, name, deploymentId }
					}
					... on InputErrors {
						errors {
							path, name, type, label, value, status, message
						}
					}
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("key", key);
		variables.put("values", values);
		variables.put("files", createNulls(files.size()));

		return new Operation(String.format("Start Process with Key %s", key), doc, variables, files);
	}

	public static Operation getTasks(String processInstanceId) {
		String doc = """
			query GetProcessInstance($id: ID!) {
				processInstance(id: $id) {
					id
					tasks { id }
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", processInstanceId);

		return new Operation(String.format("Get Tasks for ProcessInstance with ID %s", processInstanceId), doc, variables);
	}

	public static Operation getTask(String taskId) {
		String doc = """
			query GetTask($id: ID!) {
				task(id: $id) {
					id
					name
					subject
					allowedRoles
					form
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", taskId);

		return new Operation(String.format("Get Task with ID %s", taskId), doc, variables);
	}

	public static Operation claimTask(String taskId) {
		String doc = """
			mutation ClaimTask($id: ID!) {
				claimTask(id: $id)
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", taskId);

		return new Operation(String.format("Claim Task with ID %s", taskId), doc, variables);
	}

	public static Operation transferTask(String taskId, String currentAssignee) {
		String doc = """
			mutation ClaimTask($id: ID!, $currentAssignee: String!) {
				transferTask(id: $id, currentAssignee: $currentAssignee)
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", taskId);
		variables.put("currentAssignee", currentAssignee);

		return new Operation(String.format("Reassign Task with ID %s", taskId), doc, variables);
	}

	public static Operation dataAssertion(String jpql, String deploymentId) {
		String doc = """
			query CheckData($jpql: String!, $deploymentId: ID!) {
				checkData(jpql: $jpql, deploymentId: $deploymentId)
			}
			""";

		Map variables = new HashMap<>();
		variables.put("jpql", jpql);
		variables.put("deploymentId", deploymentId != null ? deploymentId : "");

		return new Operation("Data Assertion", doc, variables);
	}

	public static Operation dataQuery(String jpql, String deploymentId) {
		String doc = """
			query QueryData($jpql: String!, $deploymentId: ID!) {
				queryData(jpql: $jpql, deploymentId: $deploymentId)
			}
			""";

		Map variables = new HashMap<>();
		variables.put("jpql", jpql);
		variables.put("deploymentId", deploymentId != null ? deploymentId : "");

		return new Operation("Data Query", doc, variables);
	}

	public static Operation submitTask(String taskId, JsonNode values) {
		String doc = """
			mutation SubmitTask($id: ID!, $values: JSON!) {
				submitTask(id: $id, values: $values) {
					__typename
					... on ProcessInstance {
						id
					}
					... on ProcessInstanceEnded {
						id
					}
					... on InputErrors {
						errors {
							path, name, type, label, value, status, message
						}
					}
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", taskId);
		variables.put("values", values);

		return new Operation(String.format("Submit Task with ID %s", taskId), doc, variables);
	}

	public static Operation submitTask(String taskId, JsonNode values, List files) {
		String doc = """
			mutation SubmitTask($id: ID!, $values: JSON!, $files: [Upload!]) {
				submitTask(id: $id, values: $values, files: $files) {
					__typename
					... on ProcessInstance {
						id
					}
					... on ProcessInstanceEnded {
						id
					}
					... on InputErrors {
						errors {
							path, name, type, label, value, status, message
						}
					}
				}
			}
			""";

		Map variables = new HashMap<>();
		variables.put("id", taskId);
		variables.put("values", values);
		variables.put("files", createNulls(files.size()));

		return new Operation(String.format("Submit Task with ID %s", taskId), doc, variables, files);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy