com.hygenics.parser.ExistanceChecker.java Source code

Java tutorial

Introduction

Here is the source code for com.hygenics.parser.ExistanceChecker.java

Source

package com.hygenics.parser;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import com.hygenics.parser.getDAOTemplate;
import com.hygenics.exceptions.SchemaMissingException;
import com.hygenics.exceptions.TableMissingException;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * Sometimes a process will not generate teh proper exit code. In this circumstance, the
 * expected result can be missed and wrong data (usually an older schema) may be
 * picked up instead of the most recent offering. This class eradicates this issue
 * for database and other related expectations. 
 * 
 * This class can:
 *      - match filenames on globs looking for existance
 *      - check that a schema exists
 *      - ensure that a file does not exist (consumed)
 *      - ensure that a 
 *      - check that a table does not exist (renamed/moved)
 *      - ensure that record counts are greater than 0 for specific tables
 *      
* @author Andrew Evans
* Copyright 2016
* License Free BSD
*/
public class ExistanceChecker {
    private final Logger log = LoggerFactory.getLogger(MainApp.class);

    private getDAOTemplate template;

    private List<String> globsExists;
    private List<String> directories;
    private List<String> globsNotExists;
    private List<String> schemaExists;
    private List<String> schemaNotExists;
    private List<String> tableExists;
    private List<String> tableNotExists;

    @Autowired
    @Required
    @Resource(name = "getDAOTemplate")
    public void setTemplate(getDAOTemplate template) {
        this.template = template;
    }

    public List<String> getGlobsExists() {
        return globsExists;
    }

    public void setGlobsExists(List<String> globsExists) {
        this.globsExists = globsExists;
    }

    public List<String> getGlobsNotExists() {
        return globsNotExists;
    }

    public void setGlobsNotExists(List<String> globsNotExists) {
        this.globsNotExists = globsNotExists;
    }

    public List<String> getSchemaExists() {
        return schemaExists;
    }

    public void setSchemaExists(List<String> schemaExists) {
        this.schemaExists = schemaExists;
    }

    public List<String> getSchemaNotExists() {
        return schemaNotExists;
    }

    public void setSchemaNotExists(List<String> schemaNotExists) {
        this.schemaNotExists = schemaNotExists;
    }

    public List<String> getTableExists() {
        return tableExists;
    }

    public void setTableExists(List<String> tableExists) {
        this.tableExists = tableExists;
    }

    public List<String> getTableNotExists() {
        return tableNotExists;
    }

    public void setTableNotExists(List<String> tableNotExists) {
        this.tableNotExists = tableNotExists;
    }

    public ArrayList<Boolean> checkFilesExist(List<String> fileMatchers, List<String> directories, boolean exists) {
        ArrayList<PathMatcher> matchers = new ArrayList<PathMatcher>();
        for (String glob : fileMatchers) {
            PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
            matchers.add(matcher);
        }

        ArrayList<Boolean> matches = new ArrayList<Boolean>();
        for (String dir : directories) {

            File dirFile = new File(dir);
            if (dirFile.exists()) {
                File[] files = dirFile.listFiles();
                for (int i = 0; i < files.length; i++) {
                    for (PathMatcher matcher : matchers) {
                        matches.add(matcher.matches(files[i].toPath()) == exists);
                    }
                }
            } else {
                log.warn("Continuing. Does Not Exist: %s".format(dir));
            }
        }
        return matches;
    }

    public ArrayList<Boolean> checkSchemasExist(List<String> schemas, boolean exists) {
        ArrayList<Boolean> matches = new ArrayList<Boolean>();
        for (String schema : schemas) {
            matches.add(template.checkSchema(schema) == exists);
        }

        return matches;
    }

    public ArrayList<Boolean> checkTablesExist(List<String> tables, boolean exists) {
        ArrayList<Boolean> matches = new ArrayList<Boolean>();
        for (String table : tables) {
            String[] tarr = table.split("\\.");
            matches.add(template.checkTable(tarr[1], tarr[0]) == exists);
        }
        return matches;
    }

    public void run() {

        if (globsExists != null && globsExists.size() > 0) {
            ArrayList<Boolean> matches = checkFilesExist(globsExists, directories, true);
            if (matches.contains(false)) {
                try {
                    throw new FileNotFoundException("A Required File was Not Found");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        if (globsNotExists != null && globsNotExists.size() > 0) {
            ArrayList<Boolean> matches = checkFilesExist(globsNotExists, directories, false);

            if (matches.contains(false)) {
                try {
                    throw new Exception("A File That Cannot Be Present Was Found");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        if (schemaExists != null && schemaExists.size() > 0) {
            ArrayList<Boolean> matches = checkSchemasExist(schemaExists, true);
            if (matches.contains(false)) {
                try {
                    throw new SchemaMissingException("A Required Schema was Not Found.");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        if (schemaNotExists != null && schemaNotExists.size() > 0) {
            ArrayList<Boolean> matches = checkSchemasExist(schemaNotExists, false);
            if (matches.contains(false)) {
                try {
                    throw new Exception("A Schema was Found that Cannot Be Present");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        if (tableExists != null && tableExists.size() > 0) {
            ArrayList<Boolean> matches = checkTablesExist(tableExists, true);
            if (matches.contains(false)) {
                try {
                    throw new TableMissingException("A Required Table Was Missing");
                } catch (TableMissingException e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        if (tableNotExists != null && tableNotExists.size() > 0) {
            ArrayList<Boolean> matches = checkTablesExist(tableExists, false);
            if (matches.contains(false)) {
                try {
                    throw new Exception("A Table WasFound that Cannot Exist");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }

    }

}