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

tech.grasshopper.combiner.options.PathsHandler Maven / Gradle / Ivy

The newest version!
package tech.grasshopper.combiner.options;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import lombok.Builder;
import lombok.Getter;
import tech.grasshopper.combiner.exception.CombinerException;

@Builder
public class PathsHandler {

	private List strJsonReportPaths;
	private List strMediaPaths;
	private String strMergedReportFolderPath;

	@Getter
	private List jsonReportPaths;
	@Getter
	private List mediaPaths;
	@Getter
	private Path mergedReportFolderPath;

	private static final String DEFAULT_WORKING_FOLDER = "target/";

	public void processReportPaths() {
		verifyPaths();

		jsonReportPaths = getFilePaths(strJsonReportPaths, "jsonReportPathsMsg");

		if (null == strMediaPaths || strMediaPaths.isEmpty())
			mediaPaths = defaultMediaPaths(jsonReportPaths);
		else
			mediaPaths = getFilePaths(strMediaPaths, "mediaPathsMsg");

		mergedReportFolderPath = getMergedFolderPath(strMergedReportFolderPath, "mergedReportDirMsg");
	}

	private Path getMergedFolderPath(String strMergedReportFolderPath, String mergedMsg) {
		if (strMergedReportFolderPath == null || strMergedReportFolderPath.isEmpty())
			strMergedReportFolderPath = DEFAULT_WORKING_FOLDER;

		return getFolderPath(strMergedReportFolderPath, mergedMsg);
	}

	private void verifyPaths() {
		if (strJsonReportPaths == null || strJsonReportPaths.isEmpty())
			throw new CombinerException("No JSON Reports are mentioned.");

		if (strJsonReportPaths.size() < 2)
			throw new CombinerException("Number of JSON Reports should be greater than 1.");

		if (strMediaPaths != null && !strMediaPaths.isEmpty() && strJsonReportPaths.size() != strMediaPaths.size())
			throw new CombinerException("Size of JSON reports and media paths are not equal.");
	}

	private List defaultMediaPaths(List jsonReportPaths) {
		List paths = new ArrayList<>();

		jsonReportPaths.forEach(p -> paths.add(p.getParent()));
		return paths;
	}

	private Path getFolderPath(String folderPath, String option) {
		Path path = Paths.get(folderPath.trim());

		if (!path.toFile().exists()) {
			try {
				Files.createDirectories(path);
			} catch (IOException e) {
				throw new CombinerException("Error in creating folder for " + option, e);
			}
		}
		return path;
	}

	private List getFilePaths(List filePaths, String option) {
		List paths = new ArrayList<>();

		filePaths.forEach(p -> {
			try {
				paths.add(getFilePath(p.trim(), option));
			} catch (CombinerException e) {
				throw new CombinerException("File path value " + p + " invalid for option " + option);
			}
		});
		return paths;
	}

	private Path getFilePath(String filePath, String option) {
		Path path = Paths.get(filePath.trim());

		if (!path.toFile().exists())
			throw new CombinerException("File path value invalid for option " + option);
		return path;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy