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

com.github.siwenyan.profile.ProfileLibraryImpl Maven / Gradle / Ivy

package com.github.siwenyan.profile;

import com.github.siwenyan.common.*;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class ProfileLibraryImpl implements IProfileLibrary {
    private static final Logger log = Logger.getLogger(ProfileLibraryImpl.class.getName());

    public static final String SHEET_VIEW = "view";

    private Map> indexedMaps = new LinkedHashMap<>();

    public ProfileLibraryImpl(String... paths) {
        this(null, paths);
    }

    public ProfileLibraryImpl(String prefix, String... paths) {
        for (String path : paths) {
            List> maps = new ArrayList<>();
            File tryFile = Sys.findFile(path);
            if (!tryFile.exists()) {
                Assert.fail("Invalid path: " + path);
            }

            if (tryFile.isFile()) {
                maps.addAll(loadExcelFile(prefix, tryFile));
            } else if (tryFile.isDirectory()) {
                maps.addAll(loadJsonFiles(prefix, tryFile));
                maps.addAll(loadPropertyFiles(prefix, tryFile));
            } else {
                Assert.fail("Unsupported source: " + tryFile.getAbsolutePath());
            }
            if (maps.size() <= 0) {
                continue;
            }
            String keyHeader = maps.get(0).containsKey(IProfileLibrary.TITLE) ? IProfileLibrary.TITLE : maps.get(0).keySet().iterator().next();
            Map> indexedMapsPerPath = DataTableTools.dataTableAsMapsIndexedByKeyHeader(keyHeader, maps);
            this.indexedMaps.putAll(indexedMapsPerPath);
        }
    }

    private List> loadJsonFiles(String prefix, File rootFolder) {
        List> maps = new ArrayList<>();
        for (File file : FileUtils.listFiles(rootFolder, new String[]{"json"}, true)) {
            try {
                Map map = EasyJson.readMap(FileUtils.readFileToString(file));
                maps.add(DataTableTools.addPrefix(prefix, map));
            } catch (IOException e) {
                Assert.fail("Cannot read file: " + file.getAbsolutePath());
            }
        }
        return maps;
    }

    private List> loadPropertyFiles(String prefix, File rootFolder) {
        List> maps = new ArrayList<>();
        for (File file : FileUtils.listFiles(rootFolder, new String[]{"properties"}, true)) {
            try {
                Properties prop = new Properties();
                prop.load(new FileInputStream(file));
                Map map = DataTableTools.asMap(prop);
                maps.add(DataTableTools.addPrefix(prefix, map));
            } catch (IOException e) {
                Assert.fail("Cannot read file: " + file.getAbsolutePath());
            }
        }
        return maps;
    }

    private List> loadExcelFile(String prefix, File excelFile) {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(new FileInputStream(excelFile));
            XSSFSheet sheet = workbook.getSheet(SHEET_VIEW);
            if (null == sheet) {
                throw new RuntimeException("Missing sheet: [" + SHEET_VIEW + "]");
            }
            List> maps = EasyExcel.asMaps(workbook.getSheet(SHEET_VIEW), IProfileLibrary.TITLE_DEFAULT);
            if (null == maps || 0 == maps.size()) {
                throw new RuntimeException("Empty sheet: " + SHEET_VIEW);
            }
            return DataTableTools.addPrefix(prefix, maps);
        } catch (Exception e) {
            log.debug(e.getMessage());
            log.error(e.getMessage());
            throw new RuntimeException("Cannot load Excel file: " + excelFile.getAbsolutePath());
        } finally {
            try {
                if (null != workbook) {
                    workbook.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                // do nothing
            }
        }
    }

    @Override
    public Iterable getTitles() {
        return this.indexedMaps.keySet();
    }

    @Override
    public Map getObjectByTitle(String title, String... options) {
        if (!this.indexedMaps.containsKey(title)) {
            throw new ImmediateAbortException("No such title: " + title);
        }
        Map map = new LinkedHashMap<>(this.indexedMaps.get(title));
        String prototypeTitle = map.remove(IProfileLibrary.PROTOTYPE);

        Map result = new LinkedHashMap<>();
        if (!StringTools.isEmpty(prototypeTitle)) {
            result.putAll(getObjectByTitle(prototypeTitle));
        }
        result.putAll(map);

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy