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

com.xlrit.gears.runner.driver.LoadDriver Maven / Gradle / Ivy

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

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

import com.fasterxml.jackson.databind.JsonNode;
import com.xlrit.gears.runner.graphql.GraphQLClient;
import com.xlrit.gears.runner.graphql.Operation;
import com.xlrit.gears.runner.utils.MiscUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.xlrit.gears.runner.utils.MiscUtils.getExtension;
import static com.xlrit.gears.runner.utils.MiscUtils.getTableNamePrefix;

public class LoadDriver extends AbstractDriver {
	private static final Logger LOG = LoggerFactory.getLogger(LoadDriver.class);
	private static final String[] SUPPORTED_EXTENSIONS = new String[] { "sql", "json" };

	@Override
	public int run(LoadConfig config) {
		GraphQLClient client = getClient(config);

		File dataDir = new File(config.dataDir);
		List dataFiles = MiscUtils.listFilteredFiles(dataDir, config.filePattern, SUPPORTED_EXTENSIONS);
		if (config.dryRun) {
			for (var dataFile : dataFiles) {
				LOG.info(" - Loading data from {}", dataFile.getPath());
			}
		} else {
			LOG.info(" - Loading data from {}", config.filePattern);

			LOG.info("Loading {} data files:", dataFiles.size());
			for (File file : dataFiles) {
				LOG.info(" - {}", file.getPath());
				JsonNode jsonResponse = client.invoke(getOperation(file, getExtension(file).toLowerCase()));
				LOG.debug("loadData: jsonResponse={}", jsonResponse);
			}
		}
		if (config.initAuditing) {
			String tableNamePrefix = getTableNamePrefix(new File("."));
			LOG.info("Initializing auditing tables with prefix {}", tableNamePrefix);
			if (!config.dryRun) {
				Operation operation = Operation.initAudit(tableNamePrefix);
				JsonNode json = client.invoke(operation);
				assert(json.get("initAudit").asBoolean());
			}
		}
		return 0;
	}

	private static Operation getOperation(File file, String fileType) {
		return switch (fileType) {
			case "sql"  -> Operation.loadData(List.of(file));
			case "json" -> Operation.importData("jpa", "json", List.of(file));
			case ""     -> throw new IllegalArgumentException("Cannot determine file type of file " + file.getAbsolutePath());
			default     -> throw new IllegalArgumentException("Unsupported file type: " + fileType);
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy