org.opentestsystem.delivery.testreg.rest.view.PDFReportView.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.rest.view.PDFReportView.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.view;

import java.awt.Color;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.opentestsystem.delivery.testadmin.domain.TestAdminReport;
import org.opentestsystem.delivery.testreg.domain.FormatType;
import org.opentestsystem.delivery.testreg.domain.HierarchyLevel;
import org.opentestsystem.delivery.testreg.service.EntityCrosswalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDFReportView extends AbstractPdfView {

    private static final String DATA_LIST = "DATA_LIST";
    private static final String REPORT_TYPE = "REPORT_TYPE";
    private static final String LEVEL_OF_REPORT = "LEVEL_OF_REPORT";

    static final Font HEADER_FONT = FontFactory.getFont(FontFactory.TIMES, 6);
    static final Font HEADER_MESSAGE_FONT = FontFactory.getFont(FontFactory.TIMES, 11);
    static final Font DATA_FONT = FontFactory.getFont(FontFactory.TIMES, 6);
    private static final String HEADER_MESSAGE = "HEADER_MESSAGE";

    @Autowired
    private EntityCrosswalkService entityCrosswalkService;

    @Resource(name = "reportHeaders")
    private Map<String, String[]> reportHeaders;

    @Override
    @SuppressWarnings("unchecked")
    protected void buildPdfDocument(final Map<String, Object> model, final Document document,
            final PdfWriter writer, final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        document.setMarginMirroringTopBottom(true);
        final List<TestAdminReport> dataList = (List<TestAdminReport>) model.get(DATA_LIST);
        final String reportType = ((String) model.get(REPORT_TYPE)).toUpperCase();
        final String levelOfReport = (String) model.get(LEVEL_OF_REPORT);
        final String headerMessage = (String) model.get(HEADER_MESSAGE);
        writer.setPageEvent(new PdfReportPageEventHelper(writer));
        HierarchyLevel level = null;
        if (levelOfReport != null) {
            level = HierarchyLevel.valueOf(levelOfReport);
        }
        final String headerColumns[] = this.reportHeaders.get(reportType);
        if (headerColumns != null) {
            PdfPTable table = createMessageHeaders(headerColumns, level, headerMessage);
            table.setSpacingAfter(10f);
            if (reportType.equals("PARTICIPATION_DETAIL_REPORT") || reportType.equals("PROCTOR_SCHEDULE_REPORT")
                    || reportType.equals("STUDENT_SCHEDULE_REPORT")) {
                table = createHeaders(headerColumns, table);
            } else {
                table = createSummaryHeaders(headerColumns, level, table);
            }
            table.setHeaderRows(2);
            if (dataList.size() == 0) {
                addEmptyCell(table);
            } else {
                addData(table, dataList, level);
            }
            document.add(table);
        }
    }

    // No Data Row Found then Add Empty row
    private PdfPTable addEmptyCell(final PdfPTable table) {
        final PdfPCell cell = new PdfPCell(new Paragraph(" No Data Found", HEADER_MESSAGE_FONT));
        cell.setColspan(table.getNumberOfColumns());
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        return table;
    }

    private PdfPTable createMessageHeaders(final String[] headerColumns, final HierarchyLevel level,
            final String message) throws BadElementException {
        PdfPTable table = null;
        if (level != null && level == HierarchyLevel.CLIENT) {
            table = new PdfPTable(headerColumns.length - 3);
        } else {
            table = new PdfPTable(headerColumns.length);
        }
        table.setWidthPercentage(100);
        PdfPCell cell;
        cell = new PdfPCell(new Paragraph(message, HEADER_MESSAGE_FONT));
        cell.setColspan(headerColumns.length);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        return table;
    }

    private PdfPTable createHeaders(final String[] headerColumns, final PdfPTable table)
            throws BadElementException {
        PdfPCell cell;
        for (final String header : headerColumns) {
            cell = new PdfPCell(new Paragraph(header, HEADER_FONT));
            cell.setBackgroundColor(Color.GRAY);
            table.addCell(cell);
        }
        return table;
    }

    private PdfPTable createSummaryHeaders(final String[] headerColumns, final HierarchyLevel level,
            final PdfPTable table) throws BadElementException {
        PdfPCell cell;
        int columnIndex = 0;
        Map<String, String> crosswalkMap = null;
        if (level != HierarchyLevel.CLIENT) {
            crosswalkMap = this.entityCrosswalkService.getFormatType(FormatType.valueOf(level.name()));
        }
        for (String header : headerColumns) {
            columnIndex++;
            if (level != null && level == HierarchyLevel.CLIENT) {
                if (columnIndex == 4 || columnIndex == 5 || columnIndex == 6) {
                    continue;
                }
            } else {
                if (columnIndex == 7 || columnIndex == 8) {
                    if (crosswalkMap.get(header) != null) {
                        header = crosswalkMap.get(header);
                    }
                }

            }
            cell = new PdfPCell(new Paragraph(header, HEADER_FONT));
            cell.setBackgroundColor(Color.GRAY);
            table.addCell(cell);

        }
        return table;
    }

    private PdfPTable addData(final PdfPTable table, final List<TestAdminReport> dataSummary,
            final HierarchyLevel level) throws BadElementException {
        PdfPCell cell;
        int count = 1;

        for (final TestAdminReport dataRow : dataSummary) {
            cell = new PdfPCell(new Paragraph(count + "", DATA_FONT));

            table.addCell(cell);
            int index = 1;
            for (final String columnData : dataRow.toStringArray()) {
                if (level != null && level == HierarchyLevel.CLIENT) {
                    index = index + 1;
                    if (index == 3 || index == 4 || index == 5) {
                        continue;
                    }
                }
                cell = new PdfPCell(new Paragraph(columnData, DATA_FONT));
                table.addCell(cell);

            }
            count++;
        }
        return table;
    }
}