org.opentestsystem.delivery.testreg.rest.ExcelBasedTemplateCreator.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.rest.ExcelBasedTemplateCreator.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.rest;

import static org.opentestsystem.delivery.testreg.rest.FileType.XLS;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opentestsystem.delivery.testreg.domain.FormatType;

public class ExcelBasedTemplateCreator implements TemplateCreator {

    @Resource(name = "templateDownloadMap")
    private Map<FormatType, List<String>> templateDownloadMap;

    private static final int DEFAULT_COLUMN_WIDTH = 40 * 256; //40Character length * 256th of Character unit

    private FileType fileType;

    public ExcelBasedTemplateCreator(FileType fileType) {
        switch (fileType) {
        case XLS:
        case XLSX:
            this.fileType = fileType;
            return;
        default:
            throw new RuntimeException("Unsupported Filetype for this class" + fileType.name());
        }
    }

    @Override
    public InputStream createTemplate(FormatType formatType) throws Exception {
        String format = formatType.name();

        final Workbook template = createWorkbook();
        CellStyle defaultStyle = getTextCellSytle(template);
        Sheet sheet = template.createSheet(format);

        List<String> columns = templateDownloadMap.get(formatType);
        setColumnStyles(columns.size(), defaultStyle, sheet);
        createTemplateHeaders(sheet.createRow(0), defaultStyle, columns);

        return new ByteArrayInputStream(new ByteArrayOutputStream() {
            {
                template.write(this);
            }
        }.toByteArray());
    }

    private Workbook createWorkbook() {
        return fileType == XLS ? new HSSFWorkbook() : new XSSFWorkbook();
    }

    private CellStyle getTextCellSytle(Workbook workbook) {
        DataFormat dataFormat = workbook.createDataFormat();
        CellStyle textStyle = workbook.createCellStyle();
        textStyle.setDataFormat(dataFormat.getFormat("@")); //@ is equivalent to Text
        textStyle.setHidden(false);
        return textStyle;
    }

    private void createTemplateHeaders(Row headerRow, CellStyle style, List<String> columns) {
        int columnNo = 0;
        for (String columnName : columns) {
            Cell headerCell = headerRow.createCell(columnNo++, Cell.CELL_TYPE_STRING);
            headerCell.setCellStyle(style);
            headerCell.setAsActiveCell();
            headerCell.setCellValue(columnName);
            headerCell.setCellType(Cell.CELL_TYPE_STRING);
        }
    }

    private void setColumnStyles(int totalColumns, CellStyle style, Sheet sheet) {
        for (int columnNo = 0; columnNo < totalColumns; columnNo++) {
            sheet.setDefaultColumnStyle(columnNo, style);
            sheet.setColumnHidden(columnNo, false);
            sheet.setColumnWidth(columnNo, DEFAULT_COLUMN_WIDTH);
        }
    }
}