co.turnus.analysis.data.partitioning.io.XlsPartitioningDataWriter.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.data.partitioning.io.XlsPartitioningDataWriter.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2014 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TURNUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.analysis.data.partitioning.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.apache.poi.ss.util.CellRangeAddress;

import co.turnus.TurnusRuntimeException;
import co.turnus.analysis.data.partitioning.PartitioningData;
import co.turnus.analysis.data.partitioning.PartitionsData;
import co.turnus.dataflow.Actor;
import co.turnus.dataflow.Network;
import co.turnus.mapping.ActorsMapping;

public class XlsPartitioningDataWriter {

    private HSSFFont titleFont;

    public void write(PartitioningData report, File file) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();

            titleFont = workbook.createFont();
            titleFont.setFontName("Arial");
            titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            int solutionId = 1;
            for (PartitionsData data : report.getPartitionsData()) {
                writeData(workbook, data, report.getNetwork(), report.getAlgorithm(), solutionId++);
            }

            OutputStream out = new FileOutputStream(file);
            out = new BufferedOutputStream(out);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
        }

    }

    private void writeData(HSSFWorkbook workbook, PartitionsData data, Network network, String algorithm,
            int solutionId) {
        HSSFSheet sheet = workbook.createSheet("Partition " + solutionId);

        Cell cell = sheet.createRow(0).createCell(0);
        HSSFRichTextString title = new HSSFRichTextString(algorithm + " Results");
        title.applyFont(titleFont);
        cell.setCellValue(title);

        Row row = sheet.createRow(3);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 3));
        row.createCell(0).setCellValue("Algorithm specific Parameters");
        row = sheet.createRow(4);
        int cellNum = 0;
        for (String attrName : data.getKeyAttributes()) {
            row.createCell(cellNum++).setCellValue(attrName);
            Object val = data.getAttribute(attrName);
            row.createCell(cellNum++).setCellValue(val.toString());
        }

        row = sheet.createRow(5);
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 0, 1));
        row.createCell(0).setCellValue("Partitions");

        row = sheet.createRow(6);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("Actors");

        int rown = 7;
        row = sheet.createRow(rown);
        for (ActorsMapping p : data.getMapping().getActorsMappings()) {
            row.createCell(0).setCellValue(p.getId());
            for (Actor a : p.getActors()) {
                row.createCell(1).setCellValue(a.getId());
                row = sheet.createRow(++rown);
            }
        }

    }

}