com.anevis.jfreechartsamplespring.reader.FileReaderServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.anevis.jfreechartsamplespring.reader.FileReaderServiceImpl.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 com.anevis.jfreechartsamplespring.reader;

import com.anevis.jfreechartsamplespring.model.PieChartData;
import com.anevis.jfreechartsamplespring.model.RingChartData;
import java.io.BufferedReader;
import java.io.FileInputStream;
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 java.util.logging.Level;
import java.util.logging.Logger;
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;
import org.springframework.stereotype.Component;

/**
 *
 * @author chot
 */
@Component
public class FileReaderServiceImpl implements FileReaderService {

    @Override
    public List<PieChartData> readPieChartDataFromXls(String filepath) {
        InputStream stream;

        try {
            stream = new FileInputStream(filepath);
            List<PieChartData> pieChartDataList = 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());

                    pieChartDataList.add(data);
                }
            }

            return pieChartDataList;

        } catch (IOException ex) {
            Logger.getLogger(FileReaderServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    @Override
    public List<RingChartData> readRingChartDataFromCSV(String filepath) {
        InputStream stream;

        try {
            stream = new FileInputStream(filepath);
            List<RingChartData> ringChartDataList = 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]));

                ringChartDataList.add(chartData);
            }

            return ringChartDataList;
        } catch (IOException | ParseException ex) {
            Logger.getLogger(FileReaderServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

}