Package eu.lsem.bakalarka.service

Source Code of eu.lsem.bakalarka.service.ChartGeneratorImpl

package eu.lsem.bakalarka.service;

import eu.lsem.bakalarka.model.PieChart;
import eu.lsem.bakalarka.model.*;
import eu.lsem.bakalarka.model.ColumnChart;
import eu.lsem.bakalarka.dao.categories.CategoryDao;
import eu.lsem.bakalarka.dao.ThesesDao;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.krysalis.jcharts.properties.PropertyException;
import org.krysalis.jcharts.chartData.ChartDataException;

public class ChartGeneratorImpl implements ChartGenerator {
    private ThesesDao thesesDao;
    private CategoryDao thesesCategoriesDao;
    private CategoryDao fieldsOfStudyDao;
    private CategoryDao formsOfStudyDao;

    private final int COLUMN_CHART_WIDTH = 400;
    private final int COLUMN_CHART_HEIGHT = 250;
    private final int PIE_CHART_WIDTH = 400;
    private final int PIE_CHART_HEIGHT = 400;

    public void setThesesDao(ThesesDao thesesDao) {
        this.thesesDao = thesesDao;
    }

    public void setThesesCategoriesDao(CategoryDao thesesCategoriesDao) {
        this.thesesCategoriesDao = thesesCategoriesDao;
    }

    public void setFieldsOfStudyDao(CategoryDao fieldsOfStudyDao) {
        this.fieldsOfStudyDao = fieldsOfStudyDao;
    }

    public void setFormsOfStudyDao(CategoryDao formsOfStudyDao) {
        this.formsOfStudyDao = formsOfStudyDao;
    }


    private Map<ChartTypes, File> charts;


    private boolean regenerate = true;

    @Override
    public synchronized void setRegenerate() {
        regenerate = true;
    }

    @Override
    public synchronized InputStream getChart(ChartTypes type) {

        try {
            if (regenerate)
                regenerateCharts();
           
            try {
                return new FileInputStream(charts.get(type));
            } catch (IOException e) { /* pokud chyba, prepocitat  a znova */
                regenerateCharts();
                regenerate = false;
                return new FileInputStream(charts.get(type));
            }
        } catch (Exception e) {
            return null;
        }
    }


    private synchronized void regenerateCharts() {
        try {
            Map<ChartTypes, File> tempMap = new HashMap<ChartTypes, File>();

            /* typesChart*/
            tempMap.put(ChartTypes.CATEGORIES_CHART, categoriesChart());

            /*fields Chart */
            tempMap.put(ChartTypes.FIELDS_CHART, fieldsChart());

            /*forms chart*/
            tempMap.put(ChartTypes.FORMS_CHART, formsChart());
            /*years chart*/
            tempMap.put(ChartTypes.YEARS_CHART, yearsChart());

            /* date and field chart*/
            tempMap.put(ChartTypes.YEARS_AND_FIELDS_CHART, yearsAndFieldsChart());
            /* date and category chart*/
            tempMap.put(ChartTypes.YEARS_AND_CATEGORIES_CHART, yearsAndCategoriesChart());
            /* categories and fields pie */
            tempMap.put(ChartTypes.CATEGORIES_AND_FIELDS_CHART, categoriesAndFieldsCharts());

            /*uncomplete metadata*/
            tempMap.put(ChartTypes.UNCOMPLETE_METADATA_CHART, uncompleteMetadataChart());
            /*missing data*/
            tempMap.put(ChartTypes.MISSING_DATA_CHART, missingDataFile());
            /*not selected docs*/
            tempMap.put(ChartTypes.NOT_SELECTED_DOCUMENTATION_CHART, notSelectedDocumentationChart());

            charts = tempMap;
            regenerate = false;
        } catch (Exception e) {

        }
    }

