PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/excelgen/src/main/java/by/q64/promo/excelgen/service/schedule/sheet/CityWeekScheduleSheetGeneratorImpl.java

https://bitbucket.org/quadrate/promo
Java | 178 lines | 140 code | 35 blank | 3 comment | 5 complexity | 29dbf18c1ecb240674f9861b7e9f59e6 MD5 | raw file
  1. package by.q64.promo.excelgen.service.schedule.sheet;
  2. import by.q64.promo.excelgen.constructor.SheetTemplateConstructor;
  3. import by.q64.promo.excelgen.constructor.entity.OptionalApply;
  4. import by.q64.promo.excelgen.constructor.entity.properties.ColumnProperties;
  5. import by.q64.promo.excelgen.constructor.entity.properties.MainHeaderProperties;
  6. import by.q64.promo.excelgen.constructor.entity.properties.SheetTemplateProperties;
  7. import by.q64.promo.excelgen.constructor.tools.ExcelGenTools;
  8. import by.q64.promo.excelgen.service.schedule.source.DayProperties;
  9. import by.q64.promo.excelgen.service.schedule.source.ShopScheduleRowContainer;
  10. import org.apache.poi.hssf.usermodel.HSSFPalette;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.ss.usermodel.CellStyle;
  13. import org.apache.poi.ss.usermodel.Font;
  14. import org.apache.poi.ss.usermodel.IndexedColors;
  15. import org.apache.poi.ss.usermodel.Workbook;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Qualifier;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.stereotype.Service;
  20. import java.util.*;
  21. import java.util.function.Function;
  22. /**
  23. * Created by Pavel on 22.09.2014.
  24. */
  25. @Service
  26. public class CityWeekScheduleSheetGeneratorImpl implements CityWeekScheduleSheetGenerator {
  27. @Autowired
  28. @Qualifier("cityWeekScheduleSheetTemplateConstructor")
  29. private SheetTemplateConstructor cityWeekScheduleSheetTemplateConstructor;
  30. @Autowired
  31. private ExcelGenTools excelGenTools;
  32. @Autowired
  33. private WorkbookColorManagerFactory workbookColorManagerFactory;
  34. @Override
  35. public Workbook generate(HSSFWorkbook workbook, String title, List<String> datesColumns,
  36. List<ShopScheduleRowContainer> scheduleRowContainers) {
  37. MainHeaderProperties mainHeaderProperties = new MainHeaderProperties();
  38. mainHeaderProperties.setHeaderDepth((short) 2);
  39. mainHeaderProperties.setAutoFilter(true);
  40. mainHeaderProperties.setHeaderRowsHeights(Arrays.asList((short) excelGenTools.getWidthFromAbstractUnits(3),
  41. (short) excelGenTools.getWidthFromAbstractUnits(3)));
  42. Font font = workbook.createFont();
  43. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  44. font.setItalic(true);
  45. font.setColor(IndexedColors.WHITE.getIndex());
  46. CellStyle headerMainCellStyle = workbook.createCellStyle();
  47. headerMainCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
  48. headerMainCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  49. headerMainCellStyle.setWrapText(true);
  50. headerMainCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
  51. headerMainCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  52. headerMainCellStyle.setFont(font);
  53. headerMainCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  54. headerMainCellStyle.setBorderTop(CellStyle.BORDER_THIN);
  55. headerMainCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
  56. headerMainCellStyle.setBorderRight(CellStyle.BORDER_THIN);
  57. short borderColorIndex = IndexedColors.WHITE.getIndex();
  58. headerMainCellStyle.setBottomBorderColor(borderColorIndex);
  59. headerMainCellStyle.setTopBorderColor(borderColorIndex);
  60. headerMainCellStyle.setLeftBorderColor(borderColorIndex);
  61. headerMainCellStyle.setRightBorderColor(borderColorIndex);
  62. mainHeaderProperties.setMainHeaderStyle(headerMainCellStyle);
  63. SheetTemplateProperties<ShopScheduleRowContainer> sheetTemplateProperties = new SheetTemplateProperties<>();
  64. sheetTemplateProperties.setStartCol((short) 0);
  65. sheetTemplateProperties.setStartRow((short) 0);
  66. sheetTemplateProperties.setDefaultRowHeight((short) excelGenTools.getWidthFromAbstractUnits(4));
  67. sheetTemplateProperties.setMainHeaderProperties(mainHeaderProperties);
  68. CellStyle dataCellStyle = workbook.createCellStyle();
  69. dataCellStyle.setWrapText(true);
  70. dataCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
  71. dataCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  72. sheetTemplateProperties.setDataCellStyle(dataCellStyle);
  73. sheetTemplateProperties.setColumnPropertiesList(generateColumnProperties(datesColumns, workbook));
  74. sheetTemplateProperties.setDataSource(scheduleRowContainers);
  75. sheetTemplateProperties.setTitle(title);
  76. cityWeekScheduleSheetTemplateConstructor.generateSheet(workbook, sheetTemplateProperties);
  77. return workbook;
  78. }
  79. private List<ColumnProperties<ShopScheduleRowContainer>> generateColumnProperties(List<String> datesColumns, HSSFWorkbook hssfWorkbook) {
  80. WorkbookColorManager workbookColorManager = workbookColorManagerFactory.get(hssfWorkbook);
  81. ArrayList<ColumnProperties<ShopScheduleRowContainer>> result = new ArrayList<ColumnProperties<ShopScheduleRowContainer>>();
  82. result.add(netColumn);
  83. result.add(addressColumn);
  84. for (int i = 0; i < weekdays.length; i++) {
  85. final int finalI = i;
  86. result.add(new ColumnProperties<ShopScheduleRowContainer>(weekdays[i], Arrays.asList(
  87. new ColumnProperties<>(datesColumns.get(i), 11, ssrc -> {
  88. DayProperties dayProperties = ssrc.getDayPropertieses().get(finalI);
  89. return dayProperties.getPromoter() + "\n" + dayProperties.getStart() + " - " + dayProperties.getEnd();
  90. }, null, Arrays.<OptionalApply<ShopScheduleRowContainer>>asList(new OptionalApply<>(v -> true, (cell, ssrc) -> {
  91. DayProperties dayProperties = ssrc.getDayPropertieses().get(finalI);
  92. String color = dayProperties.getColor();
  93. if (color != null) {
  94. CellStyle cellStyle = hssfWorkbook.createCellStyle();
  95. cellStyle.cloneStyleFrom(cell.getCellStyle());
  96. cellStyle.setFillForegroundColor(workbookColorManager.getColorIndex(color));
  97. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  98. cell.setCellStyle(cellStyle);
  99. }
  100. return ssrc;
  101. })))
  102. )));
  103. }
  104. result.add(supervisorColumn);
  105. result.add(promoFormColumn);
  106. return result;
  107. }
  108. private final ColumnProperties<ShopScheduleRowContainer> netColumn = new ColumnProperties<>("Сеть", 40, ShopScheduleRowContainer::getNet),
  109. addressColumn = new ColumnProperties<>("Адрес", 40, ShopScheduleRowContainer::getShop),
  110. supervisorColumn = new ColumnProperties<>("Супервайзер", 40, ShopScheduleRowContainer::getSupervisor),
  111. promoFormColumn = new ColumnProperties<>("Форма", 40, ShopScheduleRowContainer::getPromoFormAsString);
  112. private final String[] weekdays = {"пн", "вт", "ср", "чт", "пт", "сб", "вс"};
  113. }
  114. @Component
  115. class WorkbookColorManagerFactory {
  116. public WorkbookColorManager get(HSSFWorkbook workbook) {
  117. return new WorkbookColorManager(workbook);
  118. }
  119. }
  120. class WorkbookColorManager {
  121. private HSSFPalette palette;
  122. private short curColorIndex = 41; // numbers before is occupied
  123. private Map<String, Short> colors = new HashMap<>();
  124. WorkbookColorManager(HSSFWorkbook workbook) {
  125. palette = workbook.getCustomPalette();
  126. }
  127. public Short getColorIndex(String sixNumbers) {
  128. Short colorIndex = colors.get(sixNumbers);
  129. if (colorIndex == null) {
  130. colorIndex = createNewColor(sixNumbers);
  131. }
  132. return colorIndex;
  133. }
  134. protected Short createNewColor(String sixNumbers) {
  135. String[] strParts = sixNumbers.split("(?<=\\G.{2})");
  136. int red = Integer.valueOf(strParts[0], 16);
  137. int green = Integer.valueOf(strParts[1], 16);
  138. int blue = Integer.valueOf(strParts[2], 16);
  139. palette.setColorAtIndex((byte) curColorIndex, (byte) red, (byte) green, (byte) blue);
  140. colors.put(sixNumbers, curColorIndex);
  141. return curColorIndex++;
  142. }
  143. }