Java tutorial
/******************************************************************************* * 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.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.ListUtils; import org.apache.poi.hssf.usermodel.HSSFName; 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.usermodel.Sheet; import org.opentestsystem.delivery.testreg.domain.ExplicitEligibility; import org.opentestsystem.delivery.testreg.domain.ExportStudentFormatEnums; import org.opentestsystem.delivery.testreg.domain.FormatType; import org.opentestsystem.delivery.testreg.domain.Student; import org.opentestsystem.delivery.testreg.domain.StudentGroup; import org.opentestsystem.delivery.testreg.domain.TestRegistrationBase; import org.opentestsystem.delivery.testreg.domain.User; import org.opentestsystem.delivery.testreg.domain.User.RoleAssociation; import org.opentestsystem.delivery.testreg.persistence.ExplicitEligibilityRepository; import org.opentestsystem.delivery.testreg.service.MasterResourceAccommodationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.web.servlet.view.document.AbstractExcelView; /** * This class builds an Excel spreadsheet document using Apache POI library. */ public class ExcelView extends AbstractExcelView { private static final String DATA_LIST = "DATA_LIST"; private static final String FORMAT_TYPE = "FORMAT_TYPE"; private static final String EXPORT_TYPE = "EXPORT_TYPE"; @Resource(name = "headersMap") private Map<String, String[]> headersMap; @Resource(name = "templateDownloadMap") private Map<FormatType, List<String>> templateDownloadMap; @Autowired private MasterResourceAccommodationService masterResourceService; @Autowired private Transformer<TestRegistrationBase, String[]> testRegistrationViewTransformer; @Autowired private Transformer<User.RoleAssociation, String[]> userRoleAssociationEntityExportTransformer; @Autowired private Transformer<Student, String[][]> studentEntityExportTransformer; @Autowired private Transformer<StudentGroup, String[]> studentGroupEntityExportTransformer; @Autowired private ExplicitEligibilityRepository explicitEligRepository; @SuppressWarnings("unchecked") @Override protected void buildExcelDocument(final Map<String, Object> model, final HSSFWorkbook workbook, final HttpServletRequest request, final HttpServletResponse response) { // get data model which is passed by the Spring container final List<TestRegistrationBase> entityList = (List<TestRegistrationBase>) model.get(DATA_LIST); final String formatType = ((String) model.get(FORMAT_TYPE)).toUpperCase(); String exportType = null; if (formatType.equalsIgnoreCase(FormatType.STUDENT.name())) { exportType = ((String) model.get(EXPORT_TYPE)).toUpperCase(); HSSFName name = workbook.createName(); // display filename to return to the client name.setNameName(exportType); } final Sheet sheet = workbook.createSheet(formatType); sheet.setDefaultColumnWidth(30); String headerColumns[] = new String[100]; if (formatType.equalsIgnoreCase(FormatType.STUDENT.name())) { // Based on export type for FormatType( STUDENT ) assigning headers for export file if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsPlusAccommodations.name())) { List<String> headerStudentList = templateDownloadMap.get(FormatType.STUDENT); String[] headerCodesTemp = headersMap.get(FormatType.DESIGNATEDSUPPORTSANDACCOMMODATIONS.name()); List<String> headerCodes = Arrays.asList(headerCodesTemp); List<String> headerAccommodationList = headerCodes.subList(2, headerCodes.size()); String lastElement = headerStudentList.get(headerStudentList.size() - 1); List<String> combinedHeaders = ListUtils .union(headerStudentList.subList(0, headerStudentList.size() - 1), headerAccommodationList); combinedHeaders.add(lastElement); headerColumns = combinedHeaders.toArray(new String[combinedHeaders.size()]); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsOnly.name())) { List<String> headerStudentList = templateDownloadMap.get(FormatType.STUDENT); headerColumns = headerStudentList.toArray(new String[headerStudentList.size()]); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.accommodationsOnly.name())) { headerColumns = headersMap.get(FormatType.DESIGNATEDSUPPORTSANDACCOMMODATIONS.name()); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.explicitEligibility.name())) { List<String> headerExplicitEligibilityList = templateDownloadMap .get(FormatType.EXPLICITELIGIBILITY); headerColumns = headerExplicitEligibilityList .toArray(new String[headerExplicitEligibilityList.size()]); } } else { headerColumns = this.headersMap.get(formatType); } final Row header = sheet.createRow(0); for (int i = 0; i < headerColumns.length; i++) { final Cell cell = header.createCell(i); cell.setCellValue(headerColumns[i]); } // DataRow Begin if (!CollectionUtils.isEmpty(entityList)) { int rowCount = 1; for (final TestRegistrationBase entity : entityList) { // if format is user then we need to customize export to include role associations as separate rows switch (FormatType.valueOf(formatType)) { case USER: rowCount = exportUser(sheet, rowCount, entity); break; case STUDENTGROUP: rowCount = exportStudentGroup(sheet, rowCount, entity); break; case STUDENT: rowCount = exportStudent(sheet, rowCount, (Student) entity, exportType); break; default: final Row rowData = sheet.createRow(rowCount); final String[] columnData = this.testRegistrationViewTransformer.transform(entity); addData(rowData, 0, columnData.length, columnData); } rowCount++; } } } private int exportStudent(final Sheet sheet, int rowCount, final Student entity, String exportType) { String[][] columnDataArray = new String[100][100]; HashMap<String, String> resourceTypesMap = masterResourceService.getAllResourceTypes(); List<String> accommodationNames = masterResourceService.getAllOptionsCodes(); // Based on ExportType transforming student data if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsPlusAccommodations.name())) { columnDataArray = StudentEntityExportHelper.toMultiRowStringArray(entity, resourceTypesMap, accommodationNames); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.studentsOnly.name())) { columnDataArray = StudentEntityExportHelper.transformStudentsOnly(entity); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.accommodationsOnly.name())) { columnDataArray = StudentEntityExportHelper.transformStudenttoAccommodationsOnly(entity, resourceTypesMap, accommodationNames); } else if (exportType.equalsIgnoreCase(ExportStudentFormatEnums.explicitEligibility.name())) { List<ExplicitEligibility> explicitEligibilities = explicitEligRepository .findByStudentIdAndStateAbbreviation(entity.getEntityId(), entity.getStateAbbreviation()); columnDataArray = StudentEntityExportHelper .transformStudenttoExplicitEligibility(explicitEligibilities); } if (columnDataArray != null) { for (final String[] columnData : columnDataArray) { final Row roleRowData = sheet.createRow(rowCount); addData(roleRowData, 0, columnData.length, columnData); rowCount++; } rowCount--; } return rowCount; } private int exportStudentGroup(final Sheet sheet, int rowCount, final TestRegistrationBase entity) { final StudentGroup studentGroup = (StudentGroup) entity; final String[] columnData = this.studentGroupEntityExportTransformer.transform(studentGroup); // add students if (!CollectionUtils.isEmpty(studentGroup.getStudentIds())) { for (final String studentId : studentGroup.getStudentIds()) { final Row roleRowData = sheet.createRow(rowCount); columnData[3] = studentId; addData(roleRowData, 0, columnData.length, columnData); rowCount++; } rowCount--; } else { Row rowData = sheet.createRow(rowCount); addData(rowData, 0, columnData.length, columnData); } return rowCount; } private int exportUser(final Sheet sheet, int rowCount, final TestRegistrationBase entity) { final User user = (User) entity; final String[] columnData = user.toStringArray(); // add role association if (!CollectionUtils.isEmpty(user.getRoleAssociations())) { for (final RoleAssociation role : user.getRoleAssociations()) { final Row roleRowData = sheet.createRow(rowCount); final String[] roleData = this.userRoleAssociationEntityExportTransformer.transform(role); addData(roleRowData, 0, columnData.length, columnData); addData(roleRowData, columnData.length, roleData.length + columnData.length, roleData); rowCount++; } rowCount--; } else { final Row rowData = sheet.createRow(rowCount); addData(rowData, 0, columnData.length, columnData); } return rowCount; } private void addData(final Row rowData, final int initValue, final int maxValue, final String[] columnData) { for (int i = initValue; i < maxValue; i++) { final Cell cell = rowData.createCell(i); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(columnData[i - initValue]); } } }