    private File categoriesChart() throws IOException, PropertyException, ChartDataException {
        List<Category> categories = thesesCategoriesDao.getCategories();
        double[] data = new double[categories.size()];
        String[] labels = new String[categories.size()];
        for (int i = 0; i < categories.size(); i++) {
            data[i] = thesesDao.countThesesWithThesisCategory(categories.get(i).getId());
            labels[i] = categories.get(i).getName();
        }
        File chart = File.createTempFile("chart", ".png");
        new PieChart("Graf typ� prac�", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + data.length * 20, labels, data, true).saveChart(chart);
        return chart;
    }

    private File fieldsChart() throws IOException, PropertyException, ChartDataException {
        List<Category> categories = fieldsOfStudyDao.getCategories();
        double[] data = new double[categories.size()];
        String[] labels = new String[categories.size()];
        for (int i = 0; i < categories.size(); i++) {
            data[i] = thesesDao.countThesesWithFieldOfStudy(categories.get(i).getId());
            labels[i] = categories.get(i).getName();
        }
        File chart = File.createTempFile("chart", ".png");
        new PieChart("Graf prac� s oborem studia", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + data.length * 20, labels, data, true).saveChart(chart);
        return chart;
    }

    private File formsChart() throws IOException, PropertyException, ChartDataException {
        List<Category> categories = formsOfStudyDao.getCategories();
        double[] data = new double[categories.size()];
        String[] labels = new String[categories.size()];
        for (int i = 0; i < categories.size(); i++) {
            data[i] = thesesDao.countThesesWithFormOfStudy(categories.get(i).getId());
            labels[i] = categories.get(i).getName();
        }
        File chart = File.createTempFile("chart", ".png");
        new PieChart("Graf prac� s formou studia", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + data.length * 20, labels, data, true).saveChart(chart);
        return chart;
    }

    private File yearsChart() throws IOException, PropertyException, ChartDataException {
        List<Integer> years = thesesDao.getYears();
        String[] labels = new String[years.size()];
        double[] data = new double[years.size()];
        for (int i = 0; i < years.size(); i++) {
            labels[i] = String.valueOf(years.get(i));
            data[i] = thesesDao.countThesesAtYear(years.get(i));
        }
        File chart = File.createTempFile("chart", ".png");
        new ColumnChart("Graf prac� podle data obhajoby", COLUMN_CHART_WIDTH, COLUMN_CHART_HEIGHT + data.length * 20,
                "Roky", "Po�et prac�", new double[][]{data}, labels, new String[]{"Pr�ce v dan�m roce"}).saveChart(chart);
        return chart;
    }

    private File yearsAndCategoriesChart() throws IOException, PropertyException, ChartDataException {
        List<Integer> years = thesesDao.getYears();
        List<Category> categories = thesesCategoriesDao.getCategories();
        String[] labels = new String[years.size()];
        String[] legendLabels = new String[categories.size()];
        double[][] data = new double[categories.size()][years.size()];

        for (int i = 0; i < years.size(); i++) {
            labels[i] = years.get(i).toString();
        }

        for (int i = 0; i < categories.size(); i++) {
            legendLabels[i] = categories.get(i).getName();
            for (int j = 0; j < years.size(); j++) {
                data[i][j] = thesesDao.countThesesAtYearAndCategory(years.get(j), categories.get(i).getId());
            }
        }
        File f = File.createTempFile("chart", ".png");
        new ColumnChart("Graf prac� podle data obhajoby a kategorie pr�ce",
                COLUMN_CHART_WIDTH, COLUMN_CHART_HEIGHT + data.length * 20,
                "Roky", "Po�et prac�", data, labels, legendLabels
        ).saveChart(f);
        return f;
    }

