PageRenderTime 91ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/excelgen/src/main/java/by/q64/promo/excelgen/service/marketing/MarketingReportInCityGeneratorImpl.java

https://bitbucket.org/quadrate/promo
Java | 210 lines | 164 code | 39 blank | 7 comment | 8 complexity | a58465613021fd4ec4403df58f01c572 MD5 | raw file
  1. package by.q64.promo.excelgen.service.marketing;
  2. import by.q64.promo.excelgen.service.marketing.source.*;
  3. import by.q64.promo.excelgen.service.marketing.source.settings.MarketingReportSettings;
  4. import by.q64.promo.excelgen.service.utils.PowerpointCommonUtils;
  5. import org.apache.poi.xslf.usermodel.*;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.awt.*;
  9. import java.util.List;
  10. import java.util.function.Function;
  11. import java.util.stream.Collectors;
  12. /**
  13. * Created by Pavel.Sirotkin on 25.12.2014.
  14. */
  15. @Service("marketingReportInCityGenerator")
  16. public class MarketingReportInCityGeneratorImpl implements MarketingReportPartGenerator {
  17. private static final String[] previewTableHeader = new String[]{"№", "City", "Shop", "Address"};
  18. @Autowired
  19. private PowerpointCommonUtils powerpointCommonUtils;
  20. @Override
  21. public XMLSlideShow generate(XMLSlideShow ppt, MarketingReportSettings reportSettings,
  22. MarketingReportDataOneCity marketingReportDataOneCity,
  23. Function<String, byte[]> pictureGetter) {
  24. MarketingReportData mData = marketingReportDataOneCity.getMarketingReportData();
  25. MarketingSourceByCity marketingSourceByCity = marketingReportDataOneCity.getMarketingSourceByCity();
  26. List<PreviewReportingDocument> previewData = marketingSourceByCity.getPreviewData();
  27. String cityName = marketingSourceByCity.getCityName();
  28. List<? extends MarketingSlideInfo> slides = marketingSourceByCity.getSlides();
  29. int curPage = reportSettings.getCurPage();
  30. ppt = generatePreview(ppt, previewData, cityName, curPage);
  31. ppt = generateDetailShops(ppt, slides, pictureGetter, mData);
  32. return ppt;
  33. }
  34. private XMLSlideShow generatePreview(XMLSlideShow ppt, List<PreviewReportingDocument> previewData, String cityName,
  35. int repDocsStartNum) {
  36. XSLFSlide slide = ppt.createSlide();
  37. XSLFTextBox txt1 = slide.createTextBox();
  38. txt1.setAnchor(new java.awt.Rectangle(20, 40, 600, 50));
  39. XSLFTextParagraph p1 = txt1.addNewTextParagraph();
  40. XSLFTextRun r1 = p1.addNewTextRun();
  41. r1.setText("Reporting documents");
  42. r1.setFontFamily("Verdana");
  43. r1.setFontColor(new Color(255, 0, 102));
  44. r1.setFontSize(32);
  45. r1.setBold(true);
  46. XSLFTextBox txt2 = slide.createTextBox();
  47. txt2.setAnchor(new java.awt.Rectangle(20, 80, 600, 50));
  48. XSLFTextParagraph p2 = txt2.addNewTextParagraph();
  49. XSLFTextRun r2 = p2.addNewTextRun();
  50. r2.setText("Address program: " + cityName);
  51. r2.setFontColor(Color.black);
  52. r2.setFontFamily("Verdana");
  53. r2.setFontSize(18);
  54. r2.setBold(true);
  55. //generate table
  56. XSLFTable table = slide.createTable();
  57. table.setAnchor(new Rectangle(30, 120, 100, 100));
  58. XSLFTableRow headerRow = table.addRow();
  59. headerRow.setHeight(20);
  60. // header
  61. for (int i = 0; i < previewTableHeader.length; i++) {
  62. XSLFTableCell th = headerRow.addCell();
  63. XSLFTextParagraph p = th.addNewTextParagraph();
  64. p.setTextAlign(TextAlign.CENTER);
  65. XSLFTextRun r = p.addNewTextRun();
  66. r.setText(previewTableHeader[i]);
  67. r.setBold(true);
  68. r.setFontColor(Color.white);
  69. r.setFontSize(11);
  70. th.setFillColor(Color.RED);
  71. th.setBorderBottom(2);
  72. th.setBorderBottomColor(Color.white);
  73. table.setColumnWidth(i, 160);
  74. }
  75. // fill data
  76. for (PreviewReportingDocument dataRow : previewData) {
  77. XSLFTableRow row = table.addRow();
  78. XSLFTableCell nCell = row.addCell();
  79. XSLFTextParagraph nCellP = nCell.addNewTextParagraph();
  80. XSLFTextRun nCellR = nCellP.addNewTextRun();
  81. nCellR.setText(String.valueOf(repDocsStartNum++));
  82. nCellR.setFontSize(11);
  83. XSLFTableCell cityCell = row.addCell();
  84. XSLFTextParagraph cityCellP = cityCell.addNewTextParagraph();
  85. XSLFTextRun cityCellR = cityCellP.addNewTextRun();
  86. cityCellR.setText(cityName);
  87. cityCellR.setFontSize(11);
  88. XSLFTableCell netCell = row.addCell();
  89. XSLFTextParagraph netCellP = netCell.addNewTextParagraph();
  90. XSLFTextRun netCellR = netCellP.addNewTextRun();
  91. netCellR.setText(dataRow.getNetwork());
  92. netCellR.setFontSize(11);
  93. XSLFTableCell addressCell = row.addCell();
  94. XSLFTextParagraph addressCellP = addressCell.addNewTextParagraph();
  95. XSLFTextRun addressCellR = addressCellP.addNewTextRun();
  96. addressCellR.setText(dataRow.getAddress());
  97. addressCellR.setFontSize(11);
  98. row.setHeight(11);
  99. }
  100. table.setColumnWidth(0, 160);
  101. table.setColumnWidth(1, 160);
  102. table.setColumnWidth(2, 160);
  103. table.setColumnWidth(3, 160);
  104. return ppt;
  105. }
  106. private XMLSlideShow generateDetailShops(XMLSlideShow ppt, List<? extends MarketingSlideInfo> slides, Function<String, byte[]> pictureGetter, MarketingReportData mData) {
  107. for (MarketingSlideInfo slide : slides) {
  108. ppt = generateDetailShop(ppt, slide, pictureGetter, mData);
  109. }
  110. return ppt;
  111. }
  112. private XMLSlideShow generateDetailShop(XMLSlideShow ppt, MarketingSlideInfo slide, Function<String, byte[]> pictureGetter, MarketingReportData mData) {
  113. final int maxPictureWidth = 185, maxPictureHeight = 300, leftPicturePadding = 50, betweenDifference = 30,
  114. titleHeight = 70, subTitleHeight = 80, titlesSumHeight = titleHeight + subTitleHeight,
  115. fullPictureWidthPlace = maxPictureWidth + betweenDifference;
  116. XSLFSlide pptSlide = ppt.createSlide();
  117. XSLFTextBox txt1 = pptSlide.createTextBox();
  118. txt1.setAnchor(new java.awt.Rectangle(20, 40, 650, titleHeight));
  119. XSLFTextParagraph p1 = txt1.addNewTextParagraph();
  120. XSLFTextRun r1 = p1.addNewTextRun();
  121. r1.setText(slide.getUnitRegionNetworkName() + ", " + slide.getUnitRegionName());
  122. r1.setFontFamily("Verdana");
  123. r1.setFontColor(new Color(255, 0, 102));
  124. r1.setFontSize(24);
  125. r1.setBold(true);
  126. XSLFTextBox txt2 = pptSlide.createTextBox();
  127. txt2.setAnchor(new java.awt.Rectangle(20, titleHeight + 40, 650, subTitleHeight));
  128. XSLFTextParagraph p2 = txt2.addNewTextParagraph();
  129. XSLFTextRun r2 = p2.addNewTextRun();
  130. r2.setText(generateDatesOnDetailShop(slide, mData));
  131. r2.setFontFamily("Verdana");
  132. r2.setFontColor(new Color(255, 0, 102));
  133. r2.setFontSize(20);
  134. r2.setBold(true);
  135. String[] photos = {slide.getPhoto1(), slide.getPhoto2(), slide.getPhoto3()};
  136. for (int i = 0; i < 3; i++) {
  137. byte[] picBytes1 = pictureGetter.apply(photos[i]);
  138. if (picBytes1 == null)
  139. continue;
  140. int idx = ppt.addPicture(picBytes1, XSLFPictureData.PICTURE_TYPE_PNG);
  141. XSLFPictureShape pic = pptSlide.createPicture(idx);
  142. //calculate sizes
  143. double realWidth = pic.getAnchor().getWidth();
  144. double realHeight = pic.getAnchor().getHeight();
  145. double kWidth = maxPictureWidth / realWidth;
  146. double kHeight = maxPictureHeight / realHeight;
  147. double k = Math.min(kWidth, kHeight);
  148. int height = (int) (k * realHeight);
  149. int width = (int) (k *realWidth);
  150. int centerWidthPadding = (maxPictureWidth - width) / 2;
  151. int centerHeightPadding = (maxPictureHeight - height) / 2;
  152. pic.setAnchor(new Rectangle(leftPicturePadding + fullPictureWidthPlace * i + centerWidthPadding,
  153. titlesSumHeight + 20 + centerHeightPadding,
  154. width, height));
  155. }
  156. return ppt;
  157. }
  158. private String generateDatesOnDetailShop(MarketingSlideInfo slide,
  159. MarketingReportData mData){
  160. List<Integer> days = slide.getDays();
  161. String s = null;
  162. if(days.size() == 0){
  163. s = "Не работал";
  164. }else {
  165. s = "(" + days.stream().sorted()
  166. .map(Object::toString).collect(Collectors.joining(", ")) +
  167. "." + mData.getMonth() + "." + mData.getYear() + ")";
  168. }
  169. return s;
  170. }
  171. }