io.yields.math.framework.kpi.ExplorerDAO.java Source code

Java tutorial

Introduction

Here is the source code for io.yields.math.framework.kpi.ExplorerDAO.java

Source

/*
 * Copyright 2014 by Yields.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.yields.math.framework.kpi;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import io.yields.math.framework.Explorer;

import static java.lang.String.format;
import static org.apache.commons.io.FileUtils.forceMkdir;
import static org.apache.commons.io.FileUtils.getTempDirectory;

/**
 * DAO for Explorer data sets.
 */
public class ExplorerDAO {

    private static final String NO_GROUP = "no_group";

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");

    private static final String FOLDER_NAME = "yields_data";

    private static final String FILE_SUFFIX_CSV = "csv";
    private static final String FILE_SUFFIX_JSON = "json";

    private static File exportFolder;

    private static ExplorerCsvExporter csvExporter = new ExplorerCsvExporter();

    private static ExplorerJsonExporter jsonExporter = new ExplorerJsonExporter();

    public static void save(Explorer<?> explorer) {
        String group = explorer.getGroup();
        if (StringUtils.isBlank(group)) {
            group = NO_GROUP;
        }
        File destinationFolder = getRootFolder(group);

        if (!destinationFolder.exists()) {
            try {
                forceMkdir(destinationFolder);
            } catch (IOException ioe) {
                throw new IllegalStateException(
                        format("Destination folder for data export could not be created at %s",
                                destinationFolder.getAbsolutePath()),
                        ioe);
            }
        }

        if (!destinationFolder.isDirectory()) {
            throw new IllegalStateException(format("Destination path for data export %s is not a folder",
                    destinationFolder.getAbsolutePath()));
        }

        if (!destinationFolder.canWrite()) {
            throw new IllegalStateException(format("Destination folder for data export %s is not writable",
                    destinationFolder.getAbsolutePath()));
        }

        String fileName = explorer.getName().replaceAll("[^a-zA-Z0-9]", "_") + "_"
                + DATE_TIME_FORMATTER.format(LocalDateTime.now());

        File csvDestinationFile = new File(destinationFolder, fileName + "." + FILE_SUFFIX_CSV);
        csvExporter.export(explorer, csvDestinationFile);

        File jsonDestinationFile = new File(destinationFolder, fileName + "." + FILE_SUFFIX_JSON);
        jsonExporter.export(explorer, jsonDestinationFile);

    }

    private static File getRootFolder(String group) {
        if (exportFolder == null) {
            exportFolder = new File(getTempDirectory(), DATE_TIME_FORMATTER.format(LocalDateTime.now()));
        }
        return new File(exportFolder, group);
    }

}