    private File yearsAndFieldsChart() throws IOException, PropertyException, ChartDataException {
        List<Integer> years = thesesDao.getYears();
        List<Category> fieldsOfStudy = fieldsOfStudyDao.getCategories();
        String[] labels = new String[years.size()];
        String[] legendLabels = new String[fieldsOfStudy.size()];
        double[][] data = new double[fieldsOfStudy.size()][years.size()];

        for (int i = 0; i < years.size(); i++) {
            labels[i] = years.get(i).toString();
        }

        for (int i = 0; i < fieldsOfStudy.size(); i++) {
            legendLabels[i] = fieldsOfStudy.get(i).getName();
            for (int j = 0; j < years.size(); j++) {
                data[i][j] = thesesDao.countThesesAtYearAndFieldOfStudy(years.get(j), fieldsOfStudy.get(i).getId());
            }
        }

        File f = File.createTempFile("chart", ".png");
        new ColumnChart("Graf prac� podle data obhajoby a oboru studia",
                COLUMN_CHART_WIDTH, COLUMN_CHART_HEIGHT + data.length * 20,
                "Roky", "Po�et prac�", data, labels, legendLabels).saveChart(f);
        return f;
    }

    private File categoriesAndFieldsCharts() throws IOException, PropertyException, ChartDataException {
        List<Category> categories = thesesCategoriesDao.getCategories();
        List<Category> fieldsOfStudy = fieldsOfStudyDao.getCategories();
        double[] data = new double[categories.size() * fieldsOfStudy.size()];
        String[] labels = new String[categories.size() * fieldsOfStudy.size()];

        int counter = 0;
        for (Category category : categories) {
            String temp = category.getName() + " v oboru ";
            for (Category fieldOfStudy : fieldsOfStudy) {

                data[counter] = thesesDao.countThesesWithThesisCategoryAndFieldOfStudy(category.getId(), fieldOfStudy.getId());
                labels[counter] = temp + fieldOfStudy.getName();
                counter++;
            }
        }
        File f = File.createTempFile("chart", ".png");
        new PieChart("Graf typ� prac� v jednotliv�ch oborech studia", PIE_CHART_WIDTH*3/2, PIE_CHART_HEIGHT + data.length * 20, labels, data, true).saveChart(f);
        return f;
    }

    private File uncompleteMetadataChart() throws IOException, PropertyException, ChartDataException {
        double nekompletniPrace = thesesDao.countThesesWithUncompleteMetadata();
        double kompletniPrace = thesesDao.countTheses() - nekompletniPrace;
        File f = File.createTempFile("chart", ".png");
        new PieChart("Graf kompletnosti metadat", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + 2 * 20,
                new String[]{"Pr�ce s kompletn�mi metadaty", "Pr�ce s nekompletn�mi metadaty"},
                new double[]{kompletniPrace, nekompletniPrace}, true).saveChart(f);
        return f;
    }

    private File missingDataFile() throws IOException, PropertyException, ChartDataException {
        double missingDataCount = thesesDao.countThesesWithMissingData();
        double restCount = thesesDao.countTheses() - missingDataCount;
        File f = File.createTempFile("chart", ".png");
        new PieChart("Graf pom�ru prac� s daty", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + 40,
                new String[]{"Pr�ce s nechyb�j�c�mi daty", "Pr�ce s chyb�j�c�mi daty"},
                new double[]{restCount, missingDataCount}, true).saveChart(f);
        return f;
    }

    private File notSelectedDocumentationChart() throws IOException, PropertyException, ChartDataException {
        double notSelectedDocumentationCount = thesesDao.countThesesWithoutSelectedDocumentation();
        double restCount = thesesDao.countTheses() - notSelectedDocumentationCount;
        File f = File.createTempFile("chart", ".png");
        new PieChart("Graf pom�ru prac� s vybranou dokumentac�", PIE_CHART_WIDTH, PIE_CHART_HEIGHT + 40,
                new String[]{"Pr�ce s vybranou dokumentac�", "Pr�ce bez vybran� dokumentace"},
                new double[]{restCount, notSelectedDocumentationCount}, true).saveChart(f);
        return f;
    }
}
TOP

Related Classes of eu.lsem.bakalarka.service.ChartGeneratorImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.