de.chott.jfreechartsample.reader.FileReaderService.java Source code

Java tutorial

Introduction

Here is the source code for de.chott.jfreechartsample.reader.FileReaderService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.chott.jfreechartsample.reader;

import de.chott.jfreechartsample.model.PieChartData;
import de.chott.jfreechartsample.model.RingChartData;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * Dieser Service stellt Methoden zum Auslesen der Data-Files zur Verfgung. Da die Files direkt als InputStream geladen werden,
 * sind die Methoden auf InputStreams als bergabeparameter angepasst.
 * 
 * @author CHOTT
 */
@Stateless
public class FileReaderService {

    /**
     * Liest mit Hilfe der Apache-Poi-Library ein XLS-File aus und gibt die Daten darin als Liste von PieChartData-Objekten zurck. 
     * 
     * @param stream Das File als Resource-InputStream
     * @return eine Liste der PieChartData
     * @throws IOException 
     */
    public List<PieChartData> readPieChartDataFromXls(InputStream stream) throws IOException {
        List<PieChartData> retVal = new ArrayList<>();
        HSSFWorkbook workbook = new HSSFWorkbook(stream);

        HSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            PieChartData data = new PieChartData();

            Cell countryCell = row.getCell(0);
            Cell weightCell = row.getCell(1);

            if (countryCell != null && weightCell != null) {
                data.setCountry(countryCell.getStringCellValue());
                data.setWeight(weightCell.getNumericCellValue());

                retVal.add(data);
            }
        }

        return retVal;
    }

    /**
     * Liest einen InputStream von einem CSV-File aus und mapped die darin liegenden Daten in eine Liste aus RingChartData-Objekten. 
     * 
     * @param stream Der InputStream vom CSV-File
     * @return Eine Liste an RingChartData-Objekten
     * @throws IOException
     * @throws ParseException 
     */
    public List<RingChartData> readRingChartDataFromCsv(InputStream stream) throws IOException, ParseException {
        List<RingChartData> retVal = new ArrayList<>();

        String delimiter = ",";

        InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
        BufferedReader br = new BufferedReader(reader);

        br.readLine();
        String s;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");

        while ((s = br.readLine()) != null) {
            String[] splitArray = s.split(delimiter);
            RingChartData chartData = new RingChartData();

            chartData.setDate(sdf.parse(splitArray[0]));
            chartData.setSecurity(splitArray[1]);
            chartData.setWeighting(Double.valueOf(splitArray[2]));

            retVal.add(chartData);
        }

        return retVal;
    }

}