package be.mentoringsystems.merke.presentation.excel; import be.mentoringsystems.merke.model.db.VendorRequest; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; 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.ss.util.CellRangeAddress; import org.springframework.web.servlet.view.document.AbstractXlsxView; /** * * @author anthonyarents */ public class VendorRequests extends AbstractXlsxView{ private void styledCell(Cell cell, String value, CellStyle style) { cell.setCellValue(value); cell.setCellStyle(style); } @Override protected void buildExcelDocument(Map map, Workbook wrkbk, HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { @SuppressWarnings("unchecked") final List data = (List) map.get("data"); final Boolean waitlist = (Boolean) map.get("waitlist"); String documentName = "Aanvragen overzicht"; String documentTitle = "Export overzicht "; if(Boolean.TRUE.equals(waitlist)) { documentName = "Aanvragen wachtlijst"; documentTitle = "Export wachtlijst "; } final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); hsr1.setHeader("Content-Disposition", "attachment; filename=\"" + documentName + ".xlsx\""); final CellStyle acceptedStyle = wrkbk.createCellStyle(); acceptedStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); acceptedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); final CellStyle declinedStyle = wrkbk.createCellStyle(); declinedStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); declinedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); final CellStyle headerStyle = wrkbk.createCellStyle(); headerStyle.setFillForegroundColor(IndexedColors.PALE_BLUE .getIndex()); headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); final Sheet sheet = wrkbk.createSheet(); final Row titlerow = sheet.createRow(0); sheet.addMergedRegion(new CellRangeAddress(0,0,0,6)); titlerow.createCell(0).setCellValue(documentTitle + sdf.format(new Date())); final Row headerrow = sheet.createRow(1); styledCell(headerrow.createCell(0), "Wachtnr.", headerStyle); styledCell(headerrow.createCell(1), "Voornaam", headerStyle); styledCell(headerrow.createCell(2), "Naam", headerStyle); styledCell(headerrow.createCell(3), "E-mailadres", headerStyle); styledCell(headerrow.createCell(4), "Tel", headerStyle); styledCell(headerrow.createCell(5), "Datum", headerStyle); styledCell(headerrow.createCell(6), "Status", headerStyle); int rowcounter = 0; int rowoffset = 2; for(VendorRequest request : data) { final Row row = sheet.createRow(rowcounter + rowoffset); rowcounter++; row.createCell(0).setCellValue(rowcounter); row.createCell(1).setCellValue(request.getVendor().getFirstname()); row.createCell(2).setCellValue(request.getVendor().getLastname()); row.createCell(3).setCellValue(request.getVendor().getEmail()); row.createCell(4).setCellValue(request.getVendor().getTelephonenumber()); row.createCell(5).setCellValue(sdf.format(request.getCreatedOn())); if(Boolean.TRUE.equals(waitlist)) { if(VendorRequest.WaitStatus.ACCEPTED.equals(request.getWaitstatus())) { styledCell(row.createCell(6), "Goedgekeurd", acceptedStyle); } else { row.createCell(6).setCellValue("In afwachting"); } } else { switch (request.getStatus()) { case ACCEPTED: styledCell(row.createCell(6), "Goedgekeurd", acceptedStyle); break; case DECLINED: styledCell(row.createCell(6), "Afgekeurd", declinedStyle); break; default: row.createCell(6).setCellValue("In afwachting"); break; } } } sheet.autoSizeColumn(0, false); sheet.autoSizeColumn(1, false); sheet.autoSizeColumn(2, false); sheet.autoSizeColumn(3, false); sheet.autoSizeColumn(4, false); sheet.autoSizeColumn(5, false); sheet.autoSizeColumn(6, false); } }